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