Add junit coverage to TimedOutException class
[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-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24 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         String templateModelId = ctx.getAttribute("template-model-id");
194         QueryStatus status;
195         String responsePrefix1 = "";
196
197         try {
198
199             responsePrefix1 = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
200             log.info("RESPONSEPREFIX : " + responsePrefix);
201             log.info("RESPONSEPREFIX1 : " + responsePrefix1);
202
203             if (StringUtils.isBlank(templateName)) {
204                 if (StringUtils.isNotBlank(templateModelId)) {
205                     status = db.getTemplateWithTemplateModelId(ctx, responsePrefix, fileCategory,templateModelId);
206                     if (status == QueryStatus.FAILURE) {
207                         throw new QueryException(UNABLE_TO_READ_STR + fileCategory);
208                     }
209                     if (!(status == QueryStatus.NOT_FOUND) ) {
210                         ctx.setAttribute(responsePrefix1 + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
211                                 AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS);
212                         log.info("GetTemplate Successful ");
213                         return;
214                     }
215                 }
216                 status = db.getTemplate(ctx, responsePrefix, fileCategory);
217                 if (status == QueryStatus.FAILURE) {
218                     throw new QueryException(UNABLE_TO_READ_STR + fileCategory);
219                 }
220
221                 if (status == QueryStatus.NOT_FOUND) {
222                     if (StringUtils.isNotBlank(templateModelId)) {
223                         status = db.getTemplateByVnfTypeNActionWithTemplateModelId(ctx, responsePrefix, fileCategory,templateModelId);
224                         if (status == QueryStatus.FAILURE) {
225                             throw new QueryException(UNABLE_TO_READ_STR + fileCategory);
226                         }
227                         if (!(status == QueryStatus.NOT_FOUND) ) {
228                             ctx.setAttribute(responsePrefix1 + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
229                                     AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS);
230                             log.info("GetTemplate Successful ");
231                             return;
232                         }
233                     }
234                     if (status == QueryStatus.NOT_FOUND) {
235
236
237                         status = db.getTemplateByVnfTypeNAction(ctx, responsePrefix, fileCategory);
238
239                         if (status == QueryStatus.FAILURE) {
240                             throw new QueryException(UNABLE_TO_READ_STR + fileCategory);
241                         }
242
243                         if (status == QueryStatus.NOT_FOUND) {
244                             throw new QueryException(UNABLE_TO_READ_STR + fileCategory);
245                         }
246                     }
247
248
249                 }
250             } else {
251
252                 status = db.getTemplateByTemplateName(ctx, responsePrefix, templateName);
253
254                 if (status == QueryStatus.NOT_FOUND || status == QueryStatus.FAILURE) {
255                     throw new QueryException(UNABLE_TO_READ_STR + fileCategory + " template");
256                 }
257             }
258
259             ctx.setAttribute(responsePrefix1 + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
260                 AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS);
261             log.info("GetTemplate Successful ");
262         } catch (Exception e) {
263             ctx.setAttribute(responsePrefix1 + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
264                 AppcDataServiceConstant.OUTPUT_STATUS_FAILURE);
265             ctx.setAttribute(responsePrefix1 + AppcDataServiceConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
266             log.error("Failed in getTemplate", e);
267
268             throw new SvcLogicException(e.getMessage());
269         }
270     }
271
272     void saveConfigFiles(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
273
274         log.info("Received saveConfigFiles call with params : " + inParams);
275
276         String responsePrefix = inParams.get(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX);
277
278         try {
279
280             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
281             QueryStatus status = db.saveConfigFiles(ctx, CONFIG_FILES_PREFIX);
282
283             if (status == QueryStatus.FAILURE) {
284                 throw new QueryException("Unable to Save " + ctx.getAttribute(FILE_CATEGORY_PARAM) + " in configfiles");
285             }
286
287             status = db.getMaxConfigFileId(ctx, MAX_CONF_FILE_PREFIX, ctx.getAttribute(FILE_CATEGORY_PARAM));
288
289             if (status == QueryStatus.NOT_FOUND || status == QueryStatus.FAILURE) {
290                 throw new QueryException(
291                     "Unable to get " + ctx.getAttribute(FILE_CATEGORY_PARAM) + " from configfiles");
292             }
293
294             status = db.savePrepareRelationship(ctx, PREPARE_RELATIONSHIP_PARAM,
295                 ctx.getAttribute(CONFIG_FILE_ID_PARAM), SDC_IND);
296             if (status == QueryStatus.FAILURE) {
297                 throw new QueryException(UNABLE_TO_SAVE_RELATIONSHIP_STR);
298             }
299
300             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
301                 AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS);
302             log.info("saveConfigFiles Successful ");
303         } catch (Exception e) {
304             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
305                 AppcDataServiceConstant.OUTPUT_STATUS_FAILURE);
306             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
307             log.error("Failed in saveConfigFiles", e);
308
309             throw new SvcLogicException(e.getMessage());
310         }
311     }
312
313     public void updateUploadConfig(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
314
315         log.info("Received updateUploadConfig call with params : " + inParams);
316
317         String responsePrefix = inParams.get(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX);
318
319         try {
320
321             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
322
323             ctx.setAttribute("tmp.escaped.devicerunningconfig",
324                 EscapeUtils.escapeSql(ctx.getAttribute("device-running-config")));
325
326             QueryStatus status = db.saveUploadConfig(ctx, UPLOAD_CONFIG_PREFIX);
327
328             if (status == QueryStatus.FAILURE) {
329                 throw new QueryException("Unable to Save configuration in upload_config");
330             }
331
332             status = db.getUploadConfigInfo(ctx, UPLOAD_CONFIG_INFO_PREFIX);
333
334             if (status == QueryStatus.NOT_FOUND || status == QueryStatus.FAILURE) {
335                 throw new QueryException("Unable to get record from upload_config");
336             }
337
338             status = db.updateUploadConfig(ctx, UPLOAD_CONFIG_PREFIX,
339                     Integer.parseInt(ctx.getAttribute(UPLOAD_CONFIG_ID_PARAM)));
340             if (status == QueryStatus.FAILURE)
341                 throw new QueryException("Unable to upload upload_config");
342
343             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
344                 AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS);
345             log.info("updateUploadConfig Successful ");
346         } catch (Exception e) {
347             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
348                 AppcDataServiceConstant.OUTPUT_STATUS_FAILURE);
349             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
350             log.error("Failed in updateUploadConfig", e);
351
352             throw new SvcLogicException(e.getMessage());
353         }
354     }
355
356     public void savePrepareRelationship(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
357
358         log.info("Received savePrepareRelationship call with params : " + inParams);
359
360         String responsePrefix = inParams.get(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX);
361         String sdcArtifactInd = inParams.get(AppcDataServiceConstant.INPUT_PARAM_SDC_ARTIFACT_IND);
362         String fileId = inParams.get(AppcDataServiceConstant.INPUT_PARAM_FILE_ID);
363         try {
364
365             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
366
367             QueryStatus status = db.savePrepareRelationship(ctx, PREPARE_RELATIONSHIP_PARAM, fileId, sdcArtifactInd);
368             if (status == QueryStatus.FAILURE) {
369                 throw new QueryException(UNABLE_TO_SAVE_RELATIONSHIP_STR);
370             }
371
372             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
373                 AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS);
374             log.info("savePrepareRelationship Successful ");
375         } catch (Exception e) {
376             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
377                 AppcDataServiceConstant.OUTPUT_STATUS_FAILURE);
378             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
379             log.error("Failed in saveConfigFiles", e);
380
381             throw new SvcLogicException(e.getMessage());
382         }
383     }
384
385     public void saveConfigBlock(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
386
387         log.info("Received saveConfigBlock call with params : " + inParams);
388
389         String responsePrefix = inParams.get(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX);
390
391         try {
392             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
393             ctx.setAttribute(TMP_CONVERTCONFIG_ESC_DATA, EscapeUtils.escapeSql(ctx.getAttribute("configuration")));
394
395             if (StringUtils.isBlank(ctx.getAttribute(CONFIG_PARAMS))) {
396                 saveDeviceConfiguration(inParams, ctx, "Request", ctx.getAttribute(TMP_CONVERTCONFIG_ESC_DATA),
397                     ctx.getAttribute("configuration"));
398             } else {
399
400                 saveConfigurationBlock(inParams, ctx);
401
402                 ctx.setAttribute(TMP_CONVERTCONFIG_ESC_DATA,
403                     EscapeUtils.escapeSql(ctx.getAttribute(TMP_MERGE_MERGED_DATA)));
404                 saveDeviceConfiguration(inParams, ctx, "Configurator", ctx.getAttribute(TMP_CONVERTCONFIG_ESC_DATA),
405                     ctx.getAttribute(TMP_MERGE_MERGED_DATA));
406
407                 saveConfigurationData(inParams, ctx);
408             }
409
410             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
411                 AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS);
412             log.info("saveConfigBlock Successful ");
413         } catch (Exception e) {
414             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
415                 AppcDataServiceConstant.OUTPUT_STATUS_FAILURE);
416             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
417             log.error("Failed in saveConfigBlock", e);
418
419             throw new SvcLogicException(e.getMessage());
420         }
421     }
422
423     public void saveTemplateConfig(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
424
425         log.info("Received saveTemplateConfig call with params : " + inParams);
426
427         String responsePrefix = inParams.get(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX);
428
429         try {
430             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
431
432             if (StringUtils.isBlank(ctx.getAttribute(CONFIG_PARAMS))) {
433
434                 ctx.setAttribute(TMP_CONVERTCONFIG_ESC_DATA,
435                     EscapeUtils.escapeSql(ctx.getAttribute("config-template.file-content")));
436                 saveDeviceConfiguration(inParams, ctx, "Template", ctx.getAttribute(TMP_CONVERTCONFIG_ESC_DATA),
437                     ctx.getAttribute("config-template.file-content"));
438
439             } else {
440                 saveConfigurationData(inParams, ctx);
441
442                 ctx.setAttribute(TMP_CONVERTCONFIG_ESC_DATA,
443                     EscapeUtils.escapeSql(ctx.getAttribute(TMP_MERGE_MERGED_DATA)));
444                 saveDeviceConfiguration(inParams, ctx, "Configurator", ctx.getAttribute(TMP_CONVERTCONFIG_ESC_DATA),
445                     ctx.getAttribute(TMP_MERGE_MERGED_DATA));
446
447             }
448
449             QueryStatus status = db.savePrepareRelationship(ctx, PREPARE_RELATIONSHIP_PARAM,
450                 ctx.getAttribute("config-template.config-file-id"), "Y");
451             if (status == QueryStatus.FAILURE) {
452                 throw new QueryException(UNABLE_TO_SAVE_RELATIONSHIP_STR);
453             }
454
455             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
456                 AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS);
457             log.info("saveTemplateConfig Successful ");
458         } catch (Exception e) {
459             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
460                 AppcDataServiceConstant.OUTPUT_STATUS_FAILURE);
461             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
462             log.error("Failed in saveTemplateConfig", e);
463
464             throw new SvcLogicException(e.getMessage());
465         }
466     }
467
468     public void saveStyleSheetConfig(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
469
470         log.info("Received saveStyleSheet call with params : " + inParams);
471
472         String responsePrefix = inParams.get(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX);
473
474         try {
475
476             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
477             ctx.setAttribute(TMP_CONVERTCONFIG_ESC_DATA,
478                 EscapeUtils.escapeSql(ctx.getAttribute(TMP_MERGE_MERGED_DATA)));
479             saveDeviceConfiguration(inParams, ctx, "StyleSheet", ctx.getAttribute(TMP_CONVERTCONFIG_ESC_DATA),
480                 ctx.getAttribute(TMP_MERGE_MERGED_DATA));
481
482             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
483                 AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS);
484             log.info("saveStyleSheet Successful ");
485         } catch (Exception e) {
486             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
487                 AppcDataServiceConstant.OUTPUT_STATUS_FAILURE);
488             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
489             log.error("Failed in saveStyleSheet", e);
490
491             throw new SvcLogicException(e.getMessage());
492         }
493     }
494
495     public void getSmmChainKeyFiles(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
496
497         log.info("Received saveStyleSheet call with params : " + inParams);
498
499         String responsePrefix = inParams.get(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX);
500         String siteLocation = ctx.getAttribute(SITE_LOCATION_PARAM);
501
502         QueryStatus status;
503
504         try {
505
506             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
507
508             status = db.getTemplateByArtifactType(ctx, "smm", "smm", siteLocation);
509
510             if (status == QueryStatus.FAILURE) {
511                 throw new QueryException("Unable to Read smm file");
512             }
513
514             status = db.getTemplateByArtifactType(ctx, "intermediate-ca-chain", "intermediate_ca_chain", siteLocation);
515
516             if (status == QueryStatus.FAILURE) {
517                 throw new QueryException("Unable to Read intermediate_ca_chain file");
518             }
519
520             status = db.getTemplateByArtifactType(ctx, "server-certificate-and-key", "server_certificate_and_key",
521                 siteLocation);
522
523             if (status == QueryStatus.FAILURE) {
524                 throw new QueryException("Unable to Read server_certificate_and_key file");
525             }
526
527             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
528                 AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS);
529             log.info("saveStyleSheet Successful ");
530         } catch (Exception e) {
531             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
532                 AppcDataServiceConstant.OUTPUT_STATUS_FAILURE);
533             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
534             log.error("Failed in saveStyleSheet", e);
535
536             throw new SvcLogicException(e.getMessage());
537         }
538     }
539
540     public void saveDeviceConfiguration(Map<String, String> inParams, SvcLogicContext ctx, String dataSource,
541                                         String fileContent, String deviceConfig) throws SvcLogicException {
542         ctx.setAttribute(DATA_SOURCE, dataSource);
543         ctx.setAttribute(FILE_CONTENT, fileContent);
544         ctx.setAttribute(FILE_CATEGORY_PARAM, "device_configuration");
545         ctx.setAttribute("deviceconfig-file-content", deviceConfig);
546
547         saveConfigFiles(inParams, ctx);
548     }
549
550     private void saveConfigurationBlock(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
551         ctx.setAttribute(DATA_SOURCE, "Request");
552         ctx.setAttribute(FILE_CONTENT, ctx.getAttribute(TMP_CONVERTCONFIG_ESC_DATA));
553         ctx.setAttribute(FILE_CATEGORY_PARAM, "configuration_block");
554         saveConfigFiles(inParams, ctx);
555     }
556
557     private void saveConfigurationData(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
558         ctx.setAttribute(DATA_SOURCE, ctx.getAttribute("originator-id"));
559         ctx.setAttribute(FILE_CONTENT, ctx.getAttribute(CONFIG_PARAMS));
560         ctx.setAttribute(FILE_CATEGORY_PARAM, "config_data");
561         saveConfigFiles(inParams, ctx);
562     }
563
564     public void getConfigFilesByVnfVmNCategory(Map<String, String> inParams, SvcLogicContext ctx)
565         throws SvcLogicException {
566
567         log.info("Received getConfigFilesByVnfVmNCategory call with params : " + inParams);
568
569         String responsePrefix = inParams.get(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX);
570         String fileCategory = inParams.get(AppcDataServiceConstant.INPUT_PARAM_FILE_CATEGORY);
571         String vnfId = inParams.get(AppcDataServiceConstant.INPUT_PARAM_VNF_ID);
572         String vmName = inParams.get(AppcDataServiceConstant.INPUT_PARAM_VM_NAME);
573         try {
574             QueryStatus status = db.getConfigFilesByVnfVmNCategory(ctx, responsePrefix, fileCategory, vnfId, vmName);
575
576             if (status == QueryStatus.NOT_FOUND || status == QueryStatus.FAILURE) {
577                 throw new QueryException("Unable to get " + ctx.getAttribute("fileCategory") + " from configfiles");
578             }
579
580             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
581             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
582                 AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS);
583             log.info("getConfigFilesByVnfVmNCategory Successful "
584                 + ctx.getAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS));
585         } catch (Exception e) {
586             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
587                 AppcDataServiceConstant.OUTPUT_STATUS_FAILURE);
588             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
589             log.error("Failed in getConfigFilesByVnfVmNCategory", e);
590
591             throw new SvcLogicException(e.getMessage());
592         }
593     }
594
595     public void getDownloadConfigTemplateByVnf(Map<String, String> inParams, SvcLogicContext ctx)
596         throws SvcLogicException {
597
598         log.info("Received getDownloadConfigTemplateByVnfNProtocol call with params : " + inParams);
599
600         String responsePrefix = inParams.get(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX);
601         try {
602             QueryStatus status = db.getDownloadConfigTemplateByVnf(ctx, responsePrefix);
603
604             if (status == QueryStatus.NOT_FOUND || status == QueryStatus.FAILURE) {
605                 throw new QueryException("Unable to get download config template.");
606             }
607
608             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
609             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
610                 AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS);
611             log.info("getDownloadConfigTemplateByVnf Successful "
612                 + ctx.getAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS));
613         } catch (Exception e) {
614             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
615                 AppcDataServiceConstant.OUTPUT_STATUS_FAILURE);
616             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
617             log.error("Failed in getDownloadConfigTemplateByVnf", e);
618
619             throw new SvcLogicException(e.getMessage());
620         }
621     }
622
623     public void saveConfigTransactionLog(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
624
625         String responsePrefix = inParams.get(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX);
626
627         String messageType = inParams.get(AppcDataServiceConstant.INPUT_PARAM_MESSAGE_TYPE);
628         String message = inParams.get(AppcDataServiceConstant.INPUT_PARAM_MESSAGE);
629
630         try {
631
632             SvcLogicContext logctx = new SvcLogicContext();
633             String escapedMessage = EscapeUtils.escapeSql(message);
634
635             logctx.setAttribute("request-id", ctx.getAttribute("request-id"));
636             logctx.setAttribute("log-message-type", messageType);
637             logctx.setAttribute("log-message", escapedMessage);
638
639             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
640             QueryStatus status = db.saveConfigTransactionLog(logctx, responsePrefix);
641
642             logctx.setAttribute("log-message", null);
643
644             if (status == QueryStatus.FAILURE) {
645                 throw new QueryException("Unable to insert into config_transaction_log");
646             }
647
648         } catch (Exception e) {
649             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
650                 AppcDataServiceConstant.OUTPUT_STATUS_FAILURE);
651             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
652             log.error("Failed in saveConfigTransactionLog", e);
653             throw new SvcLogicException(e.getMessage());
654         }
655     }
656
657     public void getVnfcReference(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
658
659         log.info("Received getVnfcReference call with params : " + inParams);
660
661         String responsePrefix = inParams.get(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX);
662         String templateModelId = ctx.getAttribute("template-model-id");
663         log.info("getVnfcReference():::"+templateModelId);
664         QueryStatus status = null;
665
666         try {
667             if (!StringUtils.isBlank(ctx.getAttribute("vnfc-type"))) {
668
669
670                 status = db.getVnfcReferenceByVnfcTypeNAction(ctx, responsePrefix);
671
672                 if (status == QueryStatus.FAILURE) {
673                     throw new QueryException("Unable to Read vnfc-reference");
674                 }
675
676             }
677             if (StringUtils.isNotBlank(templateModelId)) {
678                 status = db.getVnfcReferenceByVnfTypeNActionWithTemplateModelId(ctx, responsePrefix,templateModelId);
679                 if (status == QueryStatus.FAILURE) {
680                     throw new QueryException("Unable to Read vnfc-reference with template-model-id");
681                 }
682             }
683             if (StringUtils.isBlank(templateModelId) || (StringUtils.isNotBlank(templateModelId) && (status == QueryStatus.NOT_FOUND))) {
684                 status = db.getVnfcReferenceByVnfTypeNAction(ctx, responsePrefix);
685
686                 if (status == QueryStatus.NOT_FOUND || status == QueryStatus.FAILURE) {
687                     throw new QueryException("Unable to Read vnfc reference");
688                 }
689             }
690
691             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
692             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
693                 AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS);
694             log.info("getVnfcReference Successful ");
695         } catch (Exception e) {
696             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
697                 AppcDataServiceConstant.OUTPUT_STATUS_FAILURE);
698             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
699             log.error("Failed in getVnfcReference", e);
700
701             throw new SvcLogicException(e.getMessage());
702         }
703     }
704
705     public void getCapability(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
706         log.info("Received getCapability call with params : " + inParams);
707         String responsePrefix = inParams.get(AppcDataServiceConstant.INPUT_PARAM_RESPONSE_PREFIX);
708         responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
709         String caplevel = inParams.get("caplevel");
710         String findCapability = inParams.get("checkCapability");
711         String vServerId = inParams.get("vServerId");
712         if (!checkIfCapabilityCheckNeeded(caplevel, findCapability)) {
713             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
714                 AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS);
715             log.info("getCapability Successful - No need for capability check for this action");
716             return;
717         }
718         try {
719             String cap = db.getCapability(ctx, inParams.get("vnf-type"));
720             log.info("getCapability::returned from DB::+cap");
721             if (StringUtils.isBlank(cap)) {
722                 ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
723                     AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS);
724                 log.info("getCapability Successful - No capability blocks found");
725                 return;
726             }
727             ObjectMapper mapper = new ObjectMapper();
728             JsonNode caps = mapper.readTree(cap);
729             log.info("From DB =   " + caps);
730             JsonNode capabilities = caps.get(CAPABILITIES);
731             log.info("capabilities =   " + capabilities);
732             if (caplevel != null && !caplevel.isEmpty()) {
733                 JsonNode subCapabilities = capabilities.get(caplevel);
734                 log.info("subCapabilities =  " + caplevel + " : " + subCapabilities);
735                 if (caplevel.equalsIgnoreCase(AppcDataServiceConstant.CAPABILITY_VM_LEVEL)
736                     && (null == subCapabilities || subCapabilities.isNull() || subCapabilities.size() == 0)) {
737                     ctx.setAttribute(CAPABILITIES, "None");
738                     ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
739                         AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS);
740                     log.info("getCapability Successful ");
741                     return;
742                 }
743                 if (findCapability != null && !findCapability.isEmpty()) {
744                     if (subCapabilities != null && subCapabilities.toString().contains(findCapability)) {
745                         if (caplevel.equalsIgnoreCase(AppcDataServiceConstant.CAPABILITY_VM_LEVEL)) {
746                             processCapabilitiesForVMLevel(vServerId, ctx, findCapability, subCapabilities);
747                         } else {
748                             ctx.setAttribute(CAPABILITIES, "Supported");
749                         }
750                     } else {
751                         ctx.setAttribute(CAPABILITIES, NOT_SUPPORTED);
752                     }
753                 } else {
754                     ctx.setAttribute(responsePrefix + "capabilities." + caplevel, subCapabilities.toString());
755                 }
756
757             } else {
758                 ctx.setAttribute(responsePrefix + CAPABILITIES, capabilities.toString());
759             }
760             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
761                 AppcDataServiceConstant.OUTPUT_STATUS_SUCCESS);
762             log.info("getCapability Successful ");
763         } catch (Exception e) {
764             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_STATUS,
765                 AppcDataServiceConstant.OUTPUT_STATUS_FAILURE);
766             ctx.setAttribute(responsePrefix + AppcDataServiceConstant.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
767             log.error("Failed in getCapability", e);
768
769             throw new SvcLogicException(e.getMessage());
770         }
771     }
772
773     public void processCapabilitiesForVMLevel(String vServerId, SvcLogicContext ctx, String findCapability,
774         JsonNode subCapabilities) {
775         log.info("processCapabilitiesForVMLevel():::subCapabilities::" + subCapabilities.toString() + ",vServerId::"
776             + vServerId);
777         if (subCapabilities.size() == 0) {
778             ctx.setAttribute(CAPABILITIES, "None");
779             log.info("processCapabilitiesForVMLevel :: No VM block found!!");
780             return;
781         }
782         JsonNode vmCaps = null;
783         for (JsonNode cap : subCapabilities) {
784             if (null != cap && null != cap.get(findCapability)
785                 && StringUtils.isNotBlank(cap.get(findCapability).toString())) {
786                 vmCaps = cap.get(findCapability);
787                 log.info("processCapabilitiesForVMLevel()::vmCaps found" + vmCaps.toString());
788                 break;
789             }
790         }
791
792         if (null == vmCaps || vmCaps.isNull() || vmCaps.size() == 0) {
793             ctx.setAttribute(CAPABILITIES, NOT_SUPPORTED);
794             log.info("processCapabilitiesForVMLevel :: Found non-empty VM block but Not desired capability!!");
795             return;
796         }
797
798         String vnfcFunctionCode = getVnfcFunctionCodeForVserver(ctx, vServerId);
799         if (StringUtils.isBlank(vnfcFunctionCode)) {
800             log.info("processCapabilitiesForVMLevel() :: vnfcFunctionCode is not present in context!!!");
801             ctx.setAttribute(CAPABILITIES, NOT_SUPPORTED);
802             return;
803         }
804
805         if (vmCaps.toString().contains(vnfcFunctionCode)) {
806             ctx.setAttribute(CAPABILITIES, "Supported");
807         } else {
808             ctx.setAttribute(CAPABILITIES, NOT_SUPPORTED);
809         }
810         log.info("End processCapabilitiesForVMLevel():capabilities is ::" + ctx.getAttribute(CAPABILITIES));
811     }
812
813     private String getVnfcFunctionCodeForVserver(SvcLogicContext ctx, String vServerId) {
814         log.info("getVnfcFunctionCodeForVserver()::vServerId=" + vServerId);
815         for (Object key : ctx.getAttributeKeySet()) {
816             String parmName = (String) key;
817             String parmValue = ctx.getAttribute(parmName);
818             log.info(parmName + "=" + parmValue);
819
820         }
821         String vnfcFunctionCode = ctx.getAttribute("tmp.vnfInfo.vm.vnfc.vnfc-function-code");
822         log.info("getVnfcFunctionCodeForVserver()::vnfcFunctionCode=" + vnfcFunctionCode);
823         return vnfcFunctionCode;
824     }
825
826     public boolean checkIfCapabilityCheckNeeded(String caplevel, String findCapability) {
827         boolean capabilityCheckNeeded = true;
828         if (!StringUtils.equalsIgnoreCase(caplevel, AppcDataServiceConstant.CAPABILITY_VM_LEVEL)) {
829             List<AppcDataServiceConstant.ACTIONS> actionList = new ArrayList<>(
830                 Arrays.asList(AppcDataServiceConstant.ACTIONS.values()));
831             for (AppcDataServiceConstant.ACTIONS action : actionList) {
832                 if (StringUtils.equalsIgnoreCase(action.toString(), findCapability)) {
833                     capabilityCheckNeeded = false;
834                     break;
835                 }
836             }
837         }
838         return capabilityCheckNeeded;
839     }
840
841 }