Healthcheck bug 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  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.artifact.handler.node;
25
26 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ACTION;
27 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ACTION_LEVEL;
28 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ACTION_LEVEL_VF_MODULE;
29 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ACTION_LEVEL_VM;
30 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ACTION_LEVEL_VNF;
31 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ACTION_LEVEL_VNFC;
32 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ARTIFACT_CONTENTS;
33 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ARTIFACT_DESRIPTION;
34 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ARTIFACT_NAME;
35 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ARTIFACT_NAME_CAPABILITY;
36 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ARTIFACT_NAME_REFERENCE;
37 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ARTIFACT_TYPE;
38 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ARTIFACT_UUID;
39 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.ARTIFACT_VERSION;
40 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.CAPABILITY;
41 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.DB_CONFIG_ACTION_DG;
42 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.DB_DEVICE_AUTHENTICATION;
43 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.DB_DEVICE_INTERFACE_PROTOCOL;
44 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE;
45 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.DB_SDC_REFERENCE;
46 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.DEVICE_PROTOCOL;
47 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.DISTRIBUTION_ID;
48 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.DOCUMENT_PARAMETERS;
49 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE;
50 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.FILE_CATEGORY;
51 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.GROUP_NOTATION_TYPE;
52 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.GROUP_NOTATION_VALUE;
53 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.IPADDRESS_V4_OAM_VIP;
54 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.PARAMETER_YANG;
55 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.PD;
56 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.PORT_NUMBER;
57 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.REFERENCE;
58 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.REQUEST_ID;
59 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.REQUEST_INFORMATION;
60 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.RESOURCE_INSTANCE_NAME;
61 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.RESOURCE_TYPE;
62 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.RESOURCE_UUID;
63 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.RESOURCE_VERSION;
64 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.SERVICE_DESCRIPTION;
65 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.SERVICE_NAME;
66 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.SERVICE_UUID;
67 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.TEMPLATE;
68 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.TEMPLATE_ID;
69 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.TOSCA_MODEL;
70 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.USER_NAME;
71 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.VM;
72 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.VM_INSTANCE;
73 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.VNFC;
74 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.VNFC_FUNCTION_CODE;
75 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.VNFC_FUNCTION_CODE_LIST;
76 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.VNFC_INSTANCE;
77 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.VNFC_TYPE;
78 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.VNF_TYPE;
79 import static org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants.URL;
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                 if (content.has(URL)) {
324                     setAttribute(context, content::getString, URL);
325                 }
326                 processArtifactList(content, dbservice, context);
327                 processConfigTypeActions(content, dbservice, context);
328                 dbservice.processDeviceAuthentication(context,
329                     dbservice.isArtifactUpdateRequired(context, DB_DEVICE_AUTHENTICATION));
330
331                 populateProtocolReference(dbservice, content);
332
333                 context.setAttribute(VNFC_TYPE, null);
334
335                 if (content.has(VM)
336                     && content.get(VM) instanceof JSONArray) {
337                     processVmList(content, context, dbservice);
338                 }
339             }
340             if (storeCapabilityArtifact) {
341                 capabilities.put("vnf", vnfActionList);
342                 capabilities.put("vf-module", vfModuleActionList);
343                 capabilities.put("vnfc", vnfcActionList);
344                 capabilities.put("vm", vmActionVnfcFunctionCodesList);
345                 processAndStoreCapabilitiesArtifact(dbservice, documentInfo, capabilities,
346                     capabilityArtifactName,
347                     vnfType);
348             }
349
350         } catch (Exception e) {
351
352             log.error("Error while storing reference data", e);
353             throw new ArtifactHandlerInternalException("Error while storing reference data", e);
354         }
355
356         return true;
357     }
358
359     public boolean isCapabilityArtifactNeeded(JSONObject scope, SvcLogicContext context) {
360         boolean storeCapabilityArtifact = true;
361         if (scope.has(VNFC_TYPE)
362             && !scope.isNull(VNFC_TYPE)) {
363             String vnfcTypeScope = scope.getString(VNFC_TYPE);
364             if (StringUtils.isNotBlank(vnfcTypeScope)) {
365                 setAttribute(context, scope::getString, VNFC_TYPE);
366                 storeCapabilityArtifact = false;
367                 log.info("No capability Artifact for this reference data as it is at VNFC level!!");
368             } else {
369                 context.setAttribute(VNFC_TYPE, null);
370             }
371         } else {
372             context.setAttribute(VNFC_TYPE, null);
373         }
374         return storeCapabilityArtifact;
375     }
376
377     public void processActionLists(JSONObject content, String actionLevel, JSONArray vnfcActionList,
378         JSONArray vfModuleActionList,
379         JSONArray vnfActionList, JSONArray vmActionVnfcFunctionCodesList) {
380         if (validateActionLevel(actionLevel, ACTION_LEVEL_VNFC)) {
381             vnfcActionList.put(content.getString(ACTION));
382         }
383         if (validateActionLevel(actionLevel, ACTION_LEVEL_VF_MODULE)) {
384             vfModuleActionList.put(content.getString(ACTION));
385         }
386         if (validateActionLevel(actionLevel, ACTION_LEVEL_VNF)) {
387             vnfActionList.put(content.getString(ACTION));
388         }
389         if (validateActionLevel(actionLevel, ACTION_LEVEL_VM)) {
390             if (content.has(VNFC_FUNCTION_CODE_LIST)
391                 && !content.isNull(VNFC_FUNCTION_CODE_LIST) && content.get(
392                 VNFC_FUNCTION_CODE_LIST) instanceof JSONArray) {
393                 log.info("Found vnfc-function-code-list!!");
394                 JSONArray vnfcList = content.getJSONArray(VNFC_FUNCTION_CODE_LIST);
395                 JSONObject obj = new JSONObject();
396                 obj.put(content.getString(ACTION), vnfcList);
397                 vmActionVnfcFunctionCodesList.put(obj);
398             } else {
399                 log.info("Not getting JSONArray for VNFC FUNCTION CODES");
400             }
401         }
402
403     }
404
405     private boolean validateActionLevel(String actionLevel, String actionLevelVnfc) {
406         return null != actionLevel && actionLevel.equalsIgnoreCase(actionLevelVnfc);
407     }
408
409     public void processArtifactList(JSONObject content, DBService dbservice, SvcLogicContext context)
410         throws ArtifactHandlerInternalException {
411
412
413         try {
414             if (content.has(ARTIFACT_LIST_PARAM) && content.get(ARTIFACT_LIST_PARAM) instanceof JSONArray) {
415                 JSONArray artifactLists = (JSONArray) content.get(ARTIFACT_LIST_PARAM);
416                 JSONArray templateIdList = null;
417                 if (content.has("template-id-list") && null != content.get("template-id-list")
418                         && content.get("template-id-list") instanceof JSONArray) {
419                         templateIdList = content.getJSONArray("template-id-list");
420                 }
421                 doProcessArtifactList(dbservice, context, artifactLists, templateIdList);
422             }
423         } catch (Exception e) {
424             log.error("An error occurred when processing artifact list", e);
425             throw new ArtifactHandlerInternalException(e);
426         }
427     }
428
429     private void doProcessArtifactList(DBService dbservice, SvcLogicContext context, JSONArray artifactLists,
430         JSONArray templateIdList)
431         throws SvcLogicException, SQLException, ConfigurationException, DBException {
432         boolean pdFile = false;
433         int modelInd = 0;
434
435         for (int i = 0; i < artifactLists.length(); i++) {
436             String suffix = null;
437             String model = null;
438             JSONObject artifact = (JSONObject) artifactLists.get(i);
439             log.info("artifact is " + artifact);
440
441             //Get Model details
442             if (null != templateIdList && i>0 && i%2==0) {
443                 modelInd++;
444             }
445
446             setAttribute(context, artifact::getString, ARTIFACT_NAME);
447             context.setAttribute(FILE_CATEGORY,
448                 artifact.getString(ARTIFACT_TYPE));
449
450             if (artifact.getString(ARTIFACT_NAME) != null
451                 && artifact.getString(ARTIFACT_NAME).toLowerCase().startsWith(PD)) {
452
453                 suffix = artifact.getString(ARTIFACT_NAME).substring(PD.length());
454                 pdFile = true;
455             }
456             log.info("Artifact-type = " + context.getAttribute(FILE_CATEGORY));
457             log.info("Artifact-name = " + context.getAttribute(ARTIFACT_NAME));
458
459             if (null != templateIdList  && modelInd < templateIdList.length()) {
460                 model = templateIdList.getString(modelInd);
461                 log.info("Model is ::: "+model+"  ,modelInd = "+modelInd);
462             }
463
464             if (StringUtils.isNotBlank(model)) {
465                 dbservice.processSdcReferences(context, dbservice.isArtifactUpdateRequired(context,
466                     DB_SDC_REFERENCE, model),model);
467             }
468             else {
469                 dbservice.processSdcReferences(context, dbservice.isArtifactUpdateRequired(context,
470                 DB_SDC_REFERENCE));
471             }
472
473             cleanArtifactInstanceData(context);
474             //Moving this into the for loop to account for mulitple artifact sets with pds
475             if (pdFile) {
476                 log.info("Sending information related to pdfile Artifact");
477                 tryUpdateContext(dbservice, context, pdFile, suffix, model);
478                 pdFile=false;//set to false afterprocessing yang and Tosca
479             }
480         }
481
482     }
483
484     private void tryUpdateContext(DBService dbservice, SvcLogicContext context, boolean pdFile,
485             String suffix, String model)
486         throws SvcLogicException, SQLException, ConfigurationException, DBException {
487         if (pdFile) {
488             context.setAttribute(ARTIFACT_NAME, "Tosca".concat(suffix));
489             context.setAttribute(FILE_CATEGORY, TOSCA_MODEL);
490             dbservice.processSdcReferences(context,
491                 dbservice.isArtifactUpdateRequired(context, DB_SDC_REFERENCE, model), model);
492             context.setAttribute(ARTIFACT_NAME, "Yang".concat(suffix));
493             context.setAttribute(FILE_CATEGORY, PARAMETER_YANG);
494             dbservice.processSdcReferences(context,
495                 dbservice.isArtifactUpdateRequired(context, DB_SDC_REFERENCE, model), model);
496         }
497     }
498
499     public void processConfigTypeActions(JSONObject content, DBService dbservice, SvcLogicContext context)
500         throws ArtifactHandlerInternalException {
501
502         try {
503             if (contentsActionEquals(content, CONFIGURE_PARAM)
504                 || contentsActionEquals(content, CONFIG_MODIFY_PARAM)
505                 || contentsActionEquals(content, CONFIG_SCALE_OUT_PARAM)) {
506
507                 if (content.has(DOWNLOAD_DG_REFERENCE) && content.getString(DOWNLOAD_DG_REFERENCE).length() > 0) {
508
509                     setAttribute(context, content::getString, DOWNLOAD_DG_REFERENCE);
510                     dbservice.processDownloadDgReference(context,
511                         dbservice.isArtifactUpdateRequired(context, DB_DOWNLOAD_DG_REFERENCE));
512                 }
513                 if (StringUtils.isBlank(context.getAttribute(DOWNLOAD_DG_REFERENCE))) {
514                     context.setAttribute(DOWNLOAD_DG_REFERENCE,
515                         dbservice.getDownLoadDGReference(context));
516                 }
517                 dbservice
518                     .processConfigActionDg(context, dbservice.isArtifactUpdateRequired(context, DB_CONFIG_ACTION_DG));
519
520                 tryProcessInterfaceProtocol(content, dbservice, context);
521             }
522         } catch (Exception e) {
523             log.error("An error occurred when processing config type actions", e);
524             throw new ArtifactHandlerInternalException(e);
525         }
526     }
527
528     private void tryProcessInterfaceProtocol(JSONObject content, DBService dbservice, SvcLogicContext context)
529         throws SvcLogicException, SQLException, ConfigurationException, DBException {
530
531         if (contentsActionEquals(content, CONFIGURE_PARAM) || contentsActionEquals(content, CONFIG_SCALE_OUT_PARAM)) {
532             boolean isUpdateRequired = dbservice.isArtifactUpdateRequired(context, DB_DEVICE_INTERFACE_PROTOCOL);
533             if (contentsActionEquals(content, CONFIGURE_PARAM)
534                 || (contentsActionEquals(content, CONFIG_SCALE_OUT_PARAM)
535                 && !isUpdateRequired)) {
536
537                 dbservice.processDeviceInterfaceProtocol(context, isUpdateRequired);
538             }
539         }
540     }
541
542     private boolean contentsActionEquals(JSONObject content, String action) {
543         return content.getString(ACTION).equals(action);
544     }
545
546     public void processVmList(JSONObject content, SvcLogicContext context, DBService dbservice)
547         throws SvcLogicException {
548         JSONArray vmList = (JSONArray) content.get(VM);
549         dbservice.cleanUpVnfcReferencesForVnf(context);
550         for (int i = 0; i < vmList.length(); i++) {
551             JSONObject vmInstance = (JSONObject) vmList.get(i);
552             setAttribute(context, s -> String.valueOf(vmInstance.getInt(s)), VM_INSTANCE);
553             log.info("VALUE = " + context.getAttribute(VM_INSTANCE));
554             String templateId = vmInstance.optString(TEMPLATE_ID);
555             trySetContext(context, vmInstance, templateId);
556             if (vmInstance.get(VNFC) instanceof JSONArray) {
557                 JSONArray vnfcInstanceList = (JSONArray) vmInstance.get(VNFC);
558                 for (int k = 0; k < vnfcInstanceList.length(); k++) {
559                     JSONObject vnfcInstance = (JSONObject) vnfcInstanceList.get(k);
560
561                     setAttribute(context, s -> String.valueOf(vnfcInstance.getInt(s)), VNFC_INSTANCE);
562                     setAttribute(context, vnfcInstance::getString, VNFC_TYPE);
563                     setAttribute(context, vnfcInstance::getString, VNFC_FUNCTION_CODE);
564                     resolveContext(context, vnfcInstance);
565                     tryProcessVnfcReference(content, context, dbservice);
566                     cleanVnfcInstance(context);
567                 }
568                 context.setAttribute(VM_INSTANCE, null);
569                 context.setAttribute(TEMPLATE_ID, null);
570             }
571         }
572     }
573
574     private void trySetContext(SvcLogicContext context, JSONObject vmInstance, String templateId) {
575         if (StringUtils.isNotBlank(templateId)) {
576             setAttribute(context, vmInstance::optString, TEMPLATE_ID);
577         }
578     }
579
580     private void tryProcessVnfcReference(JSONObject content, SvcLogicContext context, DBService dbservice)
581         throws SvcLogicException {
582         if (content.getString(ACTION).equals(CONFIGURE_PARAM)
583             || content.getString(ACTION).equals(CONFIG_SCALE_OUT_PARAM)) {
584
585             dbservice.processVnfcReference(context, false);
586         }
587     }
588
589     private void resolveContext(SvcLogicContext context, JSONObject vnfcInstance) {
590         if (vnfcInstance.has(IPADDRESS_V4_OAM_VIP)) {
591             setAttribute(context, vnfcInstance::getString, IPADDRESS_V4_OAM_VIP);
592         }
593         if (vnfcInstance.has(GROUP_NOTATION_TYPE)) {
594             setAttribute(context, vnfcInstance::getString, GROUP_NOTATION_TYPE);
595         }
596         if (vnfcInstance.has(GROUP_NOTATION_VALUE)) {
597             setAttribute(context, vnfcInstance::getString, GROUP_NOTATION_VALUE);
598         }
599     }
600
601     private void cleanArtifactInstanceData(SvcLogicContext context) {
602         context.setAttribute(ARTIFACT_NAME, null);
603         context.setAttribute(FILE_CATEGORY, null);
604     }
605
606     private void cleanVnfcInstance(SvcLogicContext context) {
607
608         context.setAttribute(VNFC_INSTANCE, null);
609         context.setAttribute(VNFC_TYPE, null);
610         context.setAttribute(VNFC_FUNCTION_CODE, null);
611         context.setAttribute(IPADDRESS_V4_OAM_VIP, null);
612         context.setAttribute(GROUP_NOTATION_TYPE, null);
613         context.setAttribute(GROUP_NOTATION_VALUE, null);
614
615     }
616
617     private void processAndStoreCapabilitiesArtifact(DBService dbService, JSONObject documentInfo,
618         JSONObject capabilities, String capabilityArtifactName, String vnfType)
619         throws ArtifactHandlerInternalException {
620
621         log.info("Begin-->processAndStoreCapabilitiesArtifact ");
622
623         try {
624             JSONObject newCapabilitiesObject = new JSONObject();
625             newCapabilitiesObject.put("capabilities", capabilities);
626             SvcLogicContext context = new SvcLogicContext();
627             context.setAttribute(ARTIFACT_NAME, capabilityArtifactName);
628             context.setAttribute(FILE_CATEGORY, CAPABILITY);
629             context.setAttribute(ACTION, null);
630             context.setAttribute(VNFC_TYPE, null);
631             context.setAttribute(ARTIFACT_TYPE, null);
632             context.setAttribute(VNF_TYPE, vnfType);
633             context.setAttribute(ARTIFACT_CONTENTS, newCapabilitiesObject.toString());
634             dbService.processSdcReferences(context, dbService.isArtifactUpdateRequired(context, DB_SDC_REFERENCE));
635             int intVersion = 0;
636             String internalVersion = dbService.getInternalVersionNumber(context,
637                 context.getAttribute(ARTIFACT_NAME), null);
638             log.info("Internal Version number received from Database : " + internalVersion);
639             if (internalVersion != null) {
640                 intVersion = Integer.parseInt(internalVersion) + 1;
641             }
642             setAttribute(context, documentInfo::getString, SERVICE_UUID);
643             setAttribute(context, documentInfo::getString, DISTRIBUTION_ID);
644             setAttribute(context, documentInfo::getString, SERVICE_NAME);
645             setAttribute(context, documentInfo::getString, SERVICE_DESCRIPTION);
646             setAttribute(context, documentInfo::getString, RESOURCE_UUID);
647             setAttribute(context, documentInfo::getString, RESOURCE_INSTANCE_NAME);
648             setAttribute(context, documentInfo::getString, RESOURCE_VERSION);
649             setAttribute(context, documentInfo::getString, RESOURCE_TYPE);
650             setAttribute(context, documentInfo::getString, ARTIFACT_UUID);
651             setAttribute(context, documentInfo::getString, ARTIFACT_VERSION);
652             setAttribute(context, documentInfo::getString, ARTIFACT_DESRIPTION);
653             dbService.saveArtifacts(context, intVersion);
654         } catch (Exception e) {
655             log.error("Error saving capabilities artifact to DB", e);
656             throw new ArtifactHandlerInternalException("Error saving capabilities artifact to DB", e);
657         } finally {
658             log.info("End-->processAndStoreCapabilitiesArtifact ");
659         }
660     }
661
662     private void setAttribute(SvcLogicContext context, Function<String, String> value, String key) {
663         context.setAttribute(key, value.apply(key));
664     }
665
666     private void populateProtocolReference(DBService dbservice, JSONObject content)
667         throws ArtifactHandlerInternalException {
668         log.info("Begin-->populateProtocolReference ");
669         try {
670             SvcLogicContext context = new SvcLogicContext();
671             JSONObject scope = content.getJSONObject("scope");
672             String vnfType = tryGetVnfType(scope);
673             String protocol = tryGetProtocol(content);
674             String action = tryGetAction(content);
675             String actionLevel = tryGetActionLevel(content);
676             String template = tryGetTemplate(content);
677
678             boolean isUpdateNeeded = dbservice
679                 .isProtocolReferenceUpdateRequired(context, vnfType, protocol, action, actionLevel, template);
680             if (isUpdateNeeded) {
681                 dbservice.updateProtocolReference(context, vnfType, protocol, action, actionLevel, template);
682             } else {
683                 dbservice.insertProtocolReference(context, vnfType, protocol, action, actionLevel, template);
684             }
685         } catch (Exception e) {
686             log.error("Error inserting record into protocolReference", e);
687             throw new ArtifactHandlerInternalException("Error inserting record into protocolReference", e);
688         } finally {
689             log.info("End-->populateProtocolReference ");
690         }
691     }
692
693     private String tryGetVnfType(JSONObject scope) {
694         if (scope.has(VNF_TYPE) && !scope.isNull(VNF_TYPE)) {
695             return scope.getString(VNF_TYPE);
696         }
697         return null;
698     }
699
700     private String tryGetProtocol(JSONObject content) {
701         if (content.has(DEVICE_PROTOCOL)) {
702             return content.getString(DEVICE_PROTOCOL);
703         }
704         return null;
705     }
706
707     private String tryGetAction(JSONObject content) {
708         if (content.has(ACTION)) {
709             return content.getString(ACTION);
710         }
711         return null;
712     }
713
714     private String tryGetActionLevel(JSONObject content) {
715         if (content.has(ACTION_LEVEL)) {
716             return content.getString(ACTION_LEVEL);
717         }
718         return null;
719     }
720
721     private String tryGetTemplate(JSONObject content) {
722         if (content.has(TEMPLATE) && !content.isNull(TEMPLATE)) {
723             return content.getString(TEMPLATE);
724         }
725         return null;
726     }
727 }