88c89a7a9eca11d82c5b6576b09ef434c50cdcc4
[sdc/dcae-d/dt-be-main.git] / dcaedt_catalog / asdc / src / main / java / org / onap / sdc / dcae / utils / SdcRestClientUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.sdc.dcae.utils;
22
23 import com.fasterxml.jackson.core.JsonProcessingException;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25
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;
31
32 import java.net.URI;
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;
38
39 public class SdcRestClientUtils {
40
41     private static final String SDC_CATALOG_PATH = "/sdc/v1/catalog/";
42
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()));
49         }
50         String fragment = configUri.getFragment();
51         if (fragment == null)
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);
54         try {
55             userInfoMap.put(SdcConsumerInfo.CATALOG_URL, new URI(configUri.getScheme(), null, configUri.getHost(), configUri.getPort(), configUri.getPath()+SDC_CATALOG_PATH, null, null).toString());
56         }
57         catch (URISyntaxException se) {
58             throw new IllegalArgumentException("Invalid uri", se);
59         }
60         return userInfoMap;
61     }
62
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("&"));
72     }
73
74     public static UserRemarks buildUserRemarksObject(String userRemarks) {
75         return new UserRemarks(userRemarks);
76     }
77
78     private static class UserRemarks {
79         private String userRemarks;
80
81         private UserRemarks(String userRemarks) {
82             this.userRemarks = userRemarks;
83         }
84
85         public String getUserRemarks() {
86             return userRemarks;
87         }
88     }
89
90     public static String artifactToString(Artifact artifact) throws JsonProcessingException {
91         ObjectMapper mapper = new ObjectMapper();
92         return mapper.writeValueAsString(artifact);
93     }
94
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));
103         return artifact;
104     }
105
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));
111                 return artifact;
112         }
113 }