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 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 DbLibServiceQueries dblib;
43 private static DGGeneralDBService dgGeneralDBService = null;
45 public static DGGeneralDBService initialise() {
47 if (dgGeneralDBService == null) {
48 dgGeneralDBService = new DGGeneralDBService();
50 return dgGeneralDBService;
53 private DGGeneralDBService() {
55 dblib = new DbLibServiceQueries();
59 protected DGGeneralDBService(DbLibService dbLibService) {
61 dblib = new DbLibServiceQueries(dbLibService);
65 protected DGGeneralDBService(DbLibServiceQueries dbLibServiceQueries) {
67 dblib = dbLibServiceQueries;
71 public QueryStatus getDeviceProtocolByVnfType(SvcLogicContext ctx, String prefix) throws SvcLogicException {
72 QueryStatus status = null;
73 if (dblib != null && ctx != null) {
74 String key = "SELECT * FROM DEVICE_INTERFACE_PROTOCOL WHERE vnf_type = $vnf-type ;";
75 status = dblib.query(key, prefix, ctx);
80 public QueryStatus getDeviceAuthenticationByVnfType(SvcLogicContext ctx, String prefix) throws SvcLogicException {
81 QueryStatus status = null;
82 if (dblib != null && ctx != null) {
83 String key = "SELECT * FROM DEVICE_AUTHENTICATION WHERE vnf_type = $vnf-type ;";
84 status = dblib.query(key, prefix, ctx);
90 public QueryStatus getConfigFileReferenceByVnfType(SvcLogicContext ctx, String prefix) throws SvcLogicException {
91 QueryStatus status = null;
92 if (dblib != null && ctx != null) {
93 String key = "SELECT * FROM CONFIG_FILE_REFERENCE WHERE vnf_type = $vnf-type ;";
94 status = dblib.query(key, prefix, ctx);
99 public QueryStatus getConfigFileReferenceByFileTypeNVnfType(SvcLogicContext ctx, String prefix, String fileType)
100 throws SvcLogicException {
101 QueryStatus status = null;
102 if (dblib != null && ctx != null) {
103 ctx.setAttribute("file-type", fileType);
104 String key = "SELECT * FROM CONFIG_FILE_REFERENCE WHERE file_type = $file-type"
105 + " and vnf_type = $vnf-type ;";
106 status = dblib.query(key, prefix, ctx);
111 public QueryStatus getTemplate(SvcLogicContext ctx, String prefix, String fileCategory) throws SvcLogicException {
112 QueryStatus status = null;
113 if (dblib != null && ctx != null) {
114 ctx.setAttribute("file-category", fileCategory);
115 String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
116 + " FROM ASDC_ARTIFACTS "
117 + " WHERE asdc_artifacts_id = ( SELECT MAX(a.asdc_artifacts_id) configfileid "
118 + " FROM ASDC_ARTIFACTS a, ASDC_REFERENCE b " + " WHERE a.artifact_name = b.artifact_name "
119 + " AND file_category = $file-category AND action = $request-action "
120 + " AND vnf_type = $vnf-type " + " AND vnfc_type = $vnfc-type ) ; ";
121 status = dblib.query(key, prefix, ctx);
126 public QueryStatus getTemplateByVnfTypeNAction(SvcLogicContext ctx, String prefix, String fileCategory)
127 throws SvcLogicException {
128 QueryStatus status = null;
129 if (dblib != null && ctx != null) {
130 ctx.setAttribute("file-category", fileCategory);
131 String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
132 + " FROM ASDC_ARTIFACTS "
133 + " WHERE asdc_artifacts_id = (SELECT MAX(a.asdc_artifacts_id) configfileid "
134 + " FROM ASDC_ARTIFACTS a, ASDC_REFERENCE b " + " WHERE a.artifact_name = b.artifact_name "
135 + " AND file_category = $file-category AND action = $request-action "
136 + " AND vnf_type = $vnf-type ) ; ";
137 status = dblib.query(key, prefix, ctx);
143 public QueryStatus getTemplateByVnfType(SvcLogicContext ctx, String prefix, String fileCategory)
144 throws SvcLogicException {
145 QueryStatus status = null;
146 if (dblib != null && ctx != null) {
147 ctx.setAttribute("file-category", fileCategory);
148 String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
149 + " FROM ASDC_ARTIFACTS "
150 + " WHERE asdc_artifacts_id = (SELECT MAX(a.asdc_artifacts_id) configfileid "
151 + " FROM ASDC_ARTIFACTS a, ASDC_REFERENCE b " + " WHERE a.artifact_name = b.artifact_name "
152 + " AND file_category = $file-category AND vnf_type = $vnf-type ) ; ";
154 status = dblib.query(key, prefix, ctx);
159 public QueryStatus getTemplateByTemplateName(SvcLogicContext ctx, String prefix, String templateName)
160 throws SvcLogicException {
161 QueryStatus status = null;
162 if (dblib != null && ctx != null) {
163 ctx.setAttribute("template-name", templateName);
164 String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
165 + " FROM ASDC_ARTIFACTS "
166 + " WHERE asdc_artifacts_id = (SELECT MAX(asdc_artifacts_id) configfileid "
167 + " FROM ASDC_ARTIFACTS " + " WHERE artifact_name = $template-name ) ; ";
169 status = dblib.query(key, prefix, ctx);
174 public QueryStatus getConfigureActionDGByVnfTypeNAction(SvcLogicContext ctx, String prefix)
175 throws SvcLogicException {
176 QueryStatus status = null;
177 if (dblib != null && ctx != null) {
178 String key = "SELECT * " + " FROM CONFIGURE_ACTION_DG "
179 + " where vnf_type = $vnf-type and action = $request-action ; ";
181 status = dblib.query(key, prefix, ctx);
186 public QueryStatus getConfigureActionDGByVnfType(SvcLogicContext ctx, String prefix) throws SvcLogicException {
187 QueryStatus status = null;
188 if (dblib != null && ctx != null) {
189 String key = "SELECT * " + " FROM CONFIGURE_ACTION_DG "
190 + " where vnf_type = $vnf-type and action IS NULL ; ";
192 status = dblib.query(key, prefix, ctx);
197 public QueryStatus getMaxConfigFileId(SvcLogicContext ctx, String prefix, String fileCategory)
198 throws SvcLogicException {
199 QueryStatus status = null;
200 if (dblib != null && ctx != null) {
201 ctx.setAttribute("file-category", fileCategory);
202 String key = "SELECT MAX(config_file_id) configfileid " + " FROM CONFIGFILES " + " WHERE file_category = "
203 + "$file-category AND vnf_id = $vnf-id AND vm_name = $vm-name ; ";
205 status = dblib.query(key, prefix, ctx);
210 public QueryStatus saveConfigFiles(SvcLogicContext ctx, String prefix) throws SvcLogicException {
212 QueryStatus status = null;
214 if (dblib != null && ctx != null) {
215 String key = "INSERT INTO CONFIGFILES " + " SET data_source = $data-source , "
216 + " service_instance_id = $service-instance-id ," + " action = $request-action ,"
217 + " vnf_type = $vnf-type ," + " vnfc_type = $vnfc-type ,"
218 + " vnf_id = $vnf-id , " + " vnf_name = $vnf-name ,"
219 + " vm_name = $vm-name ," + " file_category = $file-category ,"
220 + " file_content = $file-content ; ";
222 status = dblib.save(key, ctx);
229 public QueryStatus savePrepareRelationship(SvcLogicContext ctx, String prefix, String fileId, String sdcInd)
230 throws SvcLogicException {
232 QueryStatus status = null;
235 if (dblib != null && ctx != null) {
236 ctx.setAttribute("file-id", fileId);
237 if ("Y".equals(sdcInd))
239 key = "INSERT INTO PREPARE_FILE_RELATIONSHIP " + " SET service_instance_id = $service-instance-id , "
240 + " request_id = $request-id , " + " asdc_artifacts_id = $file-id ;";
242 key = "INSERT INTO PREPARE_FILE_RELATIONSHIP " + " SET service_instance_id = $service-instance-id , "
243 + " request_id = $request-id , " + " config_file_id = $file-id ;";
245 status = dblib.save(key, ctx);
247 log.info("DGGeneralDBService.savePrepareRelationship()" + ctx.getAttributeKeySet());
253 public void cleanContextPropertyByPrefix(SvcLogicContext ctx, String prefix) {
254 if (ctx != null && ctx.getAttributeKeySet() != null && StringUtils.isNotBlank(prefix)) {
256 Set<String> keySet = ctx.getAttributeKeySet();
257 for (String key : keySet) {
258 //By default, context property prefixed with dot.
259 if (StringUtils.isNotBlank(key) && key.startsWith(".")) {
260 ctx.getAttributeKeySet().remove(key);
266 public QueryStatus saveUploadConfig(SvcLogicContext ctx, String prefix) throws SvcLogicException {
268 QueryStatus status = null;
270 if (dblib != null && ctx != null) {
271 String key = "INSERT INTO UPLOAD_CONFIG " + " SET request_id = $request-id , "
272 + " action = $request-action , " + " originator_id = $originator-id , " + " vnf_id = $vnf-id , "
273 + " vnf_name = $vnf-name , " + " vm_name = $vm-name , "
274 + " host_ip_address = $vnf-host-ip-address , " + " vnf_type = $vnf-type , "
275 + " vnfc_type = $vnfc-type , " + " config_indicator = 'Current' , "
276 + " content = $tmp.escaped.devicerunningconfig ; ";
278 status = dblib.save(key, ctx);
280 log.info("DGGeneralDBService.saveUploadConfig()" + ctx.getAttributeKeySet());
287 /*public QueryStatus getMaxUploadConfigFileId(SvcLogicContext ctx, String prefix) throws SvcLogicException {
288 QueryStatus status = null;
289 if (serviceLogic != null && ctx != null) {
290 String key = "SELECT MAX(upload_config_id) uploadconfigid " + " FROM UPLOAD_CONFIG "
291 + " WHERE vnf_id = $vnf-id AND vm_name = $vm-name ; ";
293 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
294 log.info("DGGeneralDBService.getMaxUploadConfigFileId()" + ctx.getAttributeKeySet());
299 public QueryStatus updateUploadConfig(SvcLogicContext ctx, String prefix, int maxId) throws SvcLogicException {
300 QueryStatus status = null;
301 if (dblib != null && ctx != null) {
302 String key = "UPDATE UPLOAD_CONFIG " + " SET config_indicator = null "
303 + " WHERE upload_config_id != " + maxId + " AND config_indicator = 'Current' "
304 + " AND vnf_id = $vnf-id " + " AND vnfc_type = $vnfc-type ; ";
306 status = dblib.save(key, ctx);
308 log.info("DGGeneralDBService.updateUploadConfig()" + ctx.getAttributeKeySet());
316 public QueryStatus getTemplateByArtifactType(SvcLogicContext ctx, String prefix, String fileCategory, String artifactType)
317 throws SvcLogicException {
318 QueryStatus status = null;
319 if (dblib != null && ctx != null) {
320 ctx.setAttribute("file-category", fileCategory);
321 ctx.setAttribute("artifact-type", artifactType);
322 String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
323 + " FROM ASDC_ARTIFACTS "
324 + " WHERE asdc_artifacts_id = (SELECT MAX(a.asdc_artifacts_id) configfileid "
325 + " FROM ASDC_ARTIFACTS a, ASDC_REFERENCE b " + " WHERE a.artifact_name = b.artifact_name "
326 + " AND file_category = $file-category AND action = $request-action "
327 + " AND artifactType = $artifact-type AND vnf_type = $vnf-type ) ; ";
329 status = dblib.query(key, prefix, ctx);
335 public QueryStatus getConfigFilesByVnfVmNCategory(SvcLogicContext ctx, String prefix, String fileCategory, String vnfId, String vmName)
336 throws SvcLogicException {
337 QueryStatus status = null;
338 if (dblib != null && ctx != null) {
339 ctx.setAttribute("file-category", fileCategory);
340 ctx.setAttribute("vnf-id", vnfId);
341 ctx.setAttribute("vm-name", vmName);
342 String key = "SELECT file_content , config_file_id "
343 + " FROM CONFIGFILES "
344 + " WHERE config_file_id = ( SELECT MAX(config_file_id) configfileid " + " FROM CONFIGFILES "
345 + " WHERE file_category = $file-category"
346 + " AND vnf_id = $vnf-id"
347 + " AND vm_name = $vm-name ) ; ";
350 status = dblib.query(key, prefix, ctx);
356 public QueryStatus getDownloadConfigTemplateByVnf(SvcLogicContext ctx, String prefix)
357 throws SvcLogicException {
358 QueryStatus status = null;
359 if (dblib != null && ctx != null) {
360 String key = "SELECT * FROM DOWNLOAD_CONFIG_TEMPLATE WHERE vnf_type = $vnf-type ; ";
361 status = dblib.query(key, prefix, ctx);
368 public QueryStatus saveConfigTransactionLog(SvcLogicContext ctx, String prefix) throws SvcLogicException {
370 QueryStatus status = null;
372 if (dblib != null && ctx != null) {
374 String key = "INSERT INTO CONFIG_TRANSACTION_LOG " + " SET request_id = $request-id , "
375 + " message_type = $log-message-type , "
376 + " message = $log-message ;";
379 status = dblib.save(key, ctx);
388 public QueryStatus getVnfcReferenceByVnfcTypeNAction(SvcLogicContext ctx, String prefix)
389 throws SvcLogicException {
390 QueryStatus status = null;
391 if (dblib != null && ctx != null) {
393 String key = "SELECT * "
394 + " FROM VNFC_REFERENCE "
395 + " WHERE vnf_type = $vnf-type "
396 + " AND vnfc_type = $vnfc-type "
397 + " AND action = $request-action "
398 + " ORDER BY vm_instance, vnfc_instance ; ";
401 status = dblib.query(key, prefix, ctx);
407 public QueryStatus getVnfcReferenceByVnfTypeNAction(SvcLogicContext ctx, String prefix)
408 throws SvcLogicException {
409 QueryStatus status = null;
410 if (dblib != null && ctx != null) {
412 String key = "SELECT * "
413 + " FROM VNFC_REFERENCE "
414 + " WHERE vnf_type = $vnf-type "
415 + " AND action = $request-action "
416 + " ORDER BY vm_instance, vnfc_instance ; ";
418 status = dblib.query(key, prefix, ctx);
424 public QueryStatus getUploadConfigInfo(SvcLogicContext ctx, String prefix)
425 throws SvcLogicException {
426 QueryStatus status = null;
427 if (dblib != null && ctx != null) {
429 String key = "SELECT * , UNIX_TIMESTAMP(UPLOAD_DATE) UPLOAD_TIMESTAMP "
430 + " FROM UPLOAD_CONFIG "
431 + " WHERE upload_config_id = " +
432 "( SELECT MAX(upload_config_id) uploadconfigid " + " FROM UPLOAD_CONFIG "
433 + " WHERE vnf_id = $vnf-id AND vm_name = $vm-name ) ; ";
435 status = dblib.query(key, prefix, ctx);
439 public String getCapability(SvcLogicContext ctx, String vnf_type) throws SvcLogicException {
441 //{"capabilities":{"vnfc":[],"vm":[],"vf-module":[],"vnf":["ConfigureTest","ConfigModify","HealthCheck"]}}
442 String fn = "getCapability ";
443 QueryStatus status = null;
444 SvcLogicContext localContext = new SvcLogicContext();
445 localContext.setAttribute("vnf-type", vnf_type);
447 String queryString = "select max(internal_version) as maxInternalVersion, artifact_name as artifactName from ASDC_ARTIFACTS " +
448 " where artifact_name in (select artifact_name from ASDC_REFERENCE where vnf_type= $vnf-type " +
449 " and file_category = 'capability' )" ;
451 log.info(fn + "Query String : " + queryString);
452 status = dblib.query(queryString, localContext);
454 if(status.toString().equals("FAILURE"))
455 throw new SvcLogicException("Error - while getting capabilitiesData ");
457 String queryString1 = "select artifact_content from ASDC_ARTIFACTS " +
458 " where artifact_name = $artifactName and internal_version = $maxInternalVersion ";
460 log.debug(fn + "Query String : " + queryString1);
461 status = dblib.query(queryString1, localContext);
462 if (status.toString().equals("NOT_FOUND"))
464 if(status.toString().equals("FAILURE"))
465 throw new SvcLogicException("Error - while getting capabilitiesData ");
468 return localContext.getAttribute("artifact-content");
471 public QueryStatus getTemplateWithTemplateModelId(SvcLogicContext ctx, String prefix, String fileCategory,
472 String templateModelId) throws SvcLogicException {
473 QueryStatus status = null;
474 String templatePattern = "%_"+ templateModelId +"%";
475 if (dblib != null && ctx != null) {
476 ctx.setAttribute("file-category", fileCategory);
477 ctx.setAttribute("template-pattern", templatePattern);
478 String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
479 + " FROM ASDC_ARTIFACTS "
480 + " WHERE asdc_artifacts_id = ( SELECT MAX(a.asdc_artifacts_id) configfileid "
481 + " FROM ASDC_ARTIFACTS a, ASDC_REFERENCE b " + " WHERE a.artifact_name = b.artifact_name "
482 + " AND file_category = $file-category AND action = $request-action "
483 + " AND vnf_type = $vnf-type " + " AND vnfc_type = $vnfc-type ) and ASDC_ARTIFACTS.artifact_name like "
484 + "$template-pattern ; ";
485 log.info("getTemplateWithTemplateModelId()::: with template:::"+ key);
487 status = dblib.query(key, prefix, ctx);
492 public QueryStatus getTemplateByVnfTypeNActionWithTemplateModelId(SvcLogicContext ctx, String prefix,
493 String fileCategory, String templateModelId) throws SvcLogicException {
494 QueryStatus status = null;
495 String templatePattern = "%_"+ templateModelId +"%";
496 if (dblib != null && ctx != null) {
497 ctx.setAttribute("file-category", fileCategory);
498 ctx.setAttribute("template-pattern", templatePattern);
499 String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
500 + " FROM ASDC_ARTIFACTS "
501 + " WHERE asdc_artifacts_id = (SELECT MAX(a.asdc_artifacts_id) configfileid "
502 + " FROM ASDC_ARTIFACTS a, ASDC_REFERENCE b " + " WHERE a.artifact_name = b.artifact_name "
503 + " AND file_category = $file-category AND action = $request-action "
504 + " AND vnf_type = $vnf-type ) and ASDC_ARTIFACTS.artifact_name like "
505 + "$template-pattern ; ";
506 log.info("getTemplateByVnfTypeNActionWithTemplateModelId()::: with template:::"+ key);
508 status = dblib.query(key, prefix, ctx);
514 public QueryStatus getVnfcReferenceByVnfTypeNActionWithTemplateModelId(SvcLogicContext ctx, String prefix,
515 String templateModelId) throws SvcLogicException {
516 QueryStatus status = null;
517 if (dblib != null && ctx != null) {
518 ctx.setAttribute("template-model-id", templateModelId);
519 String key = "SELECT * "
520 + " FROM VNFC_REFERENCE "
521 + " WHERE vnf_type = $vnf-type "
522 + " AND action = $request-action "
523 + " AND template_id = "
524 + "$template-model-id"
525 + " ORDER BY vm_instance, vnfc_instance ; ";
527 log.info("getVnfcReferenceByVnfTypeNActionWithTemplateModelId()::: with template:::"+ key);
528 status = dblib.query(key, prefix, ctx);