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