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.put("messageReaderConsumerGroup", prop.getModelName());
147 configurationObjectNode.set("serviceConfigurations", serviceConf);
148 propsObject.put("deployment_JSON", deployJsonNode.toString());
149 String blueprint = yaml.dump(loadedYaml);
150 logger.info("value of updated Yaml File:" + blueprint);
152 blueprint = yaml.dump(loadedYaml);
153 logger.info("value of updated Yaml File:" + blueprint);
158 private static String getUpdatedBlueprintWithConfiguration(RefProp refProp, ModelProperties prop, String yamlValue,
159 ObjectNode serviceConf) throws JsonProcessingException, IOException {
160 String blueprint = "";
161 Yaml yaml = new Yaml();
162 // Serialiaze Yaml file
163 Map<String, Map> loadedYaml = (Map<String, Map>) yaml.load(yamlValue);
164 // Get node templates information from Yaml
165 Map<String, Map> nodeTemplates = loadedYaml.get("node_templates");
166 logger.info("value of NodeTemplates:" + nodeTemplates);
167 // Get Tca Object information from node templates of Yaml
168 Map<String, Map> tcaObject = nodeTemplates.get("MTCA");
169 logger.info("value of Tca:" + tcaObject);
170 // Get Properties Object information from tca of Yaml
171 Map<String, String> propsObject = tcaObject.get("properties");
172 logger.info("value of PropsObject:" + propsObject);
173 String deploymentJsonObject = propsObject.get("deployment_JSON");
174 logger.info("value of deploymentJson:" + deploymentJsonObject);
176 ObjectMapper mapper = new ObjectMapper();
178 ObjectNode deployJsonNode = (ObjectNode) mapper.readTree(deploymentJsonObject);
180 // "policyName":"example_model06.ClosedLoop_FRWL_SIG_0538e6f2_8c1b_4656_9999_3501b3c59ad7_Tca_",
181 String policyNamePrefix = refProp.getStringValue("policy.ms.policyNamePrefix");
182 String policyName = prop.getCurrentPolicyScopeAndFullPolicyName(policyNamePrefix);
183 serviceConf.put("policyName", policyName);
185 deployJsonNode.set("configuration", serviceConf);
186 propsObject.put("deployment_JSON", deployJsonNode.toString());
187 blueprint = yaml.dump(loadedYaml);
188 logger.info("value of updated Yaml File:" + blueprint);
193 public static String formatSdcLocationsReq(ModelProperties prop, String artifactName) {
194 ObjectMapper objectMapper = new ObjectMapper();
195 Global global = prop.getGlobal();
196 List<String> locationsList = global.getLocation();
197 ArrayNode locationsArrayNode = objectMapper.createArrayNode();
198 ObjectNode locationObject = objectMapper.createObjectNode();
199 for (String currLocation : locationsList) {
200 locationsArrayNode.add(currLocation);
202 locationObject.put("artifactName", artifactName);
203 locationObject.putPOJO("locations", locationsArrayNode);
204 String locationJsonFormat = locationObject.toString();
205 logger.info("Value of locaation Json Artifact:" + locationsArrayNode);
206 return locationJsonFormat;
209 public static String formatSdcReq(String payloadData, String artifactName, String artifactLabel,
210 String artifactType) throws IOException {
211 logger.info("artifact=" + payloadData);
212 String base64Artifact = base64Encode(payloadData);
213 return "{ \n" + "\"payloadData\" : \"" + base64Artifact + "\",\n" + "\"artifactLabel\" : \"" + artifactLabel
214 + "\",\n" + "\"artifactName\" :\"" + artifactName + "\",\n" + "\"artifactType\" : \"" + artifactType
215 + "\",\n" + "\"artifactGroupType\" : \"DEPLOYMENT\",\n" + "\"description\" : \"from CLAMP Cockpit\"\n"
219 public static String getSdcReqUrl(ModelProperties prop, String url) {
220 Global globalProps = prop.getGlobal();
221 String serviceUUID = "";
222 String resourceInstanceName = "";
223 if (globalProps != null) {
224 List<String> resourceVf = globalProps.getResourceVf();
225 if (resourceVf != null && resourceVf.size() > 0) {
226 resourceInstanceName = resourceVf.get(0);
228 if (globalProps.getService() != null) {
229 serviceUUID = globalProps.getService();
232 String normalizedResourceInstanceName = normalizeResourceInstanceName(resourceInstanceName);
233 return url + "/" + serviceUUID + "/resourceInstances/" + normalizedResourceInstanceName + "/artifacts";
237 * To get List of urls for all vfresources
241 * @param sdcCatalogServices
245 public static List<String> getSdcReqUrlsList(ModelProperties prop, String baseUrl,
246 SdcCatalogServices sdcCatalogServices, DelegateExecution execution) throws Exception {
247 // TODO : refact and regroup with very similar code
248 List<String> urlList = new ArrayList<>();
249 Global globalProps = prop.getGlobal();
250 if (globalProps != null) {
251 if (globalProps.getService() != null) {
252 String serviceInvariantUUID = globalProps.getService();
253 execution.setVariable("serviceInvariantUUID", serviceInvariantUUID);
254 List<String> resourceVfList = globalProps.getResourceVf();
255 String serviceUUID = sdcCatalogServices.getServiceUuidFromServiceInvariantId(serviceInvariantUUID);
256 String sdcServicesInformation = sdcCatalogServices.getSdcServicesInformation(serviceUUID);
257 CldsSdcServiceDetail CldsSdcServiceDetail = sdcCatalogServices
258 .getCldsSdcServiceDetailFromJson(sdcServicesInformation);
259 if (CldsSdcServiceDetail != null && resourceVfList != null) {
260 List<CldsSdcResource> CldsSdcResourcesList = CldsSdcServiceDetail.getResources();
261 if (CldsSdcResourcesList != null && CldsSdcResourcesList.size() > 0) {
262 for (CldsSdcResource CldsSdcResource : CldsSdcResourcesList) {
263 if (CldsSdcResource != null && CldsSdcResource.getResoucreType() != null
264 && CldsSdcResource.getResoucreType().equalsIgnoreCase("VF")) {
265 if (resourceVfList.contains(CldsSdcResource.getResourceInvariantUUID())) {
266 String normalizedResourceInstanceName = normalizeResourceInstanceName(
267 CldsSdcResource.getResourceInstanceName());
268 String svcUrl = baseUrl + "/" + serviceUUID + "/resourceInstances/"
269 + normalizedResourceInstanceName + "/artifacts";
282 * "Normalize" the resource instance name: - Remove spaces, underscores,
283 * dashes, and periods. - make lower case This is required by SDC when using
284 * the resource instance name to upload an artifact.
289 public static String normalizeResourceInstanceName(String inText) {
290 return inText.replace(" ", "").replace("-", "").replace(".", "").toLowerCase();
299 public static String calculateMD5ByString(String data) {
300 String calculatedMd5 = DigestUtils.md5Hex(data);
301 // encode base-64 result
302 return base64Encode(calculatedMd5.getBytes());
306 * Base 64 encode a String.
311 public static String base64Encode(String inText) {
312 return base64Encode(stringToByteArray(inText));
316 * Convert String to byte array.
321 public static byte[] stringToByteArray(String inText) {
322 return inText.getBytes(StandardCharsets.UTF_8);
326 * Base 64 encode a byte array.
331 public static String base64Encode(byte[] bytes) {
332 Base64.Encoder encoder = Base64.getEncoder();
333 return encoder.encodeToString(bytes);
337 * Return SDC id and pw as a HTTP Basic Auth string (for example: Basic
342 public static String getSdcBasicAuth(RefProp refProp) {
343 String sdcId = refProp.getStringValue("sdc.serviceUsername");
344 String sdcPw = refProp.getStringValue("sdc.servicePassword");
345 String idPw = base64Encode(sdcId + ":" + sdcPw);
346 return "Basic " + idPw;
349 private static String getYamlvalue(String docText) throws IOException {
350 ObjectMapper objectMapper = new ObjectMapper();
351 String yamlFileValue = "";
352 ObjectNode root = objectMapper.readValue(docText, ObjectNode.class);
353 Iterator<Entry<String, JsonNode>> entryItr = root.fields();
354 while (entryItr.hasNext()) {
355 Entry<String, JsonNode> entry = entryItr.next();
356 String key = entry.getKey();
357 if (key != null && key.equalsIgnoreCase("global")) {
358 ArrayNode arrayNode = (ArrayNode) entry.getValue();
359 for (JsonNode anArrayNode : arrayNode) {
360 ObjectNode node = (ObjectNode) anArrayNode;
361 ArrayNode arrayValueNode = (ArrayNode) node.get("value");
362 JsonNode jsonNode = arrayValueNode.get(0);
363 yamlFileValue = jsonNode.asText();
364 logger.info("value:" + yamlFileValue);
369 return yamlFileValue;