c0580b43beb830b08db6c834fb427aefe0471d13
[appc.git] / appc-config / appc-data-services / provider / src / main / java / org / onap / appc / data / services / node / ConfigResourceNode.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 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 package org.onap.appc.data.services.node;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.fasterxml.jackson.databind.JsonNode;
29 import com.fasterxml.jackson.databind.ObjectMapper;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.List;
33 import java.util.Map;
34 import org.apache.commons.lang3.StringUtils;
35 import org.onap.appc.data.services.AppcDataServiceConstant;
36 import org.onap.appc.data.services.db.DGGeneralDBService;
37 import org.onap.appc.data.services.utils.EscapeUtils;
38 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
39 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
40 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
41 import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
42
43 public class ConfigResourceNode implements SvcLogicJavaPlugin {
44
45     static final String DEVICE_CONF_PREFIX = "configfilereference-deviceconfig";
46     static final String DEVICE_CONF_FILE_TYPE = "device_configuration";
47
48     static final String SUCCESS_PREFIX = "configfilereference-success";
49     static final String SUCCESS_FILE_TYPE = "configuration_success";
50
51     static final String FAILURE_PREFIX = "configfilereference-failure";
52     static final String FAILURE_FILE_TYPE = "configuration_error";
53
54     static final String LOG_PREFIX = "configfilereference-log";
55     static final String LOG_FILE_TYPE = "log";
56
57     static final String DEVICE_PROTOCOL_PREFIX = "tmp.deviceinterfaceprotocol";
58     static final String CONF_ACTION_PREFIX = "tmp.configureactiondg";
59
60     static final String CONFIG_FILES_PREFIX = "tmp.configFiles";
61     static final String MAX_CONF_FILE_PREFIX = "tmp.configfilesmax";
62     static final String UPLOAD_CONFIG_PREFIX = "tmp.uploadConfig";
63     static final String UPLOAD_CONFIG_INFO_PREFIX = "tmp.uploadConfigInfo";
64
65     static final String PREPARE_RELATIONSHIP_PARAM = "tmp.preparerel";
66     static final String CONFIG_FILE_ID_PARAM = "tmp.configfilesmax.configfileid";
67     static final String FILE_CATEGORY_PARAM = "file-category";
68     static final String UPLOAD_CONFIG_ID_PARAM = "tmp.uploadConfigInfo.UPLOAD-CONFIG-ID";
69
70     static final String SDC_IND = "N";
71     static final String TMP_CONVERTCONFIG_ESC_DATA = "tmp.convertconfig.escapeData";
72     static final String CONFIG_PARAMS = "configuration-params";
73     static final String TMP_MERGE_MERGED_DATA = "tmp.merge.mergedData";
74     static final String DATA_SOURCE = "data-source";
75     static final String FILE_CONTENT = "file-content";
76     static final String CAPABILITIES = "capabilities";
77     static final String NOT_SUPPORTED = "Not-Supported";
78     static final String UNABLE_TO_READ_STR = "Unable to Read ";
79     static final String UNABLE_TO_SAVE_RELATIONSHIP_STR = "Unable to save prepare_relationship";
80
81
82     static final String SITE_LOCATION_PARAM = "site-location";
83
84     private static final EELFLogger log = EELFManager.getInstance().getLogger(ConfigResourceNode.class);
85     private final DGGeneralDBService db;
86
87     /**
88      * Constructor which provide default DB service
89      */
90     public ConfigResourceNode() {
91         db = DGGeneralDBService.initialise();
92     }
93
94     /**
95      * Constructor which allow to provide custom DB service, prefer to use no-arg constructor
96      */
97     public ConfigResourceNode(DGGeneralDBService dbService) {
98         db = dbService;
99     }
100
101     public void getConfigFileReference(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
102
103         log.info("Received getConfigFiles call with params : " + inParams);
104         String responsePrefix = inParams.get(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX);
105
106         try {
107             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
108             QueryStatus status = db
109                 .getConfigFileReferenceByFileTypeNVnfType(ctx, DEVICE_CONF_PREFIX, DEVICE_CONF_FILE_TYPE);
110
111             if (status == QueryStatus.NOT_FOUND || status == QueryStatus.FAILURE) {
112                 throw new QueryException("Unable to Read ConfigFileReference:device-configuration");
113             }
114
115             status = db.getConfigFileReferenceByFileTypeNVnfType(ctx, SUCCESS_PREFIX, SUCCESS_FILE_TYPE);
116
117             if (status == QueryStatus.NOT_FOUND || status == QueryStatus.FAILURE) {
118                 throw new QueryException("Unable to Read ConfigFileReference:configuration_success");
119             }
120
121             status = db.getConfigFileReferenceByFileTypeNVnfType(ctx, FAILURE_PREFIX, FAILURE_FILE_TYPE);
122
123             if (status == QueryStatus.NOT_FOUND || status == QueryStatus.FAILURE) {
124                 throw new QueryException("Unable to Read ConfigFileReference:configuration_error");
125             }
126
127             status = db.getConfigFileReferenceByFileTypeNVnfType(ctx, LOG_PREFIX, LOG_FILE_TYPE);
128
129             if (status == QueryStatus.NOT_FOUND || status == QueryStatus.FAILURE) {
130                 throw new QueryException("Unable to Read ConfigFileReference:configuration_log");
131             }
132
133             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
134                 AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS);
135             log.info("GetConfigFileReference Successful ");
136         } catch (Exception e) {
137             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
138                 AppcDataServiceConstant.OUTPUT_STATUS_FAILURE);
139             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
140             log.error("Failed in GetConfigFileReference", e);
141             throw new SvcLogicException(e.getMessage());
142         }
143     }
144
145     public void getCommonConfigInfo(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
146
147         log.info("Received getDeviceInfo call with params : " + inParams);
148         String responsePrefix = inParams.get(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX);
149
150         try {
151             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
152             QueryStatus status = db.getDeviceProtocolByVnfType(ctx, DEVICE_PROTOCOL_PREFIX);
153
154             if (status == QueryStatus.NOT_FOUND || status == QueryStatus.FAILURE) {
155                 throw new QueryException("Unable to Read device_interface_protocol");
156             }
157
158             status = db.getConfigureActionDGByVnfTypeNAction(ctx, CONF_ACTION_PREFIX);
159             if (status == QueryStatus.FAILURE) {
160                 throw new QueryException("Unable to Read configure_action_dg");
161             }
162
163             if (status == QueryStatus.NOT_FOUND) {
164                 status = db.getConfigureActionDGByVnfType(ctx, CONF_ACTION_PREFIX);
165
166                 if (status == QueryStatus.NOT_FOUND || status == QueryStatus.FAILURE) {
167                     throw new QueryException("Unable to Read configure_action_dg");
168                 }
169             }
170
171             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
172                 AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS);
173             log.info("getCommonConfigInfo Successful ");
174         } catch (Exception e) {
175             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
176                 AppcDataServiceConstant.OUTPUT_STATUS_FAILURE);
177             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
178             log.error("Failed in getCommonConfigInfo", e);
179             throw new SvcLogicException(e.getMessage());
180         }
181     }
182
183     /**
184      * FileCategory can be config_template, parameter_definitions, parameter_yang
185      */
186     public void getTemplate(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
187
188         log.info("Received getTemplate call with params : " + inParams);
189
190         String responsePrefix = inParams.get(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX);
191         String fileCategory = inParams.get(AppcDataServiceConstant.INPUT_PARAM_FILE_CATEGORY);
192         String templateName = ctx.getAttribute("template-name");
193         QueryStatus status;
194         String responsePrefix1 = "";
195
196         try {
197
198             responsePrefix1 = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
199             log.info("RESPONSEPREFIX : " + responsePrefix);
200             log.info("RESPONSEPREFIX1 : " + responsePrefix1);
201
202             if (StringUtils.isBlank(templateName)) {
203                 status = db.getTemplate(ctx, responsePrefix, fileCategory);
204                 if (status == QueryStatus.FAILURE) {
205                     throw new QueryException(UNABLE_TO_READ_STR + fileCategory);
206                 }
207
208                 if (status == QueryStatus.NOT_FOUND) {
209
210                     status = db.getTemplateByVnfTypeNAction(ctx, responsePrefix, fileCategory);
211
212                     if (status == QueryStatus.FAILURE) {
213                         throw new QueryException(UNABLE_TO_READ_STR + fileCategory);
214                     }
215
216                     if (status == QueryStatus.NOT_FOUND) {
217                         throw new QueryException(UNABLE_TO_READ_STR + fileCategory);
218                     }
219                 }
220             } else {
221
222                 status = db.getTemplateByTemplateName(ctx, responsePrefix, templateName);
223
224                 if (status == QueryStatus.NOT_FOUND || status == QueryStatus.FAILURE) {
225                     throw new QueryException(UNABLE_TO_READ_STR + fileCategory + " template");
226                 }
227             }
228
229             ctx.setAttribute(responsePrefix1 + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
230                 AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS);
231             log.info("GetTemplate Successful ");
232         } catch (Exception e) {
233             ctx.setAttribute(responsePrefix1 + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
234                 AppcDataServiceConstant.OUTPUT_STATUS_FAILURE);
235             ctx.setAttribute(responsePrefix1 + AppcDataServiceConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
236             log.error("Failed in getTemplate", e);
237
238             throw new SvcLogicException(e.getMessage());
239         }
240     }
241
242     void saveConfigFiles(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
243
244         log.info("Received saveConfigFiles call with params : " + inParams);
245
246         String responsePrefix = inParams.get(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX);
247
248         try {
249
250             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
251             QueryStatus status = db.saveConfigFiles(ctx, CONFIG_FILES_PREFIX);
252
253             if (status == QueryStatus.FAILURE) {
254                 throw new QueryException("Unable to Save " + ctx.getAttribute(FILE_CATEGORY_PARAM) + " in configfiles");
255             }
256
257             status = db.getMaxConfigFileId(ctx, MAX_CONF_FILE_PREFIX, ctx.getAttribute(FILE_CATEGORY_PARAM));
258
259             if (status == QueryStatus.NOT_FOUND || status == QueryStatus.FAILURE) {
260                 throw new QueryException(
261                     "Unable to get " + ctx.getAttribute(FILE_CATEGORY_PARAM) + " from configfiles");
262             }
263
264             status = db.savePrepareRelationship(ctx, PREPARE_RELATIONSHIP_PARAM,
265                 ctx.getAttribute(CONFIG_FILE_ID_PARAM), SDC_IND);
266             if (status == QueryStatus.FAILURE) {
267                 throw new QueryException(UNABLE_TO_SAVE_RELATIONSHIP_STR);
268             }
269
270             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
271                 AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS);
272             log.info("saveConfigFiles Successful ");
273         } catch (Exception e) {
274             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
275                 AppcDataServiceConstant.OUTPUT_STATUS_FAILURE);
276             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
277             log.error("Failed in saveConfigFiles", e);
278
279             throw new SvcLogicException(e.getMessage());
280         }
281     }
282
283     public void updateUploadConfig(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
284
285         log.info("Received updateUploadConfig call with params : " + inParams);
286
287         String responsePrefix = inParams.get(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX);
288
289         try {
290
291             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
292
293             ctx.setAttribute("tmp.escaped.devicerunningconfig",
294                 EscapeUtils.escapeSql(ctx.getAttribute("device-running-config")));
295
296             QueryStatus status = db.saveUploadConfig(ctx, UPLOAD_CONFIG_PREFIX);
297
298             if (status == QueryStatus.FAILURE) {
299                 throw new QueryException("Unable to Save configuration in upload_config");
300             }
301
302             status = db.getUploadConfigInfo(ctx, UPLOAD_CONFIG_INFO_PREFIX);
303
304             if (status == QueryStatus.NOT_FOUND || status == QueryStatus.FAILURE) {
305                 throw new QueryException("Unable to get record from upload_config");
306             }
307
308             status = db.updateUploadConfig(ctx, UPLOAD_CONFIG_PREFIX,
309                     Integer.parseInt(ctx.getAttribute(UPLOAD_CONFIG_ID_PARAM)));
310             if (status == QueryStatus.FAILURE)
311                 throw new QueryException("Unable to upload upload_config");
312
313             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
314                 AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS);
315             log.info("updateUploadConfig Successful ");
316         } catch (Exception e) {
317             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
318                 AppcDataServiceConstant.OUTPUT_STATUS_FAILURE);
319             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
320             log.error("Failed in updateUploadConfig", e);
321
322             throw new SvcLogicException(e.getMessage());
323         }
324     }
325
326     public void savePrepareRelationship(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
327
328         log.info("Received savePrepareRelationship call with params : " + inParams);
329
330         String responsePrefix = inParams.get(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX);
331         String sdcArtifactInd = inParams.get(AppcDataServiceConstant.INPUT_PARAM_SDC_ARTIFACT_IND);
332         String fileId = inParams.get(AppcDataServiceConstant.INPUT_PARAM_FILE_ID);
333         try {
334
335             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
336
337             QueryStatus status = db.savePrepareRelationship(ctx, PREPARE_RELATIONSHIP_PARAM, fileId, sdcArtifactInd);
338             if (status == QueryStatus.FAILURE) {
339                 throw new QueryException(UNABLE_TO_SAVE_RELATIONSHIP_STR);
340             }
341
342             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
343                 AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS);
344             log.info("savePrepareRelationship Successful ");
345         } catch (Exception e) {
346             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
347                 AppcDataServiceConstant.OUTPUT_STATUS_FAILURE);
348             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
349             log.error("Failed in saveConfigFiles", e);
350
351             throw new SvcLogicException(e.getMessage());
352         }
353     }
354
355     public void saveConfigBlock(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
356
357         log.info("Received saveConfigBlock call with params : " + inParams);
358
359         String responsePrefix = inParams.get(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX);
360
361         try {
362             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
363             ctx.setAttribute(TMP_CONVERTCONFIG_ESC_DATA, EscapeUtils.escapeSql(ctx.getAttribute("configuration")));
364
365             if (StringUtils.isBlank(ctx.getAttribute(CONFIG_PARAMS))) {
366                 saveDeviceConfiguration(inParams, ctx, "Request", ctx.getAttribute(TMP_CONVERTCONFIG_ESC_DATA),
367                     ctx.getAttribute("configuration"));
368             } else {
369
370                 saveConfigurationBlock(inParams, ctx);
371
372                 ctx.setAttribute(TMP_CONVERTCONFIG_ESC_DATA,
373                     EscapeUtils.escapeSql(ctx.getAttribute(TMP_MERGE_MERGED_DATA)));
374                 saveDeviceConfiguration(inParams, ctx, "Configurator", ctx.getAttribute(TMP_CONVERTCONFIG_ESC_DATA),
375                     ctx.getAttribute(TMP_MERGE_MERGED_DATA));
376
377                 saveConfigurationData(inParams, ctx);
378             }
379
380             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
381                 AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS);
382             log.info("saveConfigBlock Successful ");
383         } catch (Exception e) {
384             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
385                 AppcDataServiceConstant.OUTPUT_STATUS_FAILURE);
386             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
387             log.error("Failed in saveConfigBlock", e);
388
389             throw new SvcLogicException(e.getMessage());
390         }
391     }
392
393     public void saveTemplateConfig(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
394
395         log.info("Received saveTemplateConfig call with params : " + inParams);
396
397         String responsePrefix = inParams.get(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX);
398
399         try {
400             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
401
402             if (StringUtils.isBlank(ctx.getAttribute(CONFIG_PARAMS))) {
403
404                 ctx.setAttribute(TMP_CONVERTCONFIG_ESC_DATA,
405                     EscapeUtils.escapeSql(ctx.getAttribute("config-template.file-content")));
406                 saveDeviceConfiguration(inParams, ctx, "Template", ctx.getAttribute(TMP_CONVERTCONFIG_ESC_DATA),
407                     ctx.getAttribute("config-template.file-content"));
408
409             } else {
410                 saveConfigurationData(inParams, ctx);
411
412                 ctx.setAttribute(TMP_CONVERTCONFIG_ESC_DATA,
413                     EscapeUtils.escapeSql(ctx.getAttribute(TMP_MERGE_MERGED_DATA)));
414                 saveDeviceConfiguration(inParams, ctx, "Configurator", ctx.getAttribute(TMP_CONVERTCONFIG_ESC_DATA),
415                     ctx.getAttribute(TMP_MERGE_MERGED_DATA));
416
417             }
418
419             QueryStatus status = db.savePrepareRelationship(ctx, PREPARE_RELATIONSHIP_PARAM,
420                 ctx.getAttribute("config-template.config-file-id"), "Y");
421             if (status == QueryStatus.FAILURE) {
422                 throw new QueryException(UNABLE_TO_SAVE_RELATIONSHIP_STR);
423             }
424
425             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
426                 AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS);
427             log.info("saveTemplateConfig Successful ");
428         } catch (Exception e) {
429             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
430                 AppcDataServiceConstant.OUTPUT_STATUS_FAILURE);
431             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
432             log.error("Failed in saveTemplateConfig", e);
433
434             throw new SvcLogicException(e.getMessage());
435         }
436     }
437
438     public void saveStyleSheetConfig(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
439
440         log.info("Received saveStyleSheet call with params : " + inParams);
441
442         String responsePrefix = inParams.get(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX);
443
444         try {
445
446             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
447             ctx.setAttribute(TMP_CONVERTCONFIG_ESC_DATA,
448                 EscapeUtils.escapeSql(ctx.getAttribute(TMP_MERGE_MERGED_DATA)));
449             saveDeviceConfiguration(inParams, ctx, "StyleSheet", ctx.getAttribute(TMP_CONVERTCONFIG_ESC_DATA),
450                 ctx.getAttribute(TMP_MERGE_MERGED_DATA));
451
452             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
453                 AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS);
454             log.info("saveStyleSheet Successful ");
455         } catch (Exception e) {
456             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
457                 AppcDataServiceConstant.OUTPUT_STATUS_FAILURE);
458             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
459             log.error("Failed in saveStyleSheet", e);
460
461             throw new SvcLogicException(e.getMessage());
462         }
463     }
464
465     public void getSmmChainKeyFiles(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
466
467         log.info("Received saveStyleSheet call with params : " + inParams);
468
469         String responsePrefix = inParams.get(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX);
470         String siteLocation = ctx.getAttribute(SITE_LOCATION_PARAM);
471
472         QueryStatus status;
473
474         try {
475
476             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
477
478             status = db.getTemplateByArtifactType(ctx, "smm", "smm", siteLocation);
479
480             if (status == QueryStatus.FAILURE) {
481                 throw new QueryException("Unable to Read smm file");
482             }
483
484             status = db.getTemplateByArtifactType(ctx, "intermediate-ca-chain", "intermediate_ca_chain", siteLocation);
485
486             if (status == QueryStatus.FAILURE) {
487                 throw new QueryException("Unable to Read intermediate_ca_chain file");
488             }
489
490             status = db.getTemplateByArtifactType(ctx, "server-certificate-and-key", "server_certificate_and_key",
491                 siteLocation);
492
493             if (status == QueryStatus.FAILURE) {
494                 throw new QueryException("Unable to Read server_certificate_and_key file");
495             }
496
497             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
498                 AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS);
499             log.info("saveStyleSheet Successful ");
500         } catch (Exception e) {
501             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
502                 AppcDataServiceConstant.OUTPUT_STATUS_FAILURE);
503             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
504             log.error("Failed in saveStyleSheet", e);
505
506             throw new SvcLogicException(e.getMessage());
507         }
508     }
509
510     public void saveDeviceConfiguration(Map<String, String> inParams, SvcLogicContext ctx, String dataSource,
511                                         String fileContent, String deviceConfig) throws SvcLogicException {
512         ctx.setAttribute(DATA_SOURCE, dataSource);
513         ctx.setAttribute(FILE_CONTENT, fileContent);
514         ctx.setAttribute(FILE_CATEGORY_PARAM, "device_configuration");
515         ctx.setAttribute("deviceconfig-file-content", deviceConfig);
516
517         saveConfigFiles(inParams, ctx);
518     }
519
520     private void saveConfigurationBlock(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
521         ctx.setAttribute(DATA_SOURCE, "Request");
522         ctx.setAttribute(FILE_CONTENT, ctx.getAttribute(TMP_CONVERTCONFIG_ESC_DATA));
523         ctx.setAttribute(FILE_CATEGORY_PARAM, "configuration_block");
524         saveConfigFiles(inParams, ctx);
525     }
526
527     private void saveConfigurationData(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
528         ctx.setAttribute(DATA_SOURCE, ctx.getAttribute("originator-id"));
529         ctx.setAttribute(FILE_CONTENT, ctx.getAttribute(CONFIG_PARAMS));
530         ctx.setAttribute(FILE_CATEGORY_PARAM, "config_data");
531         saveConfigFiles(inParams, ctx);
532     }
533
534     public void getConfigFilesByVnfVmNCategory(Map<String, String> inParams, SvcLogicContext ctx)
535         throws SvcLogicException {
536
537         log.info("Received getConfigFilesByVnfVmNCategory call with params : " + inParams);
538
539         String responsePrefix = inParams.get(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX);
540         String fileCategory = inParams.get(AppcDataServiceConstant.INPUT_PARAM_FILE_CATEGORY);
541         String vnfId = inParams.get(AppcDataServiceConstant.INPUT_PARAM_VNF_ID);
542         String vmName = inParams.get(AppcDataServiceConstant.INPUT_PARAM_VM_NAME);
543         try {
544             QueryStatus status = db.getConfigFilesByVnfVmNCategory(ctx, responsePrefix, fileCategory, vnfId, vmName);
545
546             if (status == QueryStatus.NOT_FOUND || status == QueryStatus.FAILURE) {
547                 throw new QueryException("Unable to get " + ctx.getAttribute("fileCategory") + " from configfiles");
548             }
549
550             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
551             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
552                 AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS);
553             log.info("getConfigFilesByVnfVmNCategory Successful "
554                 + ctx.getAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS));
555         } catch (Exception e) {
556             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
557                 AppcDataServiceConstant.OUTPUT_STATUS_FAILURE);
558             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
559             log.error("Failed in getConfigFilesByVnfVmNCategory", e);
560
561             throw new SvcLogicException(e.getMessage());
562         }
563     }
564
565     public void getDownloadConfigTemplateByVnf(Map<String, String> inParams, SvcLogicContext ctx)
566         throws SvcLogicException {
567
568         log.info("Received getDownloadConfigTemplateByVnfNProtocol call with params : " + inParams);
569
570         String responsePrefix = inParams.get(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX);
571         try {
572             QueryStatus status = db.getDownloadConfigTemplateByVnf(ctx, responsePrefix);
573
574             if (status == QueryStatus.NOT_FOUND || status == QueryStatus.FAILURE) {
575                 throw new QueryException("Unable to get download config template.");
576             }
577
578             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
579             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
580                 AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS);
581             log.info("getDownloadConfigTemplateByVnf Successful "
582                 + ctx.getAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS));
583         } catch (Exception e) {
584             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
585                 AppcDataServiceConstant.OUTPUT_STATUS_FAILURE);
586             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
587             log.error("Failed in getDownloadConfigTemplateByVnf", e);
588
589             throw new SvcLogicException(e.getMessage());
590         }
591     }
592
593     public void saveConfigTransactionLog(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
594
595         String responsePrefix = inParams.get(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX);
596
597         String messageType = inParams.get(AppcDataServiceConstant.INPUT_PARAM_MESSAGE_TYPE);
598         String message = inParams.get(AppcDataServiceConstant.INPUT_PARAM_MESSAGE);
599
600         try {
601
602             SvcLogicContext logctx = new SvcLogicContext();
603             String escapedMessage = EscapeUtils.escapeSql(message);
604
605             logctx.setAttribute("request-id", ctx.getAttribute("request-id"));
606             logctx.setAttribute("log-message-type", messageType);
607             logctx.setAttribute("log-message", escapedMessage);
608
609             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
610             QueryStatus status = db.saveConfigTransactionLog(logctx, responsePrefix);
611
612             logctx.setAttribute("log-message", null);
613
614             if (status == QueryStatus.FAILURE) {
615                 throw new QueryException("Unable to insert into config_transaction_log");
616             }
617
618         } catch (Exception e) {
619             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
620                 AppcDataServiceConstant.OUTPUT_STATUS_FAILURE);
621             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
622             log.error("Failed in saveConfigTransactionLog", e);
623             throw new SvcLogicException(e.getMessage());
624         }
625     }
626
627     public void getVnfcReference(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
628
629         log.info("Received getVnfcReference call with params : " + inParams);
630
631         String responsePrefix = inParams.get(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX);
632         QueryStatus status;
633
634         try {
635             if (!StringUtils.isBlank(ctx.getAttribute("vnfc-type"))) {
636                 status = db.getVnfcReferenceByVnfcTypeNAction(ctx, responsePrefix);
637
638                 if (status == QueryStatus.FAILURE) {
639                     throw new QueryException("Unable to Read vnfc-reference");
640                 }
641             }
642             status = db.getVnfcReferenceByVnfTypeNAction(ctx, responsePrefix);
643
644             if (status == QueryStatus.NOT_FOUND || status == QueryStatus.FAILURE) {
645                 throw new QueryException("Unable to Read vnfc reference");
646             }
647
648             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
649             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
650                 AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS);
651             log.info("getVnfcReference Successful ");
652         } catch (Exception e) {
653             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
654                 AppcDataServiceConstant.OUTPUT_STATUS_FAILURE);
655             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
656             log.error("Failed in getVnfcReference", e);
657
658             throw new SvcLogicException(e.getMessage());
659         }
660     }
661
662     public void getCapability(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
663         log.info("Received getCapability call with params : " + inParams);
664         String responsePrefix = inParams.get(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX);
665         responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
666         String caplevel = inParams.get("caplevel");
667         String findCapability = inParams.get("checkCapability");
668         String vServerId = inParams.get("vServerId");
669         if (!checkIfCapabilityCheckNeeded(caplevel, findCapability)) {
670             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
671                 AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS);
672             log.info("getCapability Successful - No need for capability check for this action");
673             return;
674         }
675         try {
676             String cap = db.getCapability(ctx, inParams.get("vnf-type"));
677             log.info("getCapability::returned from DB::+cap");
678             if (StringUtils.isBlank(cap)) {
679                 ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
680                     AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS);
681                 log.info("getCapability Successful - No capability blocks found");
682                 return;
683             }
684             ObjectMapper mapper = new ObjectMapper();
685             JsonNode caps = mapper.readTree(cap);
686             log.info("From DB =   " + caps);
687             JsonNode capabilities = caps.get(CAPABILITIES);
688             log.info("capabilities =   " + capabilities);
689             if (caplevel != null && !caplevel.isEmpty()) {
690                 JsonNode subCapabilities = capabilities.get(caplevel);
691                 log.info("subCapabilities =  " + caplevel + " : " + subCapabilities);
692                 if (caplevel.equalsIgnoreCase(AppcDataServiceConstant.CAPABILITY_VM_LEVEL)
693                     && (null == subCapabilities || subCapabilities.isNull() || subCapabilities.size() == 0)) {
694                     ctx.setAttribute(CAPABILITIES, "None");
695                     ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
696                         AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS);
697                     log.info("getCapability Successful ");
698                     return;
699                 }
700                 if (findCapability != null && !findCapability.isEmpty()) {
701                     if (subCapabilities != null && subCapabilities.toString().contains(findCapability)) {
702                         if (caplevel.equalsIgnoreCase(AppcDataServiceConstant.CAPABILITY_VM_LEVEL)) {
703                             processCapabilitiesForVMLevel(vServerId, ctx, findCapability, subCapabilities);
704                         } else {
705                             ctx.setAttribute(CAPABILITIES, "Supported");
706                         }
707                     } else {
708                         ctx.setAttribute(CAPABILITIES, NOT_SUPPORTED);
709                     }
710                 } else {
711                     ctx.setAttribute(responsePrefix + "capabilities." + caplevel, subCapabilities.toString());
712                 }
713
714             } else {
715                 ctx.setAttribute(responsePrefix + CAPABILITIES, capabilities.toString());
716             }
717             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
718                 AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS);
719             log.info("getCapability Successful ");
720         } catch (Exception e) {
721             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
722                 AppcDataServiceConstant.OUTPUT_STATUS_FAILURE);
723             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
724             log.error("Failed in getCapability", e);
725
726             throw new SvcLogicException(e.getMessage());
727         }
728     }
729
730     public void processCapabilitiesForVMLevel(String vServerId, SvcLogicContext ctx, String findCapability,
731         JsonNode subCapabilities) {
732         log.info("processCapabilitiesForVMLevel():::subCapabilities::" + subCapabilities.toString() + ",vServerId::"
733             + vServerId);
734         if (subCapabilities.size() == 0) {
735             ctx.setAttribute(CAPABILITIES, "None");
736             log.info("processCapabilitiesForVMLevel :: No VM block found!!");
737             return;
738         }
739         JsonNode vmCaps = null;
740         for (JsonNode cap : subCapabilities) {
741             if (null != cap && null != cap.get(findCapability)
742                 && StringUtils.isNotBlank(cap.get(findCapability).toString())) {
743                 vmCaps = cap.get(findCapability);
744                 log.info("processCapabilitiesForVMLevel()::vmCaps found" + vmCaps.toString());
745                 break;
746             }
747         }
748
749         if (null == vmCaps || vmCaps.isNull() || vmCaps.size() == 0) {
750             ctx.setAttribute(CAPABILITIES, NOT_SUPPORTED);
751             log.info("processCapabilitiesForVMLevel :: Found non-empty VM block but Not desired capability!!");
752             return;
753         }
754
755         String vnfcFunctionCode = getVnfcFunctionCodeForVserver(ctx, vServerId);
756         if (StringUtils.isBlank(vnfcFunctionCode)) {
757             log.info("processCapabilitiesForVMLevel() :: vnfcFunctionCode is not present in context!!!");
758             ctx.setAttribute(CAPABILITIES, NOT_SUPPORTED);
759             return;
760         }
761
762         if (vmCaps.toString().contains(vnfcFunctionCode)) {
763             ctx.setAttribute(CAPABILITIES, "Supported");
764         } else {
765             ctx.setAttribute(CAPABILITIES, NOT_SUPPORTED);
766         }
767         log.info("End processCapabilitiesForVMLevel():capabilities is ::" + ctx.getAttribute(CAPABILITIES));
768     }
769
770     private String getVnfcFunctionCodeForVserver(SvcLogicContext ctx, String vServerId) {
771         log.info("getVnfcFunctionCodeForVserver()::vServerId=" + vServerId);
772         for (Object key : ctx.getAttributeKeySet()) {
773             String parmName = (String) key;
774             String parmValue = ctx.getAttribute(parmName);
775             log.info(parmName + "=" + parmValue);
776
777         }
778         String vnfcFunctionCode = ctx.getAttribute("tmp.vnfInfo.vm.vnfc.vnfc-function-code");
779         log.info("getVnfcFunctionCodeForVserver()::vnfcFunctionCode=" + vnfcFunctionCode);
780         return vnfcFunctionCode;
781     }
782
783     public boolean checkIfCapabilityCheckNeeded(String caplevel, String findCapability) {
784         boolean capabilityCheckNeeded = true;
785         if (!StringUtils.equalsIgnoreCase(caplevel, AppcDataServiceConstant.CAPABILITY_VM_LEVEL)) {
786             List<AppcDataServiceConstant.ACTIONS> actionList = new ArrayList<>(
787                 Arrays.asList(AppcDataServiceConstant.ACTIONS.values()));
788             for (AppcDataServiceConstant.ACTIONS action : actionList) {
789                 if (StringUtils.equalsIgnoreCase(action.toString(), findCapability)) {
790                     capabilityCheckNeeded = false;
791                     break;
792                 }
793             }
794         }
795         return capabilityCheckNeeded;
796     }
797 }