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 * ============LICENSE_END=========================================================
23 package org.onap.appc.data.services.db;
27 import org.apache.commons.lang3.StringUtils;
29 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
30 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
31 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
33 import org.onap.ccsdk.sli.adaptors.resource.sql.SqlResource;
34 import com.att.eelf.configuration.EELFLogger;
35 import com.att.eelf.configuration.EELFManager;
37 public class DGGeneralDBService {
39 private static final EELFLogger log = EELFManager.getInstance().getLogger(DGGeneralDBService.class);
40 private SvcLogicResource serviceLogic;
41 private static DGGeneralDBService dgGeneralDBService = null;
43 public static DGGeneralDBService initialise() {
45 if (dgGeneralDBService == null) {
46 dgGeneralDBService = new DGGeneralDBService();
48 return dgGeneralDBService;
51 private DGGeneralDBService() {
52 if (serviceLogic == null) {
53 serviceLogic = new SqlResource();
57 protected DGGeneralDBService(SqlResource svcLogic) {
58 if (serviceLogic == null) {
59 serviceLogic = svcLogic;
63 public QueryStatus getDeviceProtocolByVnfType(SvcLogicContext ctx, String prefix) throws SvcLogicException {
64 QueryStatus status = null;
65 if (serviceLogic != null && ctx != null) {
66 String key = "SELECT * FROM DEVICE_INTERFACE_PROTOCOL WHERE vnf_type = $vnf-type ;";
67 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
72 public QueryStatus getDeviceAuthenticationByVnfType(SvcLogicContext ctx, String prefix) throws SvcLogicException {
73 QueryStatus status = null;
74 if (serviceLogic != null && ctx != null) {
75 String key = "SELECT * FROM DEVICE_AUTHENTICATION WHERE vnf_type = $vnf-type ;";
76 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
82 public QueryStatus getConfigFileReferenceByVnfType(SvcLogicContext ctx, String prefix) throws SvcLogicException {
83 QueryStatus status = null;
84 if (serviceLogic != null && ctx != null) {
85 String key = "SELECT * FROM CONFIG_FILE_REFERENCE WHERE vnf_type = $vnf-type ;";
86 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
91 public QueryStatus getConfigFileReferenceByFileTypeNVnfType(SvcLogicContext ctx, String prefix, String fileType)
92 throws SvcLogicException {
93 QueryStatus status = null;
94 if (serviceLogic != null && ctx != null) {
95 String key = "SELECT * FROM CONFIG_FILE_REFERENCE WHERE file_type = '" + fileType
96 + "' and vnf_type = $vnf-type ;";
97 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
102 public QueryStatus getTemplate(SvcLogicContext ctx, String prefix, String fileCategory) throws SvcLogicException {
103 QueryStatus status = null;
104 if (serviceLogic != null && ctx != null) {
105 String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
106 + " FROM ASDC_ARTIFACTS "
107 + " WHERE asdc_artifacts_id = ( SELECT MAX(a.asdc_artifacts_id) configfileid "
108 + " FROM ASDC_ARTIFACTS a, ASDC_REFERENCE b " + " WHERE a.artifact_name = b.artifact_name "
109 + " AND file_category = '" + fileCategory + "'" + " AND action = $request-action "
110 + " AND vnf_type = $vnf-type " + " AND vnfc_type = $vnfc-type ) ; ";
111 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
116 public QueryStatus getTemplateByVnfTypeNAction(SvcLogicContext ctx, String prefix, String fileCategory)
117 throws SvcLogicException {
118 QueryStatus status = null;
119 if (serviceLogic != null && ctx != null) {
120 String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
121 + " FROM ASDC_ARTIFACTS "
122 + " WHERE asdc_artifacts_id = (SELECT MAX(a.asdc_artifacts_id) configfileid "
123 + " FROM ASDC_ARTIFACTS a, ASDC_REFERENCE b " + " WHERE a.artifact_name = b.artifact_name "
124 + " AND file_category = '" + fileCategory + "'" + " AND action = $request-action "
125 + " AND vnf_type = $vnf-type ) ; ";
126 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
132 public QueryStatus getTemplateByVnfType(SvcLogicContext ctx, String prefix, String fileCategory)
133 throws SvcLogicException {
134 QueryStatus status = null;
135 if (serviceLogic != null && ctx != null) {
136 String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
137 + " FROM ASDC_ARTIFACTS "
138 + " WHERE asdc_artifacts_id = (SELECT MAX(a.asdc_artifacts_id) configfileid "
139 + " FROM ASDC_ARTIFACTS a, ASDC_REFERENCE b " + " WHERE a.artifact_name = b.artifact_name "
140 + " AND file_category = '" + fileCategory + "'" + " AND vnf_type = $vnf-type ) ; ";
142 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
147 public QueryStatus getTemplateByTemplateName(SvcLogicContext ctx, String prefix, String templateName)
148 throws SvcLogicException {
149 QueryStatus status = null;
150 if (serviceLogic != null && ctx != null) {
151 String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
152 + " FROM ASDC_ARTIFACTS "
153 + " WHERE asdc_artifacts_id = (SELECT MAX(asdc_artifacts_id) configfileid "
154 + " FROM ASDC_ARTIFACTS " + " WHERE artifact_name = '" + templateName + "' ) ; ";
156 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
161 public QueryStatus getConfigureActionDGByVnfTypeNAction(SvcLogicContext ctx, String prefix)
162 throws SvcLogicException {
163 QueryStatus status = null;
164 if (serviceLogic != null && ctx != null) {
165 String key = "SELECT * " + " FROM CONFIGURE_ACTION_DG "
166 + " where vnf_type = $vnf-type and action = $request-action ; ";
168 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
173 public QueryStatus getConfigureActionDGByVnfType(SvcLogicContext ctx, String prefix) throws SvcLogicException {
174 QueryStatus status = null;
175 if (serviceLogic != null && ctx != null) {
176 String key = "SELECT * " + " FROM CONFIGURE_ACTION_DG "
177 + " where vnf_type = $vnf-type and action IS NULL ; ";
179 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
184 public QueryStatus getMaxConfigFileId(SvcLogicContext ctx, String prefix, String fileCategory)
185 throws SvcLogicException {
186 QueryStatus status = null;
187 if (serviceLogic != null && ctx != null) {
188 String key = "SELECT MAX(config_file_id) configfileid " + " FROM CONFIGFILES " + " WHERE file_category = '"
189 + fileCategory + "'" + " AND vnf_id = $vnf-id AND vm_name = $vm-name ; ";
191 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
196 public QueryStatus saveConfigFiles(SvcLogicContext ctx, String prefix) throws SvcLogicException {
198 QueryStatus status = null;
200 if (serviceLogic != null && ctx != null) {
201 String key = "INSERT INTO CONFIGFILES " + " SET data_source = $data-source , "
202 + " service_instance_id = $service-instance-id ," + " action = $request-action ,"
203 + " vnf_type = $vnf-type ," + " vnfc_type = $vnfc-type ,"
204 + " vnf_id = $vnf-id , " + " vnf_name = $vnf-name ,"
205 + " vm_name = $vm-name ," + " file_category = $file-category ,"
206 + " file_content = $file-content ; ";
208 status = serviceLogic.save("SQL", false, false, key, null, prefix, ctx);
215 public QueryStatus savePrepareRelationship(SvcLogicContext ctx, String prefix, String fileId, String sdcInd)
216 throws SvcLogicException {
218 QueryStatus status = null;
221 if (serviceLogic != null && ctx != null) {
223 if ("Y".equals(sdcInd))
225 key = "INSERT INTO PREPARE_FILE_RELATIONSHIP " + " SET service_instance_id = $service-instance-id , "
226 + " request_id = $request-id , " + " asdc_artifacts_id = " + fileId + " ;";
228 key = "INSERT INTO PREPARE_FILE_RELATIONSHIP " + " SET service_instance_id = $service-instance-id , "
229 + " request_id = $request-id , " + " config_file_id = " + fileId + " ;";
231 status = serviceLogic.save("SQL", false, false, key, null, prefix, ctx);
233 log.info("DGGeneralDBService.savePrepareRelationship()" + ctx.getAttributeKeySet());
239 public void cleanContextPropertyByPrefix(SvcLogicContext ctx, String prefix) {
240 if (ctx != null && ctx.getAttributeKeySet() != null && StringUtils.isNotBlank(prefix)) {
242 Set<String> keySet = ctx.getAttributeKeySet();
243 for (String key : keySet) {
244 if (StringUtils.isNotBlank(key) && key.startsWith(prefix = ".")) {
245 ctx.getAttributeKeySet().remove(key);
251 public QueryStatus saveUploadConfig(SvcLogicContext ctx, String prefix) throws SvcLogicException {
253 QueryStatus status = null;
255 if (serviceLogic != null && ctx != null) {
256 String key = "INSERT INTO UPLOAD_CONFIG " + " SET request_id = $request-id , "
257 + " action = $request-action , " + " originator_id = $originator-id , " + " vnf_id = $vnf-id , "
258 + " vnf_name = $vnf-name , " + " vm_name = $vm-name , "
259 + " host_ip_address = $vnf-host-ip-address , " + " vnf_type = $vnf-type , "
260 + " vnfc_type = $vnfc-type , " + " config_indicator = 'Current' , "
261 + " content = $tmp.escaped.devicerunningconfig ; ";
263 status = serviceLogic.save("SQL", false, false, key, null, prefix, ctx);
265 log.info("DGGeneralDBService.saveUploadConfig()" + ctx.getAttributeKeySet());
272 /*public QueryStatus getMaxUploadConfigFileId(SvcLogicContext ctx, String prefix) throws SvcLogicException {
273 QueryStatus status = null;
274 if (serviceLogic != null && ctx != null) {
275 String key = "SELECT MAX(upload_config_id) uploadconfigid " + " FROM UPLOAD_CONFIG "
276 + " WHERE vnf_id = $vnf-id AND vm_name = $vm-name ; ";
278 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
279 log.info("DGGeneralDBService.getMaxUploadConfigFileId()" + ctx.getAttributeKeySet());
284 public QueryStatus updateUploadConfig(SvcLogicContext ctx, String prefix, int maxId) throws SvcLogicException {
285 QueryStatus status = null;
286 if (serviceLogic != null && ctx != null) {
287 String key = "UPDATE UPLOAD_CONFIG " + " SET config_indicator = null "
288 + " WHERE upload_config_id != " + maxId + " AND config_indicator = 'Current' "
289 + " AND vnf_id = $vnf-id " + " AND vm_name = $vm-name ; ";
291 status = serviceLogic.save("SQL", false, false, key, null, prefix, ctx);
293 log.info("DGGeneralDBService.updateUploadConfig()" + ctx.getAttributeKeySet());
301 public QueryStatus getTemplateByArtifactType(SvcLogicContext ctx, String prefix, String fileCategory, String artifactType)
302 throws SvcLogicException {
303 QueryStatus status = null;
304 if (serviceLogic != null && ctx != null) {
305 String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
306 + " FROM ASDC_ARTIFACTS "
307 + " WHERE asdc_artifacts_id = (SELECT MAX(a.asdc_artifacts_id) configfileid "
308 + " FROM ASDC_ARTIFACTS a, ASDC_REFERENCE b " + " WHERE a.artifact_name = b.artifact_name "
309 + " AND file_category = '" + fileCategory + "'" + " AND action = $request-action "
310 + " AND artifactType = '" + artifactType + "'" + " AND vnf_type = $vnf-type ) ; ";
312 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
318 public QueryStatus getConfigFilesByVnfVmNCategory(SvcLogicContext ctx, String prefix, String fileCategory, String vnfId, String vmName)
319 throws SvcLogicException {
320 QueryStatus status = null;
321 if (serviceLogic != null && ctx != null) {
323 String key = "SELECT file_content , config_file_id "
324 + " FROM CONFIGFILES "
325 + " WHERE config_file_id = ( SELECT MAX(config_file_id) configfileid " + " FROM CONFIGFILES "
326 + " WHERE file_category = '" + fileCategory + "'"
327 + " AND vnf_id = '" + vnfId + "'"
328 + " AND vm_name = '" + vmName + "' ) ; ";
331 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
337 public QueryStatus getDownloadConfigTemplateByVnf(SvcLogicContext ctx, String prefix)
338 throws SvcLogicException {
339 QueryStatus status = null;
340 if (serviceLogic != null && ctx != null) {
341 String key = "SELECT * FROM DOWNLOAD_CONFIG_TEMPLATE WHERE vnf_type = $vnf-type ; ";
342 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
349 public QueryStatus saveConfigTransactionLog(SvcLogicContext ctx, String prefix) throws SvcLogicException {
351 QueryStatus status = null;
353 if (serviceLogic != null && ctx != null) {
356 String key = "INSERT INTO CONFIG_TRANSACTION_LOG " + " SET request_id = $request-id , "
357 + " message_type = $log-message-type , "
358 + " message = $log-message ;";
361 status = serviceLogic.save("SQL", false, false, key, null, prefix, ctx);
371 public QueryStatus getVnfcReferenceByVnfcTypeNAction(SvcLogicContext ctx, String prefix)
372 throws SvcLogicException {
373 QueryStatus status = null;
374 if (serviceLogic != null && ctx != null) {
376 String key = "SELECT * "
377 + " FROM VNFC_REFERENCE "
378 + " WHERE vnf_type = $vnf-type "
379 + " AND vnfc_type = $vnfc-type "
380 + " AND action = $request-action "
381 + " ORDER BY vm_instance, vnfc_instance ; ";
384 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
390 public QueryStatus getVnfcReferenceByVnfTypeNAction(SvcLogicContext ctx, String prefix)
391 throws SvcLogicException {
392 QueryStatus status = null;
393 if (serviceLogic != null && ctx != null) {
395 String key = "SELECT * "
396 + " FROM VNFC_REFERENCE "
397 + " WHERE vnf_type = $vnf-type "
398 + " AND action = $request-action "
399 + " ORDER BY vm_instance, vnfc_instance ; ";
401 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
407 public QueryStatus getUploadConfigInfo(SvcLogicContext ctx, String prefix)
408 throws SvcLogicException {
409 QueryStatus status = null;
410 if (serviceLogic != null && ctx != null) {
412 String key = "SELECT * , UNIX_TIMESTAMP(UPLOAD_DATE) UPLOAD_TIMESTAMP "
413 + " FROM UPLOAD_CONFIG "
414 + " WHERE upload_config_id = " +
415 "( SELECT MAX(upload_config_id) uploadconfigid " + " FROM UPLOAD_CONFIG "
416 + " WHERE vnf_id = $vnf-id AND vm_name = $vm-name ) ; ";
418 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
422 public String getCapability(SvcLogicContext ctx, String vnf_type) throws SvcLogicException {
424 //{"capabilities":{"vnfc":[],"vm":[],"vf-module":[],"vnf":["ConfigureTest","ConfigModify","HealthCheck"]}}
425 String fn = "getCapability ";
426 QueryStatus status = null;
427 SvcLogicContext localContext = new SvcLogicContext();
428 localContext.setAttribute("vnf-type", vnf_type);
429 if (serviceLogic != null) {
430 String queryString = "select max(internal_version) as maxInternalVersion, artifact_name as artifactName from ASDC_ARTIFACTS " +
431 " where artifact_name in (select artifact_name from ASDC_REFERENCE where vnf_type= $vnf-type " +
432 " and file_category = 'capability' )" ;
434 log.info(fn + "Query String : " + queryString);
435 status = serviceLogic.query("SQL", false, null, queryString, null, null, localContext);
437 if(status.toString().equals("FAILURE"))
438 throw new SvcLogicException("Error - while getting capabilitiesData ");
440 String queryString1 = "select artifact_content from ASDC_ARTIFACTS " +
441 " where artifact_name = $artifactName and internal_version = $maxInternalVersion ";
443 log.debug(fn + "Query String : " + queryString1);
444 status = serviceLogic.query("SQL", false, null, queryString1, null, null, localContext);
445 if (status.toString().equals("NOT_FOUND"))
447 if(status.toString().equals("FAILURE"))
448 throw new SvcLogicException("Error - while getting capabilitiesData ");
451 return localContext.getAttribute("artifact-content");
454 public QueryStatus getTemplateWithTemplateModelId(SvcLogicContext ctx, String prefix, String fileCategory,
455 String templateModelId) throws SvcLogicException {
456 QueryStatus status = null;
457 String templatePattern = "'%_"+ templateModelId +"%'";
458 if (serviceLogic != null && ctx != null) {
459 String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
460 + " FROM ASDC_ARTIFACTS "
461 + " WHERE asdc_artifacts_id = ( SELECT MAX(a.asdc_artifacts_id) configfileid "
462 + " FROM ASDC_ARTIFACTS a, ASDC_REFERENCE b " + " WHERE a.artifact_name = b.artifact_name "
463 + " AND file_category = '" + fileCategory + "'" + " AND action = $request-action "
464 + " AND vnf_type = $vnf-type " + " AND vnfc_type = $vnfc-type ) and ASDC_ARTIFACTS.artifact_name like "
465 + templatePattern + "; ";
466 log.info("getTemplateWithTemplateModelId()::: with template:::"+ key);
468 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
473 public QueryStatus getTemplateByVnfTypeNActionWithTemplateModelId(SvcLogicContext ctx, String prefix,
474 String fileCategory, String templateModelId) throws SvcLogicException {
475 QueryStatus status = null;
476 String templatePattern = "'%_"+ templateModelId +"%'";
477 if (serviceLogic != null && ctx != null) {
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 = '" + fileCategory + "'" + " AND action = $request-action "
483 + " AND vnf_type = $vnf-type ) and ASDC_ARTIFACTS.artifact_name like "
484 + templatePattern + "; ";
485 log.info("getTemplateByVnfTypeNActionWithTemplateModelId()::: with template:::"+ key);
487 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
493 public QueryStatus getVnfcReferenceByVnfTypeNActionWithTemplateModelId(SvcLogicContext ctx, String prefix,
494 String templateModelId) throws SvcLogicException {
495 QueryStatus status = null;
496 if (serviceLogic != null && ctx != null) {
498 String key = "SELECT * "
499 + " FROM VNFC_REFERENCE "
500 + " WHERE vnf_type = $vnf-type "
501 + " AND action = $request-action "
502 + " AND template_id = '"
503 + templateModelId + "'"
504 + " ORDER BY vm_instance, vnfc_instance ; ";
506 log.info("getVnfcReferenceByVnfTypeNActionWithTemplateModelId()::: with template:::"+ key);
507 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);