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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.artifact.handler.node;
27 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ACTION;
28 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ACTION_LEVEL;
29 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ACTION_LEVEL_VF_MODULE;
30 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ACTION_LEVEL_VM;
31 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ACTION_LEVEL_VNF;
32 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ACTION_LEVEL_VNFC;
33 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ARTIFACT_CONTENTS;
34 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ARTIFACT_DESRIPTION;
35 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ARTIFACT_NAME;
36 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ARTIFACT_NAME_CAPABILITY;
37 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ARTIFACT_NAME_REFERENCE;
38 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ARTIFACT_TYPE;
39 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ARTIFACT_UUID;
40 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ARTIFACT_VERSION;
41 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.CAPABILITY;
42 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.DB_CONFIG_ACTION_DG;
43 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.DB_DEVICE_AUTHENTICATION;
44 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.DB_DEVICE_INTERFACE_PROTOCOL;
45 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE;
46 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.DB_SDC_REFERENCE;
47 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.DEVICE_PROTOCOL;
48 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.DISTRIBUTION_ID;
49 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.DOCUMENT_PARAMETERS;
50 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE;
51 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.FILE_CATEGORY;
52 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.GROUP_NOTATION_TYPE;
53 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.GROUP_NOTATION_VALUE;
54 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.IPADDRESS_V4_OAM_VIP;
55 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.PARAMETER_YANG;
56 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.PD;
57 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.PORT_NUMBER;
58 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.REFERENCE;
59 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.REQUEST_ID;
60 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.REQUEST_INFORMATION;
61 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.RESOURCE_INSTANCE_NAME;
62 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.RESOURCE_TYPE;
63 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.RESOURCE_UUID;
64 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.RESOURCE_VERSION;
65 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.SERVICE_DESCRIPTION;
66 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.SERVICE_NAME;
67 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.SERVICE_UUID;
68 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.TEMPLATE;
69 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.TEMPLATE_ID;
70 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.TOSCA_MODEL;
71 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.USER_NAME;
72 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.VM;
73 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.VM_INSTANCE;
74 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.VNFC;
75 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.VNFC_FUNCTION_CODE;
76 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.VNFC_FUNCTION_CODE_LIST;
77 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.VNFC_INSTANCE;
78 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.VNFC_TYPE;
79 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.VNF_TYPE;
81 import com.att.eelf.configuration.EELFLogger;
82 import com.att.eelf.configuration.EELFManager;
83 import java.io.ByteArrayOutputStream;
84 import java.io.OutputStream;
85 import java.sql.SQLException;
87 import java.util.function.Function;
88 import org.apache.commons.configuration.ConfigurationException;
89 import org.apache.commons.lang.StringUtils;
90 import org.json.JSONArray;
91 import org.json.JSONObject;
92 import org.onap.appc.artifact.handler.dbservices.DBException;
93 import org.onap.appc.artifact.handler.dbservices.DBService;
94 import org.onap.appc.artifact.handler.utils.ArtifactHandlerProviderUtil;
95 import org.onap.appc.yang.YANGGenerator;
96 import org.onap.appc.yang.impl.YANGGeneratorFactory;
97 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
98 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
99 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
100 import org.onap.sdnc.config.params.transformer.tosca.ArtifactProcessorImpl;
101 import org.onap.sdnc.config.params.transformer.tosca.exceptions.ArtifactProcessorException;
103 public class ArtifactHandlerNode implements SvcLogicJavaPlugin {
105 private static final EELFLogger log = EELFManager.getInstance().getLogger(ArtifactHandlerNode.class);
106 private static final String TOSCA_PARAM = "Tosca";
107 private static final String YANG_PARAM = "Yang";
108 private static final String ARTIFACT_LIST_PARAM = "artifact-list";
109 private static final String CONFIGURE_PARAM = "Configure";
110 private static final String CONFIG_SCALE_OUT_PARAM = "ConfigScaleOut";
111 private static final String CONFIG_MODIFY_PARAM = "ConfigModify";
113 public void processArtifact(Map<String, String> inParams, SvcLogicContext ctx) throws ArtifactProcessorException {
115 if (inParams == null || inParams.isEmpty()) {
118 String postData = inParams.get("postData");
119 if (postData == null || postData.isEmpty()) {
123 log.info("Received request for process Artifact with params: " + inParams.toString());
124 JSONObject input = new JSONObject(postData).getJSONObject("input");
125 storeUpdateSdcArtifacts(input);
126 } catch (Exception e) {
127 log.error("Error when processing artifact", e);
128 throw new ArtifactProcessorException("Error occurred while processing artifact", e);
132 private boolean storeUpdateSdcArtifacts(JSONObject postDataJson) throws ArtifactHandlerInternalException {
133 log.info("Starting processing of SDC Artifacs into Handler with Data : " + postDataJson.toString());
135 JSONObject requestInfo = (JSONObject) postDataJson.get(REQUEST_INFORMATION);
136 JSONObject documentInfo = (JSONObject) postDataJson.get(DOCUMENT_PARAMETERS);
137 String artifactName = documentInfo.getString(ARTIFACT_NAME);
139 if (artifactName != null) {
140 updateStoreArtifacts(requestInfo, documentInfo);
141 if (artifactName.toLowerCase().startsWith(REFERENCE)) {
142 return storeReferenceData(requestInfo, documentInfo);
143 } else if (artifactName.toLowerCase().startsWith(PD)) {
144 return createDataForPD(requestInfo, documentInfo);
148 throw new ArtifactHandlerInternalException("Missing Artifact Name for Request: "
149 + requestInfo.getString(REQUEST_ID));
151 } catch (Exception e) {
152 log.error("Error while processing request with id: "
153 + ((JSONObject) postDataJson.get(REQUEST_INFORMATION)).getString(REQUEST_ID), e);
155 throw new ArtifactHandlerInternalException("Error while processing request with id: "
156 + ((JSONObject) postDataJson.get(REQUEST_INFORMATION)).getString(REQUEST_ID), e);
161 private boolean createDataForPD(JSONObject requestInfo, JSONObject documentInfo)
162 throws ArtifactHandlerInternalException {
164 String fn = "ArtifactHandlerNode.createReferenceDataForPD";
165 String artifactName = documentInfo.getString(ARTIFACT_NAME);
166 log.info(fn + "Received PD File Name: " + artifactName + " and suffix lenght "
170 String suffix = artifactName.substring(PD.length());
171 createArtifactRecords(requestInfo, documentInfo, suffix);
172 } catch (Exception e) {
173 log.error("Error while creating PD data records", e);
174 throw new ArtifactHandlerInternalException("Error while creating PD data records", e);
179 private void createArtifactRecords(JSONObject requestInfo, JSONObject documentInfo, String suffix)
180 throws ArtifactHandlerInternalException {
183 log.info("Creating Tosca Records and storing into SDC Artifacs");
184 String[] docs = {TOSCA_PARAM, YANG_PARAM};
185 ArtifactHandlerProviderUtil ahpUtil = new ArtifactHandlerProviderUtil();
186 String pdFileContents = documentInfo.getString(ARTIFACT_CONTENTS);
189 OutputStream toscaStream = new ByteArrayOutputStream();
190 String toscaContents;
191 ArtifactProcessorImpl toscaGenerator = new ArtifactProcessorImpl();
192 toscaGenerator.generateArtifact(pdFileContents, toscaStream);
193 toscaContents = toscaStream.toString();
194 log.info("Generated Tosca File : " + toscaContents);
196 String yangContents = "YANG generation is in Progress";
197 String yangName = null;
199 for (String doc : docs) {
200 documentInfo.put(ARTIFACT_TYPE, doc.concat("Type"));
201 documentInfo.put(ARTIFACT_DESRIPTION, doc.concat("Model"));
202 if (doc.equals(TOSCA_PARAM)) {
203 documentInfo.put(ARTIFACT_CONTENTS, ahpUtil.escapeSql(toscaContents));
204 } else if (doc.equals(YANG_PARAM)) {
205 documentInfo.put(ARTIFACT_CONTENTS, ahpUtil.escapeSql(yangContents));
207 documentInfo.put(ARTIFACT_NAME, doc.concat(suffix));
208 yangName = doc.concat(suffix);
209 updateStoreArtifacts(requestInfo, documentInfo);
212 String artifactId = getArtifactID(yangName);
213 OutputStream yangStream = new ByteArrayOutputStream();
214 YANGGenerator yangGenerator = YANGGeneratorFactory.getYANGGenerator();
215 yangGenerator.generateYANG(artifactId, toscaContents, yangStream);
216 yangContents = yangStream.toString();
218 if (yangContents != null) {
219 updateYangContents(artifactId, ahpUtil.escapeSql(yangContents));
221 } catch (Exception e) {
222 log.error("Error while creating artifact records", e);
223 throw new ArtifactHandlerInternalException("Error while creating artifact records", e);
228 private void updateYangContents(String artifactId, String yangContents) throws SvcLogicException {
229 SvcLogicContext context = new SvcLogicContext();
230 DBService dbservice = DBService.initialise();
231 dbservice.updateYangContents(context, artifactId, yangContents);
234 private String getArtifactID(String yangName) throws SvcLogicException {
235 SvcLogicContext context = new SvcLogicContext();
236 DBService dbservice = DBService.initialise();
237 return dbservice.getArtifactID(context, yangName);
240 protected boolean updateStoreArtifacts(JSONObject requestInfo, JSONObject documentInfo)
241 throws SvcLogicException {
242 log.info("UpdateStoreArtifactsStarted storing of SDC Artifacs ");
244 SvcLogicContext context = new SvcLogicContext();
245 DBService dbservice = DBService.initialise();
246 ArtifactHandlerProviderUtil ahpUtil = new ArtifactHandlerProviderUtil();
248 context.setAttribute("artifact_name",
249 documentInfo.getString(ARTIFACT_NAME));
250 String internalVersion = dbservice.getInternalVersionNumber(context,
251 documentInfo.getString(ARTIFACT_NAME), null);
252 log.info("Internal Version number received from Database : " + internalVersion);
253 if (internalVersion != null) {
254 intversion = Integer.parseInt(internalVersion);
257 setAttribute(context, documentInfo::getString, SERVICE_UUID);
258 setAttribute(context, documentInfo::getString, DISTRIBUTION_ID);
259 setAttribute(context, documentInfo::getString, SERVICE_NAME);
260 setAttribute(context, documentInfo::getString, SERVICE_DESCRIPTION);
261 setAttribute(context, documentInfo::getString, RESOURCE_UUID);
262 setAttribute(context, documentInfo::getString, RESOURCE_INSTANCE_NAME);
263 setAttribute(context, documentInfo::getString, RESOURCE_VERSION);
264 setAttribute(context, documentInfo::getString, RESOURCE_TYPE);
265 setAttribute(context, documentInfo::getString, ARTIFACT_UUID);
266 setAttribute(context, documentInfo::getString, ARTIFACT_TYPE);
267 setAttribute(context, documentInfo::getString, ARTIFACT_VERSION);
268 setAttribute(context, documentInfo::getString, ARTIFACT_DESRIPTION);
269 setAttribute(context, documentInfo::getString, ARTIFACT_NAME);
270 setAttribute(context, s -> ahpUtil.escapeSql(documentInfo.getString(s)), ARTIFACT_CONTENTS);
272 dbservice.saveArtifacts(context, intversion);
276 public boolean storeReferenceData(JSONObject requestInfo, JSONObject documentInfo)
277 throws ArtifactHandlerInternalException {
278 log.info("Started storing of SDC Artifacs into Handler");
280 DBService dbservice = DBService.initialise();
281 ArtifactHandlerProviderUtil ahpUtil = new ArtifactHandlerProviderUtil();
282 String contentString =
283 ahpUtil.escapeSql(documentInfo.getString(ARTIFACT_CONTENTS));
284 String artifactName =
285 ahpUtil.escapeSql(documentInfo.getString(ARTIFACT_NAME));
286 String capabilityArtifactName =
287 StringUtils.replace(artifactName, ARTIFACT_NAME_REFERENCE,
288 ARTIFACT_NAME_CAPABILITY);
289 JSONObject capabilities = new JSONObject();
290 JSONArray vnfActionList = new JSONArray();
291 JSONArray vfModuleActionList = new JSONArray();
292 JSONArray vnfcActionList = new JSONArray();
293 JSONArray vmActionVnfcFunctionCodesList = new JSONArray();
294 String vnfType = null;
295 JSONObject contentObject = new JSONObject(contentString);
296 JSONArray contentArray = contentObject.getJSONArray("reference_data");
297 boolean storeCapabilityArtifact = true;
299 for (int a = 0; a < contentArray.length(); a++) {
300 JSONObject content = (JSONObject) contentArray.get(a);
301 log.info("contentString =" + content.toString());
302 JSONObject scope = content.getJSONObject("scope");
303 log.info("scope :" + scope);
304 SvcLogicContext context = new SvcLogicContext();
305 vnfType = scope.getString(VNF_TYPE);
306 setAttribute(context, scope::getString, VNF_TYPE);
307 setAttribute(context, content::getString, ACTION);
308 String actionLevel = content.getString(ACTION_LEVEL);
309 setAttribute(context, content::getString, ACTION_LEVEL);
310 setAttribute(context, documentInfo::getString, ARTIFACT_TYPE);
311 processActionLists(content, actionLevel, vnfcActionList, vfModuleActionList, vnfActionList,
312 vmActionVnfcFunctionCodesList);
313 storeCapabilityArtifact = isCapabilityArtifactNeeded(scope, context);
314 if (content.has(DEVICE_PROTOCOL)) {
315 setAttribute(context, content::getString, DEVICE_PROTOCOL);
317 if (content.has(USER_NAME)) {
318 setAttribute(context, content::getString, USER_NAME);
320 if (content.has(PORT_NUMBER)) {
321 setAttribute(context, content::getString, PORT_NUMBER);
323 processArtifactList(content, dbservice, context);
324 processConfigTypeActions(content, dbservice, context);
325 dbservice.processDeviceAuthentication(context,
326 dbservice.isArtifactUpdateRequired(context, DB_DEVICE_AUTHENTICATION));
328 populateProtocolReference(dbservice, content);
330 context.setAttribute(VNFC_TYPE, null);
333 && content.get(VM) instanceof JSONArray) {
334 processVmList(content, context, dbservice);
337 if (storeCapabilityArtifact) {
338 capabilities.put("vnf", vnfActionList);
339 capabilities.put("vf-module", vfModuleActionList);
340 capabilities.put("vnfc", vnfcActionList);
341 capabilities.put("vm", vmActionVnfcFunctionCodesList);
342 processAndStoreCapabilitiesArtifact(dbservice, documentInfo, capabilities,
343 capabilityArtifactName,
347 } catch (Exception e) {
349 log.error("Error while storing reference data", e);
350 throw new ArtifactHandlerInternalException("Error while storing reference data", e);
356 public boolean isCapabilityArtifactNeeded(JSONObject scope, SvcLogicContext context) {
357 boolean storeCapabilityArtifact = true;
358 if (scope.has(VNFC_TYPE)
359 && !scope.isNull(VNFC_TYPE)) {
360 String vnfcTypeScope = scope.getString(VNFC_TYPE);
361 if (StringUtils.isNotBlank(vnfcTypeScope)) {
362 setAttribute(context, scope::getString, VNFC_TYPE);
363 storeCapabilityArtifact = false;
364 log.info("No capability Artifact for this reference data as it is at VNFC level!!");
366 context.setAttribute(VNFC_TYPE, null);
369 context.setAttribute(VNFC_TYPE, null);
371 return storeCapabilityArtifact;
374 public void processActionLists(JSONObject content, String actionLevel, JSONArray vnfcActionList,
375 JSONArray vfModuleActionList,
376 JSONArray vnfActionList, JSONArray vmActionVnfcFunctionCodesList) {
377 if (validateActionLevel(actionLevel, ACTION_LEVEL_VNFC)) {
378 vnfcActionList.put(content.getString(ACTION));
380 if (validateActionLevel(actionLevel, ACTION_LEVEL_VF_MODULE)) {
381 vfModuleActionList.put(content.getString(ACTION));
383 if (validateActionLevel(actionLevel, ACTION_LEVEL_VNF)) {
384 vnfActionList.put(content.getString(ACTION));
386 if (validateActionLevel(actionLevel, ACTION_LEVEL_VM)) {
387 if (content.has(VNFC_FUNCTION_CODE_LIST)
388 && !content.isNull(VNFC_FUNCTION_CODE_LIST) && content.get(
389 VNFC_FUNCTION_CODE_LIST) instanceof JSONArray) {
390 log.info("Found vnfc-function-code-list!!");
391 JSONArray vnfcList = content.getJSONArray(VNFC_FUNCTION_CODE_LIST);
392 JSONObject obj = new JSONObject();
393 obj.put(content.getString(ACTION), vnfcList);
394 vmActionVnfcFunctionCodesList.put(obj);
396 log.info("Not getting JSONArray for VNFC FUNCTION CODES");
402 private boolean validateActionLevel(String actionLevel, String actionLevelVnfc) {
403 return null != actionLevel && actionLevel.equalsIgnoreCase(actionLevelVnfc);
406 public void processArtifactList(JSONObject content, DBService dbservice, SvcLogicContext context)
407 throws ArtifactHandlerInternalException {
411 if (content.has(ARTIFACT_LIST_PARAM) && content.get(ARTIFACT_LIST_PARAM) instanceof JSONArray) {
412 JSONArray artifactLists = (JSONArray) content.get(ARTIFACT_LIST_PARAM);
413 doProcessArtifactList(dbservice, context, artifactLists);
415 } catch (Exception e) {
416 log.error("An error occurred when processing artifact list", e);
417 throw new ArtifactHandlerInternalException(e);
421 private void doProcessArtifactList(DBService dbservice, SvcLogicContext context, JSONArray artifactLists)
422 throws SvcLogicException, SQLException, ConfigurationException, DBException {
423 boolean pdFile = false;
424 String suffix = null;
426 for (int i = 0; i < artifactLists.length(); i++) {
427 JSONObject artifact = (JSONObject) artifactLists.get(i);
428 log.info("artifact is " + artifact);
429 setAttribute(context, artifact::getString, ARTIFACT_NAME);
430 context.setAttribute(FILE_CATEGORY,
431 artifact.getString(ARTIFACT_TYPE));
433 if (artifact.getString(ARTIFACT_NAME) != null
434 && artifact.getString(ARTIFACT_NAME).toLowerCase().startsWith(PD)) {
436 suffix = artifact.getString(ARTIFACT_NAME).substring(PD.length());
439 log.info("Artifact-type = " + context.getAttribute(ARTIFACT_TYPE));
440 dbservice.processSdcReferences(context, dbservice.isArtifactUpdateRequired(context,
443 cleanArtifactInstanceData(context);
445 tryUpdateContext(dbservice, context, pdFile, suffix);
448 private void tryUpdateContext(DBService dbservice, SvcLogicContext context, boolean pdFile, String suffix)
449 throws SvcLogicException, SQLException, ConfigurationException, DBException {
451 context.setAttribute(ARTIFACT_NAME, "Tosca".concat(suffix));
452 context.setAttribute(FILE_CATEGORY, TOSCA_MODEL);
453 dbservice.processSdcReferences(context,
454 dbservice.isArtifactUpdateRequired(context, DB_SDC_REFERENCE));
455 context.setAttribute(ARTIFACT_NAME, "Yang".concat(suffix));
456 context.setAttribute(FILE_CATEGORY, PARAMETER_YANG);
457 dbservice.processSdcReferences(context,
458 dbservice.isArtifactUpdateRequired(context, DB_SDC_REFERENCE));
462 public void processConfigTypeActions(JSONObject content, DBService dbservice, SvcLogicContext context)
463 throws ArtifactHandlerInternalException {
466 if (contentsActionEquals(content, CONFIGURE_PARAM)
467 || contentsActionEquals(content, CONFIG_MODIFY_PARAM)
468 || contentsActionEquals(content, CONFIG_SCALE_OUT_PARAM)) {
470 if (content.has(DOWNLOAD_DG_REFERENCE) && content.getString(DOWNLOAD_DG_REFERENCE).length() > 0) {
472 setAttribute(context, content::getString, DOWNLOAD_DG_REFERENCE);
473 dbservice.processDownloadDgReference(context,
474 dbservice.isArtifactUpdateRequired(context, DB_DOWNLOAD_DG_REFERENCE));
476 if (StringUtils.isBlank(context.getAttribute(DOWNLOAD_DG_REFERENCE))) {
477 context.setAttribute(DOWNLOAD_DG_REFERENCE,
478 dbservice.getDownLoadDGReference(context));
481 .processConfigActionDg(context, dbservice.isArtifactUpdateRequired(context, DB_CONFIG_ACTION_DG));
483 tryProcessInterfaceProtocol(content, dbservice, context);
485 } catch (Exception e) {
486 log.error("An error occurred when processing config type actions", e);
487 throw new ArtifactHandlerInternalException(e);
491 private void tryProcessInterfaceProtocol(JSONObject content, DBService dbservice, SvcLogicContext context)
492 throws SvcLogicException, SQLException, ConfigurationException, DBException {
494 if (contentsActionEquals(content, CONFIGURE_PARAM) || contentsActionEquals(content, CONFIG_SCALE_OUT_PARAM)) {
495 boolean isUpdateRequired = dbservice.isArtifactUpdateRequired(context, DB_DEVICE_INTERFACE_PROTOCOL);
496 if (contentsActionEquals(content, CONFIGURE_PARAM)
497 || (contentsActionEquals(content, CONFIG_SCALE_OUT_PARAM)
498 && !isUpdateRequired)) {
500 dbservice.processDeviceInterfaceProtocol(context, isUpdateRequired);
505 private boolean contentsActionEquals(JSONObject content, String action) {
506 return content.getString(ACTION).equals(action);
509 public void processVmList(JSONObject content, SvcLogicContext context, DBService dbservice)
510 throws SvcLogicException {
511 JSONArray vmList = (JSONArray) content.get(VM);
512 dbservice.cleanUpVnfcReferencesForVnf(context);
513 for (int i = 0; i < vmList.length(); i++) {
514 JSONObject vmInstance = (JSONObject) vmList.get(i);
515 setAttribute(context, s -> String.valueOf(vmInstance.getInt(s)), VM_INSTANCE);
516 log.info("VALUE = " + context.getAttribute(VM_INSTANCE));
517 String templateId = vmInstance.optString(TEMPLATE_ID);
518 trySetContext(context, vmInstance, templateId);
519 if (vmInstance.get(VNFC) instanceof JSONArray) {
520 JSONArray vnfcInstanceList = (JSONArray) vmInstance.get(VNFC);
521 for (int k = 0; k < vnfcInstanceList.length(); k++) {
522 JSONObject vnfcInstance = (JSONObject) vnfcInstanceList.get(k);
524 setAttribute(context, s -> String.valueOf(vnfcInstance.getInt(s)), VNFC_INSTANCE);
525 setAttribute(context, vnfcInstance::getString, VNFC_TYPE);
526 setAttribute(context, vnfcInstance::getString, VNFC_FUNCTION_CODE);
527 resolveContext(context, vnfcInstance);
528 tryProcessVnfcReference(content, context, dbservice);
529 cleanVnfcInstance(context);
531 context.setAttribute(VM_INSTANCE, null);
532 context.setAttribute(TEMPLATE_ID, null);
537 private void trySetContext(SvcLogicContext context, JSONObject vmInstance, String templateId) {
538 if (StringUtils.isNotBlank(templateId)) {
539 setAttribute(context, vmInstance::optString, TEMPLATE_ID);
543 private void tryProcessVnfcReference(JSONObject content, SvcLogicContext context, DBService dbservice)
544 throws SvcLogicException {
545 if (content.getString(ACTION).equals(CONFIGURE_PARAM)
546 || content.getString(ACTION).equals(CONFIG_SCALE_OUT_PARAM)) {
548 dbservice.processVnfcReference(context, false);
552 private void resolveContext(SvcLogicContext context, JSONObject vnfcInstance) {
553 if (vnfcInstance.has(IPADDRESS_V4_OAM_VIP)) {
554 setAttribute(context, vnfcInstance::getString, IPADDRESS_V4_OAM_VIP);
556 if (vnfcInstance.has(GROUP_NOTATION_TYPE)) {
557 setAttribute(context, vnfcInstance::getString, GROUP_NOTATION_TYPE);
559 if (vnfcInstance.has(GROUP_NOTATION_VALUE)) {
560 setAttribute(context, vnfcInstance::getString, GROUP_NOTATION_VALUE);
564 private void cleanArtifactInstanceData(SvcLogicContext context) {
565 context.setAttribute(ARTIFACT_NAME, null);
566 context.setAttribute(FILE_CATEGORY, null);
569 private void cleanVnfcInstance(SvcLogicContext context) {
571 context.setAttribute(VNFC_INSTANCE, null);
572 context.setAttribute(VNFC_TYPE, null);
573 context.setAttribute(VNFC_FUNCTION_CODE, null);
574 context.setAttribute(IPADDRESS_V4_OAM_VIP, null);
575 context.setAttribute(GROUP_NOTATION_TYPE, null);
576 context.setAttribute(GROUP_NOTATION_VALUE, null);
580 private void processAndStoreCapabilitiesArtifact(DBService dbService, JSONObject documentInfo,
581 JSONObject capabilities, String capabilityArtifactName, String vnfType)
582 throws ArtifactHandlerInternalException {
584 log.info("Begin-->processAndStoreCapabilitiesArtifact ");
587 JSONObject newCapabilitiesObject = new JSONObject();
588 newCapabilitiesObject.put("capabilities", capabilities);
589 SvcLogicContext context = new SvcLogicContext();
590 context.setAttribute(ARTIFACT_NAME, capabilityArtifactName);
591 context.setAttribute(FILE_CATEGORY, CAPABILITY);
592 context.setAttribute(ACTION, null);
593 context.setAttribute(VNFC_TYPE, null);
594 context.setAttribute(ARTIFACT_TYPE, null);
595 context.setAttribute(VNF_TYPE, vnfType);
596 context.setAttribute(ARTIFACT_CONTENTS, newCapabilitiesObject.toString());
597 dbService.processSdcReferences(context, dbService.isArtifactUpdateRequired(context, DB_SDC_REFERENCE));
599 String internalVersion = dbService.getInternalVersionNumber(context,
600 context.getAttribute(ARTIFACT_NAME), null);
601 log.info("Internal Version number received from Database : " + internalVersion);
602 if (internalVersion != null) {
603 intVersion = Integer.parseInt(internalVersion) + 1;
605 setAttribute(context, documentInfo::getString, SERVICE_UUID);
606 setAttribute(context, documentInfo::getString, DISTRIBUTION_ID);
607 setAttribute(context, documentInfo::getString, SERVICE_NAME);
608 setAttribute(context, documentInfo::getString, SERVICE_DESCRIPTION);
609 setAttribute(context, documentInfo::getString, RESOURCE_UUID);
610 setAttribute(context, documentInfo::getString, RESOURCE_INSTANCE_NAME);
611 setAttribute(context, documentInfo::getString, RESOURCE_VERSION);
612 setAttribute(context, documentInfo::getString, RESOURCE_TYPE);
613 setAttribute(context, documentInfo::getString, ARTIFACT_UUID);
614 setAttribute(context, documentInfo::getString, ARTIFACT_VERSION);
615 setAttribute(context, documentInfo::getString, ARTIFACT_DESRIPTION);
616 dbService.saveArtifacts(context, intVersion);
617 } catch (Exception e) {
618 log.error("Error saving capabilities artifact to DB", e);
619 throw new ArtifactHandlerInternalException("Error saving capabilities artifact to DB", e);
621 log.info("End-->processAndStoreCapabilitiesArtifact ");
625 private void setAttribute(SvcLogicContext context, Function<String, String> value, String key) {
626 context.setAttribute(key, value.apply(key));
629 private void populateProtocolReference(DBService dbservice, JSONObject content)
630 throws ArtifactHandlerInternalException {
631 log.info("Begin-->populateProtocolReference ");
633 SvcLogicContext context = new SvcLogicContext();
634 JSONObject scope = content.getJSONObject("scope");
635 String vnfType = tryGetVnfType(scope);
636 String protocol = tryGetProtocol(content);
637 String action = tryGetAction(content);
638 String actionLevel = tryGetActionLevel(content);
639 String template = tryGetTemplate(content);
641 boolean isUpdateNeeded = dbservice
642 .isProtocolReferenceUpdateRequired(context, vnfType, protocol, action, actionLevel, template);
643 if (isUpdateNeeded) {
644 dbservice.updateProtocolReference(context, vnfType, protocol, action, actionLevel, template);
646 dbservice.insertProtocolReference(context, vnfType, protocol, action, actionLevel, template);
648 } catch (Exception e) {
649 log.error("Error inserting record into protocolReference", e);
650 throw new ArtifactHandlerInternalException("Error inserting record into protocolReference", e);
652 log.info("End-->populateProtocolReference ");
656 private String tryGetVnfType(JSONObject scope) {
657 if (scope.has(VNF_TYPE) && !scope.isNull(VNF_TYPE)) {
658 return scope.getString(VNF_TYPE);
663 private String tryGetProtocol(JSONObject content) {
664 if (content.has(DEVICE_PROTOCOL)) {
665 return content.getString(DEVICE_PROTOCOL);
670 private String tryGetAction(JSONObject content) {
671 if (content.has(ACTION)) {
672 return content.getString(ACTION);
677 private String tryGetActionLevel(JSONObject content) {
678 if (content.has(ACTION_LEVEL)) {
679 return content.getString(ACTION_LEVEL);
684 private String tryGetTemplate(JSONObject content) {
685 if (content.has(TEMPLATE) && !content.isNull(TEMPLATE)) {
686 return content.getString(TEMPLATE);