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