2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Modifications Copyright (C) 2019 IBM.
10 * =============================================================================
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 * ============LICENSE_END=========================================================
25 package org.onap.appc.data.services.db;
29 import org.apache.commons.lang3.StringUtils;
30 import org.onap.ccsdk.sli.core.dblib.DbLibService;
31 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
35 import org.onap.ccsdk.sli.adaptors.resource.sql.SqlResource;
36 import com.att.eelf.configuration.EELFLogger;
37 import com.att.eelf.configuration.EELFManager;
39 public class DGGeneralDBService {
41 private static final EELFLogger log = EELFManager.getInstance().getLogger(DGGeneralDBService.class);
42 //private SvcLogicResource serviceLogic;
43 private DbLibServiceQueries dblib;
44 private static DGGeneralDBService dgGeneralDBService = null;
46 public static DGGeneralDBService initialise() {
48 if (dgGeneralDBService == null) {
49 dgGeneralDBService = new DGGeneralDBService();
51 return dgGeneralDBService;
54 private DGGeneralDBService() {
56 dblib = new DbLibServiceQueries();
60 protected DGGeneralDBService(DbLibService dbLibService) {
62 dblib = new DbLibServiceQueries(dbLibService);
66 protected DGGeneralDBService(DbLibServiceQueries dbLibServiceQueries) {
68 dblib = dbLibServiceQueries;
72 public QueryStatus getDeviceProtocolByVnfType(SvcLogicContext ctx, String prefix) throws SvcLogicException {
73 QueryStatus status = null;
74 if (dblib != null && ctx != null) {
75 String key = "SELECT * FROM DEVICE_INTERFACE_PROTOCOL WHERE vnf_type = $vnf-type ;";
76 status = dblib.query(key, prefix, ctx);
81 public QueryStatus getDeviceAuthenticationByVnfType(SvcLogicContext ctx, String prefix) throws SvcLogicException {
82 QueryStatus status = null;
83 if (dblib != null && ctx != null) {
84 String key = "SELECT * FROM DEVICE_AUTHENTICATION WHERE vnf_type = $vnf-type ;";
85 status = dblib.query(key, prefix, ctx);
91 public QueryStatus getConfigFileReferenceByVnfType(SvcLogicContext ctx, String prefix) throws SvcLogicException {
92 QueryStatus status = null;
93 if (dblib != null && ctx != null) {
94 String key = "SELECT * FROM CONFIG_FILE_REFERENCE WHERE vnf_type = $vnf-type ;";
95 status = dblib.query(key, prefix, ctx);
100 public QueryStatus getConfigFileReferenceByFileTypeNVnfType(SvcLogicContext ctx, String prefix, String fileType)
101 throws SvcLogicException {
102 QueryStatus status = null;
103 if (dblib != null && ctx != null) {
104 ctx.setAttribute("file-type", fileType);
105 String key = "SELECT * FROM CONFIG_FILE_REFERENCE WHERE file_type = $file-type"
106 + " and vnf_type = $vnf-type ;";
107 status = dblib.query(key, prefix, ctx);
112 public QueryStatus getTemplate(SvcLogicContext ctx, String prefix, String fileCategory) throws SvcLogicException {
113 QueryStatus status = null;
114 if (dblib != null && ctx != null) {
115 ctx.setAttribute("file-category", fileCategory);
116 String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
117 + " FROM ASDC_ARTIFACTS "
118 + " WHERE asdc_artifacts_id = ( SELECT MAX(a.asdc_artifacts_id) configfileid "
119 + " FROM ASDC_ARTIFACTS a, ASDC_REFERENCE b " + " WHERE a.artifact_name = b.artifact_name "
120 + " AND file_category = $file-category AND action = $request-action "
121 + " AND vnf_type = $vnf-type " + " AND vnfc_type = $vnfc-type ) ; ";
122 status = dblib.query(key, prefix, ctx);
127 public QueryStatus getTemplateByVnfTypeNAction(SvcLogicContext ctx, String prefix, String fileCategory)
128 throws SvcLogicException {
129 QueryStatus status = null;
130 if (dblib != null && ctx != null) {
131 ctx.setAttribute("file-category", fileCategory);
132 String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
133 + " FROM ASDC_ARTIFACTS "
134 + " WHERE asdc_artifacts_id = (SELECT MAX(a.asdc_artifacts_id) configfileid "
135 + " FROM ASDC_ARTIFACTS a, ASDC_REFERENCE b " + " WHERE a.artifact_name = b.artifact_name "
136 + " AND file_category = $file-category AND action = $request-action "
137 + " AND vnf_type = $vnf-type ) ; ";
138 status = dblib.query(key, prefix, ctx);
144 public QueryStatus getTemplateByVnfType(SvcLogicContext ctx, String prefix, String fileCategory)
145 throws SvcLogicException {
146 QueryStatus status = null;
147 if (dblib != null && ctx != null) {
148 ctx.setAttribute("file-category", fileCategory);
149 String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
150 + " FROM ASDC_ARTIFACTS "
151 + " WHERE asdc_artifacts_id = (SELECT MAX(a.asdc_artifacts_id) configfileid "
152 + " FROM ASDC_ARTIFACTS a, ASDC_REFERENCE b " + " WHERE a.artifact_name = b.artifact_name "
153 + " AND file_category = $file-category AND vnf_type = $vnf-type ) ; ";
155 status = dblib.query(key, prefix, ctx);
160 public QueryStatus getTemplateByTemplateName(SvcLogicContext ctx, String prefix, String templateName)
161 throws SvcLogicException {
162 QueryStatus status = null;
163 if (dblib != null && ctx != null) {
164 ctx.setAttribute("template-name", templateName);
165 String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
166 + " FROM ASDC_ARTIFACTS "
167 + " WHERE asdc_artifacts_id = (SELECT MAX(asdc_artifacts_id) configfileid "
168 + " FROM ASDC_ARTIFACTS " + " WHERE artifact_name = $template-name ) ; ";
170 status = dblib.query(key, prefix, ctx);
175 public QueryStatus getConfigureActionDGByVnfTypeNAction(SvcLogicContext ctx, String prefix)
176 throws SvcLogicException {
177 QueryStatus status = null;
178 if (dblib != null && ctx != null) {
179 String key = "SELECT * " + " FROM CONFIGURE_ACTION_DG "
180 + " where vnf_type = $vnf-type and action = $request-action ; ";
182 status = dblib.query(key, prefix, ctx);
187 public QueryStatus getConfigureActionDGByVnfType(SvcLogicContext ctx, String prefix) throws SvcLogicException {
188 QueryStatus status = null;
189 if (dblib != null && ctx != null) {
190 String key = "SELECT * " + " FROM CONFIGURE_ACTION_DG "
191 + " where vnf_type = $vnf-type and action IS NULL ; ";
193 status = dblib.query(key, prefix, ctx);
198 public QueryStatus getMaxConfigFileId(SvcLogicContext ctx, String prefix, String fileCategory)
199 throws SvcLogicException {
200 QueryStatus status = null;
201 if (dblib != null && ctx != null) {
202 ctx.setAttribute("file-category", fileCategory);
203 String key = "SELECT MAX(config_file_id) configfileid " + " FROM CONFIGFILES " + " WHERE file_category = "
204 + "$file-category AND vnf_id = $vnf-id AND vm_name = $vm-name ; ";
206 status = dblib.query(key, prefix, ctx);
211 public QueryStatus saveConfigFiles(SvcLogicContext ctx, String prefix) throws SvcLogicException {
213 QueryStatus status = null;
215 if (dblib != null && ctx != null) {
216 String key = "INSERT INTO CONFIGFILES " + " SET data_source = $data-source , "
217 + " service_instance_id = $service-instance-id ," + " action = $request-action ,"
218 + " vnf_type = $vnf-type ," + " vnfc_type = $vnfc-type ,"
219 + " vnf_id = $vnf-id , " + " vnf_name = $vnf-name ,"
220 + " vm_name = $vm-name ," + " file_category = $file-category ,"
221 + " file_content = $file-content ; ";
223 status = dblib.save(key, ctx);
230 public QueryStatus savePrepareRelationship(SvcLogicContext ctx, String prefix, String fileId, String sdcInd)
231 throws SvcLogicException {
233 QueryStatus status = null;
236 if (dblib != null && ctx != null) {
237 ctx.setAttribute("file-id", fileId);
238 if ("Y".equals(sdcInd))
240 key = "INSERT INTO PREPARE_FILE_RELATIONSHIP " + " SET service_instance_id = $service-instance-id , "
241 + " request_id = $request-id , " + " asdc_artifacts_id = $file-id ;";
243 key = "INSERT INTO PREPARE_FILE_RELATIONSHIP " + " SET service_instance_id = $service-instance-id , "
244 + " request_id = $request-id , " + " config_file_id = $file-id ;";
246 status = dblib.save(key, ctx);
248 log.info("DGGeneralDBService.savePrepareRelationship()" + ctx.getAttributeKeySet());
254 public void cleanContextPropertyByPrefix(SvcLogicContext ctx, String prefix) {
255 if (ctx != null && ctx.getAttributeKeySet() != null && StringUtils.isNotBlank(prefix)) {
257 Set<String> keySet = ctx.getAttributeKeySet();
258 for (String key : keySet) {
259 //By default, context property prefixed with dot.
260 if (StringUtils.isNotBlank(key) && key.startsWith(".")) {
261 ctx.getAttributeKeySet().remove(key);
267 public QueryStatus saveUploadConfig(SvcLogicContext ctx, String prefix) throws SvcLogicException {
269 QueryStatus status = null;
271 if (dblib != null && ctx != null) {
272 String key = "INSERT INTO UPLOAD_CONFIG " + " SET request_id = $request-id , "
273 + " action = $request-action , " + " originator_id = $originator-id , " + " vnf_id = $vnf-id , "
274 + " vnf_name = $vnf-name , " + " vm_name = $vm-name , "
275 + " host_ip_address = $vnf-host-ip-address , " + " vnf_type = $vnf-type , "
276 + " vnfc_type = $vnfc-type , " + " config_indicator = 'Current' , "
277 + " content = $tmp.escaped.devicerunningconfig ; ";
279 status = dblib.save(key, ctx);
281 log.info("DGGeneralDBService.saveUploadConfig()" + ctx.getAttributeKeySet());
288 /*public QueryStatus getMaxUploadConfigFileId(SvcLogicContext ctx, String prefix) throws SvcLogicException {
289 QueryStatus status = null;
290 if (serviceLogic != null && ctx != null) {
291 String key = "SELECT MAX(upload_config_id) uploadconfigid " + " FROM UPLOAD_CONFIG "
292 + " WHERE vnf_id = $vnf-id AND vm_name = $vm-name ; ";
294 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
295 log.info("DGGeneralDBService.getMaxUploadConfigFileId()" + ctx.getAttributeKeySet());
300 public QueryStatus updateUploadConfig(SvcLogicContext ctx, String prefix, int maxId) throws SvcLogicException {
301 QueryStatus status = null;
302 if (dblib != null && ctx != null) {
303 String key = "UPDATE UPLOAD_CONFIG " + " SET config_indicator = null "
304 + " WHERE upload_config_id != " + maxId + " AND config_indicator = 'Current' "
305 + " AND vnf_id = $vnf-id " + " AND vnfc_type = $vnfc-type ; ";
307 status = dblib.save(key, ctx);
309 log.info("DGGeneralDBService.updateUploadConfig()" + ctx.getAttributeKeySet());
317 public QueryStatus getTemplateByArtifactType(SvcLogicContext ctx, String prefix, String fileCategory, String artifactType)
318 throws SvcLogicException {
319 QueryStatus status = null;
320 if (dblib != null && ctx != null) {
321 ctx.setAttribute("file-category", fileCategory);
322 ctx.setAttribute("artifact-type", artifactType);
323 String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
324 + " FROM ASDC_ARTIFACTS "
325 + " WHERE asdc_artifacts_id = (SELECT MAX(a.asdc_artifacts_id) configfileid "
326 + " FROM ASDC_ARTIFACTS a, ASDC_REFERENCE b " + " WHERE a.artifact_name = b.artifact_name "
327 + " AND file_category = $file-category AND action = $request-action "
328 + " AND artifactType = $artifact-type AND vnf_type = $vnf-type ) ; ";
330 status = dblib.query(key, prefix, ctx);
336 public QueryStatus getConfigFilesByVnfVmNCategory(SvcLogicContext ctx, String prefix, String fileCategory, String vnfId, String vmName)
337 throws SvcLogicException {
338 QueryStatus status = null;
339 if (dblib != null && ctx != null) {
340 ctx.setAttribute("file-category", fileCategory);
341 ctx.setAttribute("vnf-id", vnfId);
342 ctx.setAttribute("vm-name", vmName);
343 String key = "SELECT file_content , config_file_id "
344 + " FROM CONFIGFILES "
345 + " WHERE config_file_id = ( SELECT MAX(config_file_id) configfileid " + " FROM CONFIGFILES "
346 + " WHERE file_category = $file-category"
347 + " AND vnf_id = $vnf-id"
348 + " AND vm_name = $vm-name ) ; ";
351 status = dblib.query(key, prefix, ctx);
357 public QueryStatus getDownloadConfigTemplateByVnf(SvcLogicContext ctx, String prefix)
358 throws SvcLogicException {
359 QueryStatus status = null;
360 if (dblib != null && ctx != null) {
361 String key = "SELECT * FROM DOWNLOAD_CONFIG_TEMPLATE WHERE vnf_type = $vnf-type ; ";
362 status = dblib.query(key, prefix, ctx);
369 public QueryStatus saveConfigTransactionLog(SvcLogicContext ctx, String prefix) throws SvcLogicException {
371 QueryStatus status = null;
373 if (dblib != null && ctx != null) {
375 String key = "INSERT INTO CONFIG_TRANSACTION_LOG " + " SET request_id = $request-id , "
376 + " message_type = $log-message-type , "
377 + " message = $log-message ;";
380 status = dblib.save(key, ctx);
389 public QueryStatus getVnfcReferenceByVnfcTypeNAction(SvcLogicContext ctx, String prefix)
390 throws SvcLogicException {
391 QueryStatus status = null;
392 if (dblib != null && ctx != null) {
394 String key = "SELECT * "
395 + " FROM VNFC_REFERENCE "
396 + " WHERE vnf_type = $vnf-type "
397 + " AND vnfc_type = $vnfc-type "
398 + " AND action = $request-action "
399 + " ORDER BY vm_instance, vnfc_instance ; ";
402 status = dblib.query(key, prefix, ctx);
408 public QueryStatus getVnfcReferenceByVnfTypeNAction(SvcLogicContext ctx, String prefix)
409 throws SvcLogicException {
410 QueryStatus status = null;
411 if (dblib != null && ctx != null) {
413 String key = "SELECT * "
414 + " FROM VNFC_REFERENCE "
415 + " WHERE vnf_type = $vnf-type "
416 + " AND action = $request-action "
417 + " ORDER BY vm_instance, vnfc_instance ; ";
419 status = dblib.query(key, prefix, ctx);
425 public QueryStatus getUploadConfigInfo(SvcLogicContext ctx, String prefix)
426 throws SvcLogicException {
427 QueryStatus status = null;
428 if (dblib != null && ctx != null) {
430 String key = "SELECT * , UNIX_TIMESTAMP(UPLOAD_DATE) UPLOAD_TIMESTAMP "
431 + " FROM UPLOAD_CONFIG "
432 + " WHERE upload_config_id = " +
433 "( SELECT MAX(upload_config_id) uploadconfigid " + " FROM UPLOAD_CONFIG "
434 + " WHERE vnf_id = $vnf-id AND vm_name = $vm-name ) ; ";
436 status = dblib.query(key, prefix, ctx);
440 public String getCapability(SvcLogicContext ctx, String vnf_type) throws SvcLogicException {
442 //{"capabilities":{"vnfc":[],"vm":[],"vf-module":[],"vnf":["ConfigureTest","ConfigModify","HealthCheck"]}}
443 String fn = "getCapability ";
444 QueryStatus status = null;
445 SvcLogicContext localContext = new SvcLogicContext();
446 localContext.setAttribute("vnf-type", vnf_type);
448 String queryString = "select max(internal_version) as maxInternalVersion, artifact_name as artifactName from ASDC_ARTIFACTS " +
449 " where artifact_name in (select artifact_name from ASDC_REFERENCE where vnf_type= $vnf-type " +
450 " and file_category = 'capability' )" ;
452 log.info(fn + "Query String : " + queryString);
453 status = dblib.query(queryString, localContext);
455 if(status.toString().equals("FAILURE"))
456 throw new SvcLogicException("Error - while getting capabilitiesData ");
458 String queryString1 = "select artifact_content from ASDC_ARTIFACTS " +
459 " where artifact_name = $artifactName and internal_version = $maxInternalVersion ";
461 log.debug(fn + "Query String : " + queryString1);
462 status = dblib.query(queryString1, localContext);
463 if (status.toString().equals("NOT_FOUND"))
465 if(status.toString().equals("FAILURE"))
466 throw new SvcLogicException("Error - while getting capabilitiesData ");
469 return localContext.getAttribute("artifact-content");
472 public QueryStatus getTemplateWithTemplateModelId(SvcLogicContext ctx, String prefix, String fileCategory,
473 String templateModelId) throws SvcLogicException {
474 QueryStatus status = null;
475 String templatePattern = "%_"+ templateModelId +"%";
476 if (dblib != null && ctx != null) {
477 ctx.setAttribute("file-category", fileCategory);
478 ctx.setAttribute("template-pattern", templatePattern);
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 = $file-category AND action = $request-action "
484 + " AND vnf_type = $vnf-type " + " AND vnfc_type = $vnfc-type ) and ASDC_ARTIFACTS.artifact_name like "
485 + "$template-pattern ; ";
486 log.info("getTemplateWithTemplateModelId()::: with template:::"+ key);
488 status = dblib.query(key, prefix, ctx);
493 public QueryStatus getTemplateByVnfTypeNActionWithTemplateModelId(SvcLogicContext ctx, String prefix,
494 String fileCategory, String templateModelId) throws SvcLogicException {
495 QueryStatus status = null;
496 String templatePattern = "%_"+ templateModelId +"%";
497 if (dblib != null && ctx != null) {
498 ctx.setAttribute("file-category", fileCategory);
499 ctx.setAttribute("template-pattern", templatePattern);
500 String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
501 + " FROM ASDC_ARTIFACTS "
502 + " WHERE asdc_artifacts_id = (SELECT MAX(a.asdc_artifacts_id) configfileid "
503 + " FROM ASDC_ARTIFACTS a, ASDC_REFERENCE b " + " WHERE a.artifact_name = b.artifact_name "
504 + " AND file_category = $file-category AND action = $request-action "
505 + " AND vnf_type = $vnf-type ) and ASDC_ARTIFACTS.artifact_name like "
506 + "$template-pattern ; ";
507 log.info("getTemplateByVnfTypeNActionWithTemplateModelId()::: with template:::"+ key);
509 status = dblib.query(key, prefix, ctx);
515 public QueryStatus getVnfcReferenceByVnfTypeNActionWithTemplateModelId(SvcLogicContext ctx, String prefix,
516 String templateModelId) throws SvcLogicException {
517 QueryStatus status = null;
518 if (dblib != null && ctx != null) {
519 ctx.setAttribute("template-model-id", templateModelId);
520 String key = "SELECT * "
521 + " FROM VNFC_REFERENCE "
522 + " WHERE vnf_type = $vnf-type "
523 + " AND action = $request-action "
524 + " AND template_id = "
525 + "$template-model-id"
526 + " ORDER BY vm_instance, vnfc_instance ; ";
528 log.info("getVnfcReferenceByVnfTypeNActionWithTemplateModelId()::: with template:::"+ key);
529 status = dblib.query(key, prefix, ctx);