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