2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.appc.data.services.db;
25 import org.apache.commons.lang3.StringUtils;
27 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
28 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
29 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
30 import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
31 import org.onap.ccsdk.sli.adaptors.resource.sql.SqlResource;
32 import com.att.eelf.configuration.EELFLogger;
33 import com.att.eelf.configuration.EELFManager;
35 public class DGGeneralDBService {
37 private static final EELFLogger log = EELFManager.getInstance().getLogger(DGGeneralDBService.class);
38 private SvcLogicResource serviceLogic;
39 private static DGGeneralDBService dgGeneralDBService = null;
41 public static DGGeneralDBService initialise() {
42 if (dgGeneralDBService == null) {
43 dgGeneralDBService = new DGGeneralDBService();
45 return dgGeneralDBService;
48 private DGGeneralDBService() {
49 if (serviceLogic == null) {
50 serviceLogic = new SqlResource();
54 public QueryStatus getDeviceProtocolByVnfType(SvcLogicContext ctx, String prefix) throws SvcLogicException {
55 QueryStatus status = null;
56 if (serviceLogic != null && ctx != null) {
57 String key = "SELECT * FROM DEVICE_INTERFACE_PROTOCOL WHERE vnf_type = $vnf-type ;";
58 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
63 public QueryStatus getDeviceAuthenticationByVnfType(SvcLogicContext ctx, String prefix) throws SvcLogicException {
64 QueryStatus status = null;
65 if (serviceLogic != null && ctx != null) {
66 String key = "SELECT * FROM DEVICE_AUTHENTICATION WHERE vnf_type = $vnf-type ;";
67 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
73 public QueryStatus getConfigFileReferenceByVnfType(SvcLogicContext ctx, String prefix) throws SvcLogicException {
74 QueryStatus status = null;
75 if (serviceLogic != null && ctx != null) {
76 String key = "SELECT * FROM CONFIG_FILE_REFERENCE WHERE vnf_type = $vnf-type ;";
77 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
82 public QueryStatus getConfigFileReferenceByFileTypeNVnfType(SvcLogicContext ctx, String prefix, String fileType)
83 throws SvcLogicException {
84 QueryStatus status = null;
85 if (serviceLogic != null && ctx != null) {
86 String key = "SELECT * FROM CONFIG_FILE_REFERENCE WHERE file_type = '" + fileType
87 + "' and vnf_type = $vnf-type ;";
88 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
93 public QueryStatus getTemplate(SvcLogicContext ctx, String prefix, String fileCategory) throws SvcLogicException {
94 QueryStatus status = null;
95 if (serviceLogic != null && ctx != null) {
96 String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
97 + " FROM ASDC_ARTIFACTS "
98 + " WHERE asdc_artifacts_id = ( SELECT MAX(a.asdc_artifacts_id) configfileid "
99 + " FROM ASDC_ARTIFACTS a, ASDC_REFERENCE b " + " WHERE a.artifact_name = b.artifact_name "
100 + " AND file_category = '" + fileCategory + "'" + " AND action = $request-action "
101 + " AND vnf_type = $vnf-type " + " AND vnfc_type = $vnfc-type ) ; ";
102 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
107 public QueryStatus getTemplateByVnfTypeNAction(SvcLogicContext ctx, String prefix, String fileCategory)
108 throws SvcLogicException {
109 QueryStatus status = null;
110 if (serviceLogic != null && ctx != null) {
111 String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
112 + " FROM ASDC_ARTIFACTS "
113 + " WHERE asdc_artifacts_id = (SELECT MAX(a.asdc_artifacts_id) configfileid "
114 + " FROM ASDC_ARTIFACTS a, ASDC_REFERENCE b " + " WHERE a.artifact_name = b.artifact_name "
115 + " AND file_category = '" + fileCategory + "'" + " AND action = $request-action "
116 + " AND vnf_type = $vnf-type ) ; ";
118 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
123 public QueryStatus getTemplateByVnfType(SvcLogicContext ctx, String prefix, String fileCategory)
124 throws SvcLogicException {
125 QueryStatus status = null;
126 if (serviceLogic != null && ctx != null) {
127 String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
128 + " FROM ASDC_ARTIFACTS "
129 + " WHERE asdc_artifacts_id = (SELECT MAX(a.asdc_artifacts_id) configfileid "
130 + " FROM ASDC_ARTIFACTS a, ASDC_REFERENCE b " + " WHERE a.artifact_name = b.artifact_name "
131 + " AND file_category = '" + fileCategory + "'" + " AND vnf_type = $vnf-type ) ; ";
133 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
138 public QueryStatus getTemplateByTemplateName(SvcLogicContext ctx, String prefix, String templateName)
139 throws SvcLogicException {
140 QueryStatus status = null;
141 if (serviceLogic != null && ctx != null) {
142 String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
143 + " FROM ASDC_ARTIFACTS "
144 + " WHERE asdc_artifacts_id = (SELECT MAX(asdc_artifacts_id) configfileid "
145 + " FROM ASDC_ARTIFACTS " + " WHERE artifact_name = '" + templateName + "' ) ; ";
147 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
152 public QueryStatus getConfigureActionDGByVnfTypeNAction(SvcLogicContext ctx, String prefix)
153 throws SvcLogicException {
154 QueryStatus status = null;
155 if (serviceLogic != null && ctx != null) {
156 String key = "SELECT * " + " FROM CONFIGURE_ACTION_DG "
157 + " where vnf_type = $vnf-type and action = $request-action ; ";
159 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
164 public QueryStatus getConfigureActionDGByVnfType(SvcLogicContext ctx, String prefix) 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 IS NULL ; ";
170 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
175 public QueryStatus getMaxConfigFileId(SvcLogicContext ctx, String prefix, String fileCategory)
176 throws SvcLogicException {
177 QueryStatus status = null;
178 if (serviceLogic != null && ctx != null) {
179 String key = "SELECT MAX(config_file_id) configfileid " + " FROM CONFIGFILES " + " WHERE file_category = '"
180 + fileCategory + "'" + " AND vnf_id = $vnf-id AND vm_name = $vm-name ; ";
182 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
187 public QueryStatus saveConfigFiles(SvcLogicContext ctx, String prefix) throws SvcLogicException {
189 QueryStatus status = null;
191 if (serviceLogic != null && ctx != null) {
192 String key = "INSERT INTO CONFIGFILES " + " SET data_source = $data-source , "
193 + " service_instance_id = $service-instance-id ," + " action = $request-action ,"
194 + " vnf_type = $vnf-type ," + " vnfc_type = $vnfc-type ,"
195 + " vnf_id = $vnf-id , " + " vnf_name = $vnf-name ,"
196 + " vm_name = $vm-name ," + " file_category = $file-category ,"
197 + " file_content = $file-content ; ";
199 status = serviceLogic.save("SQL", false, false, key, null, prefix, ctx);
206 public QueryStatus savePrepareRelationship(SvcLogicContext ctx, String prefix, String fileId, String sdcInd)
207 throws SvcLogicException {
209 QueryStatus status = null;
212 if (serviceLogic != null && ctx != null) {
214 if ("Y".equals(sdcInd))
216 key = "INSERT INTO PREPARE_FILE_RELATIONSHIP " + " SET service_instance_id = $service-instance-id , "
217 + " request_id = $request-id , " + " asdc_artifacts_id = " + fileId + " ;";
219 key = "INSERT INTO PREPARE_FILE_RELATIONSHIP " + " SET service_instance_id = $service-instance-id , "
220 + " request_id = $request-id , " + " config_file_id = " + fileId + " ;";
222 status = serviceLogic.save("SQL", false, false, key, null, prefix, ctx);
224 log.info("DGGeneralDBService.savePrepareRelationship()" + ctx.getAttributeKeySet());
230 public void cleanContextPropertyByPrefix(SvcLogicContext ctx, String prefix) {
231 if (ctx != null && ctx.getAttributeKeySet() != null && StringUtils.isNotBlank(prefix)) {
233 Set<String> keySet = ctx.getAttributeKeySet();
234 for (String key : keySet) {
235 if (StringUtils.isNotBlank(key) && key.startsWith(prefix = ".")) {
236 ctx.getAttributeKeySet().remove(key);
242 public QueryStatus saveUploadConfig(SvcLogicContext ctx, String prefix) throws SvcLogicException {
244 QueryStatus status = null;
246 if (serviceLogic != null && ctx != null) {
247 String key = "INSERT INTO UPLOAD_CONFIG " + " SET request_id = $request-id , "
248 + " action = $request-action , " + " originator_id = $originator-id , " + " vnf_id = $vnf-id , "
249 + " vnf_name = $vnf-name , " + " vm_name = $vm-name , "
250 + " host_ip_address = $vnf-host-ip-address , " + " vnf_type = $vnf-type , "
251 + " vnfc_type = $vnfc-type , " + " config_indicator = 'Current' , "
252 + " content = $tmp.escaped.devicerunningconfig ; ";
254 status = serviceLogic.save("SQL", false, false, key, null, prefix, ctx);
256 log.info("DGGeneralDBService.saveUploadConfig()" + ctx.getAttributeKeySet());
263 /*public QueryStatus getMaxUploadConfigFileId(SvcLogicContext ctx, String prefix) throws SvcLogicException {
264 QueryStatus status = null;
265 if (serviceLogic != null && ctx != null) {
266 String key = "SELECT MAX(upload_config_id) uploadconfigid " + " FROM UPLOAD_CONFIG "
267 + " WHERE vnf_id = $vnf-id AND vm_name = $vm-name ; ";
269 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
270 log.info("DGGeneralDBService.getMaxUploadConfigFileId()" + ctx.getAttributeKeySet());
275 public QueryStatus updateUploadConfig(SvcLogicContext ctx, String prefix, int maxId) throws SvcLogicException {
276 QueryStatus status = null;
277 if (serviceLogic != null && ctx != null) {
278 String key = "UPDATE UPLOAD_CONFIG " + " SET config_indicator = null "
279 + " WHERE upload_config_id != " + maxId + " AND config_indicator = 'Current' "
280 + " AND vnf_id = $vnf-id " + " AND vm_name = $vm-name ; ";
282 status = serviceLogic.save("SQL", false, false, key, null, prefix, ctx);
284 log.info("DGGeneralDBService.updateUploadConfig()" + ctx.getAttributeKeySet());
292 public QueryStatus getTemplateByArtifactType(SvcLogicContext ctx, String prefix, String fileCategory, String artifactType)
293 throws SvcLogicException {
294 QueryStatus status = null;
295 if (serviceLogic != null && ctx != null) {
296 String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
297 + " FROM ASDC_ARTIFACTS "
298 + " WHERE asdc_artifacts_id = (SELECT MAX(a.asdc_artifacts_id) configfileid "
299 + " FROM ASDC_ARTIFACTS a, ASDC_REFERENCE b " + " WHERE a.artifact_name = b.artifact_name "
300 + " AND file_category = '" + fileCategory + "'" + " AND action = $request-action "
301 + " AND artifactType = '" + artifactType + "'" + " AND vnf_type = $vnf-type ) ; ";
303 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
309 public QueryStatus getConfigFilesByVnfVmNCategory(SvcLogicContext ctx, String prefix, String fileCategory, String vnfId, String vmName)
310 throws SvcLogicException {
311 QueryStatus status = null;
312 if (serviceLogic != null && ctx != null) {
314 String key = "SELECT file_content , config_file_id "
315 + " FROM CONFIGFILES "
316 + " WHERE config_file_id = ( SELECT MAX(config_file_id) configfileid " + " FROM CONFIGFILES "
317 + " WHERE file_category = '" + fileCategory + "'"
318 + " AND vnf_id = '" + vnfId + "'"
319 + " AND vm_name = '" + vmName + "' ) ; ";
322 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
328 public QueryStatus getDownloadConfigTemplateByVnf(SvcLogicContext ctx, String prefix)
329 throws SvcLogicException {
330 QueryStatus status = null;
331 if (serviceLogic != null && ctx != null) {
332 String key = "SELECT * FROM DOWNLOAD_CONFIG_TEMPLATE WHERE vnf_type = $vnf-type ; ";
333 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
340 public QueryStatus saveConfigTransactionLog(SvcLogicContext ctx, String prefix) throws SvcLogicException {
342 QueryStatus status = null;
344 if (serviceLogic != null && ctx != null) {
347 String key = "INSERT INTO CONFIG_TRANSACTION_LOG " + " SET request_id = $request-id , "
348 + " message_type = $log-message-type , "
349 + " message = $log-message ;";
352 status = serviceLogic.save("SQL", false, false, key, null, prefix, ctx);
362 public QueryStatus getVnfcReferenceByVnfcTypeNAction(SvcLogicContext ctx, String prefix)
363 throws SvcLogicException {
364 QueryStatus status = null;
365 if (serviceLogic != null && ctx != null) {
367 String key = "SELECT * "
368 + " FROM VNFC_REFERENCE "
369 + " WHERE vnf_type = $vnf-type "
370 + " AND vnfc_type = $vnfc-type "
371 + " AND action = $request-action "
372 + " ORDER BY vm_instance, vnfc_instance ; ";
375 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
381 public QueryStatus getVnfcReferenceByVnfTypeNAction(SvcLogicContext ctx, String prefix)
382 throws SvcLogicException {
383 QueryStatus status = null;
384 if (serviceLogic != null && ctx != null) {
386 String key = "SELECT * "
387 + " FROM VNFC_REFERENCE "
388 + " WHERE vnf_type = $vnf-type "
389 + " AND action = $request-action "
390 + " ORDER BY vm_instance, vnfc_instance ; ";
392 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
398 public QueryStatus getUploadConfigInfo(SvcLogicContext ctx, String prefix)
399 throws SvcLogicException {
400 QueryStatus status = null;
401 if (serviceLogic != null && ctx != null) {
403 String key = "SELECT * , UNIX_TIMESTAMP(UPLOAD_DATE) UPLOAD_TIMESTAMP "
404 + " FROM UPLOAD_CONFIG "
405 + " WHERE upload_config_id = " +
406 "( SELECT MAX(upload_config_id) uploadconfigid " + " FROM UPLOAD_CONFIG "
407 + " WHERE vnf_id = $vnf-id AND vm_name = $vm-name ) ; ";
409 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
413 public String getCapability(SvcLogicContext ctx, String vnf_type) throws SvcLogicException {
415 //{"capabilities":{"vnfc":[],"vm":[],"vf-module":[],"vnf":["ConfigureTest","ConfigModify","HealthCheck"]}}
416 String fn = "getCapability ";
417 QueryStatus status = null;
418 SvcLogicContext localContext = new SvcLogicContext();
419 localContext.setAttribute("vnf-type", vnf_type);
420 if (serviceLogic != null) {
421 String queryString = "select max(internal_version) as maxInternalVersion, artifact_name as artifactName from ASDC_ARTIFACTS " +
422 " where artifact_name in (select artifact_name from ASDC_REFERENCE where vnf_type= $vnf-type " +
423 " and file_category = 'capability' )" ;
425 log.info(fn + "Query String : " + queryString);
426 status = serviceLogic.query("SQL", false, null, queryString, null, null, localContext);
428 if(status.toString().equals("FAILURE"))
429 throw new SvcLogicException("Error - while getting capabilitiesData ");
431 String queryString1 = "select artifact_content from ASDC_ARTIFACTS " +
432 " where artifact_name = $artifactName and internal_version = $maxInternalVersion ";
434 log.debug(fn + "Query String : " + queryString1);
435 status = serviceLogic.query("SQL", false, null, queryString1, null, null, localContext);
436 if (status.toString().equals("NOT_FOUND"))
438 if(status.toString().equals("FAILURE"))
439 throw new SvcLogicException("Error - while getting capabilitiesData ");
442 return localContext.getAttribute("artifact-content");