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