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