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