2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.sdc.dcae.utils;
23 import com.fasterxml.jackson.core.JsonProcessingException;
24 import com.fasterxml.jackson.databind.ObjectMapper;
26 import org.onap.sdc.dcae.composition.restmodels.sdc.Artifact;
27 import org.onap.sdc.dcae.enums.ArtifactGroupType;
28 import org.onap.sdc.dcae.enums.SdcConsumerInfo;
29 import org.springframework.util.Base64Utils;
30 import org.springframework.util.StringUtils;
33 import java.net.URISyntaxException;
34 import java.util.ArrayList;
35 import java.util.EnumMap;
36 import java.util.List;
37 import java.util.stream.Collectors;
39 public class SdcRestClientUtils {
41 private static final String SDC_CATALOG_PATH = "/sdc/v1/catalog/";
43 // TODO consider moving params elsewhere (user/password/instanceId can be constant)
44 public static EnumMap<SdcConsumerInfo, String> extractConsumerInfoFromUri(URI configUri) {
45 EnumMap<SdcConsumerInfo, String> userInfoMap = new EnumMap<>(SdcConsumerInfo.class);
46 String userInfo = configUri.getUserInfo();
47 if (userInfo != null) {
48 userInfoMap.put(SdcConsumerInfo.AUTH, "Basic "+ Base64Utils.encodeToString(userInfo.getBytes()));
50 String fragment = configUri.getFragment();
52 throw new IllegalArgumentException("The URI must contain a fragment specification, to be used as SDC instance id");
53 userInfoMap.put(SdcConsumerInfo.INSTANCE_ID, fragment);
55 userInfoMap.put(SdcConsumerInfo.CATALOG_URL, new URI(configUri.getScheme(), null, configUri.getHost(), configUri.getPort(), configUri.getPath()+SDC_CATALOG_PATH, null, null).toString());
57 catch (URISyntaxException se) {
58 throw new IllegalArgumentException("Invalid uri", se);
63 public static String buildResourceFilterQuery(String resourceType, String category, String subcategory) {
64 List<String> filters = new ArrayList<>();
65 if(!StringUtils.isEmpty(resourceType))
66 filters.add("resourceType="+resourceType);
67 if(!StringUtils.isEmpty(category))
68 filters.add("category="+category);
69 if(!StringUtils.isEmpty(subcategory))
70 filters.add("subCategory="+subcategory);
71 return "?"+filters.stream().collect(Collectors.joining("&"));
74 public static UserRemarks buildUserRemarksObject(String userRemarks) {
75 return new UserRemarks(userRemarks);
78 private static class UserRemarks {
79 private String userRemarks;
81 private UserRemarks(String userRemarks) {
82 this.userRemarks = userRemarks;
85 public String getUserRemarks() {
90 public static String artifactToString(Artifact artifact) throws JsonProcessingException {
91 ObjectMapper mapper = new ObjectMapper();
92 return mapper.writeValueAsString(artifact);
95 public static Artifact generateDeploymentArtifact(String description, String name, String type, String label, byte[] payload) {
96 Artifact artifact = new Artifact();
97 artifact.setDescription(description);
98 artifact.setArtifactName(name);
99 artifact.setArtifactGroupType(ArtifactGroupType.DEPLOYMENT.name());
100 artifact.setArtifactType(type);
101 artifact.setArtifactLabel(label);
102 artifact.setPayloadData(Base64Utils.encodeToString(payload));
106 public static Artifact generateCatalogDcaeToscaArtifact(String name, String path, byte[] payload) {
107 Artifact artifact = new Artifact();
108 artifact.setArtifactName(name);
109 artifact.setArtifactURL(path);
110 artifact.setPayloadData(new String(payload));