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;
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 static DGGeneralDBService dgGeneralDBService = null;
45 public static DGGeneralDBService initialise() {
47 if (dgGeneralDBService == null) {
48 dgGeneralDBService = new DGGeneralDBService();
50 return dgGeneralDBService;
53 private DGGeneralDBService() {
54 if (serviceLogic == null) {
55 serviceLogic = new SqlResource();
59 protected DGGeneralDBService(SqlResource svcLogic) {
60 if (serviceLogic == null) {
61 serviceLogic = svcLogic;
65 public QueryStatus getDeviceProtocolByVnfType(SvcLogicContext ctx, String prefix) throws SvcLogicException {
66 QueryStatus status = null;
67 if (serviceLogic != null && ctx != null) {
68 String key = "SELECT * FROM DEVICE_INTERFACE_PROTOCOL WHERE vnf_type = $vnf-type ;";
69 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
74 public QueryStatus getDeviceAuthenticationByVnfType(SvcLogicContext ctx, String prefix) throws SvcLogicException {
75 QueryStatus status = null;
76 if (serviceLogic != null && ctx != null) {
77 String key = "SELECT * FROM DEVICE_AUTHENTICATION WHERE vnf_type = $vnf-type ;";
78 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
84 public QueryStatus getConfigFileReferenceByVnfType(SvcLogicContext ctx, String prefix) throws SvcLogicException {
85 QueryStatus status = null;
86 if (serviceLogic != null && ctx != null) {
87 String key = "SELECT * FROM CONFIG_FILE_REFERENCE WHERE vnf_type = $vnf-type ;";
88 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
93 public QueryStatus getConfigFileReferenceByFileTypeNVnfType(SvcLogicContext ctx, String prefix, String fileType)
94 throws SvcLogicException {
95 QueryStatus status = null;
96 if (serviceLogic != null && ctx != null) {
97 String key = "SELECT * FROM CONFIG_FILE_REFERENCE WHERE file_type = '" + fileType
98 + "' and vnf_type = $vnf-type ;";
99 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
104 public QueryStatus getTemplate(SvcLogicContext ctx, String prefix, String fileCategory) throws SvcLogicException {
105 QueryStatus status = null;
106 if (serviceLogic != null && ctx != null) {
107 String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
108 + " FROM ASDC_ARTIFACTS "
109 + " WHERE asdc_artifacts_id = ( SELECT MAX(a.asdc_artifacts_id) configfileid "
110 + " FROM ASDC_ARTIFACTS a, ASDC_REFERENCE b " + " WHERE a.artifact_name = b.artifact_name "
111 + " AND file_category = '" + fileCategory + "'" + " AND action = $request-action "
112 + " AND vnf_type = $vnf-type " + " AND vnfc_type = $vnfc-type ) ; ";
113 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
118 public QueryStatus getTemplateByVnfTypeNAction(SvcLogicContext ctx, String prefix, String fileCategory)
119 throws SvcLogicException {
120 QueryStatus status = null;
121 if (serviceLogic != null && ctx != null) {
122 String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
123 + " FROM ASDC_ARTIFACTS "
124 + " WHERE asdc_artifacts_id = (SELECT MAX(a.asdc_artifacts_id) configfileid "
125 + " FROM ASDC_ARTIFACTS a, ASDC_REFERENCE b " + " WHERE a.artifact_name = b.artifact_name "
126 + " AND file_category = '" + fileCategory + "'" + " AND action = $request-action "
127 + " AND vnf_type = $vnf-type ) ; ";
128 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
134 public QueryStatus getTemplateByVnfType(SvcLogicContext ctx, String prefix, String fileCategory)
135 throws SvcLogicException {
136 QueryStatus status = null;
137 if (serviceLogic != null && ctx != null) {
138 String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
139 + " FROM ASDC_ARTIFACTS "
140 + " WHERE asdc_artifacts_id = (SELECT MAX(a.asdc_artifacts_id) configfileid "
141 + " FROM ASDC_ARTIFACTS a, ASDC_REFERENCE b " + " WHERE a.artifact_name = b.artifact_name "
142 + " AND file_category = '" + fileCategory + "'" + " AND vnf_type = $vnf-type ) ; ";
144 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
149 public QueryStatus getTemplateByTemplateName(SvcLogicContext ctx, String prefix, String templateName)
150 throws SvcLogicException {
151 QueryStatus status = null;
152 if (serviceLogic != null && ctx != null) {
153 String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
154 + " FROM ASDC_ARTIFACTS "
155 + " WHERE asdc_artifacts_id = (SELECT MAX(asdc_artifacts_id) configfileid "
156 + " FROM ASDC_ARTIFACTS " + " WHERE artifact_name = '" + templateName + "' ) ; ";
158 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
163 public QueryStatus getConfigureActionDGByVnfTypeNAction(SvcLogicContext ctx, String prefix)
164 throws SvcLogicException {
165 QueryStatus status = null;
166 if (serviceLogic != null && ctx != null) {
167 String key = "SELECT * " + " FROM CONFIGURE_ACTION_DG "
168 + " where vnf_type = $vnf-type and action = $request-action ; ";
170 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
175 public QueryStatus getConfigureActionDGByVnfType(SvcLogicContext ctx, String prefix) throws SvcLogicException {
176 QueryStatus status = null;
177 if (serviceLogic != null && ctx != null) {
178 String key = "SELECT * " + " FROM CONFIGURE_ACTION_DG "
179 + " where vnf_type = $vnf-type and action IS NULL ; ";
181 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
186 public QueryStatus getMaxConfigFileId(SvcLogicContext ctx, String prefix, String fileCategory)
187 throws SvcLogicException {
188 QueryStatus status = null;
189 if (serviceLogic != null && ctx != null) {
190 String key = "SELECT MAX(config_file_id) configfileid " + " FROM CONFIGFILES " + " WHERE file_category = '"
191 + fileCategory + "'" + " AND vnf_id = $vnf-id AND vm_name = $vm-name ; ";
193 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
198 public QueryStatus saveConfigFiles(SvcLogicContext ctx, String prefix) throws SvcLogicException {
200 QueryStatus status = null;
202 if (serviceLogic != null && ctx != null) {
203 String key = "INSERT INTO CONFIGFILES " + " SET data_source = $data-source , "
204 + " service_instance_id = $service-instance-id ," + " action = $request-action ,"
205 + " vnf_type = $vnf-type ," + " vnfc_type = $vnfc-type ,"
206 + " vnf_id = $vnf-id , " + " vnf_name = $vnf-name ,"
207 + " vm_name = $vm-name ," + " file_category = $file-category ,"
208 + " file_content = $file-content ; ";
210 status = serviceLogic.save("SQL", false, false, key, null, prefix, ctx);
217 public QueryStatus savePrepareRelationship(SvcLogicContext ctx, String prefix, String fileId, String sdcInd)
218 throws SvcLogicException {
220 QueryStatus status = null;
223 if (serviceLogic != null && ctx != null) {
225 if ("Y".equals(sdcInd))
227 key = "INSERT INTO PREPARE_FILE_RELATIONSHIP " + " SET service_instance_id = $service-instance-id , "
228 + " request_id = $request-id , " + " asdc_artifacts_id = " + fileId + " ;";
230 key = "INSERT INTO PREPARE_FILE_RELATIONSHIP " + " SET service_instance_id = $service-instance-id , "
231 + " request_id = $request-id , " + " config_file_id = " + fileId + " ;";
233 status = serviceLogic.save("SQL", false, false, key, null, prefix, ctx);
235 log.info("DGGeneralDBService.savePrepareRelationship()" + ctx.getAttributeKeySet());
241 public void cleanContextPropertyByPrefix(SvcLogicContext ctx, String prefix) {
242 if (ctx != null && ctx.getAttributeKeySet() != null && StringUtils.isNotBlank(prefix)) {
244 Set<String> keySet = ctx.getAttributeKeySet();
245 for (String key : keySet) {
246 //By default, context property prefixed with dot.
247 if (StringUtils.isNotBlank(key) && key.startsWith(".")) {
248 ctx.getAttributeKeySet().remove(key);
254 public QueryStatus saveUploadConfig(SvcLogicContext ctx, String prefix) throws SvcLogicException {
256 QueryStatus status = null;
258 if (serviceLogic != null && ctx != null) {
259 String key = "INSERT INTO UPLOAD_CONFIG " + " SET request_id = $request-id , "
260 + " action = $request-action , " + " originator_id = $originator-id , " + " vnf_id = $vnf-id , "
261 + " vnf_name = $vnf-name , " + " vm_name = $vm-name , "
262 + " host_ip_address = $vnf-host-ip-address , " + " vnf_type = $vnf-type , "
263 + " vnfc_type = $vnfc-type , " + " config_indicator = 'Current' , "
264 + " content = $tmp.escaped.devicerunningconfig ; ";
266 status = serviceLogic.save("SQL", false, false, key, null, prefix, ctx);
268 log.info("DGGeneralDBService.saveUploadConfig()" + ctx.getAttributeKeySet());
275 /*public QueryStatus getMaxUploadConfigFileId(SvcLogicContext ctx, String prefix) throws SvcLogicException {
276 QueryStatus status = null;
277 if (serviceLogic != null && ctx != null) {
278 String key = "SELECT MAX(upload_config_id) uploadconfigid " + " FROM UPLOAD_CONFIG "
279 + " WHERE vnf_id = $vnf-id AND vm_name = $vm-name ; ";
281 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
282 log.info("DGGeneralDBService.getMaxUploadConfigFileId()" + ctx.getAttributeKeySet());
287 public QueryStatus updateUploadConfig(SvcLogicContext ctx, String prefix, int maxId) throws SvcLogicException {
288 QueryStatus status = null;
289 if (serviceLogic != null && ctx != null) {
290 String key = "UPDATE UPLOAD_CONFIG " + " SET config_indicator = null "
291 + " WHERE upload_config_id != " + maxId + " AND config_indicator = 'Current' "
292 + " AND vnf_id = $vnf-id " + " AND vnfc_type = $vnfc-type ; ";
294 status = serviceLogic.save("SQL", false, false, key, null, prefix, ctx);
296 log.info("DGGeneralDBService.updateUploadConfig()" + ctx.getAttributeKeySet());
304 public QueryStatus getTemplateByArtifactType(SvcLogicContext ctx, String prefix, String fileCategory, String artifactType)
305 throws SvcLogicException {
306 QueryStatus status = null;
307 if (serviceLogic != null && ctx != null) {
308 String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
309 + " FROM ASDC_ARTIFACTS "
310 + " WHERE asdc_artifacts_id = (SELECT MAX(a.asdc_artifacts_id) configfileid "
311 + " FROM ASDC_ARTIFACTS a, ASDC_REFERENCE b " + " WHERE a.artifact_name = b.artifact_name "
312 + " AND file_category = '" + fileCategory + "'" + " AND action = $request-action "
313 + " AND artifactType = '" + artifactType + "'" + " AND vnf_type = $vnf-type ) ; ";
315 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
321 public QueryStatus getConfigFilesByVnfVmNCategory(SvcLogicContext ctx, String prefix, String fileCategory, String vnfId, String vmName)
322 throws SvcLogicException {
323 QueryStatus status = null;
324 if (serviceLogic != null && ctx != null) {
326 String key = "SELECT file_content , config_file_id "
327 + " FROM CONFIGFILES "
328 + " WHERE config_file_id = ( SELECT MAX(config_file_id) configfileid " + " FROM CONFIGFILES "
329 + " WHERE file_category = '" + fileCategory + "'"
330 + " AND vnf_id = '" + vnfId + "'"
331 + " AND vm_name = '" + vmName + "' ) ; ";
334 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
340 public QueryStatus getDownloadConfigTemplateByVnf(SvcLogicContext ctx, String prefix)
341 throws SvcLogicException {
342 QueryStatus status = null;
343 if (serviceLogic != null && ctx != null) {
344 String key = "SELECT * FROM DOWNLOAD_CONFIG_TEMPLATE WHERE vnf_type = $vnf-type ; ";
345 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
352 public QueryStatus saveConfigTransactionLog(SvcLogicContext ctx, String prefix) throws SvcLogicException {
354 QueryStatus status = null;
356 if (serviceLogic != null && ctx != null) {
359 String key = "INSERT INTO CONFIG_TRANSACTION_LOG " + " SET request_id = $request-id , "
360 + " message_type = $log-message-type , "
361 + " message = $log-message ;";
364 status = serviceLogic.save("SQL", false, false, key, null, prefix, ctx);
374 public QueryStatus getVnfcReferenceByVnfcTypeNAction(SvcLogicContext ctx, String prefix)
375 throws SvcLogicException {
376 QueryStatus status = null;
377 if (serviceLogic != null && ctx != null) {
379 String key = "SELECT * "
380 + " FROM VNFC_REFERENCE "
381 + " WHERE vnf_type = $vnf-type "
382 + " AND vnfc_type = $vnfc-type "
383 + " AND action = $request-action "
384 + " ORDER BY vm_instance, vnfc_instance ; ";
387 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
393 public QueryStatus getVnfcReferenceByVnfTypeNAction(SvcLogicContext ctx, String prefix)
394 throws SvcLogicException {
395 QueryStatus status = null;
396 if (serviceLogic != null && ctx != null) {
398 String key = "SELECT * "
399 + " FROM VNFC_REFERENCE "
400 + " WHERE vnf_type = $vnf-type "
401 + " AND action = $request-action "
402 + " ORDER BY vm_instance, vnfc_instance ; ";
404 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
410 public QueryStatus getUploadConfigInfo(SvcLogicContext ctx, String prefix)
411 throws SvcLogicException {
412 QueryStatus status = null;
413 if (serviceLogic != null && ctx != null) {
415 String key = "SELECT * , UNIX_TIMESTAMP(UPLOAD_DATE) UPLOAD_TIMESTAMP "
416 + " FROM UPLOAD_CONFIG "
417 + " WHERE upload_config_id = " +
418 "( SELECT MAX(upload_config_id) uploadconfigid " + " FROM UPLOAD_CONFIG "
419 + " WHERE vnf_id = $vnf-id AND vm_name = $vm-name ) ; ";
421 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
425 public String getCapability(SvcLogicContext ctx, String vnf_type) throws SvcLogicException {
427 //{"capabilities":{"vnfc":[],"vm":[],"vf-module":[],"vnf":["ConfigureTest","ConfigModify","HealthCheck"]}}
428 String fn = "getCapability ";
429 QueryStatus status = null;
430 SvcLogicContext localContext = new SvcLogicContext();
431 localContext.setAttribute("vnf-type", vnf_type);
432 if (serviceLogic != null) {
433 String queryString = "select max(internal_version) as maxInternalVersion, artifact_name as artifactName from ASDC_ARTIFACTS " +
434 " where artifact_name in (select artifact_name from ASDC_REFERENCE where vnf_type= $vnf-type " +
435 " and file_category = 'capability' )" ;
437 log.info(fn + "Query String : " + queryString);
438 status = serviceLogic.query("SQL", false, null, queryString, null, null, localContext);
440 if(status.toString().equals("FAILURE"))
441 throw new SvcLogicException("Error - while getting capabilitiesData ");
443 String queryString1 = "select artifact_content from ASDC_ARTIFACTS " +
444 " where artifact_name = $artifactName and internal_version = $maxInternalVersion ";
446 log.debug(fn + "Query String : " + queryString1);
447 status = serviceLogic.query("SQL", false, null, queryString1, null, null, localContext);
448 if (status.toString().equals("NOT_FOUND"))
450 if(status.toString().equals("FAILURE"))
451 throw new SvcLogicException("Error - while getting capabilitiesData ");
454 return localContext.getAttribute("artifact-content");
457 public QueryStatus getTemplateWithTemplateModelId(SvcLogicContext ctx, String prefix, String fileCategory,
458 String templateModelId) throws SvcLogicException {
459 QueryStatus status = null;
460 String templatePattern = "'%_"+ templateModelId +"%'";
461 if (serviceLogic != null && ctx != null) {
462 String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
463 + " FROM ASDC_ARTIFACTS "
464 + " WHERE asdc_artifacts_id = ( SELECT MAX(a.asdc_artifacts_id) configfileid "
465 + " FROM ASDC_ARTIFACTS a, ASDC_REFERENCE b " + " WHERE a.artifact_name = b.artifact_name "
466 + " AND file_category = '" + fileCategory + "'" + " AND action = $request-action "
467 + " AND vnf_type = $vnf-type " + " AND vnfc_type = $vnfc-type ) and ASDC_ARTIFACTS.artifact_name like "
468 + templatePattern + "; ";
469 log.info("getTemplateWithTemplateModelId()::: with template:::"+ key);
471 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
476 public QueryStatus getTemplateByVnfTypeNActionWithTemplateModelId(SvcLogicContext ctx, String prefix,
477 String fileCategory, String templateModelId) throws SvcLogicException {
478 QueryStatus status = null;
479 String templatePattern = "'%_"+ templateModelId +"%'";
480 if (serviceLogic != null && ctx != null) {
481 String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
482 + " FROM ASDC_ARTIFACTS "
483 + " WHERE asdc_artifacts_id = (SELECT MAX(a.asdc_artifacts_id) configfileid "
484 + " FROM ASDC_ARTIFACTS a, ASDC_REFERENCE b " + " WHERE a.artifact_name = b.artifact_name "
485 + " AND file_category = '" + fileCategory + "'" + " AND action = $request-action "
486 + " AND vnf_type = $vnf-type ) and ASDC_ARTIFACTS.artifact_name like "
487 + templatePattern + "; ";
488 log.info("getTemplateByVnfTypeNActionWithTemplateModelId()::: with template:::"+ key);
490 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
496 public QueryStatus getVnfcReferenceByVnfTypeNActionWithTemplateModelId(SvcLogicContext ctx, String prefix,
497 String templateModelId) throws SvcLogicException {
498 QueryStatus status = null;
499 if (serviceLogic != null && ctx != null) {
501 String key = "SELECT * "
502 + " FROM VNFC_REFERENCE "
503 + " WHERE vnf_type = $vnf-type "
504 + " AND action = $request-action "
505 + " AND template_id = '"
506 + templateModelId + "'"
507 + " ORDER BY vm_instance, vnfc_instance ; ";
509 log.info("getVnfcReferenceByVnfTypeNActionWithTemplateModelId()::: with template:::"+ key);
510 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);