2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights
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============================================
20 * ===================================================================
24 package org.onap.clamp.clds.client.req.sdc;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.fasterxml.jackson.databind.JsonNode;
29 import com.fasterxml.jackson.databind.ObjectMapper;
30 import com.fasterxml.jackson.databind.node.ArrayNode;
31 import com.fasterxml.jackson.databind.node.ObjectNode;
33 import java.io.IOException;
34 import java.nio.charset.StandardCharsets;
35 import java.security.GeneralSecurityException;
36 import java.util.ArrayList;
37 import java.util.Base64;
38 import java.util.Iterator;
39 import java.util.List;
40 import java.util.Map.Entry;
42 import org.apache.commons.codec.DecoderException;
43 import org.onap.clamp.clds.client.req.tca.TcaRequestFormatter;
44 import org.onap.clamp.clds.config.ClampProperties;
45 import org.onap.clamp.clds.model.properties.Global;
46 import org.onap.clamp.clds.model.properties.ModelProperties;
47 import org.onap.clamp.clds.model.properties.Tca;
48 import org.onap.clamp.clds.model.sdc.SdcResource;
49 import org.onap.clamp.clds.model.sdc.SdcServiceDetail;
50 import org.onap.clamp.clds.util.JacksonUtils;
51 import org.springframework.beans.factory.annotation.Autowired;
52 import org.springframework.context.annotation.Primary;
53 import org.springframework.stereotype.Component;
56 * Construct a Sdc request given CLDS objects.
60 public class SdcRequests {
62 protected static final EELFLogger logger = EELFManager.getInstance().getLogger(SdcRequests.class);
63 protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
65 private SdcCatalogServices sdcCatalogServices;
67 protected ClampProperties refProp;
70 * Format the Blueprint from a Yaml
73 * The ModelProperties describing the clds model
75 * The Yaml file that must be converted
76 * @return A String containing the BluePrint
80 public String formatBlueprint(ModelProperties prop, String docText) throws IOException {
81 String yamlvalue = getYamlvalue(docText);
82 String updatedBlueprint = "";
83 Tca tca = prop.getType(Tca.class);
85 updatedBlueprint = TcaRequestFormatter.updatedBlueprintWithConfiguration(refProp, prop, yamlvalue);
87 logger.info("value of blueprint:" + updatedBlueprint);
88 return updatedBlueprint;
92 * Format the SDC Locations Request in the JSON Format
95 * The ModelProperties describing the clds model
97 * The name of the artifact
98 * @return SDC Locations request in the JSON Format
100 public String formatSdcLocationsReq(ModelProperties prop, String artifactName) {
101 ObjectMapper objectMapper = JacksonUtils.getObjectMapperInstance();
102 Global global = prop.getGlobal();
103 List<String> locationsList = global.getLocation();
104 ArrayNode locationsArrayNode = objectMapper.createArrayNode();
105 ObjectNode locationObject = objectMapper.createObjectNode();
106 for (String currLocation : locationsList) {
107 locationsArrayNode.add(currLocation);
109 locationObject.put("artifactName", artifactName);
110 locationObject.putPOJO("locations", locationsArrayNode);
111 String locationJsonFormat = locationObject.toString();
112 logger.info("Value of location Json Artifact:" + locationsArrayNode);
113 return locationJsonFormat;
117 * Format the SDC Request
120 * The ModelProperties describing the clds model
121 * @param artifactName
122 * The name of the artifact
123 * @param artifactLabel
124 * The Label of the artifact
125 * @param artifactType
126 * The type of the artifact
127 * @return formatted SDC Request
129 public String formatSdcReq(String payloadData, String artifactName, String artifactLabel, String artifactType) {
130 logger.info("artifact=" + payloadData);
131 String base64Artifact = Base64.getEncoder().encodeToString(payloadData.getBytes(StandardCharsets.UTF_8));
132 return "{ \n" + "\"payloadData\" : \"" + base64Artifact + "\",\n" + "\"artifactLabel\" : \"" + artifactLabel
133 + "\",\n" + "\"artifactName\" :\"" + artifactName + "\",\n" + "\"artifactType\" : \"" + artifactType
134 + "\",\n" + "\"artifactGroupType\" : \"DEPLOYMENT\",\n" + "\"description\" : \"from CLAMP Cockpit\"\n"
138 private List<String> filterVfResourceList(String serviceUuid, List<SdcResource> sdcResourcesList,
139 List<String> cldsResourceVfList) {
140 List<String> urlList = new ArrayList<>();
141 for (SdcResource cldsSdcResource : sdcResourcesList) {
142 if (cldsSdcResource != null && cldsSdcResource.getResoucreType() != null
143 && cldsSdcResource.getResoucreType().equalsIgnoreCase("VF")
144 && cldsResourceVfList.contains(cldsSdcResource.getResourceInvariantUUID())) {
145 String normalizedResourceInstanceName = normalizeResourceInstanceName(
146 cldsSdcResource.getResourceInstanceName());
147 String svcUrl = createUrlForResource(normalizedResourceInstanceName, serviceUuid);
154 private String createUrlForResource(String normalizedResourceInstanceName, String serviceUuid) {
155 return refProp.getStringValue("sdc.serviceUrl") + "/" + serviceUuid + "/resourceInstances/"
156 + normalizedResourceInstanceName + "/artifacts";
160 * To get List of urls for all vfresources
163 * The model properties JSON describing the closed loop flow
164 * @return A list of Service URL
165 * @throws GeneralSecurityException
166 * In case of issues when decrypting the password
167 * @throws DecoderException
168 * In case of issues when decoding the Hex String
170 public List<String> getSdcReqUrlsList(ModelProperties prop) throws GeneralSecurityException, DecoderException {
171 List<String> urlList = new ArrayList<>();
172 Global globalProps = prop.getGlobal();
173 if (globalProps != null && globalProps.getService() != null && globalProps.getResourceVf() != null) {
174 String serviceUuid = sdcCatalogServices.getServiceUuidFromServiceInvariantId(globalProps.getService());
175 SdcServiceDetail sdcServiceDetail = sdcCatalogServices
176 .decodeCldsSdcServiceDetailFromJson(sdcCatalogServices.getSdcServicesInformation(serviceUuid));
177 if (sdcServiceDetail != null) {
178 urlList = filterVfResourceList(serviceUuid, sdcServiceDetail.getResources(),
179 globalProps.getResourceVf());
182 logger.warn("GlobalProperties json is empty, skipping getSdcReqUrlsList and returning empty list");
188 * "Normalize" the resource instance name: - Remove spaces, underscores,
189 * dashes, and periods. - make lower case This is required by SDC when using
190 * the resource instance name to upload an artifact.
195 public String normalizeResourceInstanceName(String inText) {
196 return inText.replace(" ", "").replace("-", "").replace(".", "").toLowerCase();
200 * Method to get yaml/template properties value from json.
203 * The Json containing a Yaml file
204 * @return The yaml extracted from the JSON
205 * @throws IOException
206 * In case of issues with the Json parser
208 protected String getYamlvalue(String jsonGlobal) throws IOException {
209 String yamlFileValue = "";
210 ObjectNode root = JacksonUtils.getObjectMapperInstance().readValue(jsonGlobal, ObjectNode.class);
211 Iterator<Entry<String, JsonNode>> entryItr = root.fields();
212 while (entryItr.hasNext()) {
213 Entry<String, JsonNode> entry = entryItr.next();
214 String key = entry.getKey();
215 if (key != null && key.equalsIgnoreCase("global")) {
216 ArrayNode arrayNode = (ArrayNode) entry.getValue();
217 for (JsonNode anArrayNode : arrayNode) {
218 ObjectNode node = (ObjectNode) anArrayNode;
219 ArrayNode arrayValueNode = (ArrayNode) node.get("value");
220 JsonNode jsonNode = arrayValueNode.get(0);
221 yamlFileValue = jsonNode.asText();
222 logger.info("value:" + yamlFileValue);
227 return yamlFileValue;