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