2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
24 package org.onap.appc.data.services.db;
28 import org.apache.commons.lang3.StringUtils;
30 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
31 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
34 import org.onap.ccsdk.sli.adaptors.resource.sql.SqlResource;
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 static DGGeneralDBService dgGeneralDBService = null;
44 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 public QueryStatus getDeviceProtocolByVnfType(SvcLogicContext ctx, String prefix) throws SvcLogicException {
58 QueryStatus status = null;
59 if (serviceLogic != null && ctx != null) {
60 String key = "SELECT * FROM DEVICE_INTERFACE_PROTOCOL WHERE vnf_type = $vnf-type ;";
61 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
66 public QueryStatus getDeviceAuthenticationByVnfType(SvcLogicContext ctx, String prefix) throws SvcLogicException {
67 QueryStatus status = null;
68 if (serviceLogic != null && ctx != null) {
69 String key = "SELECT * FROM DEVICE_AUTHENTICATION WHERE vnf_type = $vnf-type ;";
70 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
76 public QueryStatus getConfigFileReferenceByVnfType(SvcLogicContext ctx, String prefix) throws SvcLogicException {
77 QueryStatus status = null;
78 if (serviceLogic != null && ctx != null) {
79 String key = "SELECT * FROM CONFIG_FILE_REFERENCE WHERE vnf_type = $vnf-type ;";
80 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
85 public QueryStatus getConfigFileReferenceByFileTypeNVnfType(SvcLogicContext ctx, String prefix, String fileType)
86 throws SvcLogicException {
87 QueryStatus status = null;
88 if (serviceLogic != null && ctx != null) {
89 String key = "SELECT * FROM CONFIG_FILE_REFERENCE WHERE file_type = '" + fileType
90 + "' and vnf_type = $vnf-type ;";
91 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
96 public QueryStatus getTemplate(SvcLogicContext ctx, String prefix, String fileCategory) throws SvcLogicException {
97 QueryStatus status = null;
98 if (serviceLogic != null && ctx != null) {
99 String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
100 + " FROM ASDC_ARTIFACTS "
101 + " WHERE asdc_artifacts_id = ( SELECT MAX(a.asdc_artifacts_id) configfileid "
102 + " FROM ASDC_ARTIFACTS a, ASDC_REFERENCE b " + " WHERE a.artifact_name = b.artifact_name "
103 + " AND file_category = '" + fileCategory + "'" + " AND action = $request-action "
104 + " AND vnf_type = $vnf-type " + " AND vnfc_type = $vnfc-type ) ; ";
105 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
110 public QueryStatus getTemplateByVnfTypeNAction(SvcLogicContext ctx, String prefix, String fileCategory)
111 throws SvcLogicException {
112 QueryStatus status = null;
113 if (serviceLogic != null && ctx != null) {
114 String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
115 + " FROM ASDC_ARTIFACTS "
116 + " WHERE asdc_artifacts_id = (SELECT MAX(a.asdc_artifacts_id) configfileid "
117 + " FROM ASDC_ARTIFACTS a, ASDC_REFERENCE b " + " WHERE a.artifact_name = b.artifact_name "
118 + " AND file_category = '" + fileCategory + "'" + " AND action = $request-action "
119 + " AND vnf_type = $vnf-type ) ; ";
121 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
126 public QueryStatus getTemplateByVnfType(SvcLogicContext ctx, String prefix, String fileCategory)
127 throws SvcLogicException {
128 QueryStatus status = null;
129 if (serviceLogic != null && ctx != null) {
130 String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
131 + " FROM ASDC_ARTIFACTS "
132 + " WHERE asdc_artifacts_id = (SELECT MAX(a.asdc_artifacts_id) configfileid "
133 + " FROM ASDC_ARTIFACTS a, ASDC_REFERENCE b " + " WHERE a.artifact_name = b.artifact_name "
134 + " AND file_category = '" + fileCategory + "'" + " AND vnf_type = $vnf-type ) ; ";
136 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
141 public QueryStatus getTemplateByTemplateName(SvcLogicContext ctx, String prefix, String templateName)
142 throws SvcLogicException {
143 QueryStatus status = null;
144 if (serviceLogic != null && ctx != null) {
145 String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
146 + " FROM ASDC_ARTIFACTS "
147 + " WHERE asdc_artifacts_id = (SELECT MAX(asdc_artifacts_id) configfileid "
148 + " FROM ASDC_ARTIFACTS " + " WHERE artifact_name = '" + templateName + "' ) ; ";
150 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
155 public QueryStatus getConfigureActionDGByVnfTypeNAction(SvcLogicContext ctx, String prefix)
156 throws SvcLogicException {
157 QueryStatus status = null;
158 if (serviceLogic != null && ctx != null) {
159 String key = "SELECT * " + " FROM CONFIGURE_ACTION_DG "
160 + " where vnf_type = $vnf-type and action = $request-action ; ";
162 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
167 public QueryStatus getConfigureActionDGByVnfType(SvcLogicContext ctx, String prefix) throws SvcLogicException {
168 QueryStatus status = null;
169 if (serviceLogic != null && ctx != null) {
170 String key = "SELECT * " + " FROM CONFIGURE_ACTION_DG "
171 + " where vnf_type = $vnf-type and action IS NULL ; ";
173 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
178 public QueryStatus getMaxConfigFileId(SvcLogicContext ctx, String prefix, String fileCategory)
179 throws SvcLogicException {
180 QueryStatus status = null;
181 if (serviceLogic != null && ctx != null) {
182 String key = "SELECT MAX(config_file_id) configfileid " + " FROM CONFIGFILES " + " WHERE file_category = '"
183 + fileCategory + "'" + " AND vnf_id = $vnf-id AND vm_name = $vm-name ; ";
185 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
190 public QueryStatus saveConfigFiles(SvcLogicContext ctx, String prefix) throws SvcLogicException {
192 QueryStatus status = null;
194 if (serviceLogic != null && ctx != null) {
195 String key = "INSERT INTO CONFIGFILES " + " SET data_source = $data-source , "
196 + " service_instance_id = $service-instance-id ," + " action = $request-action ,"
197 + " vnf_type = $vnf-type ," + " vnfc_type = $vnfc-type ,"
198 + " vnf_id = $vnf-id , " + " vnf_name = $vnf-name ,"
199 + " vm_name = $vm-name ," + " file_category = $file-category ,"
200 + " file_content = $file-content ; ";
202 status = serviceLogic.save("SQL", false, false, key, null, prefix, ctx);
209 public QueryStatus savePrepareRelationship(SvcLogicContext ctx, String prefix, String fileId, String sdcInd)
210 throws SvcLogicException {
212 QueryStatus status = null;
215 if (serviceLogic != null && ctx != null) {
217 if ("Y".equals(sdcInd))
219 key = "INSERT INTO PREPARE_FILE_RELATIONSHIP " + " SET service_instance_id = $service-instance-id , "
220 + " request_id = $request-id , " + " asdc_artifacts_id = " + fileId + " ;";
222 key = "INSERT INTO PREPARE_FILE_RELATIONSHIP " + " SET service_instance_id = $service-instance-id , "
223 + " request_id = $request-id , " + " config_file_id = " + fileId + " ;";
225 status = serviceLogic.save("SQL", false, false, key, null, prefix, ctx);
227 log.info("DGGeneralDBService.savePrepareRelationship()" + ctx.getAttributeKeySet());
233 public void cleanContextPropertyByPrefix(SvcLogicContext ctx, String prefix) {
234 if (ctx != null && ctx.getAttributeKeySet() != null && StringUtils.isNotBlank(prefix)) {
236 Set<String> keySet = ctx.getAttributeKeySet();
237 for (String key : keySet) {
238 if (StringUtils.isNotBlank(key) && key.startsWith(prefix = ".")) {
239 ctx.getAttributeKeySet().remove(key);
245 public QueryStatus saveUploadConfig(SvcLogicContext ctx, String prefix) throws SvcLogicException {
247 QueryStatus status = null;
249 if (serviceLogic != null && ctx != null) {
250 String key = "INSERT INTO UPLOAD_CONFIG " + " SET request_id = $request-id , "
251 + " action = $request-action , " + " originator_id = $originator-id , " + " vnf_id = $vnf-id , "
252 + " vnf_name = $vnf-name , " + " vm_name = $vm-name , "
253 + " host_ip_address = $vnf-host-ip-address , " + " vnf_type = $vnf-type , "
254 + " vnfc_type = $vnfc-type , " + " config_indicator = 'Current' , "
255 + " content = $tmp.escaped.devicerunningconfig ; ";
257 status = serviceLogic.save("SQL", false, false, key, null, prefix, ctx);
259 log.info("DGGeneralDBService.saveUploadConfig()" + ctx.getAttributeKeySet());
266 /*public QueryStatus getMaxUploadConfigFileId(SvcLogicContext ctx, String prefix) throws SvcLogicException {
267 QueryStatus status = null;
268 if (serviceLogic != null && ctx != null) {
269 String key = "SELECT MAX(upload_config_id) uploadconfigid " + " FROM UPLOAD_CONFIG "
270 + " WHERE vnf_id = $vnf-id AND vm_name = $vm-name ; ";
272 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
273 log.info("DGGeneralDBService.getMaxUploadConfigFileId()" + ctx.getAttributeKeySet());
278 public QueryStatus updateUploadConfig(SvcLogicContext ctx, String prefix, int maxId) throws SvcLogicException {
279 QueryStatus status = null;
280 if (serviceLogic != null && ctx != null) {
281 String key = "UPDATE UPLOAD_CONFIG " + " SET config_indicator = null "
282 + " WHERE upload_config_id != " + maxId + " AND config_indicator = 'Current' "
283 + " AND vnf_id = $vnf-id " + " AND vm_name = $vm-name ; ";
285 status = serviceLogic.save("SQL", false, false, key, null, prefix, ctx);
287 log.info("DGGeneralDBService.updateUploadConfig()" + ctx.getAttributeKeySet());
295 public QueryStatus getTemplateByArtifactType(SvcLogicContext ctx, String prefix, String fileCategory, String artifactType)
296 throws SvcLogicException {
297 QueryStatus status = null;
298 if (serviceLogic != null && ctx != null) {
299 String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
300 + " FROM ASDC_ARTIFACTS "
301 + " WHERE asdc_artifacts_id = (SELECT MAX(a.asdc_artifacts_id) configfileid "
302 + " FROM ASDC_ARTIFACTS a, ASDC_REFERENCE b " + " WHERE a.artifact_name = b.artifact_name "
303 + " AND file_category = '" + fileCategory + "'" + " AND action = $request-action "
304 + " AND artifactType = '" + artifactType + "'" + " AND vnf_type = $vnf-type ) ; ";
306 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
312 public QueryStatus getConfigFilesByVnfVmNCategory(SvcLogicContext ctx, String prefix, String fileCategory, String vnfId, String vmName)
313 throws SvcLogicException {
314 QueryStatus status = null;
315 if (serviceLogic != null && ctx != null) {
317 String key = "SELECT file_content , config_file_id "
318 + " FROM CONFIGFILES "
319 + " WHERE config_file_id = ( SELECT MAX(config_file_id) configfileid " + " FROM CONFIGFILES "
320 + " WHERE file_category = '" + fileCategory + "'"
321 + " AND vnf_id = '" + vnfId + "'"
322 + " AND vm_name = '" + vmName + "' ) ; ";
325 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
331 public QueryStatus getDownloadConfigTemplateByVnf(SvcLogicContext ctx, String prefix)
332 throws SvcLogicException {
333 QueryStatus status = null;
334 if (serviceLogic != null && ctx != null) {
335 String key = "SELECT * FROM DOWNLOAD_CONFIG_TEMPLATE WHERE vnf_type = $vnf-type ; ";
336 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
343 public QueryStatus saveConfigTransactionLog(SvcLogicContext ctx, String prefix) throws SvcLogicException {
345 QueryStatus status = null;
347 if (serviceLogic != null && ctx != null) {
350 String key = "INSERT INTO CONFIG_TRANSACTION_LOG " + " SET request_id = $request-id , "
351 + " message_type = $log-message-type , "
352 + " message = $log-message ;";
355 status = serviceLogic.save("SQL", false, false, key, null, prefix, ctx);
365 public QueryStatus getVnfcReferenceByVnfcTypeNAction(SvcLogicContext ctx, String prefix)
366 throws SvcLogicException {
367 QueryStatus status = null;
368 if (serviceLogic != null && ctx != null) {
370 String key = "SELECT * "
371 + " FROM VNFC_REFERENCE "
372 + " WHERE vnf_type = $vnf-type "
373 + " AND vnfc_type = $vnfc-type "
374 + " AND action = $request-action "
375 + " ORDER BY vm_instance, vnfc_instance ; ";
378 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
384 public QueryStatus getVnfcReferenceByVnfTypeNAction(SvcLogicContext ctx, String prefix)
385 throws SvcLogicException {
386 QueryStatus status = null;
387 if (serviceLogic != null && ctx != null) {
389 String key = "SELECT * "
390 + " FROM VNFC_REFERENCE "
391 + " WHERE vnf_type = $vnf-type "
392 + " AND action = $request-action "
393 + " ORDER BY vm_instance, vnfc_instance ; ";
395 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
401 public QueryStatus getUploadConfigInfo(SvcLogicContext ctx, String prefix)
402 throws SvcLogicException {
403 QueryStatus status = null;
404 if (serviceLogic != null && ctx != null) {
406 String key = "SELECT * , UNIX_TIMESTAMP(UPLOAD_DATE) UPLOAD_TIMESTAMP "
407 + " FROM UPLOAD_CONFIG "
408 + " WHERE upload_config_id = " +
409 "( SELECT MAX(upload_config_id) uploadconfigid " + " FROM UPLOAD_CONFIG "
410 + " WHERE vnf_id = $vnf-id AND vm_name = $vm-name ) ; ";
412 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
416 public String getCapability(SvcLogicContext ctx, String vnf_type) throws SvcLogicException {
418 //{"capabilities":{"vnfc":[],"vm":[],"vf-module":[],"vnf":["ConfigureTest","ConfigModify","HealthCheck"]}}
419 String fn = "getCapability ";
420 QueryStatus status = null;
421 SvcLogicContext localContext = new SvcLogicContext();
422 localContext.setAttribute("vnf-type", vnf_type);
423 if (serviceLogic != null) {
424 String queryString = "select max(internal_version) as maxInternalVersion, artifact_name as artifactName from ASDC_ARTIFACTS " +
425 " where artifact_name in (select artifact_name from ASDC_REFERENCE where vnf_type= $vnf-type " +
426 " and file_category = 'capability' )" ;
428 log.info(fn + "Query String : " + queryString);
429 status = serviceLogic.query("SQL", false, null, queryString, null, null, localContext);
431 if(status.toString().equals("FAILURE"))
432 throw new SvcLogicException("Error - while getting capabilitiesData ");
434 String queryString1 = "select artifact_content from ASDC_ARTIFACTS " +
435 " where artifact_name = $artifactName and internal_version = $maxInternalVersion ";
437 log.debug(fn + "Query String : " + queryString1);
438 status = serviceLogic.query("SQL", false, null, queryString1, null, null, localContext);
439 if (status.toString().equals("NOT_FOUND"))
441 if(status.toString().equals("FAILURE"))
442 throw new SvcLogicException("Error - while getting capabilitiesData ");
445 return localContext.getAttribute("artifact-content");