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 * ===================================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
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.springframework.beans.factory.annotation.Autowired;
51 import org.springframework.stereotype.Component;
54 * Construct a Sdc request given CLDS objects.
57 public class SdcRequests {
59 protected static final EELFLogger logger = EELFManager.getInstance().getLogger(SdcRequests.class);
60 protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
62 private SdcCatalogServices sdcCatalogServices;
64 protected ClampProperties refProp;
67 * Format the Blueprint from a Yaml
70 * The ModelProperties describing the clds model
72 * The Yaml file that must be converted
73 * @return A String containing the BluePrint
77 public String formatBlueprint(ModelProperties prop, String docText) throws IOException {
78 String yamlvalue = getYamlvalue(docText);
79 String updatedBlueprint = "";
80 Tca tca = prop.getType(Tca.class);
82 updatedBlueprint = TcaRequestFormatter.updatedBlueprintWithConfiguration(refProp, prop, yamlvalue);
84 logger.info("value of blueprint:" + updatedBlueprint);
85 return updatedBlueprint;
89 * Format the SDC Locations Request in the JSON Format
92 * The ModelProperties describing the clds model
94 * The name of the artifact
95 * @return SDC Locations request in the JSON Format
97 public String formatSdcLocationsReq(ModelProperties prop, String artifactName) {
98 ObjectMapper objectMapper = new ObjectMapper();
99 Global global = prop.getGlobal();
100 List<String> locationsList = global.getLocation();
101 ArrayNode locationsArrayNode = objectMapper.createArrayNode();
102 ObjectNode locationObject = objectMapper.createObjectNode();
103 for (String currLocation : locationsList) {
104 locationsArrayNode.add(currLocation);
106 locationObject.put("artifactName", artifactName);
107 locationObject.putPOJO("locations", locationsArrayNode);
108 String locationJsonFormat = locationObject.toString();
109 logger.info("Value of location Json Artifact:" + locationsArrayNode);
110 return locationJsonFormat;
114 * Format the SDC Request
117 * The ModelProperties describing the clds model
118 * @param artifactName
119 * The name of the artifact
120 * @param artifactLabel
121 * The Label of the artifact
122 * @param artifactType
123 * The type of the artifact
124 * @return formatted SDC Request
126 public String formatSdcReq(String payloadData, String artifactName, String artifactLabel, String artifactType) {
127 logger.info("artifact=" + payloadData);
128 String base64Artifact = Base64.getEncoder().encodeToString(payloadData.getBytes(StandardCharsets.UTF_8));
129 return "{ \n" + "\"payloadData\" : \"" + base64Artifact + "\",\n" + "\"artifactLabel\" : \"" + artifactLabel
130 + "\",\n" + "\"artifactName\" :\"" + artifactName + "\",\n" + "\"artifactType\" : \"" + artifactType
131 + "\",\n" + "\"artifactGroupType\" : \"DEPLOYMENT\",\n" + "\"description\" : \"from CLAMP Cockpit\"\n"
135 private List<String> filterVfResourceList(String serviceUuid, List<SdcResource> sdcResourcesList,
136 List<String> cldsResourceVfList) {
137 List<String> urlList = new ArrayList<>();
138 for (SdcResource cldsSdcResource : sdcResourcesList) {
139 if (cldsSdcResource != null && cldsSdcResource.getResoucreType() != null
140 && cldsSdcResource.getResoucreType().equalsIgnoreCase("VF")
141 && cldsResourceVfList.contains(cldsSdcResource.getResourceInvariantUUID())) {
142 String normalizedResourceInstanceName = normalizeResourceInstanceName(
143 cldsSdcResource.getResourceInstanceName());
144 String svcUrl = createUrlForResource(normalizedResourceInstanceName, serviceUuid);
151 private String createUrlForResource(String normalizedResourceInstanceName, String serviceUuid) {
152 return refProp.getStringValue("sdc.serviceUrl") + "/" + serviceUuid + "/resourceInstances/"
153 + normalizedResourceInstanceName + "/artifacts";
157 * To get List of urls for all vfresources
160 * The model properties JSON describing the closed loop flow
161 * @return A list of Service URL
162 * @throws GeneralSecurityException
163 * In case of issues when decrypting the password
164 * @throws DecoderException
165 * In case of issues when decoding the Hex String
167 public List<String> getSdcReqUrlsList(ModelProperties prop) throws GeneralSecurityException, DecoderException {
168 List<String> urlList = new ArrayList<>();
169 Global globalProps = prop.getGlobal();
170 if (globalProps != null && globalProps.getService() != null && globalProps.getResourceVf() != null) {
171 String serviceUuid = sdcCatalogServices.getServiceUuidFromServiceInvariantId(globalProps.getService());
172 SdcServiceDetail sdcServiceDetail = sdcCatalogServices
173 .decodeCldsSdcServiceDetailFromJson(sdcCatalogServices.getSdcServicesInformation(serviceUuid));
174 if (sdcServiceDetail != null) {
175 urlList = filterVfResourceList(serviceUuid, sdcServiceDetail.getResources(),
176 globalProps.getResourceVf());
179 logger.warn("GlobalProperties json is empty, skipping getSdcReqUrlsList and returning empty list");
185 * "Normalize" the resource instance name: - Remove spaces, underscores,
186 * dashes, and periods. - make lower case This is required by SDC when using
187 * the resource instance name to upload an artifact.
192 public String normalizeResourceInstanceName(String inText) {
193 return inText.replace(" ", "").replace("-", "").replace(".", "").toLowerCase();
197 * Method to get yaml/template properties value from json.
200 * The Json containing a Yaml file
201 * @return The yaml extracted from the JSON
202 * @throws IOException
203 * In case of issues with the Json parser
205 protected String getYamlvalue(String jsonGlobal) throws IOException {
206 ObjectMapper objectMapper = new ObjectMapper();
207 String yamlFileValue = "";
208 ObjectNode root = objectMapper.readValue(jsonGlobal, ObjectNode.class);
209 Iterator<Entry<String, JsonNode>> entryItr = root.fields();
210 while (entryItr.hasNext()) {
211 Entry<String, JsonNode> entry = entryItr.next();
212 String key = entry.getKey();
213 if (key != null && key.equalsIgnoreCase("global")) {
214 ArrayNode arrayNode = (ArrayNode) entry.getValue();
215 for (JsonNode anArrayNode : arrayNode) {
216 ObjectNode node = (ObjectNode) anArrayNode;
217 ArrayNode arrayValueNode = (ArrayNode) node.get("value");
218 JsonNode jsonNode = arrayValueNode.get(0);
219 yamlFileValue = jsonNode.asText();
220 logger.info("value:" + yamlFileValue);
225 return yamlFileValue;