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