34982172373e70ef085230a345f87f2bbdd09036
[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-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
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  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.artifact.handler.node;
25
26 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ACTION;
27 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ACTION_LEVEL;
28 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ACTION_LEVEL_VF_MODULE;
29 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ACTION_LEVEL_VM;
30 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ACTION_LEVEL_VNF;
31 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ACTION_LEVEL_VNFC;
32 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ARTIFACT_CONTENTS;
33 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ARTIFACT_DESRIPTION;
34 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ARTIFACT_NAME;
35 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ARTIFACT_NAME_CAPABILITY;
36 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ARTIFACT_NAME_REFERENCE;
37 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ARTIFACT_TYPE;
38 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ARTIFACT_UUID;
39 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ARTIFACT_VERSION;
40 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.CAPABILITY;
41 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.DB_CONFIG_ACTION_DG;
42 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.DB_DEVICE_AUTHENTICATION;
43 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.DB_DEVICE_INTERFACE_PROTOCOL;
44 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE;
45 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.DB_SDC_REFERENCE;
46 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.DEVICE_PROTOCOL;
47 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.DISTRIBUTION_ID;
48 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.DOCUMENT_PARAMETERS;
49 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE;
50 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.FILE_CATEGORY;
51 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.GROUP_NOTATION_TYPE;
52 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.GROUP_NOTATION_VALUE;
53 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.IPADDRESS_V4_OAM_VIP;
54 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.PARAMETER_YANG;
55 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.PD;
56 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.PORT_NUMBER;
57 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.REFERENCE;
58 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.REQUEST_ID;
59 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.REQUEST_INFORMATION;
60 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.RESOURCE_INSTANCE_NAME;
61 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.RESOURCE_TYPE;
62 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.RESOURCE_UUID;
63 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.RESOURCE_VERSION;
64 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.SERVICE_DESCRIPTION;
65 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.SERVICE_NAME;
66 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.SERVICE_UUID;
67 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.TEMPLATE;
68 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.TEMPLATE_ID;
69 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.TOSCA_MODEL;
70 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.USER_NAME;
71 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.VM;
72 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.VM_INSTANCE;
73 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.VNFC;
74 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.VNFC_FUNCTION_CODE;
75 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.VNFC_FUNCTION_CODE_LIST;
76 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.VNFC_INSTANCE;
77 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.VNFC_TYPE;
78 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.VNF_TYPE;
79
80 import com.att.eelf.configuration.EELFLogger;
81 import com.att.eelf.configuration.EELFManager;
82 import java.io.ByteArrayOutputStream;
83 import java.io.OutputStream;
84 import java.sql.SQLException;
85 import java.util.Map;
86 import java.util.function.Function;
87 import org.apache.commons.configuration.ConfigurationException;
88 import org.apache.commons.lang.StringUtils;
89 import org.json.JSONArray;
90 import org.json.JSONObject;
91 import org.onap.appc.artifact.handler.dbservices.DBException;
92 import org.onap.appc.artifact.handler.dbservices.DBService;
93 import org.onap.appc.artifact.handler.utils.ArtifactHandlerProviderUtil;
94 import org.onap.appc.yang.YANGGenerator;
95 import org.onap.appc.yang.impl.YANGGeneratorFactory;
96 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
97 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
98 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
99 import org.onap.sdnc.config.params.transformer.tosca.ArtifactProcessorImpl;
100 import org.onap.sdnc.config.params.transformer.tosca.exceptions.ArtifactProcessorException;
101
102 public class ArtifactHandlerNode implements SvcLogicJavaPlugin {
103
104     private static final EELFLogger log = EELFManager.getInstance().getLogger(ArtifactHandlerNode.class);
105     private static final String TOSCA_PARAM = "Tosca";
106     private static final String YANG_PARAM = "Yang";
107     private static final String ARTIFACT_LIST_PARAM = "artifact-list";
108     private static final String CONFIGURE_PARAM = "Configure";
109     private static final String CONFIG_SCALE_OUT_PARAM = "ConfigScaleOut";
110     private static final String CONFIG_MODIFY_PARAM = "ConfigModify";
111
112     public void processArtifact(Map<String, String> inParams, SvcLogicContext ctx) throws ArtifactProcessorException {
113
114         if (inParams == null || inParams.isEmpty()) {
115             return;
116         }
117         String postData = inParams.get("postData");
118         if (postData == null || postData.isEmpty()) {
119             return;
120         }
121         try {
122             log.info("Received request for process Artifact with params: " + inParams.toString());
123             JSONObject input = new JSONObject(postData).getJSONObject("input");
124             storeUpdateSdcArtifacts(input);
125         } catch (Exception e) {
126             log.error("Error when processing artifact", e);
127             throw new ArtifactProcessorException("Error occurred while processing artifact", e);
128         }
129     }
130
131     private boolean storeUpdateSdcArtifacts(JSONObject postDataJson) throws ArtifactHandlerInternalException {
132         log.info("Starting processing of SDC Artifacs into Handler with Data : " + postDataJson.toString());
133         try {
134             JSONObject requestInfo = (JSONObject) postDataJson.get(REQUEST_INFORMATION);
135             JSONObject documentInfo = (JSONObject) postDataJson.get(DOCUMENT_PARAMETERS);
136             String artifactName = documentInfo.getString(ARTIFACT_NAME);
137
138             if (artifactName != null) {
139                 updateStoreArtifacts(requestInfo, documentInfo);
140                 if (artifactName.toLowerCase().startsWith(REFERENCE)) {
141                     return storeReferenceData(requestInfo, documentInfo);
142                 } else if (artifactName.toLowerCase().startsWith(PD)) {
143                     return createDataForPD(requestInfo, documentInfo);
144                 }
145
146             } else {
147                 throw new ArtifactHandlerInternalException("Missing Artifact Name for Request: "
148                     + requestInfo.getString(REQUEST_ID));
149             }
150         } catch (Exception e) {
151             log.error("Error while processing request with id: "
152                 + ((JSONObject) postDataJson.get(REQUEST_INFORMATION)).getString(REQUEST_ID), e);
153
154             throw new ArtifactHandlerInternalException("Error while processing request with id: "
155                 + ((JSONObject) postDataJson.get(REQUEST_INFORMATION)).getString(REQUEST_ID), e);
156         }
157         return false;
158     }
159
160     private boolean createDataForPD(JSONObject requestInfo, JSONObject documentInfo)
161         throws ArtifactHandlerInternalException {
162
163         String fn = "ArtifactHandlerNode.createReferenceDataForPD";
164         String artifactName = documentInfo.getString(ARTIFACT_NAME);
165         log.info(fn + "Received PD File Name: " + artifactName + " and suffix lenght "
166             + PD.length());
167         try {
168
169             String suffix = artifactName.substring(PD.length());
170             createArtifactRecords(requestInfo, documentInfo, suffix);
171         } catch (Exception e) {
172             log.error("Error while creating PD data records", e);
173             throw new ArtifactHandlerInternalException("Error while creating PD data records", e);
174         }
175         return true;
176     }
177
178     private void createArtifactRecords(JSONObject requestInfo, JSONObject documentInfo, String suffix)
179         throws ArtifactHandlerInternalException {
180
181         try {
182             log.info("Creating Tosca Records and storing into SDC Artifacs");
183             String[] docs = {TOSCA_PARAM, YANG_PARAM};
184             ArtifactHandlerProviderUtil ahpUtil = new ArtifactHandlerProviderUtil();
185             String pdFileContents = documentInfo.getString(ARTIFACT_CONTENTS);
186
187             // Tosca generation
188             OutputStream toscaStream = new ByteArrayOutputStream();
189             String toscaContents;
190             ArtifactProcessorImpl toscaGenerator = new ArtifactProcessorImpl();
191             toscaGenerator.generateArtifact(pdFileContents, toscaStream);
192             toscaContents = toscaStream.toString();
193             log.info("Generated Tosca File : " + toscaContents);
194
195             String yangContents = "YANG generation is in Progress";
196             String yangName = null;
197
198             for (String doc : docs) {
199                 documentInfo.put(ARTIFACT_TYPE, doc.concat("Type"));
200                 documentInfo.put(ARTIFACT_DESRIPTION, doc.concat("Model"));
201                 if (doc.equals(TOSCA_PARAM)) {
202                     documentInfo.put(ARTIFACT_CONTENTS, ahpUtil.escapeSql(toscaContents));
203                 } else if (doc.equals(YANG_PARAM)) {
204                     documentInfo.put(ARTIFACT_CONTENTS, ahpUtil.escapeSql(yangContents));
205                 }
206                 documentInfo.put(ARTIFACT_NAME, doc.concat(suffix));
207                 yangName = doc.concat(suffix);
208                 updateStoreArtifacts(requestInfo, documentInfo);
209             }
210
211             String artifactId = getArtifactID(yangName);
212             OutputStream yangStream = new ByteArrayOutputStream();
213             YANGGenerator yangGenerator = YANGGeneratorFactory.getYANGGenerator();
214             yangGenerator.generateYANG(artifactId, toscaContents, yangStream);
215             yangContents = yangStream.toString();
216
217             if (yangContents != null) {
218                 updateYangContents(artifactId, ahpUtil.escapeSql(yangContents));
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 requestInfo, JSONObject documentInfo)
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             documentInfo.getString(ARTIFACT_NAME));
249         String internalVersion = dbservice.getInternalVersionNumber(context,
250             documentInfo.getString(ARTIFACT_NAME), null);
251         log.info("Internal Version number received from Database : " + internalVersion);
252         if (internalVersion != null) {
253             intversion = Integer.parseInt(internalVersion);
254             intversion++;
255         }
256         setAttribute(context, documentInfo::getString, SERVICE_UUID);
257         setAttribute(context, documentInfo::getString, DISTRIBUTION_ID);
258         setAttribute(context, documentInfo::getString, SERVICE_NAME);
259         setAttribute(context, documentInfo::getString, SERVICE_DESCRIPTION);
260         setAttribute(context, documentInfo::getString, RESOURCE_UUID);
261         setAttribute(context, documentInfo::getString, RESOURCE_INSTANCE_NAME);
262         setAttribute(context, documentInfo::getString, RESOURCE_VERSION);
263         setAttribute(context, documentInfo::getString, RESOURCE_TYPE);
264         setAttribute(context, documentInfo::getString, ARTIFACT_UUID);
265         setAttribute(context, documentInfo::getString, ARTIFACT_TYPE);
266         setAttribute(context, documentInfo::getString, ARTIFACT_VERSION);
267         setAttribute(context, documentInfo::getString, ARTIFACT_DESRIPTION);
268         setAttribute(context, documentInfo::getString, ARTIFACT_NAME);
269         setAttribute(context, s -> ahpUtil.escapeSql(documentInfo.getString(s)), ARTIFACT_CONTENTS);
270
271         dbservice.saveArtifacts(context, intversion);
272         return true;
273     }
274
275     public boolean storeReferenceData(JSONObject requestInfo, JSONObject documentInfo)
276         throws ArtifactHandlerInternalException {
277         log.info("Started storing of SDC Artifacs into Handler");
278         try {
279             DBService dbservice = DBService.initialise();
280             ArtifactHandlerProviderUtil ahpUtil = new ArtifactHandlerProviderUtil();
281             String contentString =
282                 ahpUtil.escapeSql(documentInfo.getString(ARTIFACT_CONTENTS));
283             String artifactName =
284                 ahpUtil.escapeSql(documentInfo.getString(ARTIFACT_NAME));
285             String capabilityArtifactName =
286                 StringUtils.replace(artifactName, ARTIFACT_NAME_REFERENCE,
287                     ARTIFACT_NAME_CAPABILITY);
288             JSONObject capabilities = new JSONObject();
289             JSONArray vnfActionList = new JSONArray();
290             JSONArray vfModuleActionList = new JSONArray();
291             JSONArray vnfcActionList = new JSONArray();
292             JSONArray vmActionVnfcFunctionCodesList = new JSONArray();
293             String vnfType = null;
294             JSONObject contentObject = new JSONObject(contentString);
295             JSONArray contentArray = contentObject.getJSONArray("reference_data");
296             boolean storeCapabilityArtifact = true;
297
298             for (int a = 0; a < contentArray.length(); a++) {
299                 JSONObject content = (JSONObject) contentArray.get(a);
300                 log.info("contentString =" + content.toString());
301                 JSONObject scope = content.getJSONObject("scope");
302                 log.info("scope :" + scope);
303                 SvcLogicContext context = new SvcLogicContext();
304                 vnfType = scope.getString(VNF_TYPE);
305                 setAttribute(context, scope::getString, VNF_TYPE);
306                 setAttribute(context, content::getString, ACTION);
307                 String actionLevel = content.getString(ACTION_LEVEL);
308                 setAttribute(context, content::getString, ACTION_LEVEL);
309                 setAttribute(context, documentInfo::getString, ARTIFACT_TYPE);
310                 processActionLists(content, actionLevel, vnfcActionList, vfModuleActionList, vnfActionList,
311                     vmActionVnfcFunctionCodesList);
312                 storeCapabilityArtifact = isCapabilityArtifactNeeded(scope, context);
313                 if (content.has(DEVICE_PROTOCOL)) {
314                     setAttribute(context, content::getString, DEVICE_PROTOCOL);
315                 }
316                 if (content.has(USER_NAME)) {
317                     setAttribute(context, content::getString, USER_NAME);
318                 }
319                 if (content.has(PORT_NUMBER)) {
320                     setAttribute(context, content::getString, PORT_NUMBER);
321                 }
322                 processArtifactList(content, dbservice, context);
323                 processConfigTypeActions(content, dbservice, context);
324                 dbservice.processDeviceAuthentication(context,
325                     dbservice.isArtifactUpdateRequired(context, DB_DEVICE_AUTHENTICATION));
326
327                 populateProtocolReference(dbservice, content);
328
329                 context.setAttribute(VNFC_TYPE, null);
330
331                 if (content.has(VM)
332                     && content.get(VM) instanceof JSONArray) {
333                     processVmList(content, context, dbservice);
334                 }
335             }
336             if (storeCapabilityArtifact) {
337                 capabilities.put("vnf", vnfActionList);
338                 capabilities.put("vf-module", vfModuleActionList);
339                 capabilities.put("vnfc", vnfcActionList);
340                 capabilities.put("vm", vmActionVnfcFunctionCodesList);
341                 processAndStoreCapabilitiesArtifact(dbservice, documentInfo, capabilities,
342                     capabilityArtifactName,
343                     vnfType);
344             }
345
346         } catch (Exception e) {
347
348             log.error("Error while storing reference data", e);
349             throw new ArtifactHandlerInternalException("Error while storing reference data", e);
350         }
351
352         return true;
353     }
354
355     public boolean isCapabilityArtifactNeeded(JSONObject scope, SvcLogicContext context) {
356         boolean storeCapabilityArtifact = true;
357         if (scope.has(VNFC_TYPE)
358             && !scope.isNull(VNFC_TYPE)) {
359             String vnfcTypeScope = scope.getString(VNFC_TYPE);
360             if (StringUtils.isNotBlank(vnfcTypeScope)) {
361                 setAttribute(context, scope::getString, VNFC_TYPE);
362                 storeCapabilityArtifact = false;
363                 log.info("No capability Artifact for this reference data as it is at VNFC level!!");
364             } else {
365                 context.setAttribute(VNFC_TYPE, null);
366             }
367         } else {
368             context.setAttribute(VNFC_TYPE, null);
369         }
370         return storeCapabilityArtifact;
371     }
372
373     public void processActionLists(JSONObject content, String actionLevel, JSONArray vnfcActionList,
374         JSONArray vfModuleActionList,
375         JSONArray vnfActionList, JSONArray vmActionVnfcFunctionCodesList) {
376         if (validateActionLevel(actionLevel, ACTION_LEVEL_VNFC)) {
377             vnfcActionList.put(content.getString(ACTION));
378         }
379         if (validateActionLevel(actionLevel, ACTION_LEVEL_VF_MODULE)) {
380             vfModuleActionList.put(content.getString(ACTION));
381         }
382         if (validateActionLevel(actionLevel, ACTION_LEVEL_VNF)) {
383             vnfActionList.put(content.getString(ACTION));
384         }
385         if (validateActionLevel(actionLevel, ACTION_LEVEL_VM)) {
386             if (content.has(VNFC_FUNCTION_CODE_LIST)
387                 && !content.isNull(VNFC_FUNCTION_CODE_LIST) && content.get(
388                 VNFC_FUNCTION_CODE_LIST) instanceof JSONArray) {
389                 log.info("Found vnfc-function-code-list!!");
390                 JSONArray vnfcList = content.getJSONArray(VNFC_FUNCTION_CODE_LIST);
391                 JSONObject obj = new JSONObject();
392                 obj.put(content.getString(ACTION), vnfcList);
393                 vmActionVnfcFunctionCodesList.put(obj);
394             } else {
395                 log.info("Not getting JSONArray for VNFC FUNCTION CODES");
396             }
397         }
398
399     }
400
401     private boolean validateActionLevel(String actionLevel, String actionLevelVnfc) {
402         return null != actionLevel && actionLevel.equalsIgnoreCase(actionLevelVnfc);
403     }
404
405     public void processArtifactList(JSONObject content, DBService dbservice, SvcLogicContext context)
406         throws ArtifactHandlerInternalException {
407
408
409         try {
410             if (content.has(ARTIFACT_LIST_PARAM) && content.get(ARTIFACT_LIST_PARAM) instanceof JSONArray) {
411                 JSONArray artifactLists = (JSONArray) content.get(ARTIFACT_LIST_PARAM);
412                 JSONArray templateIdList = null;
413                 if (content.has("template-id-list") && null != content.get("template-id-list")
414                         && content.get("template-id-list") instanceof JSONArray) {
415                         templateIdList = content.getJSONArray("template-id-list");
416                 }
417                 doProcessArtifactList(dbservice, context, artifactLists, templateIdList);
418             }
419         } catch (Exception e) {
420             log.error("An error occurred when processing artifact list", e);
421             throw new ArtifactHandlerInternalException(e);
422         }
423     }
424
425     private void doProcessArtifactList(DBService dbservice, SvcLogicContext context, JSONArray artifactLists,
426         JSONArray templateIdList)
427         throws SvcLogicException, SQLException, ConfigurationException, DBException {
428         boolean pdFile = false;
429         int modelInd = 0;
430
431         for (int i = 0; i < artifactLists.length(); i++) {
432             String suffix = null;
433             String model = null;
434             JSONObject artifact = (JSONObject) artifactLists.get(i);
435             log.info("artifact is " + artifact);
436
437             //Get Model details
438             if (null != templateIdList && i>0 && i%2==0) {
439                 modelInd++;
440             }
441
442             setAttribute(context, artifact::getString, ARTIFACT_NAME);
443             context.setAttribute(FILE_CATEGORY,
444                 artifact.getString(ARTIFACT_TYPE));
445
446             if (artifact.getString(ARTIFACT_NAME) != null
447                 && artifact.getString(ARTIFACT_NAME).toLowerCase().startsWith(PD)) {
448
449                 suffix = artifact.getString(ARTIFACT_NAME).substring(PD.length());
450                 pdFile = true;
451             }
452             log.info("Artifact-type = " + context.getAttribute(FILE_CATEGORY));
453             log.info("Artifact-name = " + context.getAttribute(ARTIFACT_NAME));
454
455             if (null != templateIdList  && modelInd < templateIdList.length()) {
456                 model = templateIdList.getString(modelInd);
457                 log.info("Model is ::: "+model+"  ,modelInd = "+modelInd);
458             }
459
460             if (StringUtils.isNotBlank(model)) {
461                 dbservice.processSdcReferences(context, dbservice.isArtifactUpdateRequired(context,
462                     DB_SDC_REFERENCE, model),model);
463             }
464             else {
465                 dbservice.processSdcReferences(context, dbservice.isArtifactUpdateRequired(context,
466                 DB_SDC_REFERENCE));
467             }
468
469             cleanArtifactInstanceData(context);
470             //Moving this into the for loop to account for mulitple artifact sets with pds
471             if (pdFile) {
472                 log.info("Sending information related to pdfile Artifact");
473                 tryUpdateContext(dbservice, context, pdFile, suffix, model);
474                 pdFile=false;//set to false afterprocessing yang and Tosca
475             }
476         }
477
478     }
479
480     private void tryUpdateContext(DBService dbservice, SvcLogicContext context, boolean pdFile,
481             String suffix, String model)
482         throws SvcLogicException, SQLException, ConfigurationException, DBException {
483         if (pdFile) {
484             context.setAttribute(ARTIFACT_NAME, "Tosca".concat(suffix));
485             context.setAttribute(FILE_CATEGORY, TOSCA_MODEL);
486             dbservice.processSdcReferences(context,
487                 dbservice.isArtifactUpdateRequired(context, DB_SDC_REFERENCE, model), model);
488             context.setAttribute(ARTIFACT_NAME, "Yang".concat(suffix));
489             context.setAttribute(FILE_CATEGORY, PARAMETER_YANG);
490             dbservice.processSdcReferences(context,
491                 dbservice.isArtifactUpdateRequired(context, DB_SDC_REFERENCE, model), model);
492         }
493     }
494
495     public void processConfigTypeActions(JSONObject content, DBService dbservice, SvcLogicContext context)
496         throws ArtifactHandlerInternalException {
497
498         try {
499             if (contentsActionEquals(content, CONFIGURE_PARAM)
500                 || contentsActionEquals(content, CONFIG_MODIFY_PARAM)
501                 || contentsActionEquals(content, CONFIG_SCALE_OUT_PARAM)) {
502
503                 if (content.has(DOWNLOAD_DG_REFERENCE) && content.getString(DOWNLOAD_DG_REFERENCE).length() > 0) {
504
505                     setAttribute(context, content::getString, DOWNLOAD_DG_REFERENCE);
506                     dbservice.processDownloadDgReference(context,
507                         dbservice.isArtifactUpdateRequired(context, DB_DOWNLOAD_DG_REFERENCE));
508                 }
509                 if (StringUtils.isBlank(context.getAttribute(DOWNLOAD_DG_REFERENCE))) {
510                     context.setAttribute(DOWNLOAD_DG_REFERENCE,
511                         dbservice.getDownLoadDGReference(context));
512                 }
513                 dbservice
514                     .processConfigActionDg(context, dbservice.isArtifactUpdateRequired(context, DB_CONFIG_ACTION_DG));
515
516                 tryProcessInterfaceProtocol(content, dbservice, context);
517             }
518         } catch (Exception e) {
519             log.error("An error occurred when processing config type actions", e);
520             throw new ArtifactHandlerInternalException(e);
521         }
522     }
523
524     private void tryProcessInterfaceProtocol(JSONObject content, DBService dbservice, SvcLogicContext context)
525         throws SvcLogicException, SQLException, ConfigurationException, DBException {
526
527         if (contentsActionEquals(content, CONFIGURE_PARAM) || contentsActionEquals(content, CONFIG_SCALE_OUT_PARAM)) {
528             boolean isUpdateRequired = dbservice.isArtifactUpdateRequired(context, DB_DEVICE_INTERFACE_PROTOCOL);
529             if (contentsActionEquals(content, CONFIGURE_PARAM)
530                 || (contentsActionEquals(content, CONFIG_SCALE_OUT_PARAM)
531                 && !isUpdateRequired)) {
532
533                 dbservice.processDeviceInterfaceProtocol(context, isUpdateRequired);
534             }
535         }
536     }
537
538     private boolean contentsActionEquals(JSONObject content, String action) {
539         return content.getString(ACTION).equals(action);
540     }
541
542     public void processVmList(JSONObject content, SvcLogicContext context, DBService dbservice)
543         throws SvcLogicException {
544         JSONArray vmList = (JSONArray) content.get(VM);
545         dbservice.cleanUpVnfcReferencesForVnf(context);
546         for (int i = 0; i < vmList.length(); i++) {
547             JSONObject vmInstance = (JSONObject) vmList.get(i);
548             setAttribute(context, s -> String.valueOf(vmInstance.getInt(s)), VM_INSTANCE);
549             log.info("VALUE = " + context.getAttribute(VM_INSTANCE));
550             String templateId = vmInstance.optString(TEMPLATE_ID);
551             trySetContext(context, vmInstance, templateId);
552             if (vmInstance.get(VNFC) instanceof JSONArray) {
553                 JSONArray vnfcInstanceList = (JSONArray) vmInstance.get(VNFC);
554                 for (int k = 0; k < vnfcInstanceList.length(); k++) {
555                     JSONObject vnfcInstance = (JSONObject) vnfcInstanceList.get(k);
556
557                     setAttribute(context, s -> String.valueOf(vnfcInstance.getInt(s)), VNFC_INSTANCE);
558                     setAttribute(context, vnfcInstance::getString, VNFC_TYPE);
559                     setAttribute(context, vnfcInstance::getString, VNFC_FUNCTION_CODE);
560                     resolveContext(context, vnfcInstance);
561                     tryProcessVnfcReference(content, context, dbservice);
562                     cleanVnfcInstance(context);
563                 }
564                 context.setAttribute(VM_INSTANCE, null);
565                 context.setAttribute(TEMPLATE_ID, null);
566             }
567         }
568     }
569
570     private void trySetContext(SvcLogicContext context, JSONObject vmInstance, String templateId) {
571         if (StringUtils.isNotBlank(templateId)) {
572             setAttribute(context, vmInstance::optString, TEMPLATE_ID);
573         }
574     }
575
576     private void tryProcessVnfcReference(JSONObject content, SvcLogicContext context, DBService dbservice)
577         throws SvcLogicException {
578         if (content.getString(ACTION).equals(CONFIGURE_PARAM)
579             || content.getString(ACTION).equals(CONFIG_SCALE_OUT_PARAM)) {
580
581             dbservice.processVnfcReference(context, false);
582         }
583     }
584
585     private void resolveContext(SvcLogicContext context, JSONObject vnfcInstance) {
586         if (vnfcInstance.has(IPADDRESS_V4_OAM_VIP)) {
587             setAttribute(context, vnfcInstance::getString, IPADDRESS_V4_OAM_VIP);
588         }
589         if (vnfcInstance.has(GROUP_NOTATION_TYPE)) {
590             setAttribute(context, vnfcInstance::getString, GROUP_NOTATION_TYPE);
591         }
592         if (vnfcInstance.has(GROUP_NOTATION_VALUE)) {
593             setAttribute(context, vnfcInstance::getString, GROUP_NOTATION_VALUE);
594         }
595     }
596
597     private void cleanArtifactInstanceData(SvcLogicContext context) {
598         context.setAttribute(ARTIFACT_NAME, null);
599         context.setAttribute(FILE_CATEGORY, null);
600     }
601
602     private void cleanVnfcInstance(SvcLogicContext context) {
603
604         context.setAttribute(VNFC_INSTANCE, null);
605         context.setAttribute(VNFC_TYPE, null);
606         context.setAttribute(VNFC_FUNCTION_CODE, null);
607         context.setAttribute(IPADDRESS_V4_OAM_VIP, null);
608         context.setAttribute(GROUP_NOTATION_TYPE, null);
609         context.setAttribute(GROUP_NOTATION_VALUE, null);
610
611     }
612
613     private void processAndStoreCapabilitiesArtifact(DBService dbService, JSONObject documentInfo,
614         JSONObject capabilities, String capabilityArtifactName, String vnfType)
615         throws ArtifactHandlerInternalException {
616
617         log.info("Begin-->processAndStoreCapabilitiesArtifact ");
618
619         try {
620             JSONObject newCapabilitiesObject = new JSONObject();
621             newCapabilitiesObject.put("capabilities", capabilities);
622             SvcLogicContext context = new SvcLogicContext();
623             context.setAttribute(ARTIFACT_NAME, capabilityArtifactName);
624             context.setAttribute(FILE_CATEGORY, CAPABILITY);
625             context.setAttribute(ACTION, null);
626             context.setAttribute(VNFC_TYPE, null);
627             context.setAttribute(ARTIFACT_TYPE, null);
628             context.setAttribute(VNF_TYPE, vnfType);
629             context.setAttribute(ARTIFACT_CONTENTS, newCapabilitiesObject.toString());
630             dbService.processSdcReferences(context, dbService.isArtifactUpdateRequired(context, DB_SDC_REFERENCE));
631             int intVersion = 0;
632             String internalVersion = dbService.getInternalVersionNumber(context,
633                 context.getAttribute(ARTIFACT_NAME), null);
634             log.info("Internal Version number received from Database : " + internalVersion);
635             if (internalVersion != null) {
636                 intVersion = Integer.parseInt(internalVersion) + 1;
637             }
638             setAttribute(context, documentInfo::getString, SERVICE_UUID);
639             setAttribute(context, documentInfo::getString, DISTRIBUTION_ID);
640             setAttribute(context, documentInfo::getString, SERVICE_NAME);
641             setAttribute(context, documentInfo::getString, SERVICE_DESCRIPTION);
642             setAttribute(context, documentInfo::getString, RESOURCE_UUID);
643             setAttribute(context, documentInfo::getString, RESOURCE_INSTANCE_NAME);
644             setAttribute(context, documentInfo::getString, RESOURCE_VERSION);
645             setAttribute(context, documentInfo::getString, RESOURCE_TYPE);
646             setAttribute(context, documentInfo::getString, ARTIFACT_UUID);
647             setAttribute(context, documentInfo::getString, ARTIFACT_VERSION);
648             setAttribute(context, documentInfo::getString, ARTIFACT_DESRIPTION);
649             dbService.saveArtifacts(context, intVersion);
650         } catch (Exception e) {
651             log.error("Error saving capabilities artifact to DB", e);
652             throw new ArtifactHandlerInternalException("Error saving capabilities artifact to DB", e);
653         } finally {
654             log.info("End-->processAndStoreCapabilitiesArtifact ");
655         }
656     }
657
658     private void setAttribute(SvcLogicContext context, Function<String, String> value, String key) {
659         context.setAttribute(key, value.apply(key));
660     }
661
662     private void populateProtocolReference(DBService dbservice, JSONObject content)
663         throws ArtifactHandlerInternalException {
664         log.info("Begin-->populateProtocolReference ");
665         try {
666             SvcLogicContext context = new SvcLogicContext();
667             JSONObject scope = content.getJSONObject("scope");
668             String vnfType = tryGetVnfType(scope);
669             String protocol = tryGetProtocol(content);
670             String action = tryGetAction(content);
671             String actionLevel = tryGetActionLevel(content);
672             String template = tryGetTemplate(content);
673
674             boolean isUpdateNeeded = dbservice
675                 .isProtocolReferenceUpdateRequired(context, vnfType, protocol, action, actionLevel, template);
676             if (isUpdateNeeded) {
677                 dbservice.updateProtocolReference(context, vnfType, protocol, action, actionLevel, template);
678             } else {
679                 dbservice.insertProtocolReference(context, vnfType, protocol, action, actionLevel, template);
680             }
681         } catch (Exception e) {
682             log.error("Error inserting record into protocolReference", e);
683             throw new ArtifactHandlerInternalException("Error inserting record into protocolReference", e);
684         } finally {
685             log.info("End-->populateProtocolReference ");
686         }
687     }
688
689     private String tryGetVnfType(JSONObject scope) {
690         if (scope.has(VNF_TYPE) && !scope.isNull(VNF_TYPE)) {
691             return scope.getString(VNF_TYPE);
692         }
693         return null;
694     }
695
696     private String tryGetProtocol(JSONObject content) {
697         if (content.has(DEVICE_PROTOCOL)) {
698             return content.getString(DEVICE_PROTOCOL);
699         }
700         return null;
701     }
702
703     private String tryGetAction(JSONObject content) {
704         if (content.has(ACTION)) {
705             return content.getString(ACTION);
706         }
707         return null;
708     }
709
710     private String tryGetActionLevel(JSONObject content) {
711         if (content.has(ACTION_LEVEL)) {
712             return content.getString(ACTION_LEVEL);
713         }
714         return null;
715     }
716
717     private String tryGetTemplate(JSONObject content) {
718         if (content.has(TEMPLATE) && !content.isNull(TEMPLATE)) {
719             return content.getString(TEMPLATE);
720         }
721         return null;
722     }
723 }