summaryrefslogtreecommitdiffstats
path: root/dcaedt_catalog/asdc/src/main/java/org/onap/sdc/dcae/utils
diff options
context:
space:
mode:
Diffstat (limited to 'dcaedt_catalog/asdc/src/main/java/org/onap/sdc/dcae/utils')
-rw-r--r--dcaedt_catalog/asdc/src/main/java/org/onap/sdc/dcae/utils/Normalizers.java34
-rw-r--r--dcaedt_catalog/asdc/src/main/java/org/onap/sdc/dcae/utils/SDCResponseErrorHandler.java43
-rw-r--r--dcaedt_catalog/asdc/src/main/java/org/onap/sdc/dcae/utils/SdcRestClientUtils.java85
3 files changed, 162 insertions, 0 deletions
diff --git a/dcaedt_catalog/asdc/src/main/java/org/onap/sdc/dcae/utils/Normalizers.java b/dcaedt_catalog/asdc/src/main/java/org/onap/sdc/dcae/utils/Normalizers.java
new file mode 100644
index 0000000..4719607
--- /dev/null
+++ b/dcaedt_catalog/asdc/src/main/java/org/onap/sdc/dcae/utils/Normalizers.java
@@ -0,0 +1,34 @@
+package org.onap.sdc.dcae.utils;
+
+import org.apache.commons.lang3.text.WordUtils;
+
+import java.util.regex.Pattern;
+
+public final class Normalizers {
+
+ private static final Pattern COMPONENT_NAME_DELIMITER_PATTERN = Pattern.compile("[.\\-_]+");
+ private static final Pattern ARTIFACT_LABEL_DELIMITER_PATTERN = Pattern.compile("[ \\-+._]+");
+ private static final Pattern COMPONENT_INSTANCE_NAME_DELIMITER_PATTERN = Pattern.compile("[ \\-.]+");
+
+
+ public static String normalizeComponentName(String name) {
+ String normalizedName = name.toLowerCase();
+ normalizedName = COMPONENT_NAME_DELIMITER_PATTERN.matcher(normalizedName).replaceAll(" ");
+ String[] split = normalizedName.split(" ");
+ StringBuffer sb = new StringBuffer();
+ for (String splitElement : split) {
+ String capitalize = WordUtils.capitalize(splitElement);
+ sb.append(capitalize);
+ }
+ return sb.toString();
+ }
+
+ public static String normalizeArtifactLabel(String label) {
+ return ARTIFACT_LABEL_DELIMITER_PATTERN.matcher(label).replaceAll("").toLowerCase();
+ }
+
+ public static String normalizeComponentInstanceName(String name) {
+ return COMPONENT_INSTANCE_NAME_DELIMITER_PATTERN.matcher(name).replaceAll("").toLowerCase();
+ }
+
+}
diff --git a/dcaedt_catalog/asdc/src/main/java/org/onap/sdc/dcae/utils/SDCResponseErrorHandler.java b/dcaedt_catalog/asdc/src/main/java/org/onap/sdc/dcae/utils/SDCResponseErrorHandler.java
new file mode 100644
index 0000000..64da66a
--- /dev/null
+++ b/dcaedt_catalog/asdc/src/main/java/org/onap/sdc/dcae/utils/SDCResponseErrorHandler.java
@@ -0,0 +1,43 @@
+package org.onap.sdc.dcae.utils;
+
+import com.google.gson.Gson;
+import org.onap.sdc.dcae.catalog.asdc.ASDCException;
+import org.onap.sdc.dcae.errormng.RequestError;
+import org.onap.sdc.dcae.errormng.ResponseFormat;
+import org.springframework.http.client.ClientHttpResponse;
+import org.springframework.web.client.DefaultResponseErrorHandler;
+import org.springframework.web.client.HttpClientErrorException;
+import org.springframework.web.client.ResponseErrorHandler;
+
+import java.io.IOException;
+
+public class SDCResponseErrorHandler implements ResponseErrorHandler {
+
+ private ResponseErrorHandler errorHandler = new DefaultResponseErrorHandler();
+
+ private static Gson gson = new Gson();
+
+ public void handleError(ClientHttpResponse response) throws IOException {
+ try{
+ errorHandler.handleError(response);
+ } catch (HttpClientErrorException e) {
+ RequestError re = extractRequestError(e);
+ throw null == re ? e : new ASDCException(e.getStatusCode(), re);
+ }
+ }
+
+ public boolean hasError(ClientHttpResponse response) throws IOException{
+ return errorHandler.hasError(response);
+ }
+
+ private RequestError extractRequestError(HttpClientErrorException error) {
+ try {
+ String body = error.getResponseBodyAsString();
+ ResponseFormat responseFormat = gson.fromJson(body, ResponseFormat.class);
+ return responseFormat.getRequestError();
+ } catch (Exception e) {
+ return null;
+ }
+ }
+
+}
diff --git a/dcaedt_catalog/asdc/src/main/java/org/onap/sdc/dcae/utils/SdcRestClientUtils.java b/dcaedt_catalog/asdc/src/main/java/org/onap/sdc/dcae/utils/SdcRestClientUtils.java
new file mode 100644
index 0000000..33c2f49
--- /dev/null
+++ b/dcaedt_catalog/asdc/src/main/java/org/onap/sdc/dcae/utils/SdcRestClientUtils.java
@@ -0,0 +1,85 @@
+package org.onap.sdc.dcae.utils;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+import org.onap.sdc.dcae.composition.restmodels.sdc.Artifact;
+import org.onap.sdc.dcae.enums.ArtifactGroupType;
+import org.onap.sdc.dcae.enums.SdcConsumerInfo;
+import org.springframework.util.Base64Utils;
+import org.springframework.util.StringUtils;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.ArrayList;
+import java.util.EnumMap;
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class SdcRestClientUtils {
+
+ private static final String SDC_CATALOG_PATH = "/sdc/v1/catalog/";
+
+ // TODO consider moving params elsewhere (user/password/instanceId can be constant)
+ public static EnumMap<SdcConsumerInfo, String> extractConsumerInfoFromUri(URI configUri) {
+ EnumMap<SdcConsumerInfo, String> userInfoMap = new EnumMap<>(SdcConsumerInfo.class);
+ String userInfo = configUri.getUserInfo();
+ if (userInfo != null) {
+ userInfoMap.put(SdcConsumerInfo.AUTH, "Basic "+ Base64Utils.encodeToString(userInfo.getBytes()));
+ }
+ String fragment = configUri.getFragment();
+ if (fragment == null)
+ throw new IllegalArgumentException("The URI must contain a fragment specification, to be used as SDC instance id");
+ userInfoMap.put(SdcConsumerInfo.INSTANCE_ID, fragment);
+ try {
+ userInfoMap.put(SdcConsumerInfo.CATALOG_URL, new URI(configUri.getScheme(), null, configUri.getHost(), configUri.getPort(), configUri.getPath()+SDC_CATALOG_PATH, null, null).toString());
+ }
+ catch (URISyntaxException se) {
+ throw new IllegalArgumentException("Invalid uri", se);
+ }
+ return userInfoMap;
+ }
+
+ public static String buildResourceFilterQuery(String resourceType, String category, String subcategory) {
+ List<String> filters = new ArrayList<>();
+ if(!StringUtils.isEmpty(resourceType))
+ filters.add("resourceType="+resourceType);
+ if(!StringUtils.isEmpty(category))
+ filters.add("category="+category);
+ if(!StringUtils.isEmpty(subcategory))
+ filters.add("subCategory="+subcategory);
+ return "?"+filters.stream().collect(Collectors.joining("&"));
+ }
+
+ public static UserRemarks buildUserRemarksObject(String userRemarks) {
+ return new UserRemarks(userRemarks);
+ }
+
+ private static class UserRemarks {
+ private String userRemarks;
+
+ private UserRemarks(String userRemarks) {
+ this.userRemarks = userRemarks;
+ }
+
+ public String getUserRemarks() {
+ return userRemarks;
+ }
+ }
+
+ public static String artifactToString(Artifact artifact) throws JsonProcessingException {
+ ObjectMapper mapper = new ObjectMapper();
+ return mapper.writeValueAsString(artifact);
+ }
+
+ public static Artifact generateDeploymentArtifact(String description, String name, String type, String label, byte[] payload){
+ Artifact artifact = new Artifact();
+ artifact.setDescription(description);
+ artifact.setArtifactName(name);
+ artifact.setArtifactGroupType(ArtifactGroupType.DEPLOYMENT.name());
+ artifact.setArtifactType(type);
+ artifact.setArtifactLabel(label);
+ artifact.setPayloadData(Base64Utils.encodeToString(payload));
+ return artifact;
+ }
+}