2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.artifact.handler.node;
26 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ACTION;
27 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ACTION_LEVEL;
28 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ACTION_LEVEL_VF_MODULE;
29 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ACTION_LEVEL_VM;
30 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ACTION_LEVEL_VNF;
31 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ACTION_LEVEL_VNFC;
32 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ARTIFACT_CONTENTS;
33 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ARTIFACT_DESRIPTION;
34 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ARTIFACT_NAME;
35 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ARTIFACT_NAME_CAPABILITY;
36 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ARTIFACT_NAME_REFERENCE;
37 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ARTIFACT_TYPE;
38 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ARTIFACT_UUID;
39 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ARTIFACT_VERSION;
40 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.CAPABILITY;
41 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.DB_CONFIG_ACTION_DG;
42 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.DB_DEVICE_AUTHENTICATION;
43 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.DB_DEVICE_INTERFACE_PROTOCOL;
44 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE;
45 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.DB_SDC_REFERENCE;
46 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.DEVICE_PROTOCOL;
47 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.DISTRIBUTION_ID;
48 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.DOCUMENT_PARAMETERS;
49 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE;
50 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.FILE_CATEGORY;
51 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.GROUP_NOTATION_TYPE;
52 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.GROUP_NOTATION_VALUE;
53 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.IPADDRESS_V4_OAM_VIP;
54 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.PARAMETER_YANG;
55 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.PD;
56 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.PORT_NUMBER;
57 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.REFERENCE;
58 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.REQUEST_ID;
59 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.REQUEST_INFORMATION;
60 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.RESOURCE_INSTANCE_NAME;
61 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.RESOURCE_TYPE;
62 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.RESOURCE_UUID;
63 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.RESOURCE_VERSION;
64 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.SERVICE_DESCRIPTION;
65 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.SERVICE_NAME;
66 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.SERVICE_UUID;
67 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.TEMPLATE;
68 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.TEMPLATE_ID;
69 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.TOSCA_MODEL;
70 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.USER_NAME;
71 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.VM;
72 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.VM_INSTANCE;
73 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.VNFC;
74 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.VNFC_FUNCTION_CODE;
75 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.VNFC_FUNCTION_CODE_LIST;
76 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.VNFC_INSTANCE;
77 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.VNFC_TYPE;
78 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.VNFC_TYPE_LIST;
79 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.VNF_TYPE;
80 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.URL;
81 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.OPENSTACK;
83 import com.att.eelf.configuration.EELFLogger;
84 import com.att.eelf.configuration.EELFManager;
85 import java.io.ByteArrayOutputStream;
86 import java.io.OutputStream;
87 import java.sql.SQLException;
89 import java.util.function.Function;
90 import org.apache.commons.configuration.ConfigurationException;
91 import org.apache.commons.lang.StringUtils;
92 import org.json.JSONArray;
93 import org.json.JSONObject;
94 import org.onap.appc.artifact.handler.dbservices.DBException;
95 import org.onap.appc.artifact.handler.dbservices.DBService;
96 import org.onap.appc.artifact.handler.utils.ArtifactHandlerProviderUtil;
97 import org.onap.appc.yang.YANGGenerator;
98 import org.onap.appc.yang.impl.YANGGeneratorFactory;
99 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
100 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
101 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
102 import org.onap.sdnc.config.params.transformer.tosca.ArtifactProcessorImpl;
103 import org.onap.sdnc.config.params.transformer.tosca.exceptions.ArtifactProcessorException;
105 public class ArtifactHandlerNode implements SvcLogicJavaPlugin {
107 private static final EELFLogger log = EELFManager.getInstance().getLogger(ArtifactHandlerNode.class);
108 private static final String TOSCA_PARAM = "Tosca";
109 private static final String YANG_PARAM = "Yang";
110 private static final String ARTIFACT_LIST_PARAM = "artifact-list";
111 private static final String CONFIGURE_PARAM = "Configure";
112 private static final String CONFIG_SCALE_OUT_PARAM = "ConfigScaleOut";
113 private static final String CONFIG_MODIFY_PARAM = "ConfigModify";
115 public void processArtifact(Map<String, String> inParams, SvcLogicContext ctx) throws ArtifactProcessorException {
117 if (inParams == null || inParams.isEmpty()) {
120 String postData = inParams.get("postData");
121 if (postData == null || postData.isEmpty()) {
125 log.info("Received request for process Artifact with params: " + inParams.toString());
126 JSONObject input = new JSONObject(postData).getJSONObject("input");
127 storeUpdateSdcArtifacts(input);
128 } catch (Exception e) {
129 log.error("Error when processing artifact", e);
130 throw new ArtifactProcessorException("Error occurred while processing artifact", e);
134 private boolean storeUpdateSdcArtifacts(JSONObject postDataJson) throws ArtifactHandlerInternalException {
135 log.info("Starting processing of SDC Artifacs into Handler with Data : " + postDataJson.toString());
137 JSONObject requestInfo = (JSONObject) postDataJson.get(REQUEST_INFORMATION);
138 JSONObject documentInfo = (JSONObject) postDataJson.get(DOCUMENT_PARAMETERS);
139 String artifactName = documentInfo.getString(ARTIFACT_NAME);
141 if (artifactName != null) {
142 updateStoreArtifacts(requestInfo, documentInfo);
143 if (artifactName.toLowerCase().startsWith(REFERENCE)) {
144 return storeReferenceData(requestInfo, documentInfo);
145 } else if (artifactName.toLowerCase().startsWith(PD)) {
146 return createDataForPD(requestInfo, documentInfo);
150 throw new ArtifactHandlerInternalException("Missing Artifact Name for Request: "
151 + requestInfo.getString(REQUEST_ID));
153 } catch (Exception e) {
154 log.error("Error while processing request with id: "
155 + ((JSONObject) postDataJson.get(REQUEST_INFORMATION)).getString(REQUEST_ID), e);
157 throw new ArtifactHandlerInternalException("Error while processing request with id: "
158 + ((JSONObject) postDataJson.get(REQUEST_INFORMATION)).getString(REQUEST_ID), e);
163 private boolean createDataForPD(JSONObject requestInfo, JSONObject documentInfo)
164 throws ArtifactHandlerInternalException {
166 String fn = "ArtifactHandlerNode.createReferenceDataForPD";
167 String artifactName = documentInfo.getString(ARTIFACT_NAME);
168 log.info(fn + "Received PD File Name: " + artifactName + " and suffix lenght "
172 String suffix = artifactName.substring(PD.length());
173 createArtifactRecords(requestInfo, documentInfo, suffix);
174 } catch (Exception e) {
175 log.error("Error while creating PD data records", e);
176 throw new ArtifactHandlerInternalException("Error while creating PD data records", e);
181 private void createArtifactRecords(JSONObject requestInfo, JSONObject documentInfo, String suffix)
182 throws ArtifactHandlerInternalException {
185 log.info("Creating Tosca Records and storing into SDC Artifacs");
186 String[] docs = {TOSCA_PARAM, YANG_PARAM};
187 ArtifactHandlerProviderUtil ahpUtil = new ArtifactHandlerProviderUtil();
188 String pdFileContents = documentInfo.getString(ARTIFACT_CONTENTS);
191 OutputStream toscaStream = new ByteArrayOutputStream();
192 String toscaContents;
193 ArtifactProcessorImpl toscaGenerator = new ArtifactProcessorImpl();
194 toscaGenerator.generateArtifact(pdFileContents, toscaStream);
195 toscaContents = toscaStream.toString();
196 log.info("Generated Tosca File : " + toscaContents);
198 String yangContents = "YANG generation is in Progress";
199 String yangName = null;
201 for (String doc : docs) {
202 documentInfo.put(ARTIFACT_TYPE, doc.concat("Type"));
203 documentInfo.put(ARTIFACT_DESRIPTION, doc.concat("Model"));
204 if (doc.equals(TOSCA_PARAM)) {
205 documentInfo.put(ARTIFACT_CONTENTS, ahpUtil.escapeSql(toscaContents));
206 } else if (doc.equals(YANG_PARAM)) {
207 documentInfo.put(ARTIFACT_CONTENTS, ahpUtil.escapeSql(yangContents));
209 documentInfo.put(ARTIFACT_NAME, doc.concat(suffix));
210 yangName = doc.concat(suffix);
211 updateStoreArtifacts(requestInfo, documentInfo);
214 String artifactId = getArtifactID(yangName);
215 OutputStream yangStream = new ByteArrayOutputStream();
216 YANGGenerator yangGenerator = YANGGeneratorFactory.getYANGGenerator();
217 yangGenerator.generateYANG(artifactId, toscaContents, yangStream);
218 yangContents = yangStream.toString();
220 if (yangContents != null) {
221 updateYangContents(artifactId, ahpUtil.escapeSql(yangContents));
223 } catch (Exception e) {
224 log.error("Error while creating artifact records", e);
225 throw new ArtifactHandlerInternalException("Error while creating artifact records", e);
230 private void updateYangContents(String artifactId, String yangContents) throws SvcLogicException {
231 SvcLogicContext context = new SvcLogicContext();
232 DBService dbservice = DBService.initialise();
233 dbservice.updateYangContents(context, artifactId, yangContents);
236 private String getArtifactID(String yangName) throws SvcLogicException {
237 SvcLogicContext context = new SvcLogicContext();
238 DBService dbservice = DBService.initialise();
239 return dbservice.getArtifactID(context, yangName);
242 protected boolean updateStoreArtifacts(JSONObject requestInfo, JSONObject documentInfo)
243 throws SvcLogicException {
244 log.info("UpdateStoreArtifactsStarted storing of SDC Artifacs ");
246 SvcLogicContext context = new SvcLogicContext();
247 DBService dbservice = DBService.initialise();
248 ArtifactHandlerProviderUtil ahpUtil = new ArtifactHandlerProviderUtil();
250 context.setAttribute("artifact_name",
251 documentInfo.getString(ARTIFACT_NAME));
252 String internalVersion = dbservice.getInternalVersionNumber(context,
253 documentInfo.getString(ARTIFACT_NAME), null);
254 log.info("Internal Version number received from Database : " + internalVersion);
255 if (internalVersion != null) {
256 intversion = Integer.parseInt(internalVersion);
259 setAttribute(context, documentInfo::getString, SERVICE_UUID);
260 setAttribute(context, documentInfo::getString, DISTRIBUTION_ID);
261 setAttribute(context, documentInfo::getString, SERVICE_NAME);
262 setAttribute(context, documentInfo::getString, SERVICE_DESCRIPTION);
263 setAttribute(context, documentInfo::getString, RESOURCE_UUID);
264 setAttribute(context, documentInfo::getString, RESOURCE_INSTANCE_NAME);
265 setAttribute(context, documentInfo::getString, RESOURCE_VERSION);
266 setAttribute(context, documentInfo::getString, RESOURCE_TYPE);
267 setAttribute(context, documentInfo::getString, ARTIFACT_UUID);
268 setAttribute(context, documentInfo::getString, ARTIFACT_TYPE);
269 setAttribute(context, documentInfo::getString, ARTIFACT_VERSION);
270 setAttribute(context, documentInfo::getString, ARTIFACT_DESRIPTION);
271 setAttribute(context, documentInfo::getString, ARTIFACT_NAME);
272 setAttribute(context, s -> ahpUtil.escapeSql(documentInfo.getString(s)), ARTIFACT_CONTENTS);
274 dbservice.saveArtifacts(context, intversion);
278 public boolean storeReferenceData(JSONObject requestInfo, JSONObject documentInfo)
279 throws ArtifactHandlerInternalException {
280 log.info("Started storing of SDC Artifacs into Handler");
282 DBService dbservice = DBService.initialise();
283 ArtifactHandlerProviderUtil ahpUtil = new ArtifactHandlerProviderUtil();
284 String contentString =
285 ahpUtil.escapeSql(documentInfo.getString(ARTIFACT_CONTENTS));
286 String artifactName =
287 ahpUtil.escapeSql(documentInfo.getString(ARTIFACT_NAME));
288 String capabilityArtifactName =
289 StringUtils.replace(artifactName, ARTIFACT_NAME_REFERENCE,
290 ARTIFACT_NAME_CAPABILITY);
291 JSONObject capabilities = new JSONObject();
292 JSONArray vnfActionList = new JSONArray();
293 JSONArray vfModuleActionList = new JSONArray();
294 JSONArray vnfcActionList = new JSONArray();
295 JSONArray vmActionVnfcFunctionCodesList = new JSONArray();
296 String vnfType = null;
297 JSONObject contentObject = new JSONObject(contentString);
298 JSONArray contentArray = contentObject.getJSONArray("reference_data");
299 boolean storeCapabilityArtifact = true;
301 for (int a = 0; a < contentArray.length(); a++) {
302 JSONObject content = (JSONObject) contentArray.get(a);
303 log.info("contentString =" + content.toString());
304 JSONObject scope = content.getJSONObject("scope");
305 log.info("scope :" + scope);
306 SvcLogicContext context = new SvcLogicContext();
307 vnfType = scope.getString(VNF_TYPE);
308 setAttribute(context, scope::getString, VNF_TYPE);
309 setAttribute(context, content::getString, ACTION);
310 String actionLevel = content.getString(ACTION_LEVEL);
311 setAttribute(context, content::getString, ACTION_LEVEL);
312 setAttribute(context, documentInfo::getString, ARTIFACT_TYPE);
313 processActionLists(content, actionLevel, vnfcActionList, vfModuleActionList, vnfActionList,
314 vmActionVnfcFunctionCodesList);
315 JSONArray vnfcTypeList = setVnfcTypeInformation(scope, context);
316 storeCapabilityArtifact = isCapabilityArtifactNeeded(context);
317 if (content.has(DEVICE_PROTOCOL)) {
318 setAttribute(context, content::getString, DEVICE_PROTOCOL);
320 if (content.has(USER_NAME)) {
321 setAttribute(context, content::getString, USER_NAME);
323 if (content.has(PORT_NUMBER)) {
324 setAttribute(context, content::getString, PORT_NUMBER);
326 if (content.has(URL)) {
327 setAttribute(context, content::getString, URL);
329 processArtifactList(content, dbservice, context, vnfcTypeList);
330 processConfigTypeActions(content, dbservice, context);
331 dbservice.processDeviceAuthentication(context,
332 dbservice.isArtifactUpdateRequired(context, DB_DEVICE_AUTHENTICATION));
334 String actionProtocol = tryGetProtocol(content);
335 if (!StringUtils.equalsIgnoreCase(actionProtocol, OPENSTACK)) {
336 populateProtocolReference(dbservice, content);
339 context.setAttribute(VNFC_TYPE, null);
342 && content.get(VM) instanceof JSONArray) {
343 processVmList(content, context, dbservice);
346 if (storeCapabilityArtifact) {
347 capabilities.put("vnf", vnfActionList);
348 capabilities.put("vf-module", vfModuleActionList);
349 capabilities.put("vnfc", vnfcActionList);
350 capabilities.put("vm", vmActionVnfcFunctionCodesList);
351 processAndStoreCapabilitiesArtifact(dbservice, documentInfo, capabilities,
352 capabilityArtifactName,
356 } catch (Exception e) {
358 log.error("Error while storing reference data", e);
359 throw new ArtifactHandlerInternalException("Error while storing reference data", e);
365 public boolean isCapabilityArtifactNeeded(SvcLogicContext context) {
366 String vnfcType = context.getAttribute(VNFC_TYPE);
367 if (StringUtils.isNotBlank(vnfcType)) {
368 log.info("No capability Artifact for this reference data as it is at VNFC level!!" );
376 public JSONArray setVnfcTypeInformation(JSONObject scope, SvcLogicContext context) {
377 JSONArray vnfcTypeList = null;
378 if (scope.has(VNFC_TYPE)
379 && !scope.isNull(VNFC_TYPE)) {
380 String vnfcTypeScope = scope.getString(VNFC_TYPE);
381 if (StringUtils.isNotBlank(vnfcTypeScope)) {
382 setAttribute(context, scope::getString, VNFC_TYPE);
383 log.info("VNFC Type has been set for this reference artifact!!"+vnfcTypeScope);
385 context.setAttribute(VNFC_TYPE, null);
388 context.setAttribute(VNFC_TYPE, null);
390 if (scope.has(VNFC_TYPE_LIST) && !scope.isNull(VNFC_TYPE_LIST)
391 && scope.get(VNFC_TYPE_LIST) instanceof JSONArray) {
392 vnfcTypeList = scope.getJSONArray(VNFC_TYPE_LIST);
393 log.info("VNFC TYPE LIST found for this artifact!! "+ vnfcTypeList.toString());
398 public void processActionLists(JSONObject content, String actionLevel, JSONArray vnfcActionList,
399 JSONArray vfModuleActionList,
400 JSONArray vnfActionList, JSONArray vmActionVnfcFunctionCodesList) {
401 if (validateActionLevel(actionLevel, ACTION_LEVEL_VNFC)) {
402 vnfcActionList.put(content.getString(ACTION));
404 if (validateActionLevel(actionLevel, ACTION_LEVEL_VF_MODULE)) {
405 vfModuleActionList.put(content.getString(ACTION));
407 if (validateActionLevel(actionLevel, ACTION_LEVEL_VNF)) {
408 vnfActionList.put(content.getString(ACTION));
410 if (validateActionLevel(actionLevel, ACTION_LEVEL_VM)) {
411 if (content.has(VNFC_FUNCTION_CODE_LIST)
412 && !content.isNull(VNFC_FUNCTION_CODE_LIST) && content.get(
413 VNFC_FUNCTION_CODE_LIST) instanceof JSONArray) {
414 log.info("Found vnfc-function-code-list!!");
415 JSONArray vnfcList = content.getJSONArray(VNFC_FUNCTION_CODE_LIST);
416 JSONObject obj = new JSONObject();
417 obj.put(content.getString(ACTION), vnfcList);
418 vmActionVnfcFunctionCodesList.put(obj);
420 log.info("Not getting JSONArray for VNFC FUNCTION CODES");
426 private boolean validateActionLevel(String actionLevel, String actionLevelVnfc) {
427 return null != actionLevel && actionLevel.equalsIgnoreCase(actionLevelVnfc);
430 public void processArtifactList(JSONObject content, DBService dbservice, SvcLogicContext context, JSONArray vnfcTypeList)
431 throws ArtifactHandlerInternalException {
435 if (content.has(ARTIFACT_LIST_PARAM) && content.get(ARTIFACT_LIST_PARAM) instanceof JSONArray) {
436 JSONArray artifactLists = (JSONArray) content.get(ARTIFACT_LIST_PARAM);
437 JSONArray templateIdList = null;
438 if (content.has("template-id-list") && null != content.get("template-id-list")
439 && content.get("template-id-list") instanceof JSONArray) {
440 templateIdList = content.getJSONArray("template-id-list");
442 doProcessArtifactList(dbservice, context, artifactLists, templateIdList, vnfcTypeList);
445 } catch (Exception e) {
446 log.error("An error occurred when processing artifact list", e);
447 throw new ArtifactHandlerInternalException(e);
451 private void doProcessArtifactList(DBService dbservice, SvcLogicContext context, JSONArray artifactLists,
452 JSONArray templateIdList, JSONArray vnfcTypeList)
453 throws SvcLogicException, SQLException, ConfigurationException, DBException {
454 boolean pdFile = false;
455 int modelInd = 0, vnfcRefInd = 0;
456 for (int i = 0; i < artifactLists.length(); i++) {
457 String suffix = null;
459 JSONObject artifact = (JSONObject) artifactLists.get(i);
460 log.info("artifact is " + artifact);
463 if (null != templateIdList && i>0 && i%2==0) {//Should this be changed to 3 to account for 3 artifacts
466 if (null != vnfcTypeList && i>0 && i%3==0) {
467 //TDP 517180 - CD tool has made changes to send 3 artifacts instead of 2
470 setAttribute(context, artifact::getString, ARTIFACT_NAME);
471 context.setAttribute(FILE_CATEGORY,
472 artifact.getString(ARTIFACT_TYPE));
474 if (artifact.getString(ARTIFACT_NAME) != null
475 && artifact.getString(ARTIFACT_NAME).toLowerCase().startsWith(PD)) {
477 suffix = artifact.getString(ARTIFACT_NAME).substring(PD.length());
480 log.info("Artifact-type = " + context.getAttribute(FILE_CATEGORY));
481 log.info("Artifact-name = " + context.getAttribute(ARTIFACT_NAME));
483 if (null != templateIdList && modelInd < templateIdList.length()) {
484 model = templateIdList.getString(modelInd);
485 log.info("Model is ::: "+model+" ,modelInd = "+modelInd);
487 if (null != vnfcTypeList && vnfcRefInd < vnfcTypeList.length() ) {
488 String vnfcType = vnfcTypeList.getString(vnfcRefInd);
489 if (StringUtils.isNotBlank(vnfcType)) {
490 context.setAttribute(VNFC_TYPE, vnfcType);
492 log.info("Setting vnfc type from vnfc-type-list ::"+vnfcType);
494 if (StringUtils.isNotBlank(model)) {
495 dbservice.processSdcReferences(context, dbservice.isArtifactUpdateRequired(context,
496 DB_SDC_REFERENCE, model),model);
499 dbservice.processSdcReferences(context, dbservice.isArtifactUpdateRequired(context,
503 cleanArtifactInstanceData(context);
504 //Moving this into the for loop to account for mulitple artifact sets with pds
506 log.info("Sending information related to pdfile Artifact");
507 tryUpdateContext(dbservice, context, pdFile, suffix, model);
508 pdFile=false;//set to false afterprocessing yang and Tosca
514 private void tryUpdateContext(DBService dbservice, SvcLogicContext context, boolean pdFile,
515 String suffix, String model)
516 throws SvcLogicException, SQLException, ConfigurationException, DBException {
518 context.setAttribute(ARTIFACT_NAME, "Tosca".concat(suffix));
519 context.setAttribute(FILE_CATEGORY, TOSCA_MODEL);
520 dbservice.processSdcReferences(context,
521 dbservice.isArtifactUpdateRequired(context, DB_SDC_REFERENCE, model), model);
522 context.setAttribute(ARTIFACT_NAME, "Yang".concat(suffix));
523 context.setAttribute(FILE_CATEGORY, PARAMETER_YANG);
524 dbservice.processSdcReferences(context,
525 dbservice.isArtifactUpdateRequired(context, DB_SDC_REFERENCE, model), model);
529 public void processConfigTypeActions(JSONObject content, DBService dbservice, SvcLogicContext context)
530 throws ArtifactHandlerInternalException {
533 if (contentsActionEquals(content, CONFIGURE_PARAM)
534 || contentsActionEquals(content, CONFIG_MODIFY_PARAM)
535 || contentsActionEquals(content, CONFIG_SCALE_OUT_PARAM)) {
537 if (content.has(DOWNLOAD_DG_REFERENCE) && content.getString(DOWNLOAD_DG_REFERENCE).length() > 0) {
539 setAttribute(context, content::getString, DOWNLOAD_DG_REFERENCE);
540 dbservice.processDownloadDgReference(context,
541 dbservice.isArtifactUpdateRequired(context, DB_DOWNLOAD_DG_REFERENCE));
543 if (StringUtils.isBlank(context.getAttribute(DOWNLOAD_DG_REFERENCE))) {
544 context.setAttribute(DOWNLOAD_DG_REFERENCE,
545 dbservice.getDownLoadDGReference(context));
548 .processConfigActionDg(context, dbservice.isArtifactUpdateRequired(context, DB_CONFIG_ACTION_DG));
550 tryProcessInterfaceProtocol(content, dbservice, context);
552 } catch (Exception e) {
553 log.error("An error occurred when processing config type actions", e);
554 throw new ArtifactHandlerInternalException(e);
558 private void tryProcessInterfaceProtocol(JSONObject content, DBService dbservice, SvcLogicContext context)
559 throws SvcLogicException, SQLException, ConfigurationException, DBException {
561 if (contentsActionEquals(content, CONFIGURE_PARAM) || contentsActionEquals(content, CONFIG_SCALE_OUT_PARAM)) {
562 boolean isUpdateRequired = dbservice.isArtifactUpdateRequired(context, DB_DEVICE_INTERFACE_PROTOCOL);
563 if (contentsActionEquals(content, CONFIGURE_PARAM)
564 || (contentsActionEquals(content, CONFIG_SCALE_OUT_PARAM)
565 && !isUpdateRequired)) {
567 dbservice.processDeviceInterfaceProtocol(context, isUpdateRequired);
572 private boolean contentsActionEquals(JSONObject content, String action) {
573 return content.getString(ACTION).equals(action);
576 public void processVmList(JSONObject content, SvcLogicContext context, DBService dbservice)
577 throws SvcLogicException {
578 JSONArray vmList = (JSONArray) content.get(VM);
579 dbservice.cleanUpVnfcReferencesForVnf(context);
580 for (int i = 0; i < vmList.length(); i++) {
581 JSONObject vmInstance = (JSONObject) vmList.get(i);
582 setAttribute(context, s -> String.valueOf(vmInstance.getInt(s)), VM_INSTANCE);
583 log.info("VALUE = " + context.getAttribute(VM_INSTANCE));
584 String templateId = vmInstance.optString(TEMPLATE_ID);
585 trySetContext(context, vmInstance, templateId);
586 if (vmInstance.get(VNFC) instanceof JSONArray) {
587 JSONArray vnfcInstanceList = (JSONArray) vmInstance.get(VNFC);
588 for (int k = 0; k < vnfcInstanceList.length(); k++) {
589 JSONObject vnfcInstance = (JSONObject) vnfcInstanceList.get(k);
591 setAttribute(context, s -> String.valueOf(vnfcInstance.getInt(s)), VNFC_INSTANCE);
592 setAttribute(context, vnfcInstance::getString, VNFC_TYPE);
593 setAttribute(context, vnfcInstance::getString, VNFC_FUNCTION_CODE);
594 resolveContext(context, vnfcInstance);
595 tryProcessVnfcReference(content, context, dbservice);
596 cleanVnfcInstance(context);
598 context.setAttribute(VM_INSTANCE, null);
599 context.setAttribute(TEMPLATE_ID, null);
604 private void trySetContext(SvcLogicContext context, JSONObject vmInstance, String templateId) {
605 if (StringUtils.isNotBlank(templateId)) {
606 setAttribute(context, vmInstance::optString, TEMPLATE_ID);
610 private void tryProcessVnfcReference(JSONObject content, SvcLogicContext context, DBService dbservice)
611 throws SvcLogicException {
612 if (content.getString(ACTION).equals(CONFIGURE_PARAM)
613 || content.getString(ACTION).equals(CONFIG_SCALE_OUT_PARAM)) {
615 dbservice.processVnfcReference(context, false);
619 private void resolveContext(SvcLogicContext context, JSONObject vnfcInstance) {
620 if (vnfcInstance.has(IPADDRESS_V4_OAM_VIP)) {
621 setAttribute(context, vnfcInstance::getString, IPADDRESS_V4_OAM_VIP);
623 if (vnfcInstance.has(GROUP_NOTATION_TYPE)) {
624 setAttribute(context, vnfcInstance::getString, GROUP_NOTATION_TYPE);
626 if (vnfcInstance.has(GROUP_NOTATION_VALUE)) {
627 setAttribute(context, vnfcInstance::getString, GROUP_NOTATION_VALUE);
631 private void cleanArtifactInstanceData(SvcLogicContext context) {
632 context.setAttribute(ARTIFACT_NAME, null);
633 context.setAttribute(FILE_CATEGORY, null);
636 private void cleanVnfcInstance(SvcLogicContext context) {
638 context.setAttribute(VNFC_INSTANCE, null);
639 context.setAttribute(VNFC_TYPE, null);
640 context.setAttribute(VNFC_FUNCTION_CODE, null);
641 context.setAttribute(IPADDRESS_V4_OAM_VIP, null);
642 context.setAttribute(GROUP_NOTATION_TYPE, null);
643 context.setAttribute(GROUP_NOTATION_VALUE, null);
647 private void processAndStoreCapabilitiesArtifact(DBService dbService, JSONObject documentInfo,
648 JSONObject capabilities, String capabilityArtifactName, String vnfType)
649 throws ArtifactHandlerInternalException {
651 log.info("Begin-->processAndStoreCapabilitiesArtifact ");
654 JSONObject newCapabilitiesObject = new JSONObject();
655 newCapabilitiesObject.put("capabilities", capabilities);
656 SvcLogicContext context = new SvcLogicContext();
657 context.setAttribute(ARTIFACT_NAME, capabilityArtifactName);
658 context.setAttribute(FILE_CATEGORY, CAPABILITY);
659 context.setAttribute(ACTION, null);
660 context.setAttribute(VNFC_TYPE, null);
661 context.setAttribute(ARTIFACT_TYPE, null);
662 context.setAttribute(VNF_TYPE, vnfType);
663 context.setAttribute(ARTIFACT_CONTENTS, newCapabilitiesObject.toString());
664 dbService.processSdcReferences(context, dbService.isArtifactUpdateRequired(context, DB_SDC_REFERENCE));
666 String internalVersion = dbService.getInternalVersionNumber(context,
667 context.getAttribute(ARTIFACT_NAME), null);
668 log.info("Internal Version number received from Database : " + internalVersion);
669 if (internalVersion != null) {
670 intVersion = Integer.parseInt(internalVersion) + 1;
672 setAttribute(context, documentInfo::getString, SERVICE_UUID);
673 setAttribute(context, documentInfo::getString, DISTRIBUTION_ID);
674 setAttribute(context, documentInfo::getString, SERVICE_NAME);
675 setAttribute(context, documentInfo::getString, SERVICE_DESCRIPTION);
676 setAttribute(context, documentInfo::getString, RESOURCE_UUID);
677 setAttribute(context, documentInfo::getString, RESOURCE_INSTANCE_NAME);
678 setAttribute(context, documentInfo::getString, RESOURCE_VERSION);
679 setAttribute(context, documentInfo::getString, RESOURCE_TYPE);
680 setAttribute(context, documentInfo::getString, ARTIFACT_UUID);
681 setAttribute(context, documentInfo::getString, ARTIFACT_VERSION);
682 setAttribute(context, documentInfo::getString, ARTIFACT_DESRIPTION);
683 dbService.saveArtifacts(context, intVersion);
684 } catch (Exception e) {
685 log.error("Error saving capabilities artifact to DB", e);
686 throw new ArtifactHandlerInternalException("Error saving capabilities artifact to DB", e);
688 log.info("End-->processAndStoreCapabilitiesArtifact ");
692 private void setAttribute(SvcLogicContext context, Function<String, String> value, String key) {
693 context.setAttribute(key, value.apply(key));
696 private void populateProtocolReference(DBService dbservice, JSONObject content)
697 throws ArtifactHandlerInternalException {
698 log.info("Begin-->populateProtocolReference ");
700 SvcLogicContext context = new SvcLogicContext();
701 JSONObject scope = content.getJSONObject("scope");
702 String vnfType = tryGetVnfType(scope);
703 String protocol = tryGetProtocol(content);
704 String action = tryGetAction(content);
705 String actionLevel = tryGetActionLevel(content);
706 String template = tryGetTemplate(content);
708 boolean isUpdateNeeded = dbservice
709 .isProtocolReferenceUpdateRequired(context, vnfType, protocol, action, actionLevel, template);
710 if (isUpdateNeeded) {
711 dbservice.updateProtocolReference(context, vnfType, protocol, action, actionLevel, template);
713 dbservice.insertProtocolReference(context, vnfType, protocol, action, actionLevel, template);
715 } catch (Exception e) {
716 log.error("Error inserting record into protocolReference", e);
717 throw new ArtifactHandlerInternalException("Error inserting record into protocolReference", e);
719 log.info("End-->populateProtocolReference ");
723 private String tryGetVnfType(JSONObject scope) {
724 if (scope.has(VNF_TYPE) && !scope.isNull(VNF_TYPE)) {
725 return scope.getString(VNF_TYPE);
730 private String tryGetProtocol(JSONObject content) {
731 if (content.has(DEVICE_PROTOCOL)) {
732 return content.getString(DEVICE_PROTOCOL);
737 private String tryGetAction(JSONObject content) {
738 if (content.has(ACTION)) {
739 return content.getString(ACTION);
744 private String tryGetActionLevel(JSONObject content) {
745 if (content.has(ACTION_LEVEL)) {
746 return content.getString(ACTION_LEVEL);
751 private String tryGetTemplate(JSONObject content) {
752 if (content.has(TEMPLATE) && !content.isNull(TEMPLATE)) {
753 return content.getString(TEMPLATE);