b6fd2f59908a306b7a6ae076608f0d8e175e160a
[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 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ANSIBLE;
85
86 import com.att.eelf.configuration.EELFLogger;
87 import com.att.eelf.configuration.EELFManager;
88 import java.io.ByteArrayOutputStream;
89 import java.io.OutputStream;
90 import java.sql.SQLException;
91 import java.util.Map;
92 import java.util.function.Function;
93 import org.apache.commons.configuration.ConfigurationException;
94 import org.apache.commons.lang.StringUtils;
95 import org.json.JSONArray;
96 import org.json.JSONObject;
97 import org.onap.appc.artifact.handler.dbservices.DBException;
98 import org.onap.appc.artifact.handler.dbservices.DBService;
99 import org.onap.appc.artifact.handler.utils.ArtifactHandlerProviderUtil;
100 import org.onap.appc.yang.YANGGenerator;
101 import org.onap.appc.yang.impl.YANGGeneratorFactory;
102 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
103 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
104 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
105 import org.onap.sdnc.config.params.transformer.tosca.ArtifactProcessorImpl;
106 import org.onap.sdnc.config.params.transformer.tosca.exceptions.ArtifactProcessorException;
107
108 import org.json.JSONException;
109 import java.util.ArrayList;
110 import java.util.HashMap;
111 import java.util.List;
112
113 public class ArtifactHandlerNode implements SvcLogicJavaPlugin {
114
115     private static final EELFLogger log = EELFManager.getInstance().getLogger(ArtifactHandlerNode.class);
116     private static final String TOSCA_PARAM = "Tosca";
117     private static final String YANG_PARAM = "Yang";
118     private static final String ARTIFACT_LIST_PARAM = "artifact-list";
119     private static final String CONFIGURE_PARAM = "Configure";
120     private static final String CONFIG_SCALE_OUT_PARAM = "ConfigScaleOut";
121     private static final String CONFIG_MODIFY_PARAM = "ConfigModify";
122     private static final String GET_CONFIG = "GetConfig";
123     private static final String POST_EVACUATE= "PostEvacuate";
124     private static final String PRE_EVACUATE = "PreEvacuate";
125     private static final String POST_MIGRATE = "PostMigrate";
126     private static final String PRE_MIGRATE = "PreMigrate";
127     private static final String PRE_REBUILD = "PreRebuild";
128     private static final String POST_REBUILD = "PostRebuild";
129     private static final String STOP_TRAFFIC = "StopTraffic";
130     public void processArtifact(Map<String, String> inParams, SvcLogicContext ctx) throws ArtifactProcessorException {
131
132         if (inParams == null || inParams.isEmpty()) {
133             return;
134         }
135         String postData = inParams.get("postData");
136         if ( StringUtils.isBlank(postData)) {
137             return;
138         }
139         try {
140             log.info("Received request for process Artifact with params: " + inParams.toString());
141             JSONObject input = new JSONObject(postData).getJSONObject("input");
142             storeUpdateSdcArtifacts(input);
143         } catch (Exception e) {
144             log.error("Error when processing artifact", e);
145             throw new ArtifactProcessorException("Error occurred while processing artifact, " + e.getMessage(), e);
146         }
147     }
148
149     private boolean storeUpdateSdcArtifacts(JSONObject postDataJson) throws ArtifactHandlerInternalException {
150         log.info("Starting processing of SDC Artifacs into Handler with Data : " + postDataJson.toString());
151         try {
152             JSONObject requestInfo = (JSONObject) postDataJson.get(REQUEST_INFORMATION);
153             JSONObject documentInfo = (JSONObject) postDataJson.get(DOCUMENT_PARAMETERS);
154             String artifactName = documentInfo.getString(ARTIFACT_NAME);
155
156             if (StringUtils.isBlank(artifactName))
157                 throw new ArtifactHandlerInternalException("Missing Artifact Name ");
158
159             if (artifactName.toLowerCase().startsWith(ANSIBLE)) {
160                 validateAnsibleAdminArtifact(documentInfo);
161                 log.info("validateAnsibleAdminArtifact sucessfully done");
162             }
163
164             updateStoreArtifacts(requestInfo, documentInfo);
165
166             if (artifactName.toLowerCase().startsWith(REFERENCE)) {
167                 return storeReferenceData(requestInfo, documentInfo);
168             } else if (artifactName.toLowerCase().startsWith(PD)) {
169                 return createDataForPD(requestInfo, documentInfo);
170             }
171
172         } catch (Exception e) {
173             log.error("Error while processing request with id: "
174                     + ((JSONObject) postDataJson.get(REQUEST_INFORMATION)).getString(REQUEST_ID), e);
175             throw new ArtifactHandlerInternalException("Error while processing request with id: "
176                     + ((JSONObject) postDataJson.get(REQUEST_INFORMATION)).getString(REQUEST_ID)
177                     + ", Exception Message : " + e.getMessage(), e);
178         }
179         return false;
180     }
181
182     public void validateAnsibleAdminArtifact(JSONObject documentInfo) throws ArtifactHandlerInternalException {
183
184         String fn = "ArtifactHandlerNode.validateAnsibleAdminArtifact";
185         String artifactName = documentInfo.getString(ARTIFACT_NAME);
186         log.info(fn + ": Received Admin File Name: " + artifactName + " ArtifactCotent : "
187                 + documentInfo.getString(ARTIFACT_CONTENTS));
188         try {
189             ArtifactHandlerProviderUtil ahpUtil = new ArtifactHandlerProviderUtil();
190             String contentString = ahpUtil.escapeSql(documentInfo.getString(ARTIFACT_CONTENTS));
191             JSONObject artifact = new JSONObject(contentString);
192             JSONArray fqdnList = artifact.getJSONArray("fqdn-list");
193             Map<String, List<String>> artifactMap = new HashMap<>();
194             
195             
196             for (int i = 0; i < fqdnList.length(); i++) {
197                 JSONObject fqdn = fqdnList.getJSONObject(i);
198                 List<String> valuesforFQDN = populateValueForFQDN(fqdn);
199                 artifactMap.put(fqdn.getString("vnf-management-server-fqdn"), valuesforFQDN);
200             }
201
202             validateKeyValue(artifactMap);
203
204         } catch (JSONException je) {
205             log.error(
206                     fn + " ansible_admin artifact content may not be a valid JSON, error message : " + je.getMessage());
207             throw new ArtifactHandlerInternalException(
208                     "JSON Exception:ansible admin artifact content may not be a valid JSON, error message : " + je.getMessage(), je);
209         } catch (ArtifactHandlerInternalException ae) {
210          throw ae;
211         } catch (Exception e) {
212             
213             log.error(fn + "Error while creating Admin data records", e);
214             throw new ArtifactHandlerInternalException("Error while processing ansible admin artifact" + e.getMessage(), e);
215         }
216
217     }
218     private void validateKeyValue(Map<String, List<String>> artifactMap) throws ArtifactHandlerInternalException {
219         for (Map.Entry<String,List<String>> entry1:artifactMap.entrySet()) {    
220             for (String value : entry1.getValue()) {
221                 for(Map.Entry<String,List<String>> entry2:artifactMap.entrySet() ) {                                      
222                     if (!entry1.getKey().equals(entry2.getKey()) && entry2.getValue().contains(value)) {
223                         log.info("Validation Failure, error message : Ansible Admin artifact has CloudOwner-RegionId-Tenant : " + value
224                                 + " mapped to multiple FQDN :" + entry1.getKey() + " & " + entry2.getKey());
225                         throw new ArtifactHandlerInternalException(
226                                 "Validation Failure, error message : Ansible Admin artifact has CloudOwner-RegionId-Tenant : " + value
227                                 + " mapped to multiple FQDN :" + entry1.getKey() + " & " + entry2.getKey());
228                     }
229
230
231                 }
232             }
233     }
234 }
235
236     private List<String> populateValueForFQDN(JSONObject fqdn) {
237         log.info("Inside populateValueForFQDN :" + fqdn.getString("vnf-management-server-fqdn"));
238         List<String> valuesforFQDN = new ArrayList<>();
239         JSONArray cloudJsonList = fqdn.getJSONArray("cloud-owner-list");
240         for (int j = 0; j < cloudJsonList.length(); j++) {
241             String cloudOwner = cloudJsonList.getJSONObject(j).getString("cloud-owner");
242             JSONArray regionList = cloudJsonList.getJSONObject(j).getJSONArray("region-id-list");
243             for (int i = 0; i < regionList.length(); i++) {
244                 String region = regionList.getJSONObject(i).getString("region-id");
245                 JSONArray tenantList = regionList.getJSONObject(i).getJSONArray("tenant-id-list");
246
247                 for (int k = 0; k < tenantList.length(); k++) {
248                     String tenant =  tenantList.getString(k);
249                     String valueforFQDN = cloudOwner + "-" + region + "-" + tenant;
250                     log.info("valueforFQDN for i " + i + " & j " + j + " :" + valueforFQDN);
251                     valuesforFQDN.add(valueforFQDN);
252                 }
253             }
254         }
255         return valuesforFQDN;
256     }
257
258     private boolean createDataForPD(JSONObject requestInfo, JSONObject documentInfo)
259             throws ArtifactHandlerInternalException {
260
261         String fn = "ArtifactHandlerNode.createReferenceDataForPD";
262         String artifactName = documentInfo.getString(ARTIFACT_NAME);
263         log.info(fn + "Received PD File Name: " + artifactName + " and suffix length " + PD.length());
264         try {
265
266             String suffix = artifactName.substring(PD.length());
267             createArtifactRecords(requestInfo, documentInfo, suffix);
268         } catch (Exception e) {
269             log.error("Error while creating PD data records", e);
270             throw new ArtifactHandlerInternalException("Error while creating PD data records", e);
271         }
272         return true;
273     }
274
275     private void createArtifactRecords(JSONObject requestInfo, JSONObject documentInfo, String suffix)
276             throws ArtifactHandlerInternalException {
277
278         try {
279             log.info("Creating Tosca Records and storing into SDC Artifacs");
280             String[] docs = { TOSCA_PARAM, YANG_PARAM };
281             ArtifactHandlerProviderUtil ahpUtil = new ArtifactHandlerProviderUtil();
282             String pdFileContents = documentInfo.getString(ARTIFACT_CONTENTS);
283
284             // Tosca generation
285             OutputStream toscaStream = new ByteArrayOutputStream();
286             String toscaContents;
287             ArtifactProcessorImpl toscaGenerator = getArtifactProcessorImpl();
288             toscaGenerator.generateArtifact(pdFileContents, toscaStream);
289             toscaContents = toscaStream.toString();
290             log.info("Generated Tosca File : " + toscaContents);
291
292             String yangContents = "YANG generation is in Progress";
293             String yangName = null;
294
295             for (String doc : docs) {
296                 documentInfo.put(ARTIFACT_TYPE, doc.concat("Type"));
297                 documentInfo.put(ARTIFACT_DESRIPTION, doc.concat("Model"));
298                 if (doc.equals(TOSCA_PARAM)) {
299                     documentInfo.put(ARTIFACT_CONTENTS, ahpUtil.escapeSql(toscaContents));
300                 } else if (doc.equals(YANG_PARAM)) {
301                     documentInfo.put(ARTIFACT_CONTENTS, ahpUtil.escapeSql(yangContents));
302                 }
303                 documentInfo.put(ARTIFACT_NAME, doc.concat(suffix));
304                 yangName = doc.concat(suffix);
305                 updateStoreArtifacts(requestInfo, documentInfo);
306             }
307
308             String artifactId = getArtifactID(yangName);
309             OutputStream yangStream = new ByteArrayOutputStream();
310             YANGGenerator yangGenerator = YANGGeneratorFactory.getYANGGenerator();
311             yangGenerator.generateYANG(artifactId, toscaContents, yangStream);
312             yangContents = yangStream.toString();
313
314             if (yangContents != null) {
315                 updateYangContents(artifactId, ahpUtil.escapeSql(yangContents));
316             }
317         } catch (Exception e) {
318             log.error("Error while creating artifact records", e);
319             throw new ArtifactHandlerInternalException("Error while creating artifact records", e);
320         }
321
322     }
323
324     private void updateYangContents(String artifactId, String yangContents) throws SvcLogicException {
325         SvcLogicContext context = new SvcLogicContext();
326         DBService dbservice = DBService.initialise();
327         dbservice.updateYangContents(context, artifactId, yangContents);
328     }
329
330     private String getArtifactID(String yangName) throws SvcLogicException {
331         SvcLogicContext context = new SvcLogicContext();
332         DBService dbservice = DBService.initialise();
333         return dbservice.getArtifactID(context, yangName);
334     }
335
336     protected boolean updateStoreArtifacts(JSONObject requestInfo, JSONObject documentInfo) throws SvcLogicException {
337         log.info("UpdateStoreArtifactsStarted storing of SDC Artifacs ");
338         SvcLogicContext context = new SvcLogicContext();
339         DBService dbservice = DBService.initialise();
340         ArtifactHandlerProviderUtil ahpUtil = new ArtifactHandlerProviderUtil();
341         int intversion = 0;
342         context.setAttribute("artifact_name", documentInfo.getString(ARTIFACT_NAME));
343         String internalVersion = dbservice.getInternalVersionNumber(context, documentInfo.getString(ARTIFACT_NAME),
344                 null);
345         log.info("Internal Version number received from Database : " + internalVersion);
346         if (internalVersion != null) {
347             intversion = Integer.parseInt(internalVersion);
348             intversion++;
349         }
350         setAttribute(context, documentInfo::getString, SERVICE_UUID);
351         setAttribute(context, documentInfo::getString, DISTRIBUTION_ID);
352         setAttribute(context, documentInfo::getString, SERVICE_NAME);
353         setAttribute(context, documentInfo::getString, SERVICE_DESCRIPTION);
354         setAttribute(context, documentInfo::getString, RESOURCE_UUID);
355         setAttribute(context, documentInfo::getString, RESOURCE_INSTANCE_NAME);
356         setAttribute(context, documentInfo::getString, RESOURCE_VERSION);
357         setAttribute(context, documentInfo::getString, RESOURCE_TYPE);
358         setAttribute(context, documentInfo::getString, ARTIFACT_UUID);
359         setAttribute(context, documentInfo::getString, ARTIFACT_TYPE);
360         setAttribute(context, documentInfo::getString, ARTIFACT_VERSION);
361         setAttribute(context, documentInfo::getString, ARTIFACT_DESRIPTION);
362         setAttribute(context, documentInfo::getString, ARTIFACT_NAME);
363         setAttribute(context, s -> ahpUtil.escapeSql(documentInfo.getString(s)), ARTIFACT_CONTENTS);
364
365         dbservice.saveArtifacts(context, intversion);
366         return true;
367     }
368
369     public boolean storeReferenceData(JSONObject requestInfo, JSONObject documentInfo)
370             throws ArtifactHandlerInternalException {
371
372         log.info("Started storing of SDC Artifacs into Handler");
373         try {
374             DBService dbservice = DBService.initialise();
375             ArtifactHandlerProviderUtil ahpUtil = new ArtifactHandlerProviderUtil();
376             String contentString = ahpUtil.escapeSql(documentInfo.getString(ARTIFACT_CONTENTS));
377             String artifactName = ahpUtil.escapeSql(documentInfo.getString(ARTIFACT_NAME));
378             String capabilityArtifactName = StringUtils.replace(artifactName, ARTIFACT_NAME_REFERENCE,
379                     ARTIFACT_NAME_CAPABILITY);
380             JSONObject capabilities = new JSONObject();
381             JSONArray vnfActionList = new JSONArray();
382             JSONArray vfModuleActionList = new JSONArray();
383             JSONArray vnfcActionList = new JSONArray();
384             JSONArray vmActionVnfcFunctionCodesList = new JSONArray();
385             String vnfType = null;
386             JSONObject contentObject = new JSONObject(contentString);
387             JSONArray contentArray = contentObject.getJSONArray("reference_data");
388             boolean storeCapabilityArtifact = true;
389             for (int a = 0; a < contentArray.length(); a++) {
390                 JSONObject content = (JSONObject) contentArray.get(a);
391                 log.info("contentString =" + content.toString());
392                 JSONObject scope = content.getJSONObject("scope");
393                 log.info("scope :" + scope);
394                 SvcLogicContext context = new SvcLogicContext();
395                 vnfType = scope.getString(VNF_TYPE);
396                 setAttribute(context, scope::getString, VNF_TYPE);
397                 setAttribute(context, content::getString, ACTION);
398                 String actionLevel = content.getString(ACTION_LEVEL);
399                 setAttribute(context, content::getString, ACTION_LEVEL);
400                 setAttribute(context, documentInfo::getString, ARTIFACT_TYPE);
401                 processActionLists(content, actionLevel, vnfcActionList, vfModuleActionList, vnfActionList,
402                         vmActionVnfcFunctionCodesList);
403                 JSONArray vnfcTypeList = setVnfcTypeInformation(scope, context);
404                 storeCapabilityArtifact = isCapabilityArtifactNeeded(context);
405                 if (content.has(DEVICE_PROTOCOL)) {
406                     setAttribute(context, content::getString, DEVICE_PROTOCOL);
407                 }
408                 if (content.has(USER_NAME)) {
409                     setAttribute(context, content::getString, USER_NAME);
410                 }
411                 if (content.has(PORT_NUMBER)) {
412                     setAttribute(context, content::getString, PORT_NUMBER);
413                 }
414                 if (content.has(URL)) {
415                     setAttribute(context, content::getString, URL);
416                 }
417                 processArtifactList(content, dbservice, context, vnfcTypeList);
418                 processConfigTypeActions(content, dbservice, context);
419                 dbservice.processDeviceAuthentication(context,
420                         dbservice.isArtifactUpdateRequired(context, DB_DEVICE_AUTHENTICATION));
421
422                 String actionProtocol = tryGetProtocol(content);
423                 if (!StringUtils.equalsIgnoreCase(actionProtocol, OPENSTACK)) {
424                     populateProtocolReference(dbservice, content);
425                 }
426
427                 context.setAttribute(VNFC_TYPE, null);
428
429                 if (content.has(VM) && content.get(VM) instanceof JSONArray) {
430                     processVmList(content, context, dbservice);
431                 }
432             }
433             if (storeCapabilityArtifact) {
434                 capabilities.put("vnf", vnfActionList);
435                 capabilities.put("vf-module", vfModuleActionList);
436                 capabilities.put("vnfc", vnfcActionList);
437                 capabilities.put("vm", vmActionVnfcFunctionCodesList);
438                 processAndStoreCapabilitiesArtifact(dbservice, documentInfo, capabilities, capabilityArtifactName,
439                         vnfType);
440             }
441
442         } catch (Exception e) {
443
444             log.error("Error while storing reference data", e);
445             throw new ArtifactHandlerInternalException("Error while storing reference data", e);
446         }
447
448         return true;
449     }
450
451     public boolean isCapabilityArtifactNeeded(SvcLogicContext context) {
452         String vnfcType = context.getAttribute(VNFC_TYPE);
453         if (StringUtils.isNotBlank(vnfcType)) {
454             log.info("No capability Artifact for this reference data as it is at VNFC level!!");
455             return false;
456         } else {
457             return true;
458         }
459     }
460
461     public JSONArray setVnfcTypeInformation(JSONObject scope, SvcLogicContext context) {
462         JSONArray vnfcTypeList = null;
463         if (scope.has(VNFC_TYPE) && !scope.isNull(VNFC_TYPE)) {
464             String vnfcTypeScope = scope.getString(VNFC_TYPE);
465             if (StringUtils.isNotBlank(vnfcTypeScope)) {
466                 setAttribute(context, scope::getString, VNFC_TYPE);
467                 log.info("VNFC Type has been set for this reference artifact!!" + vnfcTypeScope);
468             } else {
469                 context.setAttribute(VNFC_TYPE, null);
470             }
471         } else {
472             context.setAttribute(VNFC_TYPE, null);
473         }
474         if (scope.has(VNFC_TYPE_LIST) && !scope.isNull(VNFC_TYPE_LIST)
475                 && scope.get(VNFC_TYPE_LIST) instanceof JSONArray) {
476             vnfcTypeList = scope.getJSONArray(VNFC_TYPE_LIST);
477             log.info("VNFC TYPE LIST found for this artifact!! " + vnfcTypeList.toString());
478         }
479         return vnfcTypeList;
480     }
481
482     public void processActionLists(JSONObject content, String actionLevel, JSONArray vnfcActionList,
483             JSONArray vfModuleActionList, JSONArray vnfActionList, JSONArray vmActionVnfcFunctionCodesList) {
484         if (validateActionLevel(actionLevel, ACTION_LEVEL_VNFC)) {
485             vnfcActionList.put(content.getString(ACTION));
486         }
487         if (validateActionLevel(actionLevel, ACTION_LEVEL_VF_MODULE)) {
488             vfModuleActionList.put(content.getString(ACTION));
489         }
490         if (validateActionLevel(actionLevel, ACTION_LEVEL_VNF)) {
491             vnfActionList.put(content.getString(ACTION));
492         }
493         if (validateActionLevel(actionLevel, ACTION_LEVEL_VM)) {
494             if (content.has(VNFC_FUNCTION_CODE_LIST) && !content.isNull(VNFC_FUNCTION_CODE_LIST)
495                     && content.get(VNFC_FUNCTION_CODE_LIST) instanceof JSONArray) {
496                 log.info("Found vnfc-function-code-list!!");
497                 JSONArray vnfcList = content.getJSONArray(VNFC_FUNCTION_CODE_LIST);
498                 JSONObject obj = new JSONObject();
499                 obj.put(content.getString(ACTION), vnfcList);
500                 vmActionVnfcFunctionCodesList.put(obj);
501             } else {
502                 log.info("Not getting JSONArray for VNFC FUNCTION CODES");
503             }
504         }
505
506     }
507
508     private boolean validateActionLevel(String actionLevel, String actionLevelVnfc) {
509         return null != actionLevel && actionLevel.equalsIgnoreCase(actionLevelVnfc);
510     }
511
512     public void processArtifactList(JSONObject content, DBService dbservice, SvcLogicContext context,
513             JSONArray vnfcTypeList) throws ArtifactHandlerInternalException {
514
515         try {
516             if (content.has(ARTIFACT_LIST_PARAM) && content.get(ARTIFACT_LIST_PARAM) instanceof JSONArray) {
517                 JSONArray artifactLists = (JSONArray) content.get(ARTIFACT_LIST_PARAM);
518                 JSONArray templateIdList = null;
519                 if (content.has("template-id-list") && null != content.get("template-id-list")
520                         && content.get("template-id-list") instanceof JSONArray) {
521                     templateIdList = content.getJSONArray("template-id-list");
522                 }
523                 doProcessArtifactList(dbservice, context, artifactLists, templateIdList, vnfcTypeList);
524
525             }
526         } catch (Exception e) {
527             log.error("An error occurred when processing artifact list", e);
528             throw new ArtifactHandlerInternalException(e);
529         }
530     }
531
532     private void doProcessArtifactList(DBService dbservice, SvcLogicContext context, JSONArray artifactLists,
533             JSONArray templateIdList, JSONArray vnfcTypeList)
534             throws SvcLogicException, SQLException, ConfigurationException, DBException {
535         boolean pdFile = false;
536         int modelInd = 0, vnfcRefInd = 0;
537         for (int i = 0; i < artifactLists.length(); i++) {
538             String suffix = null;
539             String model = null;
540             JSONObject artifact = (JSONObject) artifactLists.get(i);
541             log.info("artifact is " + artifact);
542
543             // Get Model details
544             if (null != templateIdList && i > 0 && i % 2 == 0) {// Should this be changed to 3 to account for 3
545                                                                 // artifacts
546                 modelInd++;
547             }
548             if (null != vnfcTypeList && i > 0 && i % 3 == 0) {
549                 // TDP 517180 - CD tool has made changes to send 3 artifacts instead of 2
550                 vnfcRefInd++;
551             }
552             setAttribute(context, artifact::getString, ARTIFACT_NAME);
553             context.setAttribute(FILE_CATEGORY, artifact.getString(ARTIFACT_TYPE));
554
555             if (artifact.getString(ARTIFACT_NAME) != null
556                     && artifact.getString(ARTIFACT_NAME).toLowerCase().startsWith(PD)) {
557
558                 suffix = artifact.getString(ARTIFACT_NAME).substring(PD.length());
559                 pdFile = true;
560             }
561             log.info("Artifact-type = " + context.getAttribute(FILE_CATEGORY));
562             log.info("Artifact-name = " + context.getAttribute(ARTIFACT_NAME));
563
564             if (null != templateIdList && modelInd < templateIdList.length()) {
565                 model = templateIdList.getString(modelInd);
566                 log.info("Model is ::: " + model + "  ,modelInd = " + modelInd);
567             }
568             if (null != vnfcTypeList && vnfcRefInd < vnfcTypeList.length()) {
569                 String vnfcType = vnfcTypeList.getString(vnfcRefInd);
570                 if (StringUtils.isNotBlank(vnfcType)) {
571                     context.setAttribute(VNFC_TYPE, vnfcType);
572                 }
573                 log.info("Setting vnfc type from vnfc-type-list ::" + vnfcType);
574             }
575             if (StringUtils.isNotBlank(model)) {
576                 dbservice.processSdcReferences(context,
577                         dbservice.isArtifactUpdateRequired(context, DB_SDC_REFERENCE, model), model);
578             } else {
579                 dbservice.processSdcReferences(context, dbservice.isArtifactUpdateRequired(context, DB_SDC_REFERENCE));
580             }
581
582             cleanArtifactInstanceData(context);
583             // Moving this into the for loop to account for mulitple artifact sets with pds
584             if (pdFile) {
585                 log.info("Sending information related to pdfile Artifact");
586                 tryUpdateContext(dbservice, context, pdFile, suffix, model);
587                 pdFile = false;// set to false afterprocessing yang and Tosca
588             }
589         }
590
591     }
592
593     private void tryUpdateContext(DBService dbservice, SvcLogicContext context, boolean pdFile, String suffix,
594             String model) throws SvcLogicException, SQLException, ConfigurationException, DBException {
595         if (pdFile) {
596             context.setAttribute(ARTIFACT_NAME, "Tosca".concat(suffix));
597             context.setAttribute(FILE_CATEGORY, TOSCA_MODEL);
598             dbservice.processSdcReferences(context,
599                     dbservice.isArtifactUpdateRequired(context, DB_SDC_REFERENCE, model), model);
600             context.setAttribute(ARTIFACT_NAME, "Yang".concat(suffix));
601             context.setAttribute(FILE_CATEGORY, PARAMETER_YANG);
602             dbservice.processSdcReferences(context,
603                     dbservice.isArtifactUpdateRequired(context, DB_SDC_REFERENCE, model), model);
604         }
605     }
606
607     public void processConfigTypeActions(JSONObject content, DBService dbservice, SvcLogicContext context)
608             throws ArtifactHandlerInternalException {
609         try {
610             if (isContentActionConfig(content) 
611                     || contentsActionEquals(content, GET_CONFIG)
612                     || contentsActionEquals(content, POST_EVACUATE)
613                     || contentsActionEquals(content, PRE_EVACUATE)
614                     || contentsActionEquals(content, POST_MIGRATE)
615                     || contentsActionEquals(content, PRE_MIGRATE)
616                     || contentsActionEquals(content, POST_REBUILD)
617                     || contentsActionEquals(content, PRE_REBUILD)
618                     || contentsActionEquals(content, STOP_TRAFFIC)
619                 ) {
620
621                 if (content.has(DOWNLOAD_DG_REFERENCE) && content.getString(DOWNLOAD_DG_REFERENCE).length() > 0) {
622
623                     setAttribute(context, content::getString, DOWNLOAD_DG_REFERENCE);
624                     dbservice.processDownloadDgReference(context,
625                             dbservice.isArtifactUpdateRequired(context, DB_DOWNLOAD_DG_REFERENCE));
626                 }
627                 if (StringUtils.isBlank(context.getAttribute(DOWNLOAD_DG_REFERENCE))) {
628                     context.setAttribute(DOWNLOAD_DG_REFERENCE, dbservice.getDownLoadDGReference(context));
629                 }
630                 dbservice.processConfigActionDg(context,
631                         dbservice.isArtifactUpdateRequired(context, DB_CONFIG_ACTION_DG));
632
633                 tryProcessInterfaceProtocol(content, dbservice, context);
634             }
635         } catch (Exception e) {
636             log.error("An error occurred when processing config type actions", e);
637             throw new ArtifactHandlerInternalException(e);
638         }
639     }
640
641     private void tryProcessInterfaceProtocol(JSONObject content, DBService dbservice, SvcLogicContext context)
642             throws SvcLogicException, SQLException, ConfigurationException, DBException {
643         if (isContentActionConfig(content)) {
644             boolean isUpdateRequired = dbservice.isArtifactUpdateRequired(context, DB_DEVICE_INTERFACE_PROTOCOL);
645             if (isContentActionConfig(content) && !isUpdateRequired) {
646                 dbservice.processDeviceInterfaceProtocol(context, isUpdateRequired);
647             }
648         }
649     }
650     
651     //Consolidates the if statements required to check if the action is one of the config actions
652     private boolean isContentActionConfig(JSONObject content) {
653         return contentsActionEquals(content, CONFIGURE_PARAM)
654                 || contentsActionEquals(content, CONFIG_MODIFY_PARAM)
655                 || contentsActionEquals(content, CONFIG_SCALE_OUT_PARAM);
656     }
657
658     private boolean contentsActionEquals(JSONObject content, String action) {
659         return content.getString(ACTION).equals(action);
660     }
661
662     public void processVmList(JSONObject content, SvcLogicContext context, DBService dbservice)
663             throws SvcLogicException {
664         JSONArray vmList = (JSONArray) content.get(VM);
665         dbservice.cleanUpVnfcReferencesForVnf(context);
666         for (int i = 0; i < vmList.length(); i++) {
667             JSONObject vmInstance = (JSONObject) vmList.get(i);
668             setAttribute(context, s -> String.valueOf(vmInstance.getInt(s)), VM_INSTANCE);
669             log.info("VALUE = " + context.getAttribute(VM_INSTANCE));
670             String templateId = vmInstance.optString(TEMPLATE_ID);
671             trySetContext(context, vmInstance, templateId);
672             if (vmInstance.get(VNFC) instanceof JSONArray) {
673                 JSONArray vnfcInstanceList = (JSONArray) vmInstance.get(VNFC);
674                 for (int k = 0; k < vnfcInstanceList.length(); k++) {
675                     JSONObject vnfcInstance = (JSONObject) vnfcInstanceList.get(k);
676
677                     setAttribute(context, s -> String.valueOf(vnfcInstance.getInt(s)), VNFC_INSTANCE);
678                     setAttribute(context, vnfcInstance::getString, VNFC_TYPE);
679                     setAttribute(context, vnfcInstance::getString, VNFC_FUNCTION_CODE);
680                     resolveContext(context, vnfcInstance);
681                     tryProcessVnfcReference(content, context, dbservice);
682                     cleanVnfcInstance(context);
683                 }
684                 context.setAttribute(VM_INSTANCE, null);
685                 context.setAttribute(TEMPLATE_ID, null);
686             }
687         }
688     }
689
690     private void trySetContext(SvcLogicContext context, JSONObject vmInstance, String templateId) {
691         if (StringUtils.isNotBlank(templateId)) {
692             setAttribute(context, vmInstance::optString, TEMPLATE_ID);
693         }
694     }
695
696     private void tryProcessVnfcReference(JSONObject content, SvcLogicContext context, DBService dbservice)
697             throws SvcLogicException {
698         if (content.getString(ACTION).equals(CONFIGURE_PARAM)
699                 || content.getString(ACTION).equals(CONFIG_SCALE_OUT_PARAM)) {
700
701             dbservice.processVnfcReference(context, false);
702         }
703     }
704
705     private void resolveContext(SvcLogicContext context, JSONObject vnfcInstance) {
706         if (vnfcInstance.has(IPADDRESS_V4_OAM_VIP)) {
707             setAttribute(context, vnfcInstance::getString, IPADDRESS_V4_OAM_VIP);
708         }
709         if (vnfcInstance.has(GROUP_NOTATION_TYPE)) {
710             setAttribute(context, vnfcInstance::getString, GROUP_NOTATION_TYPE);
711         }
712         if (vnfcInstance.has(GROUP_NOTATION_VALUE)) {
713             setAttribute(context, vnfcInstance::getString, GROUP_NOTATION_VALUE);
714         }
715     }
716
717     private void cleanArtifactInstanceData(SvcLogicContext context) {
718         context.setAttribute(ARTIFACT_NAME, null);
719         context.setAttribute(FILE_CATEGORY, null);
720     }
721
722     private void cleanVnfcInstance(SvcLogicContext context) {
723
724         context.setAttribute(VNFC_INSTANCE, null);
725         context.setAttribute(VNFC_TYPE, null);
726         context.setAttribute(VNFC_FUNCTION_CODE, null);
727         context.setAttribute(IPADDRESS_V4_OAM_VIP, null);
728         context.setAttribute(GROUP_NOTATION_TYPE, null);
729         context.setAttribute(GROUP_NOTATION_VALUE, null);
730
731     }
732
733     private void processAndStoreCapabilitiesArtifact(DBService dbService, JSONObject documentInfo,
734             JSONObject capabilities, String capabilityArtifactName, String vnfType)
735             throws ArtifactHandlerInternalException {
736
737         log.info("Begin-->processAndStoreCapabilitiesArtifact ");
738
739         try {
740             JSONObject newCapabilitiesObject = new JSONObject();
741             newCapabilitiesObject.put("capabilities", capabilities);
742             SvcLogicContext context = new SvcLogicContext();
743             context.setAttribute(ARTIFACT_NAME, capabilityArtifactName);
744             context.setAttribute(FILE_CATEGORY, CAPABILITY);
745             context.setAttribute(ACTION, null);
746             context.setAttribute(VNFC_TYPE, null);
747             context.setAttribute(ARTIFACT_TYPE, null);
748             context.setAttribute(VNF_TYPE, vnfType);
749             context.setAttribute(ARTIFACT_CONTENTS, newCapabilitiesObject.toString());
750             dbService.processSdcReferences(context, dbService.isArtifactUpdateRequired(context, DB_SDC_REFERENCE));
751             int intVersion = 0;
752             String internalVersion = dbService.getInternalVersionNumber(context, context.getAttribute(ARTIFACT_NAME),
753                     null);
754             log.info("Internal Version number received from Database : " + internalVersion);
755             if (internalVersion != null) {
756                 intVersion = Integer.parseInt(internalVersion) + 1;
757             }
758             setAttribute(context, documentInfo::getString, SERVICE_UUID);
759             setAttribute(context, documentInfo::getString, DISTRIBUTION_ID);
760             setAttribute(context, documentInfo::getString, SERVICE_NAME);
761             setAttribute(context, documentInfo::getString, SERVICE_DESCRIPTION);
762             setAttribute(context, documentInfo::getString, RESOURCE_UUID);
763             setAttribute(context, documentInfo::getString, RESOURCE_INSTANCE_NAME);
764             setAttribute(context, documentInfo::getString, RESOURCE_VERSION);
765             setAttribute(context, documentInfo::getString, RESOURCE_TYPE);
766             setAttribute(context, documentInfo::getString, ARTIFACT_UUID);
767             setAttribute(context, documentInfo::getString, ARTIFACT_VERSION);
768             setAttribute(context, documentInfo::getString, ARTIFACT_DESRIPTION);
769             dbService.saveArtifacts(context, intVersion);
770         } catch (Exception e) {
771             log.error("Error saving capabilities artifact to DB", e);
772             throw new ArtifactHandlerInternalException("Error saving capabilities artifact to DB", e);
773         } finally {
774             log.info("End-->processAndStoreCapabilitiesArtifact ");
775         }
776     }
777
778     private void setAttribute(SvcLogicContext context, Function<String, String> value, String key) {
779         context.setAttribute(key, value.apply(key));
780     }
781
782     private void populateProtocolReference(DBService dbservice, JSONObject content)
783             throws ArtifactHandlerInternalException {
784         log.info("Begin-->populateProtocolReference");
785         try {
786             SvcLogicContext context = new SvcLogicContext();
787             JSONObject scope = content.getJSONObject("scope");
788             String vnfType = tryGetVnfType(scope);
789             String protocol = tryGetProtocol(content);
790             String action = tryGetAction(content);
791             String actionLevel = tryGetActionLevel(content);
792             String template = tryGetTemplate(content);
793
794             boolean isUpdateNeeded = dbservice.isProtocolReferenceUpdateRequired(context, vnfType, protocol, action,
795                     actionLevel, template);
796             if (isUpdateNeeded) {
797                 dbservice.updateProtocolReference(context, vnfType, protocol, action, actionLevel, template);
798             } else {
799                 dbservice.insertProtocolReference(context, vnfType, protocol, action, actionLevel, template);
800             }
801         } catch (Exception e) {
802             log.error("Error inserting record into protocolReference", e);
803             throw new ArtifactHandlerInternalException("Error inserting record into protocolReference", e);
804         } finally {
805             log.info("End-->populateProtocolReference");
806         }
807     }
808
809     private String tryGetVnfType(JSONObject scope) {
810         if (scope.has(VNF_TYPE) && !scope.isNull(VNF_TYPE)) {
811             return scope.getString(VNF_TYPE);
812         }
813         return null;
814     }
815
816     private String tryGetProtocol(JSONObject content) {
817         if (content.has(DEVICE_PROTOCOL)) {
818             return content.getString(DEVICE_PROTOCOL);
819         }
820         return null;
821     }
822
823     private String tryGetAction(JSONObject content) {
824         if (content.has(ACTION)) {
825             return content.getString(ACTION);
826         }
827         return null;
828     }
829
830     private String tryGetActionLevel(JSONObject content) {
831         if (content.has(ACTION_LEVEL)) {
832             return content.getString(ACTION_LEVEL);
833         }
834         return null;
835     }
836
837     private String tryGetTemplate(JSONObject content) {
838         if (content.has(TEMPLATE) && !content.isNull(TEMPLATE)) {
839             return content.getString(TEMPLATE);
840         }
841         return null;
842     }
843
844     protected ArtifactProcessorImpl getArtifactProcessorImpl() {
845         return new ArtifactProcessorImpl();
846     }
847 }