2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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;
26 import java.io.IOException;
27 import java.nio.charset.StandardCharsets;
28 import java.util.ArrayList;
29 import java.util.Base64;
30 import java.util.Iterator;
31 import java.util.List;
33 import java.util.Map.Entry;
35 import org.apache.commons.codec.digest.DigestUtils;
36 import org.camunda.bpm.engine.delegate.DelegateExecution;
37 import org.onap.clamp.clds.client.SdcCatalogServices;
38 import org.onap.clamp.clds.model.CldsSdcResource;
39 import org.onap.clamp.clds.model.CldsSdcServiceDetail;
40 import org.onap.clamp.clds.model.prop.Global;
41 import org.onap.clamp.clds.model.prop.ModelProperties;
42 import org.onap.clamp.clds.model.prop.StringMatch;
43 import org.onap.clamp.clds.model.prop.Tca;
44 import org.onap.clamp.clds.model.refprop.RefProp;
46 import com.att.eelf.configuration.EELFLogger;
47 import com.att.eelf.configuration.EELFManager;
48 import com.fasterxml.jackson.core.JsonParseException;
49 import com.fasterxml.jackson.core.JsonProcessingException;
50 import com.fasterxml.jackson.databind.JsonMappingException;
51 import com.fasterxml.jackson.databind.JsonNode;
52 import com.fasterxml.jackson.databind.ObjectMapper;
53 import com.fasterxml.jackson.databind.node.ArrayNode;
54 import com.fasterxml.jackson.databind.node.ObjectNode;
55 import com.fasterxml.jackson.dataformat.yaml.snakeyaml.Yaml;
58 * Construct a Sdc request given CLDS objects.
61 protected static final EELFLogger logger = EELFManager.getInstance().getLogger(SdcReq.class);
62 protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
68 * @throws JsonParseException
69 * @throws JsonMappingException
72 public static String formatBlueprint(RefProp refProp, ModelProperties prop, String docText)
73 throws JsonParseException, JsonMappingException, IOException {
75 Global globalProp = prop.getGlobal();
76 String service = globalProp.getService();
78 String yamlvalue = getYamlvalue(docText);
80 String updatedBlueprint = "";
81 StringMatch stringMatch = prop.getType(StringMatch.class);
82 Tca tca = prop.getType(Tca.class);
83 if (stringMatch.isFound()) {
84 prop.setCurrentModelElementId(stringMatch.getId());
85 ObjectMapper objectMapper = new ObjectMapper();
86 ObjectNode serviceConfigurations = objectMapper.createObjectNode();
88 StringMatchPolicyReq.appendServiceConfigurations(refProp, service, serviceConfigurations, stringMatch,
90 logger.info("Value of serviceConfigurations:" + serviceConfigurations);
91 ObjectNode servConfNode = (ObjectNode) serviceConfigurations.get("serviceConfigurations");
93 // get updated blueprint by attaching service Conf from
95 updatedBlueprint = getUpdatedBlueprintWithServiceConf(refProp, prop, yamlvalue, servConfNode);
96 } else if (tca.isFound()) {
97 prop.setCurrentModelElementId(tca.getId());
98 ObjectNode rootNode = (ObjectNode) refProp.getJsonTemplate("tca.template", service);
99 ObjectNode content = rootNode.with("content");
100 TcaMPolicyReq.appendSignatures(refProp, service, content, tca, prop);
101 logger.info("Value of content:" + content);
102 // ObjectNode servConfNode =
103 // (ObjectNode)signatures.get("signatures");
105 // get updated blueprint by attaching service Conf from
107 updatedBlueprint = getUpdatedBlueprintWithConfiguration(refProp, prop, yamlvalue, content);
110 logger.info("value of blueprint:" + updatedBlueprint);
111 return updatedBlueprint;
114 private static String getUpdatedBlueprintWithServiceConf(RefProp refProp, ModelProperties prop, String yamlValue,
115 ObjectNode serviceConf) throws IOException {
116 Yaml yaml = new Yaml();
118 // Serialiaze Yaml file
119 Map<String, Map> loadedYaml = (Map<String, Map>) yaml.load(yamlValue);
120 // Get node templates information from Yaml
121 Map<String, Map> nodeTemplates = loadedYaml.get("node_templates");
122 logger.info("value of NodeTemplates:" + nodeTemplates);
124 // Get StringMatch Object information from node templates of Yaml
125 Map<String, Map> smObject = nodeTemplates.get("SM");
126 logger.info("value of StringMatch:" + smObject);
128 // Get Properties Object information from stringmatch of Yaml
129 Map<String, String> propsObject = smObject.get("properties");
130 logger.info("value of PropsObject:" + propsObject);
132 String deploymentJsonObject = propsObject.get("deployment_JSON");
133 logger.info("value of deploymentJson:" + deploymentJsonObject);
135 ObjectMapper mapper = new ObjectMapper();
136 ObjectNode deployJsonNode = (ObjectNode) mapper.readTree(deploymentJsonObject);
137 ObjectNode configurationObjectNode = (ObjectNode) deployJsonNode.get("configuration");
139 // "policyName":"example_model06.ClosedLoop_FRWL_SIG_0538e6f2_8c1b_4656_9999_3501b3c59ad7_StringMatch_",
140 String policyNamePrefix = refProp.getStringValue("policy.ms.policyNamePrefix");
141 String policyName = prop.getCurrentPolicyScopeAndFullPolicyName(policyNamePrefix);
142 configurationObjectNode.put("policyName", policyName);
144 // "closedLoopControlName":"ClosedLoop-FRWL-SIG-0538e6f2-8c1b-4656-9999-3501b3c59ad7",
145 configurationObjectNode.put("closedLoopControlName", prop.getControlName());
146 configurationObjectNode.set("serviceConfigurations", serviceConf);
147 propsObject.put("deployment_JSON", deployJsonNode.toString());
148 String blueprint = yaml.dump(loadedYaml);
149 logger.info("value of updated Yaml File:" + blueprint);
151 blueprint = yaml.dump(loadedYaml);
152 logger.info("value of updated Yaml File:" + blueprint);
157 private static String getUpdatedBlueprintWithConfiguration(RefProp refProp, ModelProperties prop, String yamlValue,
158 ObjectNode serviceConf) throws JsonProcessingException, IOException {
159 String blueprint = "";
160 Yaml yaml = new Yaml();
161 // Serialiaze Yaml file
162 Map<String, Map> loadedYaml = (Map<String, Map>) yaml.load(yamlValue);
163 // Get node templates information from Yaml
164 Map<String, Map> nodeTemplates = loadedYaml.get("node_templates");
165 logger.info("value of NodeTemplates:" + nodeTemplates);
166 // Get Tca Object information from node templates of Yaml
167 Map<String, Map> tcaObject = nodeTemplates.get("MTCA");
168 logger.info("value of Tca:" + tcaObject);
169 // Get Properties Object information from tca of Yaml
170 Map<String, String> propsObject = tcaObject.get("properties");
171 logger.info("value of PropsObject:" + propsObject);
172 String deploymentJsonObject = propsObject.get("deployment_JSON");
173 logger.info("value of deploymentJson:" + deploymentJsonObject);
175 ObjectMapper mapper = new ObjectMapper();
177 ObjectNode deployJsonNode = (ObjectNode) mapper.readTree(deploymentJsonObject);
179 // "policyName":"example_model06.ClosedLoop_FRWL_SIG_0538e6f2_8c1b_4656_9999_3501b3c59ad7_Tca_",
180 String policyNamePrefix = refProp.getStringValue("policy.ms.policyNamePrefix");
181 String policyName = prop.getCurrentPolicyScopeAndFullPolicyName(policyNamePrefix);
182 serviceConf.put("policyName", policyName);
184 deployJsonNode.set("configuration", serviceConf);
185 propsObject.put("deployment_JSON", deployJsonNode.toString());
186 blueprint = yaml.dump(loadedYaml);
187 logger.info("value of updated Yaml File:" + blueprint);
192 public static String formatSdcLocationsReq(ModelProperties prop, String artifactName) {
193 ObjectMapper objectMapper = new ObjectMapper();
194 Global global = prop.getGlobal();
195 List<String> locationsList = global.getLocation();
196 ArrayNode locationsArrayNode = objectMapper.createArrayNode();
197 ObjectNode locationObject = objectMapper.createObjectNode();
198 for (String currLocation : locationsList) {
199 locationsArrayNode.add(currLocation);
201 locationObject.put("artifactName", artifactName);
202 locationObject.putPOJO("locations", locationsArrayNode);
203 String locationJsonFormat = locationObject.toString();
204 logger.info("Value of locaation Json Artifact:" + locationsArrayNode);
205 return locationJsonFormat;
208 public static String formatSdcReq(String payloadData, String artifactName, String artifactLabel,
209 String artifactType) throws IOException {
210 logger.info("artifact=" + payloadData);
211 String base64Artifact = base64Encode(payloadData);
212 return "{ \n" + "\"payloadData\" : \"" + base64Artifact + "\",\n" + "\"artifactLabel\" : \"" + artifactLabel
213 + "\",\n" + "\"artifactName\" :\"" + artifactName + "\",\n" + "\"artifactType\" : \"" + artifactType
214 + "\",\n" + "\"artifactGroupType\" : \"DEPLOYMENT\",\n" + "\"description\" : \"from CLAMP Cockpit\"\n"
218 public static String getSdcReqUrl(ModelProperties prop, String url) {
219 Global globalProps = prop.getGlobal();
220 String serviceUUID = "";
221 String resourceInstanceName = "";
222 if (globalProps != null) {
223 List<String> resourceVf = globalProps.getResourceVf();
224 if (resourceVf != null && resourceVf.size() > 0) {
225 resourceInstanceName = resourceVf.get(0);
227 if (globalProps.getService() != null) {
228 serviceUUID = globalProps.getService();
231 String normalizedResourceInstanceName = normalizeResourceInstanceName(resourceInstanceName);
232 return url + "/" + serviceUUID + "/resourceInstances/" + normalizedResourceInstanceName + "/artifacts";
236 * To get List of urls for all vfresources
240 * @param sdcCatalogServices
244 public static List<String> getSdcReqUrlsList(ModelProperties prop, String baseUrl,
245 SdcCatalogServices sdcCatalogServices, DelegateExecution execution) throws Exception {
246 // TODO : refact and regroup with very similar code
247 List<String> urlList = new ArrayList<>();
248 Global globalProps = prop.getGlobal();
249 if (globalProps != null) {
250 if (globalProps.getService() != null) {
251 String serviceInvariantUUID = globalProps.getService();
252 execution.setVariable("serviceInvariantUUID", serviceInvariantUUID);
253 List<String> resourceVfList = globalProps.getResourceVf();
254 String serviceUUID = sdcCatalogServices.getServiceUuidFromServiceInvariantId(serviceInvariantUUID);
255 String sdcServicesInformation = sdcCatalogServices.getSdcServicesInformation(serviceUUID);
256 CldsSdcServiceDetail CldsSdcServiceDetail = sdcCatalogServices
257 .getCldsSdcServiceDetailFromJson(sdcServicesInformation);
258 if (CldsSdcServiceDetail != null && resourceVfList != null) {
259 List<CldsSdcResource> CldsSdcResourcesList = CldsSdcServiceDetail.getResources();
260 if (CldsSdcResourcesList != null && CldsSdcResourcesList.size() > 0) {
261 for (CldsSdcResource CldsSdcResource : CldsSdcResourcesList) {
262 if (CldsSdcResource != null && CldsSdcResource.getResoucreType() != null
263 && CldsSdcResource.getResoucreType().equalsIgnoreCase("VF")) {
264 if (resourceVfList.contains(CldsSdcResource.getResourceInvariantUUID())) {
265 String normalizedResourceInstanceName = normalizeResourceInstanceName(
266 CldsSdcResource.getResourceInstanceName());
267 String svcUrl = baseUrl + "/" + serviceUUID + "/resourceInstances/"
268 + normalizedResourceInstanceName + "/artifacts";
281 * "Normalize" the resource instance name: - Remove spaces, underscores,
282 * dashes, and periods. - make lower case This is required by SDC when using
283 * the resource instance name to upload an artifact.
288 public static String normalizeResourceInstanceName(String inText) {
289 return inText.replace(" ", "").replace("-", "").replace(".", "").toLowerCase();
298 public static String calculateMD5ByString(String data) {
299 String calculatedMd5 = DigestUtils.md5Hex(data);
300 // encode base-64 result
301 return base64Encode(calculatedMd5.getBytes());
305 * Base 64 encode a String.
310 public static String base64Encode(String inText) {
311 return base64Encode(stringToByteArray(inText));
315 * Convert String to byte array.
320 public static byte[] stringToByteArray(String inText) {
321 return inText.getBytes(StandardCharsets.UTF_8);
325 * Base 64 encode a byte array.
330 public static String base64Encode(byte[] bytes) {
331 Base64.Encoder encoder = Base64.getEncoder();
332 return encoder.encodeToString(bytes);
336 * Return SDC id and pw as a HTTP Basic Auth string (for example: Basic
341 public static String getSdcBasicAuth(RefProp refProp) {
342 String sdcId = refProp.getStringValue("sdc.serviceUsername");
343 String sdcPw = refProp.getStringValue("sdc.servicePassword");
344 String idPw = base64Encode(sdcId + ":" + sdcPw);
345 return "Basic " + idPw;
348 private static String getYamlvalue(String docText) throws IOException {
349 ObjectMapper objectMapper = new ObjectMapper();
350 String yamlFileValue = "";
351 ObjectNode root = objectMapper.readValue(docText, ObjectNode.class);
352 Iterator<Entry<String, JsonNode>> entryItr = root.fields();
353 while (entryItr.hasNext()) {
354 Entry<String, JsonNode> entry = entryItr.next();
355 String key = entry.getKey();
356 if (key != null && key.equalsIgnoreCase("global")) {
357 ArrayNode arrayNode = (ArrayNode) entry.getValue();
358 for (JsonNode anArrayNode : arrayNode) {
359 ObjectNode node = (ObjectNode) anArrayNode;
360 ArrayNode arrayValueNode = (ArrayNode) node.get("value");
361 JsonNode jsonNode = arrayValueNode.get(0);
362 yamlFileValue = jsonNode.asText();
363 logger.info("value:" + yamlFileValue);
368 return yamlFileValue;