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