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