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