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