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