Fixed exception handling in ArtifactHandlerNode
[appc.git] / appc-inbound / appc-artifact-handler / provider / src / main / java / org / onap / appc / artifact / handler / node / ArtifactHandlerNode.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.artifact.handler.node;
26
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import org.apache.commons.lang.StringUtils;
30 import org.json.JSONArray;
31 import org.json.JSONObject;
32 import org.onap.appc.artifact.handler.dbservices.DBService;
33 import org.onap.appc.artifact.handler.utils.ArtifactHandlerProviderUtil;
34 import org.onap.appc.yang.YANGGenerator;
35 import org.onap.appc.yang.impl.YANGGeneratorFactory;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
37 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
38 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
39 import org.onap.sdnc.config.params.transformer.tosca.ArtifactProcessorImpl;
40 import java.io.ByteArrayOutputStream;
41 import java.io.OutputStream;
42 import java.util.Map;
43 import java.util.function.Function;
44 import org.onap.sdnc.config.params.transformer.tosca.exceptions.ArtifactProcessorException;
45
46 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ACTION;
47 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ACTION_LEVEL;
48 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ACTION_LEVEL_VF_MODULE;
49 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ACTION_LEVEL_VM;
50 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ACTION_LEVEL_VNF;
51 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ACTION_LEVEL_VNFC;
52 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ARTIFACT_CONTENTS;
53 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ARTIFACT_DESRIPTION;
54 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ARTIFACT_NAME;
55 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ARTIFACT_NAME_CAPABILITY;
56 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ARTIFACT_NAME_REFERENCE;
57 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ARTIFACT_TYPE;
58 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ARTIFACT_UUID;
59 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ARTIFACT_VERSION;
60 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.CAPABILITY;
61 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.DB_CONFIG_ACTION_DG;
62 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.DB_DEVICE_AUTHENTICATION;
63 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.DB_DEVICE_INTERFACE_PROTOCOL;
64 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE;
65 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.DB_SDC_REFERENCE;
66 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.DEVICE_PROTOCOL;
67 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.DISTRIBUTION_ID;
68 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.DOCUMENT_PARAMETERS;
69 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE;
70 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.FILE_CATEGORY;
71 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.GROUP_NOTATION_TYPE;
72 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.GROUP_NOTATION_VALUE;
73 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.IPADDRESS_V4_OAM_VIP;
74 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.PARAMETER_YANG;
75 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.PD;
76 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.PORT_NUMBER;
77 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.REFERENCE;
78 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.REQUEST_INFORMATION;
79 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.REQUEST_ID;
80 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.RESOURCE_INSTANCE_NAME;
81 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.RESOURCE_TYPE;
82 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.RESOURCE_UUID;
83 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.RESOURCE_VERSION;
84 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.SERVICE_DESCRIPTION;
85 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.SERVICE_NAME;
86 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.SERVICE_UUID;
87 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.TEMPLATE;
88 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.TOSCA_MODEL;
89 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.USER_NAME;
90 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.VM;
91 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.VM_INSTANCE;
92 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.VNFC;
93 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.VNFC_FUNCTION_CODE;
94 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.VNFC_FUNCTION_CODE_LIST;
95 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.VNFC_INSTANCE;
96 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.VNFC_TYPE;
97 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.VNF_TYPE;
98
99 public class ArtifactHandlerNode implements SvcLogicJavaPlugin {
100
101     private static final EELFLogger log = EELFManager.getInstance().getLogger(ArtifactHandlerNode.class);
102
103     public void processArtifact(Map<String, String> inParams, SvcLogicContext ctx) throws Exception {
104         if (inParams == null || inParams.isEmpty()) {
105             return;
106         }
107         String postData = inParams.get("postData");
108         if (postData == null || postData.isEmpty()) {
109             return;
110         }
111         try {
112             log.info("Received request for process Artifact with params: " + inParams.toString());
113             JSONObject input = new JSONObject(postData).getJSONObject("input");
114             String responsePrefix = inParams.get("response_prefix");
115             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
116             storeUpdateSdcArtifacts(input);
117         } catch (Exception e) {
118             log.error("Error when processing artifact", e);
119             throw new ArtifactProcessorException("Error occurred while processing artifact", e);
120         }
121     }
122
123     private boolean storeUpdateSdcArtifacts(JSONObject postDataJson) throws ArtifactHandlerInternalException {
124         log.info("Starting processing of SDC Artifacs into Handler with Data : " + postDataJson.toString());
125         try {
126             JSONObject request_information =
127                 (JSONObject) postDataJson.get(REQUEST_INFORMATION);
128             JSONObject document_information =
129                 (JSONObject) postDataJson.get(DOCUMENT_PARAMETERS);
130             String artifact_name = document_information.getString(ARTIFACT_NAME);
131             if (artifact_name != null) {
132                 updateStoreArtifacts(request_information, document_information);
133                 if (artifact_name.toLowerCase().startsWith(REFERENCE)) {
134                     return storeReferenceData(request_information, document_information);
135                 } else if (artifact_name.toLowerCase().startsWith(PD)) {
136                     return createDataForPD(request_information, document_information);
137                 }
138
139             } else {
140                 throw new ArtifactHandlerInternalException("Missing Artifact Name for Request: "
141                     + request_information.getString(REQUEST_ID));
142             }
143         } catch (Exception e) {
144             log.error("Error while processing request with id: "
145                 + ((JSONObject) postDataJson.get(REQUEST_INFORMATION))
146                 .getString(REQUEST_ID), e);
147
148             throw new ArtifactHandlerInternalException("Error while processing request with id: "
149                 + ((JSONObject) postDataJson.get(REQUEST_INFORMATION))
150                 .getString(REQUEST_ID), e);
151         }
152         return false;
153
154     }
155
156     private boolean createDataForPD(JSONObject request_information, JSONObject document_information)
157         throws ArtifactHandlerInternalException {
158
159         String fn = "ArtifactHandlerNode.createReferenceDataForPD";
160         String artifact_name = document_information.getString(ARTIFACT_NAME);
161         log.info(fn + "Received PD File Name: " + artifact_name + " and suffix lenght "
162             + PD.length());
163         try {
164
165             String suffix = artifact_name.substring(PD.length());
166             createArtifactRecords(request_information, document_information, suffix);
167         } catch (Exception e) {
168             log.error("Error while creating PD data records", e);
169             throw new ArtifactHandlerInternalException("Error while creating PD data records", e);
170         }
171         return true;
172     }
173
174     private void createArtifactRecords(JSONObject request_information, JSONObject document_information, String suffix)
175         throws ArtifactHandlerInternalException {
176
177         try {
178             log.info("Creating Tosca Records and storing into SDC Artifacs");
179             String[] docs = {"Tosca", "Yang"};
180             ArtifactHandlerProviderUtil ahpUtil = new ArtifactHandlerProviderUtil();
181             String PDFileContents = document_information.getString(ARTIFACT_CONTENTS);
182
183             // Tosca generation
184             OutputStream toscaStream = new ByteArrayOutputStream();
185             String toscaContents = null;
186             ArtifactProcessorImpl toscaGenerator = new ArtifactProcessorImpl();
187             toscaGenerator.generateArtifact(PDFileContents, toscaStream);
188             toscaContents = toscaStream.toString();
189             log.info("Generated Tosca File : " + toscaContents);
190
191             String yangContents = "YANG generation is in Progress";
192             String yangName = null;
193
194             for (String doc : docs) {
195                 document_information.put(ARTIFACT_TYPE, doc.concat("Type"));
196                 document_information.put(ARTIFACT_DESRIPTION, doc.concat("Model"));
197                 if (doc.equals("Tosca")) {
198                     document_information.put(ARTIFACT_CONTENTS,
199                         ahpUtil.escapeSql(toscaContents));
200                 } else if (doc.equals("Yang")) {
201                     document_information.put(ARTIFACT_CONTENTS,
202                         ahpUtil.escapeSql(yangContents));
203                 }
204                 document_information.put(ARTIFACT_NAME, doc.concat(suffix));
205                 yangName = doc.concat(suffix);
206                 updateStoreArtifacts(request_information, document_information);
207             }
208
209             String artifactId = getArtifactID(yangName);
210             OutputStream yangStream = new ByteArrayOutputStream();
211             YANGGenerator yangGenerator = YANGGeneratorFactory.getYANGGenerator();
212             yangGenerator.generateYANG(artifactId, toscaContents, yangStream);
213             yangContents = yangStream.toString();
214
215             if (yangContents != null) {
216                 updateYangContents(artifactId, ahpUtil.escapeSql(yangContents));
217             }
218
219         } catch (Exception e) {
220             log.error("Error while creating artifact records", e);
221             throw new ArtifactHandlerInternalException("Error while creating artifact records", e);
222         }
223
224     }
225
226     private void updateYangContents(String artifactId, String yangContents) throws SvcLogicException {
227         SvcLogicContext context = new SvcLogicContext();
228         DBService dbservice = DBService.initialise();
229         dbservice.updateYangContents(context, artifactId, yangContents);
230     }
231
232     private String getArtifactID(String yangName) throws SvcLogicException {
233         SvcLogicContext context = new SvcLogicContext();
234         DBService dbservice = DBService.initialise();
235         return dbservice.getArtifactID(context, yangName);
236     }
237
238     protected boolean updateStoreArtifacts(JSONObject request_information, JSONObject document_information)
239         throws SvcLogicException {
240         log.info("UpdateStoreArtifactsStarted storing of SDC Artifacs ");
241
242         SvcLogicContext context = new SvcLogicContext();
243         DBService dbservice = DBService.initialise();
244         ArtifactHandlerProviderUtil ahpUtil = new ArtifactHandlerProviderUtil();
245         int intversion = 0;
246         context.setAttribute("artifact_name",
247             document_information.getString(ARTIFACT_NAME));
248         String internal_version = dbservice.getInternalVersionNumber(context,
249             document_information.getString(ARTIFACT_NAME), null);
250         log.info("Internal Version number received from Database : " + internal_version);
251         if (internal_version != null) {
252             intversion = Integer.parseInt(internal_version);
253             intversion++;
254         }
255
256         setAttribute(context, document_information::getString, SERVICE_UUID);
257         setAttribute(context, document_information::getString, DISTRIBUTION_ID);
258         setAttribute(context, document_information::getString, SERVICE_NAME);
259         setAttribute(context, document_information::getString, SERVICE_DESCRIPTION);
260         setAttribute(context, document_information::getString, RESOURCE_UUID);
261         setAttribute(context, document_information::getString, RESOURCE_INSTANCE_NAME);
262         setAttribute(context, document_information::getString, RESOURCE_VERSION);
263         setAttribute(context, document_information::getString, RESOURCE_TYPE);
264         setAttribute(context, document_information::getString, ARTIFACT_UUID);
265         setAttribute(context, document_information::getString, ARTIFACT_TYPE);
266         setAttribute(context, document_information::getString, ARTIFACT_VERSION);
267         setAttribute(context, document_information::getString, ARTIFACT_DESRIPTION);
268         setAttribute(context, document_information::getString, ARTIFACT_NAME);
269
270         setAttribute(context, s -> ahpUtil.escapeSql(document_information.getString(s)), ARTIFACT_CONTENTS);
271
272         dbservice.saveArtifacts(context, intversion);
273         return true;
274
275     }
276
277     public boolean storeReferenceData(JSONObject request_information, JSONObject document_information)
278         throws ArtifactHandlerInternalException {
279         log.info("Started storing of SDC Artifacs into Handler");
280         try {
281             boolean updateRequired = false;
282             boolean pdFile = false;
283             String suffix = null;
284             String categorySuffix = null;
285             DBService dbservice = DBService.initialise();
286             ArtifactHandlerProviderUtil ahpUtil = new ArtifactHandlerProviderUtil();
287             String contentString =
288                 ahpUtil.escapeSql(document_information.getString(ARTIFACT_CONTENTS));
289             String artifactName =
290                 ahpUtil.escapeSql(document_information.getString(ARTIFACT_NAME));
291             String capabilityArtifactName =
292                 StringUtils.replace(artifactName, ARTIFACT_NAME_REFERENCE,
293                     ARTIFACT_NAME_CAPABILITY);
294             JSONObject capabilities = new JSONObject();
295             JSONArray vnfActionList = new JSONArray();
296             JSONArray vfModuleActionList = new JSONArray();
297             JSONArray vnfcActionList = new JSONArray();
298             JSONArray vmActionVnfcFunctionCodesList = new JSONArray();
299             JSONArray vmActionList = new JSONArray();
300             String vnfType = null;
301             JSONObject contentObject = new JSONObject(contentString);
302             JSONArray contentArray = contentObject.getJSONArray("reference_data");
303             boolean storeCapabilityArtifact = true;
304             for (int a = 0; a < contentArray.length(); a++) {
305                 pdFile = false;
306                 JSONObject content = (JSONObject) contentArray.get(a);
307                 log.info("contentString =" + content.toString());
308                 JSONObject scope = content.getJSONObject("scope");
309                 log.info("scope :" + scope);
310                 SvcLogicContext context = new SvcLogicContext();
311                 vnfType = scope.getString(VNF_TYPE);
312                 setAttribute(context, scope::getString, VNF_TYPE);
313                 setAttribute(context, content::getString, ACTION);
314                 String actionLevel = content.getString(ACTION_LEVEL);
315                 setAttribute(context, content::getString, ACTION_LEVEL);
316                 setAttribute(context, document_information::getString, ARTIFACT_TYPE);
317                 if ((null != actionLevel)
318                     && actionLevel.equalsIgnoreCase(ACTION_LEVEL_VNFC)) {
319                     vnfcActionList.put(content.getString(ACTION));
320                 }
321                 if (null != actionLevel
322                     && actionLevel.equalsIgnoreCase(ACTION_LEVEL_VF_MODULE)) {
323                     vfModuleActionList.put(content.getString(ACTION));
324                 }
325                 if (null != actionLevel && actionLevel.equalsIgnoreCase(ACTION_LEVEL_VNF)) {
326                     vnfActionList.put(content.getString(ACTION));
327                 }
328                 if (null != actionLevel && actionLevel.equalsIgnoreCase(ACTION_LEVEL_VM)) {
329                     if (content.has(VNFC_FUNCTION_CODE_LIST)
330                         && !content.isNull(VNFC_FUNCTION_CODE_LIST) && content.get(
331                         VNFC_FUNCTION_CODE_LIST) instanceof JSONArray) {
332                         log.info("Found vnfc-function-code-list!!");
333                         JSONArray vnfcList = content.getJSONArray(VNFC_FUNCTION_CODE_LIST);
334                         JSONObject obj = new JSONObject();
335                         obj.put(content.getString(ACTION), vnfcList);
336                         vmActionVnfcFunctionCodesList.put(obj);
337                     } else {
338                         log.info("Not getting JSONArray for VNFC FUNCTION CODES");
339                     }
340                 }
341                 if (scope.has(VNFC_TYPE)
342                     && !scope.isNull(VNFC_TYPE)) {
343                     String vnfcTypeScope = scope.getString(VNFC_TYPE);
344                     if (StringUtils.isNotBlank(vnfcTypeScope)) {
345                         setAttribute(context, scope::getString, VNFC_TYPE);
346                         storeCapabilityArtifact = false;
347                         log.info("No capability Artifact for this reference data as it is at VNFC level!!");
348                     } else {
349                         context.setAttribute(VNFC_TYPE, null);
350                     }
351                 } else {
352                     context.setAttribute(VNFC_TYPE, null);
353                 }
354                 if (content.has(DEVICE_PROTOCOL)) {
355                     setAttribute(context, content::getString, DEVICE_PROTOCOL);
356                 }
357                 if (content.has(USER_NAME)) {
358                     setAttribute(context, content::getString, USER_NAME);
359                 }
360                 if (content.has(PORT_NUMBER)) {
361                     setAttribute(context, content::getString, PORT_NUMBER);
362                 }
363                 //context.setAttribute(ARTIFACT_TYPE, "");
364                 if (content.has("artifact-list") && content.get("artifact-list") instanceof JSONArray) {
365                     JSONArray artifactLists = (JSONArray) content.get("artifact-list");
366                     for (int i = 0; i < artifactLists.length(); i++) {
367                         JSONObject artifact = (JSONObject) artifactLists.get(i);
368                         log.info("artifact is " + artifact);
369                         setAttribute(context, artifact::getString, ARTIFACT_NAME);
370                         context.setAttribute(FILE_CATEGORY,
371                             artifact.getString(ARTIFACT_TYPE));
372
373                         if (artifact.getString(ARTIFACT_NAME) != null
374                             && artifact.getString(ARTIFACT_NAME).toLowerCase()
375                             .startsWith(PD)) {
376                             suffix = artifact.getString(ARTIFACT_NAME)
377                                 .substring(PD.length());
378                             categorySuffix = artifact.getString(ARTIFACT_TYPE)
379                                 .substring(PD.length());
380                             pdFile = true;
381                         }
382                         log.info("Artifact-type = " + context.getAttribute(ARTIFACT_TYPE));
383                         dbservice.processSdcReferences(context, dbservice.isArtifactUpdateRequired(context,
384                             DB_SDC_REFERENCE));
385
386                         cleanArtifactInstanceData(context);
387                     }
388
389                     if (pdFile) {
390                         context.setAttribute(ARTIFACT_NAME, "Tosca".concat(suffix));
391                         context.setAttribute(FILE_CATEGORY, TOSCA_MODEL);
392                         dbservice.processSdcReferences(context,
393                             dbservice.isArtifactUpdateRequired(context, DB_SDC_REFERENCE));
394                         context.setAttribute(ARTIFACT_NAME, "Yang".concat(suffix));
395                         context.setAttribute(FILE_CATEGORY, PARAMETER_YANG);
396                         dbservice.processSdcReferences(context,
397                             dbservice.isArtifactUpdateRequired(context, DB_SDC_REFERENCE));
398                     }
399                 }
400                 if (content.getString(ACTION).equals("Configure")
401                     || content.getString(ACTION).equals("ConfigModify")) {
402                     if (content.has(DOWNLOAD_DG_REFERENCE)
403                         && content.getString(DOWNLOAD_DG_REFERENCE).length() > 0) {
404                         setAttribute(context, content::getString, DOWNLOAD_DG_REFERENCE);
405                         dbservice.processDownloadDgReference(context,
406                             dbservice.isArtifactUpdateRequired(context, DB_DOWNLOAD_DG_REFERENCE));
407                     }
408                     if (StringUtils.isBlank(context.getAttribute(DOWNLOAD_DG_REFERENCE))) {
409                         context.setAttribute(DOWNLOAD_DG_REFERENCE,
410                             dbservice.getDownLoadDGReference(context));
411                     }
412                     dbservice.processConfigActionDg(context, dbservice.isArtifactUpdateRequired(context,
413                         DB_CONFIG_ACTION_DG));
414                     if (content.getString(ACTION).equals("Configure")) {
415                         dbservice.processDeviceInterfaceProtocol(context, dbservice.isArtifactUpdateRequired(context,
416                             DB_DEVICE_INTERFACE_PROTOCOL));
417                     }
418
419                 }
420                 dbservice.processDeviceAuthentication(context,
421                     dbservice.isArtifactUpdateRequired(context, DB_DEVICE_AUTHENTICATION));
422
423                 populateProtocolReference(dbservice, content);
424
425                 context.setAttribute(VNFC_TYPE, null);
426
427                 if (content.has(VM)
428                     && content.get(VM) instanceof JSONArray) {
429                     JSONArray vmList = (JSONArray) content.get(VM);
430                     dbservice.cleanUpVnfcReferencesForVnf(context);
431                     for (int i = 0; i < vmList.length(); i++) {
432                         JSONObject vmInstance = (JSONObject) vmList.get(i);
433                         setAttribute(context, s -> String.valueOf(vmInstance.getInt(s)), VM_INSTANCE);
434                         log.info("VALUE = " + context.getAttribute(VM_INSTANCE));
435                         if (vmInstance.get(VNFC) instanceof JSONArray) {
436                             JSONArray vnfcInstanceList = (JSONArray) vmInstance.get(VNFC);
437                             for (int k = 0; k < vnfcInstanceList.length(); k++) {
438                                 JSONObject vnfcInstance = (JSONObject) vnfcInstanceList.get(k);
439
440                                 setAttribute(context, s -> String.valueOf(vnfcInstance.getInt(s)), VNFC_INSTANCE);
441                                 setAttribute(context, vnfcInstance::getString, VNFC_TYPE);
442                                 setAttribute(context, vnfcInstance::getString, VNFC_FUNCTION_CODE);
443
444                                 if (vnfcInstance.has(IPADDRESS_V4_OAM_VIP)) {
445                                     setAttribute(context, vnfcInstance::getString, IPADDRESS_V4_OAM_VIP);
446                                 }
447                                 if (vnfcInstance.has(GROUP_NOTATION_TYPE)) {
448                                     setAttribute(context, vnfcInstance::getString, GROUP_NOTATION_TYPE);
449                                 }
450                                 if (vnfcInstance.has(GROUP_NOTATION_VALUE)) {
451                                     setAttribute(context, vnfcInstance::getString, GROUP_NOTATION_VALUE);
452                                 }
453                                 if (content.getString(ACTION).equals("Configure")) {
454                                     dbservice.processVnfcReference(context, false);
455                                 }
456                                 cleanVnfcInstance(context);
457                             }
458                             context.setAttribute(VM_INSTANCE, null);
459                         }
460                     }
461                 }
462
463
464             }
465             if (storeCapabilityArtifact) {
466                 capabilities.put("vnf", vnfActionList);
467                 capabilities.put("vf-module", vfModuleActionList);
468                 capabilities.put("vnfc", vnfcActionList);
469                 capabilities.put("vm", vmActionVnfcFunctionCodesList);
470                 processAndStoreCapabilitiesArtifact(dbservice, document_information, capabilities,
471                     capabilityArtifactName,
472                     vnfType);
473             }
474
475         } catch (Exception e) {
476
477             log.error("Error while storing reference data", e);
478             throw new ArtifactHandlerInternalException("Error while storing reference data", e);
479         }
480
481         return true;
482     }
483
484     private void cleanArtifactInstanceData(SvcLogicContext context) {
485         context.setAttribute(ARTIFACT_NAME, null);
486         context.setAttribute(FILE_CATEGORY, null);
487     }
488
489     private void cleanVnfcInstance(SvcLogicContext context) {
490
491         context.setAttribute(VNFC_INSTANCE, null);
492         context.setAttribute(VNFC_TYPE, null);
493         context.setAttribute(VNFC_FUNCTION_CODE, null);
494         context.setAttribute(IPADDRESS_V4_OAM_VIP, null);
495         context.setAttribute(GROUP_NOTATION_TYPE, null);
496         context.setAttribute(GROUP_NOTATION_VALUE, null);
497
498     }
499
500     private void processAndStoreCapabilitiesArtifact(DBService dbservice, JSONObject document_information,
501         JSONObject capabilities, String capabilityArtifactName, String vnfType)
502         throws ArtifactHandlerInternalException {
503
504         log.info("Begin-->processAndStoreCapabilitiesArtifact ");
505
506         try {
507
508             JSONObject newCapabilitiesObject = new JSONObject();
509             newCapabilitiesObject.put("capabilities", capabilities);
510             SvcLogicContext context = new SvcLogicContext();
511             context.setAttribute(ARTIFACT_NAME, capabilityArtifactName);
512             context.setAttribute(FILE_CATEGORY, CAPABILITY);
513             context.setAttribute(ACTION, null);
514             context.setAttribute(VNFC_TYPE, null);
515             context.setAttribute(ARTIFACT_TYPE, null);
516             context.setAttribute(VNF_TYPE, vnfType);
517             context.setAttribute(ARTIFACT_CONTENTS, newCapabilitiesObject.toString());
518             dbservice.processSdcReferences(context,
519                 dbservice.isArtifactUpdateRequired(context, DB_SDC_REFERENCE));
520             int intversion = 0;
521
522             String internal_version = dbservice.getInternalVersionNumber(context,
523                 context.getAttribute(ARTIFACT_NAME), null);
524             log.info("Internal Version number received from Database : " + internal_version);
525             if (internal_version != null) {
526                 intversion = Integer.parseInt(internal_version);
527                 intversion++;
528             }
529
530             setAttribute(context, document_information::getString, SERVICE_UUID);
531             setAttribute(context, document_information::getString, DISTRIBUTION_ID);
532             setAttribute(context, document_information::getString, SERVICE_NAME);
533             setAttribute(context, document_information::getString, SERVICE_DESCRIPTION);
534             setAttribute(context, document_information::getString, RESOURCE_UUID);
535             setAttribute(context, document_information::getString, RESOURCE_INSTANCE_NAME);
536             setAttribute(context, document_information::getString, RESOURCE_VERSION);
537             setAttribute(context, document_information::getString, RESOURCE_TYPE);
538             setAttribute(context, document_information::getString, ARTIFACT_UUID);
539             setAttribute(context, document_information::getString, ARTIFACT_VERSION);
540             setAttribute(context, document_information::getString, ARTIFACT_DESRIPTION);
541
542             dbservice.saveArtifacts(context, intversion);
543         } catch (Exception e) {
544             log.error("Error saving capabilities artifact to DB", e);
545             throw new ArtifactHandlerInternalException("Error saving capabilities artifact to DB", e);
546         } finally {
547             log.info("End-->processAndStoreCapabilitiesArtifact ");
548         }
549
550     }
551
552     private void setAttribute(SvcLogicContext context, Function<String, String> value, String key) {
553         context.setAttribute(key, value.apply(key));
554     }
555
556     private void populateProtocolReference(DBService dbservice, JSONObject content)
557         throws ArtifactHandlerInternalException {
558         log.info("Begin-->populateProtocolReference ");
559         try {
560             SvcLogicContext context = new SvcLogicContext();
561             JSONObject scope = content.getJSONObject("scope");
562             String vnfType = null, protocol = null, action = null, actionLevel = null, template = null;
563             if (scope.has(VNF_TYPE) && !scope.isNull(VNF_TYPE)) {
564                 vnfType = scope.getString(VNF_TYPE);
565             }
566             if (content.has(DEVICE_PROTOCOL)) {
567                 protocol = content.getString(DEVICE_PROTOCOL);
568             }
569             if (content.has(ACTION)) {
570                 action = content.getString(ACTION);
571             }
572             if (content.has(ACTION_LEVEL)) {
573                 actionLevel = content.getString(ACTION_LEVEL);
574             }
575             if (content.has(TEMPLATE)
576                 && !content.isNull(TEMPLATE)) {
577                 template = content.getString(TEMPLATE);
578             }
579             boolean isUpdateNeeded = dbservice
580                 .isProtocolReferenceUpdateRequired(context, vnfType, protocol, action, actionLevel, template);
581             if (isUpdateNeeded) {
582                 dbservice.updateProtocolReference(context, vnfType, protocol, action, actionLevel, template);
583             } else {
584                 dbservice.insertProtocolReference(context, vnfType, protocol, action, actionLevel, template);
585             }
586         } catch (Exception e) {
587             log.error("Error inserting record into protocolReference", e);
588             throw new ArtifactHandlerInternalException("Error inserting record into protocolReference", e);
589         } finally {
590             log.info("End-->populateProtocolReference ");
591         }
592     }
593
594 }