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