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