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=========================================================
24 package org.onap.appc.artifact.handler.dbservices;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import org.apache.commons.configuration.ConfigurationException;
29 import org.apache.commons.configuration.PropertiesConfiguration;
30 import org.apache.commons.lang.StringUtils;
31 import org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants;
32 import org.onap.ccsdk.sli.adaptors.resource.sql.SqlResource;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
38 public class DBService {
40 private static final EELFLogger log = EELFManager.getInstance().getLogger(DBService.class);
41 private static final String FAILURE_PARAM = "FAILURE";
42 private static final String RECEIVED_AS = "Internal Version received as1 : ";
43 private static final String SET_DOWNLOAD_CONFIG_QUERY_STR = " set DOWNLOAD_CONFIG_DG = $";
44 private static final String WHERE_VNF_TYPE_QUERY_STR = " where VNF_TYPE = $";
45 private static final String ACTION_QUERY_STR = " , ACTION = $";
46 private static final String VNF_TYPE_QUERY_STR = " , VNF_TYPE = $";
47 private static final String INSERT_INTO_QUERY_STR = "insert into ";
48 private static final String AND_ACTION_QUERY_STR = " and ACTION = $";
49 private static final String AND_FILE_CAT_QUERY_STR = " and FILE_CATEGORY = $";
50 private static final String AND_VNF_TYPE_QUERY_STR = " and VNF_TYPE = $";
51 private static final String UPDATE_QUERY_STR = "update ";
52 private static final String AND_VNFC_TYPE_QUERY_STR = " and VNFC_TYPE = $";
54 private SvcLogicResource serviceLogic;
55 private static DBService dgGeneralDBService = null;
58 if (serviceLogic == null) {
59 serviceLogic = new SqlResource();
63 protected DBService(SqlResource svcLogic) {
64 if (serviceLogic == null) {
65 serviceLogic = svcLogic;
69 public static DBService initialise() {
70 if (dgGeneralDBService == null) {
71 dgGeneralDBService = new DBService();
73 return dgGeneralDBService;
76 public String getInternalVersionNumber(SvcLogicContext ctx, String artifactName, String prefix)
77 throws SvcLogicException {
79 String artifactInternalVersion = null;
80 if (serviceLogic != null && ctx != null) {
81 String key = "select max(internal_version) as maximum from ASDC_ARTIFACTS WHERE ARTIFACT_NAME = '"
83 log.info("Getting internal Versoin :" + key);
84 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
85 if (status.toString().equals(FAILURE_PARAM)) {
86 throw new SvcLogicException("Error - getting internal Artifact Number");
88 artifactInternalVersion = ctx.getAttribute("maximum");
89 log.info("Internal Version received as : " + artifactInternalVersion);
90 log.info(RECEIVED_AS + ctx.getAttribute("max(internal_version)"));
91 log.info(RECEIVED_AS + ctx.getAttribute("max"));
92 log.info(RECEIVED_AS + ctx.getAttribute("internal_version"));
93 log.info(RECEIVED_AS + ctx.getAttributeKeySet().toString());
95 return artifactInternalVersion;
98 public String getArtifactID(SvcLogicContext ctx, String artifactName) throws SvcLogicException {
100 String artifactID = null;
101 if (serviceLogic != null && ctx != null) {
102 String key = "select max(ASDC_ARTIFACTS_ID) as id from ASDC_ARTIFACTS WHERE ARTIFACT_NAME = '"
103 + artifactName + "'";
104 log.info("Getting Artifact ID String :" + key);
105 status = serviceLogic.query("SQL", false, null, key, null, null, ctx);
106 if (status.toString().equals(FAILURE_PARAM)) {
107 throw new SvcLogicException("Error - getting Artifact ID from database");
109 artifactID = ctx.getAttribute("id");
110 log.info("SDC_ARTIFACTS_ID received as : " + ctx.getAttribute("id"));
115 public QueryStatus saveArtifacts(SvcLogicContext ctx, int intversion) throws SvcLogicException {
116 QueryStatus status = null;
117 if (serviceLogic != null && ctx != null) {
118 String key = "INSERT INTO ASDC_ARTIFACTS " + "SET SERVICE_UUID = $service-uuid , "
119 + " DISTRIBUTION_ID = $distribution-id ," + " SERVICE_NAME = $service-name ,"
120 + " SERVICE_DESCRIPTION = $service-description ," + " RESOURCE_UUID = $resource-uuid ,"
121 + " RESOURCE_INSTANCE_NAME = $resource-instance-name ," + " RESOURCE_NAME = $resource-name ,"
122 + " RESOURCE_VERSION = $resource-version ," + " RESOURCE_TYPE = $resource-type ,"
123 + " ARTIFACT_UUID = $artifact-uuid ," + " ARTIFACT_TYPE = $artifact-type ,"
124 + " ARTIFACT_VERSION = $artifact-version ,"
125 + " ARTIFACT_DESCRIPTION = $artifact-description ," + " INTERNAL_VERSION = " + intversion
126 + "," + " ARTIFACT_NAME = $artifact-name ," + " ARTIFACT_CONTENT = $artifact-contents ";
128 status = serviceLogic.save("SQL", false, false, key, null, null, ctx);
129 if (status.toString().equals(FAILURE_PARAM)) {
130 throw new SvcLogicException("Error While processing storing Artifact: "
131 + ctx.getAttribute(SdcArtifactHandlerConstants.ARTIFACT_NAME));
137 public QueryStatus logData(SvcLogicContext ctx, String prefix) throws SvcLogicException {
138 QueryStatus status = null;
139 if (serviceLogic != null && ctx != null) {
140 String key = "INSERT INTO CONFIG_TRANSACTION_LOG " + " SET request_id = $request-id , "
141 + " message_type = $log-message-type , " + " message = $log-message ;";
142 status = serviceLogic.save("SQL", false, false, key, null, prefix, ctx);
143 if (status.toString().equals(FAILURE_PARAM)) {
144 throw new SvcLogicException("Error while loging data");
151 public void processConfigureActionDg(SvcLogicContext context, boolean isUpdate) {
152 log.info("Update Parameter for SDC Reference " + isUpdate);
153 //TODO implement this method
156 public void processSdcReferences(SvcLogicContext context, boolean isUpdate) throws SvcLogicException {
157 processSdcReferences(context, isUpdate, null);
160 public void processSdcReferences(SvcLogicContext context, boolean isUpdate, String modelId) throws SvcLogicException {
163 if (isUpdate && context.getAttribute(SdcArtifactHandlerConstants.FILE_CATEGORY)
164 .equals(SdcArtifactHandlerConstants.CAPABILITY)) {
165 log.info("Updating capability artifact in ASDC_REFERENCE");
166 key = UPDATE_QUERY_STR + SdcArtifactHandlerConstants.DB_SDC_REFERENCE + " set ARTIFACT_NAME = $"
167 + SdcArtifactHandlerConstants.ARTIFACT_NAME + " where " + "FILE_CATEGORY = $"
168 + SdcArtifactHandlerConstants.FILE_CATEGORY + AND_VNF_TYPE_QUERY_STR
169 + SdcArtifactHandlerConstants.VNF_TYPE;
170 } else if (isUpdate) {
171 key = UPDATE_QUERY_STR + SdcArtifactHandlerConstants.DB_SDC_REFERENCE + " set ARTIFACT_NAME = $"
172 + SdcArtifactHandlerConstants.ARTIFACT_NAME + " where VNFC_TYPE = $"
173 + SdcArtifactHandlerConstants.VNFC_TYPE + AND_FILE_CAT_QUERY_STR
174 + SdcArtifactHandlerConstants.FILE_CATEGORY + AND_ACTION_QUERY_STR + SdcArtifactHandlerConstants.ACTION
175 + AND_VNF_TYPE_QUERY_STR + SdcArtifactHandlerConstants.VNF_TYPE;
176 if (StringUtils.isNotBlank(modelId)) {
177 key += createQueryListForTemplateIds(modelId);
180 if (context.getAttribute(SdcArtifactHandlerConstants.FILE_CATEGORY)
181 .equals(SdcArtifactHandlerConstants.CAPABILITY)) {
182 log.info("Inserting new record for capability artifact in ASDC_REFERENCE");
183 key = INSERT_INTO_QUERY_STR + SdcArtifactHandlerConstants.DB_SDC_REFERENCE + " set VNFC_TYPE = null "
184 + " , FILE_CATEGORY = $" + SdcArtifactHandlerConstants.FILE_CATEGORY + VNF_TYPE_QUERY_STR
185 + SdcArtifactHandlerConstants.VNF_TYPE + " , ACTION = null " + " , ARTIFACT_TYPE = null "
186 + " , ARTIFACT_NAME = $" + SdcArtifactHandlerConstants.ARTIFACT_NAME;
188 key = INSERT_INTO_QUERY_STR + SdcArtifactHandlerConstants.DB_SDC_REFERENCE + " set VNFC_TYPE = $"
189 + SdcArtifactHandlerConstants.VNFC_TYPE + " , FILE_CATEGORY = $"
190 + SdcArtifactHandlerConstants.FILE_CATEGORY + VNF_TYPE_QUERY_STR
191 + SdcArtifactHandlerConstants.VNF_TYPE + ACTION_QUERY_STR + SdcArtifactHandlerConstants.ACTION
192 + " , ARTIFACT_TYPE = $" + SdcArtifactHandlerConstants.ARTIFACT_TYPE + " , ARTIFACT_NAME = $"
193 + SdcArtifactHandlerConstants.ARTIFACT_NAME;
196 if (serviceLogic != null) {
197 log.info("Insert Key: " + key);
198 status = serviceLogic.save("SQL", false, false, key, null, null, context);
199 if (status.toString().equals(FAILURE_PARAM)) {
200 throw new SvcLogicException("Error While processing sdc_reference table ");
205 public boolean isArtifactUpdateRequired(SvcLogicContext context, String db) throws DBException {
206 return isArtifactUpdateRequired( context, db, null);
209 public boolean isArtifactUpdateRequired(SvcLogicContext context, String db, String modelId)
212 log.info("Checking if Update required for this data");
214 log.info("ACTION=" + context.getAttribute(SdcArtifactHandlerConstants.ACTION));
215 log.info("VNFC_TYPE=" + context.getAttribute(SdcArtifactHandlerConstants.VNFC_TYPE));
216 log.info("VNFC_INSTANCE=" + context.getAttribute(SdcArtifactHandlerConstants.VNFC_INSTANCE));
217 log.info("VM_INSTANCE=" + context.getAttribute(SdcArtifactHandlerConstants.VM_INSTANCE));
218 log.info("VNF_TYPE=" + context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE));
220 //Check for templates
221 //if templates are present - there might be multiple records, so validate
222 if( db.equals(SdcArtifactHandlerConstants.DB_SDC_REFERENCE) && StringUtils.isNotBlank(modelId)) {
223 log.info("ModelId is sent!!");
224 String queryPart = createQueryListForTemplateIds(modelId);
225 log.info("Querypart is = "+queryPart);
226 if (isUpdateRequiredForTemplates(queryPart, context, db)) {
227 log.info("Update is Required!!");
230 log.info("Insert is Required!!");
237 whereClause = WHERE_VNF_TYPE_QUERY_STR + SdcArtifactHandlerConstants.VNF_TYPE;
238 whereClause = resolveWhereClause(context, db, whereClause);
240 String key = "select COUNT(*) from " + db + whereClause;
241 log.info("SELECT String : " + key);
242 status = serviceLogic.query("SQL", false, null, key, null, null, context);
243 checkForFailure(db, status);
244 String count = context.getAttribute("COUNT(*)");
245 log.info("Number of row Returned : " + count + ": " + status + ":");
246 return tryAddCountAttribute(context, count);
249 } catch (SvcLogicException e) {
250 throw new DBException("An error occurred while checking for artifact update", e);
254 private void checkForFailure(String db, QueryStatus status) throws SvcLogicException {
255 if (status.toString().equals(FAILURE_PARAM)) {
256 throw new SvcLogicException("Error while reading data from " + db);
260 private boolean validate(String db) {
261 return db != null && serviceLogic != null;
264 private boolean keyExists(PropertiesConfiguration conf, String property) {
265 if (conf.subset(property) != null) {
266 if (conf.containsKey(property)) {
267 log.info("Key Exists for property" + property + "in southbound.properties file");
271 log.info("Key Does not exists and need to add the key for property" + property
272 + "in southbound.properties file");
277 private boolean tryAddCountAttribute(SvcLogicContext context, String count) {
278 if (count != null && Integer.parseInt(count) > 0) {
279 context.setAttribute(count, null);
287 private String resolveWhereClause(SvcLogicContext context, String db, String whereClause) {
289 if (hasValidAttributes(context, db)) {
290 return whereClause + AND_FILE_CAT_QUERY_STR + SdcArtifactHandlerConstants.FILE_CATEGORY;
291 } else if (db.equals(SdcArtifactHandlerConstants.DB_SDC_REFERENCE)) {
292 return whereClause + AND_VNFC_TYPE_QUERY_STR + SdcArtifactHandlerConstants.VNFC_TYPE
293 + AND_FILE_CAT_QUERY_STR + SdcArtifactHandlerConstants.FILE_CATEGORY + AND_ACTION_QUERY_STR
294 + SdcArtifactHandlerConstants.ACTION;
295 } else if (db.equals(SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE)) {
296 return " where PROTOCOL = $" + SdcArtifactHandlerConstants.DEVICE_PROTOCOL;
297 } else if (db.equals(SdcArtifactHandlerConstants.DB_DEVICE_AUTHENTICATION)) {
298 log.info(" DB validation for Device authentication " + whereClause + " AND PROTOCOL = $"
299 + SdcArtifactHandlerConstants.DEVICE_PROTOCOL + " AND ACTION = $"
300 + SdcArtifactHandlerConstants.ACTION);
301 return whereClause + " AND PROTOCOL = $" + SdcArtifactHandlerConstants.DEVICE_PROTOCOL
302 + " AND ACTION = $" + SdcArtifactHandlerConstants.ACTION;
303 } else if (db.equals(SdcArtifactHandlerConstants.DB_CONFIG_ACTION_DG)) {
304 return whereClause + AND_ACTION_QUERY_STR + SdcArtifactHandlerConstants.ACTION;
305 } else if (db.equals(SdcArtifactHandlerConstants.DB_VNFC_REFERENCE)) {
306 return whereClause + AND_ACTION_QUERY_STR + SdcArtifactHandlerConstants.ACTION
307 + AND_VNFC_TYPE_QUERY_STR + SdcArtifactHandlerConstants.VNFC_TYPE + " and VNFC_INSTANCE = $"
308 + SdcArtifactHandlerConstants.VNFC_INSTANCE + " and VM_INSTANCE = $"
309 + SdcArtifactHandlerConstants.VM_INSTANCE;
315 private boolean hasValidAttributes(SvcLogicContext context, String db) {
316 return db.equals(SdcArtifactHandlerConstants.DB_SDC_REFERENCE)
317 && context.getAttribute(SdcArtifactHandlerConstants.FILE_CATEGORY)
318 .equals(SdcArtifactHandlerConstants.CAPABILITY)
319 && context.getAttribute(SdcArtifactHandlerConstants.ACTION) == null;
322 public void processDeviceInterfaceProtocol(SvcLogicContext context, boolean isUpdate) throws SvcLogicException {
323 log.info("Starting DB operation for Device Interface Protocol " + isUpdate);
327 key = UPDATE_QUERY_STR + SdcArtifactHandlerConstants.DB_DEVICE_INTERFACE_PROTOCOL + " set PROTOCOL = $"
328 + SdcArtifactHandlerConstants.DEVICE_PROTOCOL + " , DG_RPC = 'getDeviceRunningConfig' "
329 + " , MODULE = 'APPC' " + WHERE_VNF_TYPE_QUERY_STR + SdcArtifactHandlerConstants.VNF_TYPE;
332 INSERT_INTO_QUERY_STR + SdcArtifactHandlerConstants.DB_DEVICE_INTERFACE_PROTOCOL + " set VNF_TYPE = $"
333 + SdcArtifactHandlerConstants.VNF_TYPE + " , PROTOCOL = $"
334 + SdcArtifactHandlerConstants.DEVICE_PROTOCOL + " , DG_RPC = 'getDeviceRunningConfig' "
335 + " , MODULE = 'APPC' ";
338 if (serviceLogic != null && context != null) {
340 status = serviceLogic.save("SQL", false, false, key, null, null, context);
341 if (status.toString().equals(FAILURE_PARAM)) {
342 throw new SvcLogicException("Error While processing DEVICE_INTERFACE_PROTOCOL table ");
347 public void processDeviceAuthentication(SvcLogicContext context, boolean isUpdate)
350 String fn = "DBService.processDeviceAuthentication";
351 log.info(fn + "Starting DB operation for Device Authentication " + isUpdate);
352 String port = context.getAttribute(SdcArtifactHandlerConstants.PORT_NUMBER);
353 String user = context.getAttribute(SdcArtifactHandlerConstants.USER_NAME);
354 String protocol = context.getAttribute(SdcArtifactHandlerConstants.DEVICE_PROTOCOL);
355 String action = context.getAttribute(SdcArtifactHandlerConstants.ACTION);
356 String vnftype = context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE);
358 if (StringUtils.isBlank(port)) {
361 if (StringUtils.isBlank(user)) {
364 if (isInvalidInput(SdcArtifactHandlerConstants.DEVICE_PROTOCOL, SdcArtifactHandlerConstants.ACTION,
365 SdcArtifactHandlerConstants.VNF_TYPE)) {
366 throw new SvcLogicException(
367 "Error While processing reference File as few or all of parameters VNF_TYPE,PROTOCOL,ACTION are missing ");
370 log.info("Starting DB operation for Device authentication " + isUpdate);
371 log.info("credentials"+user + "user" + "port" + port +"protocol"+protocol+"action"+action+"vnftype"+vnftype);
375 key = UPDATE_QUERY_STR + SdcArtifactHandlerConstants.DB_DEVICE_AUTHENTICATION + " set USER_NAME = $"
376 + SdcArtifactHandlerConstants.USER_NAME + " , PORT_NUMBER = $" + SdcArtifactHandlerConstants.PORT_NUMBER + "";
377 if (context.getAttributeKeySet().contains(SdcArtifactHandlerConstants.URL)) {
378 String url = context.getAttribute(SdcArtifactHandlerConstants.URL);
379 if (StringUtils.isBlank(url)) {
382 key = key + ", URL = $" + SdcArtifactHandlerConstants.URL + " ";
384 key = key + WHERE_VNF_TYPE_QUERY_STR + SdcArtifactHandlerConstants.VNF_TYPE + " AND PROTOCOL = $"
385 + SdcArtifactHandlerConstants.DEVICE_PROTOCOL + " AND ACTION = $"
386 + SdcArtifactHandlerConstants.ACTION;
388 key = "insert into DEVICE_AUTHENTICATION set VNF_TYPE = $" + SdcArtifactHandlerConstants.VNF_TYPE + " , PROTOCOL = $" + SdcArtifactHandlerConstants.DEVICE_PROTOCOL + " , " + "ACTION = $" + SdcArtifactHandlerConstants.ACTION + " , USER_NAME = $" + SdcArtifactHandlerConstants.USER_NAME + " , PORT_NUMBER = $" + SdcArtifactHandlerConstants.PORT_NUMBER + "";
389 if (context.getAttributeKeySet().contains(SdcArtifactHandlerConstants.URL)) {
390 String url = context.getAttribute(SdcArtifactHandlerConstants.URL);
391 if (StringUtils.isBlank(url)) {
394 key = key + ", URL = $" + SdcArtifactHandlerConstants.URL + " ";
398 log.info("Query forDevice authentication " + key);
399 if (serviceLogic != null && context != null) {
401 status = serviceLogic.save("SQL", false, false, key, null, null, context);
402 if (status.toString().equals(FAILURE_PARAM)) {
403 throw new SvcLogicException("Error While processing DEVICE_AUTHENTICATION table ");
407 } catch (SvcLogicException e) {
409 throw new DBException("An error occurred when processing device authentication", e);
413 private boolean isInvalidInput(String protocol, String action, String vnfType) {
414 return isInvalid(vnfType) && isInvalid(action) && isInvalid(protocol);
417 private boolean isInvalid(String str) {
418 return (str == null) || ("".equals(str));
421 public void processVnfcReference(SvcLogicContext context, boolean isUpdate) throws SvcLogicException {
422 String fn = "DBService.processVnfcReference";
423 log.info(fn + "Starting DB operation for Vnfc Reference " + isUpdate);
427 if (context.getAttribute(SdcArtifactHandlerConstants.VM_INSTANCE) != null) {
428 vmInstance = Integer.parseInt(context.getAttribute(SdcArtifactHandlerConstants.VM_INSTANCE));
431 int vnfcInstance = -1;
432 if (context.getAttribute(SdcArtifactHandlerConstants.VNFC_INSTANCE) != null) {
433 vnfcInstance = Integer.parseInt(context.getAttribute(SdcArtifactHandlerConstants.VNFC_INSTANCE));
438 key = UPDATE_QUERY_STR + SdcArtifactHandlerConstants.DB_VNFC_REFERENCE + " set VM_INSTANCE = " + vmInstance
439 + " , VNFC_INSTANCE = " + vnfcInstance + " , VNFC_TYPE = $" + SdcArtifactHandlerConstants.VNFC_TYPE
440 + " , VNFC_FUNCTION_CODE = $" + SdcArtifactHandlerConstants.VNFC_FUNCTION_CODE
441 + " , GROUP_NOTATION_TYPE = $" + SdcArtifactHandlerConstants.GROUP_NOTATION_TYPE
442 + " , GROUP_NOTATION_VALUE = $" + SdcArtifactHandlerConstants.GROUP_NOTATION_VALUE
443 + " , IPADDRESS_V4_OAM_VIP = $" + SdcArtifactHandlerConstants.IPADDRESS_V4_OAM_VIP
444 + WHERE_VNF_TYPE_QUERY_STR + SdcArtifactHandlerConstants.VNF_TYPE + AND_ACTION_QUERY_STR
445 + SdcArtifactHandlerConstants.ACTION + AND_VNFC_TYPE_QUERY_STR + SdcArtifactHandlerConstants.VNFC_TYPE
446 + " and VNFC_INSTANCE = $" + SdcArtifactHandlerConstants.VNFC_INSTANCE + " and VM_INSTANCE = $"
447 + SdcArtifactHandlerConstants.VM_INSTANCE;
449 key = INSERT_INTO_QUERY_STR + SdcArtifactHandlerConstants.DB_VNFC_REFERENCE + " set VNF_TYPE = $"
450 + SdcArtifactHandlerConstants.VNF_TYPE + ACTION_QUERY_STR + SdcArtifactHandlerConstants.ACTION
451 + " , VM_INSTANCE = $" + SdcArtifactHandlerConstants.VM_INSTANCE + " , VNFC_INSTANCE = $"
452 + SdcArtifactHandlerConstants.VNFC_INSTANCE + " , VNFC_TYPE = $"
453 + SdcArtifactHandlerConstants.VNFC_TYPE + " , VNFC_FUNCTION_CODE = $"
454 + SdcArtifactHandlerConstants.VNFC_FUNCTION_CODE + " , TEMPLATE_ID = $"
455 + SdcArtifactHandlerConstants.TEMPLATE_ID + " , GROUP_NOTATION_TYPE = $"
456 + SdcArtifactHandlerConstants.GROUP_NOTATION_TYPE + " , IPADDRESS_V4_OAM_VIP = $"
457 + SdcArtifactHandlerConstants.IPADDRESS_V4_OAM_VIP + " , GROUP_NOTATION_VALUE = $"
458 + SdcArtifactHandlerConstants.GROUP_NOTATION_VALUE;
461 if (serviceLogic != null) {
462 status = serviceLogic.save("SQL", false, false, key, null, null, context);
463 if (status.toString().equals(FAILURE_PARAM)) {
464 throw new SvcLogicException("Error While processing VNFC_REFERENCE table ");
469 public void processDownloadDgReference(SvcLogicContext context, boolean isUpdate)
470 throws SvcLogicException {
471 String fn = "DBService.processDownloadDgReference";
472 log.info(fn + "Starting DB operation for Download DG Reference " + isUpdate);
474 QueryStatus status = null;
478 UPDATE_QUERY_STR + SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE + SET_DOWNLOAD_CONFIG_QUERY_STR
479 + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " where PROTOCOL = $"
480 + SdcArtifactHandlerConstants.DEVICE_PROTOCOL;
482 key = INSERT_INTO_QUERY_STR + SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE
483 + SET_DOWNLOAD_CONFIG_QUERY_STR
484 + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " , PROTOCOL = $"
485 + SdcArtifactHandlerConstants.DEVICE_PROTOCOL;
488 if (serviceLogic != null && context != null) {
489 status = serviceLogic.save("SQL", false, false, key, null, null, context);
491 if ((status == null) || status.toString().equals(FAILURE_PARAM)) {
492 throw new SvcLogicException("Error While processing DOWNLOAD_DG_REFERENCE table ");
496 public void processConfigActionDg(SvcLogicContext context, boolean isUpdate) throws SvcLogicException {
497 String fn = "DBService.processConfigActionDg";
498 log.info(fn + "Starting DB operation for Config DG Action " + isUpdate);
500 QueryStatus status = null;
502 if (context.getAttribute(SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE) != null
503 && context.getAttribute(SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE).length() > 0) {
505 key = UPDATE_QUERY_STR + SdcArtifactHandlerConstants.DB_CONFIG_ACTION_DG + SET_DOWNLOAD_CONFIG_QUERY_STR
506 + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " where ACTION = $"
507 + SdcArtifactHandlerConstants.ACTION + AND_VNF_TYPE_QUERY_STR
508 + SdcArtifactHandlerConstants.VNF_TYPE;
510 key = INSERT_INTO_QUERY_STR + SdcArtifactHandlerConstants.DB_CONFIG_ACTION_DG
511 + SET_DOWNLOAD_CONFIG_QUERY_STR
512 + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + ACTION_QUERY_STR
513 + SdcArtifactHandlerConstants.ACTION + VNF_TYPE_QUERY_STR + SdcArtifactHandlerConstants.VNF_TYPE;
516 if (serviceLogic != null) {
517 status = serviceLogic.save("SQL", false, false, key, null, null, context);
519 if ((status == null) || status.toString().equals(FAILURE_PARAM)) {
520 throw new SvcLogicException("Error While processing Configure DG Action table ");
523 log.info("No Update required for Config DG Action");
528 public String getModelDataInformationbyArtifactName(String artifactName) throws SvcLogicException {
529 String fn = "DBService.getVnfData";
530 SvcLogicContext con = new SvcLogicContext();
534 "select VNF_TYPE, VNFC_TYPE, ACTION, FILE_CATEGORY, ARTIFACT_TYPE from ASDC_REFERENCE where ARTIFACT_NAME = "
537 if (serviceLogic != null) {
538 log.info(fn + "select Key: " + key);
539 status = serviceLogic.query("SQL", false, null, key, null, null, con);
540 if (status.toString().equals(FAILURE_PARAM)) {
541 throw new SvcLogicException("Error While processing is ArtifactUpdateRequiredforPD table ");
545 log.info(fn + "Vnf_received :" + con.getAttribute("VNF_TYPE"));
547 return con.getAttribute("VNF_TYPE");
550 public void updateYangContents(SvcLogicContext context, String artifactId, String yangContents)
551 throws SvcLogicException {
552 String fn = "DBService.updateYangContents";
553 log.info(fn + "Starting DB operation for updateYangContents");
555 QueryStatus status = null;
557 key = "update ASDC_ARTIFACTS " + " set ARTIFACT_CONTENT = '" + yangContents + "'"
558 + " where ASDC_ARTIFACTS_ID = " + artifactId;
560 if (serviceLogic != null && context != null) {
561 status = serviceLogic.save("SQL", false, false, key, null, null, context);
563 if ((status == null) || status.toString().equals(FAILURE_PARAM)) {
564 throw new SvcLogicException("Error While processing Configure DG Action table ");
570 public void insertProtocolReference(SvcLogicContext context, String vnfType, String protocol, String action,
571 String actionLevel, String template) throws SvcLogicException {
572 String fn = "DBService.insertProtocolReference";
573 log.info(fn + "Starting DB operation for insertProtocolReference");
575 QueryStatus status = null;
577 key = "insert into PROTOCOL_REFERENCE (ACTION, VNF_TYPE, PROTOCOL, UPDATED_DATE, TEMPLATE, ACTION_LEVEL)"
578 + " values (" + "'" + action + "', '" + vnfType + "', '" + protocol + "', now(),'" + template + "', '"
579 + actionLevel + "')";
581 if (serviceLogic != null && context != null) {
582 status = serviceLogic.save("SQL", false, false, key, null, null, context);
584 if ((status == null) || status.toString().equals(FAILURE_PARAM)) {
585 throw new SvcLogicException("Error While processing insertProtocolReference ");
590 public boolean isProtocolReferenceUpdateRequired(SvcLogicContext context, String vnfType, String protocol,
591 String action, String actionLevel, String template) throws SvcLogicException {
592 SvcLogicContext localContext = new SvcLogicContext();
593 String fn = "DBService.isProtocolReferenceUpdateRequired";
594 log.info(fn + "Starting DB operation for isProtocolReferenceUpdateRequired");
596 String key = "select COUNT(*) from PROTOCOL_REFERENCE where ACTION='" + action + "' and ACTION_LEVEL='" + actionLevel
597 + "' and VNF_TYPE='" + vnfType + "'";
598 serviceLogic.query("SQL", false, null, key, null, null, localContext);
600 String countStr = localContext.getAttribute("COUNT(*)");
601 int count = Integer.parseInt(countStr);
605 public void updateProtocolReference(SvcLogicContext context, String vnfType, String protocol, String action,
606 String actionLevel, String template) throws SvcLogicException {
608 String fn = "DBService.isProtocolReferenceUpdateRequired";
609 log.info(fn + "Starting DB operation for isProtocolReferenceUpdateRequired");
613 key = "update PROTOCOL_REFERENCE set UPDATED_DATE=now(), template='" + template + "', protocol ='" + protocol
614 + "' where ACTION='" + action + "' and ACTION_LEVEL='" + actionLevel + "' and VNF_TYPE='" + vnfType
616 status = serviceLogic.save("SQL", false, false, key, null, null, context);
617 if (status == QueryStatus.FAILURE) {
618 log.info("updateProtocolReference:: Error updating protocol reference");
619 throw new SvcLogicException("Error - updating PROTOCOL_REFERENCE_TABLE in updateProtocolReference");
623 public String getDownLoadDGReference(SvcLogicContext context) throws DBException {
626 String fn = "DBService.setDownLoadDGReference";
627 String downloadConfigDg;
628 log.info(fn + "Setting Download DG Reference from DB");
631 String protocol = context.getAttribute(SdcArtifactHandlerConstants.DEVICE_PROTOCOL);
632 if (StringUtils.isBlank(protocol)) {
633 log.info(fn + " :: Protocol is Blank!! Returning without querying DB");
634 throw new ConfigurationException(fn + ":: Protocol is Blank!! Returning without querying DB");
636 key = "select download_config_dg from " + SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE
637 + " where protocol = '" + protocol + "'";
638 SvcLogicContext localContext = new SvcLogicContext();
639 status = serviceLogic.query("SQL", false, null, key, null, null, localContext);
640 if (status == QueryStatus.FAILURE) {
641 log.info(fn + ":: Error retrieving download_config_dg");
642 throw new SvcLogicException("Error retrieving download_config_dg");
644 if (status == QueryStatus.NOT_FOUND) {
645 log.info(fn + ":: NOT_FOUND! No data found for download_config_dg!!");
646 throw new SvcLogicException(fn + ":: NOT_FOUND! No data found for download_config_dg!");
648 downloadConfigDg = localContext.getAttribute("download-config-dg");
649 log.info(fn + "download_config_dg::" + downloadConfigDg);
650 return downloadConfigDg;
651 } catch (SvcLogicException | ConfigurationException e) {
652 throw new DBException("An error occurred when getting DG reference", e);
656 public void cleanUpVnfcReferencesForVnf(SvcLogicContext context) throws SvcLogicException {
658 String key1 = "delete from " + SdcArtifactHandlerConstants.DB_VNFC_REFERENCE + " where action = $"
659 + SdcArtifactHandlerConstants.ACTION + " and vnf_type = $" + SdcArtifactHandlerConstants.VNF_TYPE;
660 log.debug("Action : " + context.getAttribute(SdcArtifactHandlerConstants.ACTION));
661 log.debug("vnfType: " + context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE));
663 log.info("cleanUpVnfcReferencesForVnf()::Query:" + key1);
664 if (serviceLogic != null) {
665 status = serviceLogic.save("SQL", false, false, key1, null, null, context);
666 if (status.toString().equals(FAILURE_PARAM)) {
667 log.debug("Error deleting from VNFC_REFERENCE table");
668 throw new SvcLogicException("Error While processing VNFC_REFERENCE table ");
671 } catch (Exception e) {
672 log.debug("Error deleting from VNFC_REFERENCE table : "
673 + context.getAttribute(SdcArtifactHandlerConstants.ACTION) + " and "
674 + context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE), e);
679 public boolean isUpdateRequiredForTemplates(String queryPart, SvcLogicContext context, String db) throws DBException {
681 log.info("Checking if Update required for this data");
683 log.info("ACTION=" + context.getAttribute(SdcArtifactHandlerConstants.ACTION));
684 log.info("VNF_TYPE=" + context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE));
688 whereClause = WHERE_VNF_TYPE_QUERY_STR + SdcArtifactHandlerConstants.VNF_TYPE ;
689 whereClause = resolveWhereClause(context, db, whereClause);
690 whereClause += queryPart;
692 if (!db.equals(SdcArtifactHandlerConstants.DB_DEVICE_AUTHENTICATION)) {
693 String key = "select COUNT(*) from " + db + whereClause;
694 log.info("SELECT String : " + key);
695 status = serviceLogic.query("SQL", false, null, key, null, null, context);
696 checkForFailure(db, status);
697 String count = context.getAttribute("COUNT(*)");
698 log.info("Number of row Returned : " + count + ": " + status + ":");
699 return tryAddCountAttribute(context, count);
702 log.info("Problems validating DB and/or Context ");
705 } catch (SvcLogicException e) {
706 throw new DBException("An error occurred while checking for artifact update", e);
710 public String createQueryListForTemplateIds(String modelId) {
711 String queryPart = " AND ARTIFACT_NAME like '%_" + modelId+".%'";