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