Artifact-Handler - Appc-Inbound
[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.VNFC_TYPE_LIST;
79 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.VNF_TYPE;
80 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.URL;
81 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.OPENSTACK;
82
83 import com.att.eelf.configuration.EELFLogger;
84 import com.att.eelf.configuration.EELFManager;
85 import java.io.ByteArrayOutputStream;
86 import java.io.OutputStream;
87 import java.sql.SQLException;
88 import java.util.Map;
89 import java.util.function.Function;
90 import org.apache.commons.configuration.ConfigurationException;
91 import org.apache.commons.lang.StringUtils;
92 import org.json.JSONArray;
93 import org.json.JSONObject;
94 import org.onap.appc.artifact.handler.dbservices.DBException;
95 import org.onap.appc.artifact.handler.dbservices.DBService;
96 import org.onap.appc.artifact.handler.utils.ArtifactHandlerProviderUtil;
97 import org.onap.appc.yang.YANGGenerator;
98 import org.onap.appc.yang.impl.YANGGeneratorFactory;
99 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
100 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
101 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
102 import org.onap.sdnc.config.params.transformer.tosca.ArtifactProcessorImpl;
103 import org.onap.sdnc.config.params.transformer.tosca.exceptions.ArtifactProcessorException;
104
105 public class ArtifactHandlerNode implements SvcLogicJavaPlugin {
106
107     private static final EELFLogger log = EELFManager.getInstance().getLogger(ArtifactHandlerNode.class);
108     private static final String TOSCA_PARAM = "Tosca";
109     private static final String YANG_PARAM = "Yang";
110     private static final String ARTIFACT_LIST_PARAM = "artifact-list";
111     private static final String CONFIGURE_PARAM = "Configure";
112     private static final String CONFIG_SCALE_OUT_PARAM = "ConfigScaleOut";
113     private static final String CONFIG_MODIFY_PARAM = "ConfigModify";
114
115     public void processArtifact(Map<String, String> inParams, SvcLogicContext ctx) throws ArtifactProcessorException {
116
117         if (inParams == null || inParams.isEmpty()) {
118             return;
119         }
120         String postData = inParams.get("postData");
121         if (postData == null || postData.isEmpty()) {
122             return;
123         }
124         try {
125             log.info("Received request for process Artifact with params: " + inParams.toString());
126             JSONObject input = new JSONObject(postData).getJSONObject("input");
127             storeUpdateSdcArtifacts(input);
128         } catch (Exception e) {
129             log.error("Error when processing artifact", e);
130             throw new ArtifactProcessorException("Error occurred while processing artifact", e);
131         }
132     }
133
134     private boolean storeUpdateSdcArtifacts(JSONObject postDataJson) throws ArtifactHandlerInternalException {
135         log.info("Starting processing of SDC Artifacs into Handler with Data : " + postDataJson.toString());
136         try {
137             JSONObject requestInfo = (JSONObject) postDataJson.get(REQUEST_INFORMATION);
138             JSONObject documentInfo = (JSONObject) postDataJson.get(DOCUMENT_PARAMETERS);
139             String artifactName = documentInfo.getString(ARTIFACT_NAME);
140
141             if (artifactName != null) {
142                 updateStoreArtifacts(requestInfo, documentInfo);
143                 if (artifactName.toLowerCase().startsWith(REFERENCE)) {
144                     return storeReferenceData(requestInfo, documentInfo);
145                 } else if (artifactName.toLowerCase().startsWith(PD)) {
146                     return createDataForPD(requestInfo, documentInfo);
147                 }
148
149             } else {
150                 throw new ArtifactHandlerInternalException("Missing Artifact Name for Request: "
151                     + requestInfo.getString(REQUEST_ID));
152             }
153         } catch (Exception e) {
154             log.error("Error while processing request with id: "
155                 + ((JSONObject) postDataJson.get(REQUEST_INFORMATION)).getString(REQUEST_ID), e);
156
157             throw new ArtifactHandlerInternalException("Error while processing request with id: "
158                 + ((JSONObject) postDataJson.get(REQUEST_INFORMATION)).getString(REQUEST_ID), e);
159         }
160         return false;
161     }
162
163     private boolean createDataForPD(JSONObject requestInfo, JSONObject documentInfo)
164         throws ArtifactHandlerInternalException {
165
166         String fn = "ArtifactHandlerNode.createReferenceDataForPD";
167         String artifactName = documentInfo.getString(ARTIFACT_NAME);
168         log.info(fn + "Received PD File Name: " + artifactName + " and suffix lenght "
169             + PD.length());
170         try {
171
172             String suffix = artifactName.substring(PD.length());
173             createArtifactRecords(requestInfo, documentInfo, suffix);
174         } catch (Exception e) {
175             log.error("Error while creating PD data records", e);
176             throw new ArtifactHandlerInternalException("Error while creating PD data records", e);
177         }
178         return true;
179     }
180
181     private void createArtifactRecords(JSONObject requestInfo, JSONObject documentInfo, String suffix)
182         throws ArtifactHandlerInternalException {
183
184         try {
185             log.info("Creating Tosca Records and storing into SDC Artifacs");
186             String[] docs = {TOSCA_PARAM, YANG_PARAM};
187             ArtifactHandlerProviderUtil ahpUtil = new ArtifactHandlerProviderUtil();
188             String pdFileContents = documentInfo.getString(ARTIFACT_CONTENTS);
189
190             // Tosca generation
191             OutputStream toscaStream = new ByteArrayOutputStream();
192             String toscaContents;
193             ArtifactProcessorImpl toscaGenerator = new ArtifactProcessorImpl();
194             toscaGenerator.generateArtifact(pdFileContents, toscaStream);
195             toscaContents = toscaStream.toString();
196             log.info("Generated Tosca File : " + toscaContents);
197
198             String yangContents = "YANG generation is in Progress";
199             String yangName = null;
200
201             for (String doc : docs) {
202                 documentInfo.put(ARTIFACT_TYPE, doc.concat("Type"));
203                 documentInfo.put(ARTIFACT_DESRIPTION, doc.concat("Model"));
204                 if (doc.equals(TOSCA_PARAM)) {
205                     documentInfo.put(ARTIFACT_CONTENTS, ahpUtil.escapeSql(toscaContents));
206                 } else if (doc.equals(YANG_PARAM)) {
207                     documentInfo.put(ARTIFACT_CONTENTS, ahpUtil.escapeSql(yangContents));
208                 }
209                 documentInfo.put(ARTIFACT_NAME, doc.concat(suffix));
210                 yangName = doc.concat(suffix);
211                 updateStoreArtifacts(requestInfo, documentInfo);
212             }
213
214             String artifactId = getArtifactID(yangName);
215             OutputStream yangStream = new ByteArrayOutputStream();
216             YANGGenerator yangGenerator = YANGGeneratorFactory.getYANGGenerator();
217             yangGenerator.generateYANG(artifactId, toscaContents, yangStream);
218             yangContents = yangStream.toString();
219
220             if (yangContents != null) {
221                 updateYangContents(artifactId, ahpUtil.escapeSql(yangContents));
222             }
223         } catch (Exception e) {
224             log.error("Error while creating artifact records", e);
225             throw new ArtifactHandlerInternalException("Error while creating artifact records", e);
226         }
227
228     }
229
230     private void updateYangContents(String artifactId, String yangContents) throws SvcLogicException {
231         SvcLogicContext context = new SvcLogicContext();
232         DBService dbservice = DBService.initialise();
233         dbservice.updateYangContents(context, artifactId, yangContents);
234     }
235
236     private String getArtifactID(String yangName) throws SvcLogicException {
237         SvcLogicContext context = new SvcLogicContext();
238         DBService dbservice = DBService.initialise();
239         return dbservice.getArtifactID(context, yangName);
240     }
241
242     protected boolean updateStoreArtifacts(JSONObject requestInfo, JSONObject documentInfo)
243         throws SvcLogicException {
244         log.info("UpdateStoreArtifactsStarted storing of SDC Artifacs ");
245
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         log.info("Started storing of SDC Artifacs into Handler");
281         try {
282             DBService dbservice = DBService.initialise();
283             ArtifactHandlerProviderUtil ahpUtil = new ArtifactHandlerProviderUtil();
284             String contentString =
285                 ahpUtil.escapeSql(documentInfo.getString(ARTIFACT_CONTENTS));
286             String artifactName =
287                 ahpUtil.escapeSql(documentInfo.getString(ARTIFACT_NAME));
288             String capabilityArtifactName =
289                 StringUtils.replace(artifactName, ARTIFACT_NAME_REFERENCE,
290                     ARTIFACT_NAME_CAPABILITY);
291             JSONObject capabilities = new JSONObject();
292             JSONArray vnfActionList = new JSONArray();
293             JSONArray vfModuleActionList = new JSONArray();
294             JSONArray vnfcActionList = new JSONArray();
295             JSONArray vmActionVnfcFunctionCodesList = new JSONArray();
296             String vnfType = null;
297             JSONObject contentObject = new JSONObject(contentString);
298             JSONArray contentArray = contentObject.getJSONArray("reference_data");
299             boolean storeCapabilityArtifact = true;
300
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 }