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