2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
24 package org.onap.appc.data.services.db;
28 import org.apache.commons.lang3.StringUtils;
30 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
31 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
34 import org.onap.ccsdk.sli.adaptors.resource.sql.SqlResource;
35 import com.att.eelf.configuration.EELFLogger;
36 import com.att.eelf.configuration.EELFManager;
38 public class DGGeneralDBService {
40 private static final EELFLogger log = EELFManager.getInstance().getLogger(DGGeneralDBService.class);
41 private SvcLogicResource serviceLogic;
42 private static DGGeneralDBService dgGeneralDBService = null;
44 public static DGGeneralDBService initialise() {
46 if (dgGeneralDBService == null) {
47 dgGeneralDBService = new DGGeneralDBService();
49 return dgGeneralDBService;
52 private DGGeneralDBService() {
53 if (serviceLogic == null) {
54 serviceLogic = new SqlResource();
58 protected DGGeneralDBService(SqlResource svcLogic) {
59 if (serviceLogic == null) {
60 serviceLogic = svcLogic;
64 public QueryStatus getDeviceProtocolByVnfType(SvcLogicContext ctx, String prefix) throws SvcLogicException {
65 QueryStatus status = null;
66 if (serviceLogic != null && ctx != null) {
67 String key = "SELECT * FROM DEVICE_INTERFACE_PROTOCOL WHERE vnf_type = $vnf-type ;";
68 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
73 public QueryStatus getDeviceAuthenticationByVnfType(SvcLogicContext ctx, String prefix) throws SvcLogicException {
74 QueryStatus status = null;
75 if (serviceLogic != null && ctx != null) {
76 String key = "SELECT * FROM DEVICE_AUTHENTICATION WHERE vnf_type = $vnf-type ;";
77 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
83 public QueryStatus getConfigFileReferenceByVnfType(SvcLogicContext ctx, String prefix) throws SvcLogicException {
84 QueryStatus status = null;
85 if (serviceLogic != null && ctx != null) {
86 String key = "SELECT * FROM CONFIG_FILE_REFERENCE WHERE vnf_type = $vnf-type ;";
87 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
92 public QueryStatus getConfigFileReferenceByFileTypeNVnfType(SvcLogicContext ctx, String prefix, String fileType)
93 throws SvcLogicException {
94 QueryStatus status = null;
95 if (serviceLogic != null && ctx != null) {
96 String key = "SELECT * FROM CONFIG_FILE_REFERENCE WHERE file_type = '" + fileType
97 + "' and vnf_type = $vnf-type ;";
98 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
103 public QueryStatus getTemplate(SvcLogicContext ctx, String prefix, String fileCategory) throws SvcLogicException {
104 QueryStatus status = null;
105 if (serviceLogic != null && ctx != null) {
106 String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
107 + " FROM ASDC_ARTIFACTS "
108 + " WHERE asdc_artifacts_id = ( SELECT MAX(a.asdc_artifacts_id) configfileid "
109 + " FROM ASDC_ARTIFACTS a, ASDC_REFERENCE b " + " WHERE a.artifact_name = b.artifact_name "
110 + " AND file_category = '" + fileCategory + "'" + " AND action = $request-action "
111 + " AND vnf_type = $vnf-type " + " AND vnfc_type = $vnfc-type ) ; ";
112 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
117 public QueryStatus getTemplateByVnfTypeNAction(SvcLogicContext ctx, String prefix, String fileCategory)
118 throws SvcLogicException {
119 QueryStatus status = null;
120 if (serviceLogic != null && ctx != null) {
121 String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
122 + " FROM ASDC_ARTIFACTS "
123 + " WHERE asdc_artifacts_id = (SELECT MAX(a.asdc_artifacts_id) configfileid "
124 + " FROM ASDC_ARTIFACTS a, ASDC_REFERENCE b " + " WHERE a.artifact_name = b.artifact_name "
125 + " AND file_category = '" + fileCategory + "'" + " AND action = $request-action "
126 + " AND vnf_type = $vnf-type ) ; ";
127 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
133 public QueryStatus getTemplateByVnfType(SvcLogicContext ctx, String prefix, String fileCategory)
134 throws SvcLogicException {
135 QueryStatus status = null;
136 if (serviceLogic != null && ctx != null) {
137 String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
138 + " FROM ASDC_ARTIFACTS "
139 + " WHERE asdc_artifacts_id = (SELECT MAX(a.asdc_artifacts_id) configfileid "
140 + " FROM ASDC_ARTIFACTS a, ASDC_REFERENCE b " + " WHERE a.artifact_name = b.artifact_name "
141 + " AND file_category = '" + fileCategory + "'" + " AND vnf_type = $vnf-type ) ; ";
143 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
148 public QueryStatus getTemplateByTemplateName(SvcLogicContext ctx, String prefix, String templateName)
149 throws SvcLogicException {
150 QueryStatus status = null;
151 if (serviceLogic != null && ctx != null) {
152 String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
153 + " FROM ASDC_ARTIFACTS "
154 + " WHERE asdc_artifacts_id = (SELECT MAX(asdc_artifacts_id) configfileid "
155 + " FROM ASDC_ARTIFACTS " + " WHERE artifact_name = '" + templateName + "' ) ; ";
157 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
162 public QueryStatus getConfigureActionDGByVnfTypeNAction(SvcLogicContext ctx, String prefix)
163 throws SvcLogicException {
164 QueryStatus status = null;
165 if (serviceLogic != null && ctx != null) {
166 String key = "SELECT * " + " FROM CONFIGURE_ACTION_DG "
167 + " where vnf_type = $vnf-type and action = $request-action ; ";
169 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
174 public QueryStatus getConfigureActionDGByVnfType(SvcLogicContext ctx, String prefix) throws SvcLogicException {
175 QueryStatus status = null;
176 if (serviceLogic != null && ctx != null) {
177 String key = "SELECT * " + " FROM CONFIGURE_ACTION_DG "
178 + " where vnf_type = $vnf-type and action IS NULL ; ";
180 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
185 public QueryStatus getMaxConfigFileId(SvcLogicContext ctx, String prefix, String fileCategory)
186 throws SvcLogicException {
187 QueryStatus status = null;
188 if (serviceLogic != null && ctx != null) {
189 String key = "SELECT MAX(config_file_id) configfileid " + " FROM CONFIGFILES " + " WHERE file_category = '"
190 + fileCategory + "'" + " AND vnf_id = $vnf-id AND vm_name = $vm-name ; ";
192 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
197 public QueryStatus saveConfigFiles(SvcLogicContext ctx, String prefix) throws SvcLogicException {
199 QueryStatus status = null;
201 if (serviceLogic != null && ctx != null) {
202 String key = "INSERT INTO CONFIGFILES " + " SET data_source = $data-source , "
203 + " service_instance_id = $service-instance-id ," + " action = $request-action ,"
204 + " vnf_type = $vnf-type ," + " vnfc_type = $vnfc-type ,"
205 + " vnf_id = $vnf-id , " + " vnf_name = $vnf-name ,"
206 + " vm_name = $vm-name ," + " file_category = $file-category ,"
207 + " file_content = $file-content ; ";
209 status = serviceLogic.save("SQL", false, false, key, null, prefix, ctx);
216 public QueryStatus savePrepareRelationship(SvcLogicContext ctx, String prefix, String fileId, String sdcInd)
217 throws SvcLogicException {
219 QueryStatus status = null;
222 if (serviceLogic != null && ctx != null) {
224 if ("Y".equals(sdcInd))
226 key = "INSERT INTO PREPARE_FILE_RELATIONSHIP " + " SET service_instance_id = $service-instance-id , "
227 + " request_id = $request-id , " + " asdc_artifacts_id = " + fileId + " ;";
229 key = "INSERT INTO PREPARE_FILE_RELATIONSHIP " + " SET service_instance_id = $service-instance-id , "
230 + " request_id = $request-id , " + " config_file_id = " + fileId + " ;";
232 status = serviceLogic.save("SQL", false, false, key, null, prefix, ctx);
234 log.info("DGGeneralDBService.savePrepareRelationship()" + ctx.getAttributeKeySet());
240 public void cleanContextPropertyByPrefix(SvcLogicContext ctx, String prefix) {
241 if (ctx != null && ctx.getAttributeKeySet() != null && StringUtils.isNotBlank(prefix)) {
243 Set<String> keySet = ctx.getAttributeKeySet();
244 for (String key : keySet) {
245 if (StringUtils.isNotBlank(key) && key.startsWith(prefix = ".")) {
246 ctx.getAttributeKeySet().remove(key);
252 public QueryStatus saveUploadConfig(SvcLogicContext ctx, String prefix) throws SvcLogicException {
254 QueryStatus status = null;
256 if (serviceLogic != null && ctx != null) {
257 String key = "INSERT INTO UPLOAD_CONFIG " + " SET request_id = $request-id , "
258 + " action = $request-action , " + " originator_id = $originator-id , " + " vnf_id = $vnf-id , "
259 + " vnf_name = $vnf-name , " + " vm_name = $vm-name , "
260 + " host_ip_address = $vnf-host-ip-address , " + " vnf_type = $vnf-type , "
261 + " vnfc_type = $vnfc-type , " + " config_indicator = 'Current' , "
262 + " content = $tmp.escaped.devicerunningconfig ; ";
264 status = serviceLogic.save("SQL", false, false, key, null, prefix, ctx);
266 log.info("DGGeneralDBService.saveUploadConfig()" + ctx.getAttributeKeySet());
273 /*public QueryStatus getMaxUploadConfigFileId(SvcLogicContext ctx, String prefix) throws SvcLogicException {
274 QueryStatus status = null;
275 if (serviceLogic != null && ctx != null) {
276 String key = "SELECT MAX(upload_config_id) uploadconfigid " + " FROM UPLOAD_CONFIG "
277 + " WHERE vnf_id = $vnf-id AND vm_name = $vm-name ; ";
279 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
280 log.info("DGGeneralDBService.getMaxUploadConfigFileId()" + ctx.getAttributeKeySet());
285 public QueryStatus updateUploadConfig(SvcLogicContext ctx, String prefix, int maxId) throws SvcLogicException {
286 QueryStatus status = null;
287 if (serviceLogic != null && ctx != null) {
288 String key = "UPDATE UPLOAD_CONFIG " + " SET config_indicator = null "
289 + " WHERE upload_config_id != " + maxId + " AND config_indicator = 'Current' "
290 + " AND vnf_id = $vnf-id " + " AND vm_name = $vm-name ; ";
292 status = serviceLogic.save("SQL", false, false, key, null, prefix, ctx);
294 log.info("DGGeneralDBService.updateUploadConfig()" + ctx.getAttributeKeySet());
302 public QueryStatus getTemplateByArtifactType(SvcLogicContext ctx, String prefix, String fileCategory, String artifactType)
303 throws SvcLogicException {
304 QueryStatus status = null;
305 if (serviceLogic != null && ctx != null) {
306 String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
307 + " FROM ASDC_ARTIFACTS "
308 + " WHERE asdc_artifacts_id = (SELECT MAX(a.asdc_artifacts_id) configfileid "
309 + " FROM ASDC_ARTIFACTS a, ASDC_REFERENCE b " + " WHERE a.artifact_name = b.artifact_name "
310 + " AND file_category = '" + fileCategory + "'" + " AND action = $request-action "
311 + " AND artifactType = '" + artifactType + "'" + " AND vnf_type = $vnf-type ) ; ";
313 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
319 public QueryStatus getConfigFilesByVnfVmNCategory(SvcLogicContext ctx, String prefix, String fileCategory, String vnfId, String vmName)
320 throws SvcLogicException {
321 QueryStatus status = null;
322 if (serviceLogic != null && ctx != null) {
324 String key = "SELECT file_content , config_file_id "
325 + " FROM CONFIGFILES "
326 + " WHERE config_file_id = ( SELECT MAX(config_file_id) configfileid " + " FROM CONFIGFILES "
327 + " WHERE file_category = '" + fileCategory + "'"
328 + " AND vnf_id = '" + vnfId + "'"
329 + " AND vm_name = '" + vmName + "' ) ; ";
332 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
338 public QueryStatus getDownloadConfigTemplateByVnf(SvcLogicContext ctx, String prefix)
339 throws SvcLogicException {
340 QueryStatus status = null;
341 if (serviceLogic != null && ctx != null) {
342 String key = "SELECT * FROM DOWNLOAD_CONFIG_TEMPLATE WHERE vnf_type = $vnf-type ; ";
343 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
350 public QueryStatus saveConfigTransactionLog(SvcLogicContext ctx, String prefix) throws SvcLogicException {
352 QueryStatus status = null;
354 if (serviceLogic != null && ctx != null) {
357 String key = "INSERT INTO CONFIG_TRANSACTION_LOG " + " SET request_id = $request-id , "
358 + " message_type = $log-message-type , "
359 + " message = $log-message ;";
362 status = serviceLogic.save("SQL", false, false, key, null, prefix, ctx);
372 public QueryStatus getVnfcReferenceByVnfcTypeNAction(SvcLogicContext ctx, String prefix)
373 throws SvcLogicException {
374 QueryStatus status = null;
375 if (serviceLogic != null && ctx != null) {
377 String key = "SELECT * "
378 + " FROM VNFC_REFERENCE "
379 + " WHERE vnf_type = $vnf-type "
380 + " AND vnfc_type = $vnfc-type "
381 + " AND action = $request-action "
382 + " ORDER BY vm_instance, vnfc_instance ; ";
385 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
391 public QueryStatus getVnfcReferenceByVnfTypeNAction(SvcLogicContext ctx, String prefix)
392 throws SvcLogicException {
393 QueryStatus status = null;
394 if (serviceLogic != null && ctx != null) {
396 String key = "SELECT * "
397 + " FROM VNFC_REFERENCE "
398 + " WHERE vnf_type = $vnf-type "
399 + " AND action = $request-action "
400 + " ORDER BY vm_instance, vnfc_instance ; ";
402 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
408 public QueryStatus getUploadConfigInfo(SvcLogicContext ctx, String prefix)
409 throws SvcLogicException {
410 QueryStatus status = null;
411 if (serviceLogic != null && ctx != null) {
413 String key = "SELECT * , UNIX_TIMESTAMP(UPLOAD_DATE) UPLOAD_TIMESTAMP "
414 + " FROM UPLOAD_CONFIG "
415 + " WHERE upload_config_id = " +
416 "( SELECT MAX(upload_config_id) uploadconfigid " + " FROM UPLOAD_CONFIG "
417 + " WHERE vnf_id = $vnf-id AND vm_name = $vm-name ) ; ";
419 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
423 public String getCapability(SvcLogicContext ctx, String vnf_type) throws SvcLogicException {
425 //{"capabilities":{"vnfc":[],"vm":[],"vf-module":[],"vnf":["ConfigureTest","ConfigModify","HealthCheck"]}}
426 String fn = "getCapability ";
427 QueryStatus status = null;
428 SvcLogicContext localContext = new SvcLogicContext();
429 localContext.setAttribute("vnf-type", vnf_type);
430 if (serviceLogic != null) {
431 String queryString = "select max(internal_version) as maxInternalVersion, artifact_name as artifactName from ASDC_ARTIFACTS " +
432 " where artifact_name in (select artifact_name from ASDC_REFERENCE where vnf_type= $vnf-type " +
433 " and file_category = 'capability' )" ;
435 log.info(fn + "Query String : " + queryString);
436 status = serviceLogic.query("SQL", false, null, queryString, null, null, localContext);
438 if(status.toString().equals("FAILURE"))
439 throw new SvcLogicException("Error - while getting capabilitiesData ");
441 String queryString1 = "select artifact_content from ASDC_ARTIFACTS " +
442 " where artifact_name = $artifactName and internal_version = $maxInternalVersion ";
444 log.debug(fn + "Query String : " + queryString1);
445 status = serviceLogic.query("SQL", false, null, queryString1, null, null, localContext);
446 if (status.toString().equals("NOT_FOUND"))
448 if(status.toString().equals("FAILURE"))
449 throw new SvcLogicException("Error - while getting capabilitiesData ");
452 return localContext.getAttribute("artifact-content");
455 public QueryStatus getTemplateWithTemplateModelId(SvcLogicContext ctx, String prefix, String fileCategory,
456 String templateModelId) throws SvcLogicException {
457 QueryStatus status = null;
458 String templatePattern = "'%_"+ templateModelId +"%'";
459 if (serviceLogic != null && ctx != null) {
460 String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
461 + " FROM ASDC_ARTIFACTS "
462 + " WHERE asdc_artifacts_id = ( SELECT MAX(a.asdc_artifacts_id) configfileid "
463 + " FROM ASDC_ARTIFACTS a, ASDC_REFERENCE b " + " WHERE a.artifact_name = b.artifact_name "
464 + " AND file_category = '" + fileCategory + "'" + " AND action = $request-action "
465 + " AND vnf_type = $vnf-type " + " AND vnfc_type = $vnfc-type ) and ASDC_ARTIFACTS.artifact_name like "
466 + templatePattern + "; ";
467 log.info("getTemplateWithTemplateModelId()::: with template:::"+ key);
469 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
474 public QueryStatus getTemplateByVnfTypeNActionWithTemplateModelId(SvcLogicContext ctx, String prefix,
475 String fileCategory, String templateModelId) throws SvcLogicException {
476 QueryStatus status = null;
477 String templatePattern = "'%_"+ templateModelId +"%'";
478 if (serviceLogic != null && ctx != null) {
479 String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
480 + " FROM ASDC_ARTIFACTS "
481 + " WHERE asdc_artifacts_id = (SELECT MAX(a.asdc_artifacts_id) configfileid "
482 + " FROM ASDC_ARTIFACTS a, ASDC_REFERENCE b " + " WHERE a.artifact_name = b.artifact_name "
483 + " AND file_category = '" + fileCategory + "'" + " AND action = $request-action "
484 + " AND vnf_type = $vnf-type ) and ASDC_ARTIFACTS.artifact_name like "
485 + templatePattern + "; ";
486 log.info("getTemplateByVnfTypeNActionWithTemplateModelId()::: with template:::"+ key);
488 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
494 public QueryStatus getVnfcReferenceByVnfTypeNActionWithTemplateModelId(SvcLogicContext ctx, String prefix,
495 String templateModelId) throws SvcLogicException {
496 QueryStatus status = null;
497 if (serviceLogic != null && ctx != null) {
499 String key = "SELECT * "
500 + " FROM VNFC_REFERENCE "
501 + " WHERE vnf_type = $vnf-type "
502 + " AND action = $request-action "
503 + " AND template_id = '"
504 + templateModelId + "'"
505 + " ORDER BY vm_instance, vnfc_instance ; ";
507 log.info("getVnfcReferenceByVnfTypeNActionWithTemplateModelId()::: with template:::"+ key);
508 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);