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 Ericsson
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=========================================================
26 package org.onap.appc.artifact.handler.dbservices;
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
30 import org.apache.commons.configuration.ConfigurationException;
31 import org.apache.commons.configuration.PropertiesConfiguration;
32 import org.apache.commons.lang.StringUtils;
33 import org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants;
34 import org.onap.ccsdk.sli.adaptors.resource.sql.SqlResource;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
37 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
38 import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
40 public class DBService {
42 private static final EELFLogger log = EELFManager.getInstance().getLogger(DBService.class);
43 private static final String FAILURE_PARAM = "FAILURE";
44 private static final String RECEIVED_AS = "Internal Version received as1 : ";
45 private static final String SET_DOWNLOAD_CONFIG_QUERY_STR = " set DOWNLOAD_CONFIG_DG = $";
46 private static final String WHERE_VNF_TYPE_QUERY_STR = " where VNF_TYPE = $";
47 private static final String ACTION_QUERY_STR = " , ACTION = $";
48 private static final String VNF_TYPE_QUERY_STR = " , VNF_TYPE = $";
49 private static final String INSERT_INTO_QUERY_STR = "insert into ";
50 private static final String AND_ACTION_QUERY_STR = " and ACTION = $";
51 private static final String AND_FILE_CAT_QUERY_STR = " and FILE_CATEGORY = $";
52 private static final String AND_VNF_TYPE_QUERY_STR = " and VNF_TYPE = $";
53 private static final String UPDATE_QUERY_STR = "update ";
54 private static final String AND_VNFC_TYPE_QUERY_STR = " and VNFC_TYPE = $";
56 private SvcLogicResource serviceLogic;
57 private static DBService dgGeneralDBService = null;
60 if (serviceLogic == null) {
61 serviceLogic = new SqlResource();
65 protected DBService(SqlResource svcLogic) {
66 if (serviceLogic == null) {
67 serviceLogic = svcLogic;
71 public static DBService initialise() {
72 if (dgGeneralDBService == null) {
73 dgGeneralDBService = new DBService();
75 return dgGeneralDBService;
78 public String getInternalVersionNumber(SvcLogicContext ctx, String artifactName, String prefix)
79 throws SvcLogicException {
81 String artifactInternalVersion = null;
82 if (serviceLogic != null && ctx != null) {
83 String key = "select max(internal_version) as maximum from ASDC_ARTIFACTS WHERE ARTIFACT_NAME = '"
85 log.info("Getting internal Version :" + key);
86 status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
87 if (status.toString().equals(FAILURE_PARAM)) {
88 throw new SvcLogicException("Error - getting internal Artifact Number");
90 artifactInternalVersion = ctx.getAttribute("maximum");
91 log.info("Internal Version received as : " + artifactInternalVersion);
92 log.info(RECEIVED_AS + ctx.getAttribute("max(internal_version)"));
93 log.info(RECEIVED_AS + ctx.getAttribute("max"));
94 log.info(RECEIVED_AS + ctx.getAttribute("internal_version"));
95 log.info(RECEIVED_AS + ctx.getAttributeKeySet().toString());
97 return artifactInternalVersion;
100 public String getArtifactID(SvcLogicContext ctx, String artifactName) throws SvcLogicException {
102 String artifactID = null;
103 if (serviceLogic != null && ctx != null) {
104 String key = "select max(ASDC_ARTIFACTS_ID) as id from ASDC_ARTIFACTS WHERE ARTIFACT_NAME = '"
105 + artifactName + "'";
106 log.info("Getting Artifact ID String :" + key);
107 status = serviceLogic.query("SQL", false, null, key, null, null, ctx);
108 if (status.toString().equals(FAILURE_PARAM)) {
109 throw new SvcLogicException("Error - getting Artifact ID from database");
111 artifactID = ctx.getAttribute("id");
112 log.info("SDC_ARTIFACTS_ID received as : " + ctx.getAttribute("id"));
117 public QueryStatus saveArtifacts(SvcLogicContext ctx, int intversion) throws SvcLogicException {
118 QueryStatus status = null;
119 if (serviceLogic != null && ctx != null) {
120 String key = "INSERT INTO ASDC_ARTIFACTS " + "SET SERVICE_UUID = $service-uuid , "
121 + " DISTRIBUTION_ID = $distribution-id ," + " SERVICE_NAME = $service-name ,"
122 + " SERVICE_DESCRIPTION = $service-description ," + " RESOURCE_UUID = $resource-uuid ,"
123 + " RESOURCE_INSTANCE_NAME = $resource-instance-name ," + " RESOURCE_NAME = $resource-name ,"
124 + " RESOURCE_VERSION = $resource-version ," + " RESOURCE_TYPE = $resource-type ,"
125 + " ARTIFACT_UUID = $artifact-uuid ," + " ARTIFACT_TYPE = $artifact-type ,"
126 + " ARTIFACT_VERSION = $artifact-version ,"
127 + " ARTIFACT_DESCRIPTION = $artifact-description ," + " INTERNAL_VERSION = " + intversion
128 + "," + " ARTIFACT_NAME = $artifact-name ," + " ARTIFACT_CONTENT = $artifact-contents ";
130 status = serviceLogic.save("SQL", false, false, key, null, null, ctx);
131 if (status.toString().equals(FAILURE_PARAM)) {
132 throw new SvcLogicException("Error While processing storing Artifact: "
133 + ctx.getAttribute(SdcArtifactHandlerConstants.ARTIFACT_NAME));
139 public QueryStatus logData(SvcLogicContext ctx, String prefix) throws SvcLogicException {
140 QueryStatus status = null;
141 if (serviceLogic != null && ctx != null) {
142 String key = "INSERT INTO CONFIG_TRANSACTION_LOG " + " SET request_id = $request-id , "
143 + " message_type = $log-message-type , " + " message = $log-message ;";
144 status = serviceLogic.save("SQL", false, false, key, null, prefix, ctx);
145 if (status.toString().equals(FAILURE_PARAM)) {
146 throw new SvcLogicException("Error while logging data");
153 public void processConfigureActionDg(SvcLogicContext context, boolean isUpdate) {
154 log.info("Update Parameter for SDC Reference " + isUpdate);
155 //TODO implement this method
158 public void processSdcReferences(SvcLogicContext context, boolean isUpdate) throws SvcLogicException {
159 processSdcReferences(context, isUpdate, null);
162 public void processSdcReferences(SvcLogicContext context, boolean isUpdate, String modelId) throws SvcLogicException {
165 if (isUpdate && context.getAttribute(SdcArtifactHandlerConstants.FILE_CATEGORY)
166 .equals(SdcArtifactHandlerConstants.CAPABILITY)) {
167 log.info("Updating capability artifact in ASDC_REFERENCE");
168 key = UPDATE_QUERY_STR + SdcArtifactHandlerConstants.DB_SDC_REFERENCE + " set ARTIFACT_NAME = $"
169 + SdcArtifactHandlerConstants.ARTIFACT_NAME + " where " + "FILE_CATEGORY = $"
170 + SdcArtifactHandlerConstants.FILE_CATEGORY + AND_VNF_TYPE_QUERY_STR
171 + SdcArtifactHandlerConstants.VNF_TYPE;
172 } else if (isUpdate) {
173 key = UPDATE_QUERY_STR + SdcArtifactHandlerConstants.DB_SDC_REFERENCE + " set ARTIFACT_NAME = $"
174 + SdcArtifactHandlerConstants.ARTIFACT_NAME + " where VNFC_TYPE = $"
175 + SdcArtifactHandlerConstants.VNFC_TYPE + AND_FILE_CAT_QUERY_STR
176 + SdcArtifactHandlerConstants.FILE_CATEGORY + AND_ACTION_QUERY_STR + SdcArtifactHandlerConstants.ACTION
177 + AND_VNF_TYPE_QUERY_STR + SdcArtifactHandlerConstants.VNF_TYPE;
178 if (StringUtils.isNotBlank(modelId)) {
179 key += createQueryListForTemplateIds(modelId);
182 if (context.getAttribute(SdcArtifactHandlerConstants.FILE_CATEGORY)
183 .equals(SdcArtifactHandlerConstants.CAPABILITY)) {
184 log.info("Inserting new record for capability artifact in ASDC_REFERENCE");
185 key = INSERT_INTO_QUERY_STR + SdcArtifactHandlerConstants.DB_SDC_REFERENCE + " set VNFC_TYPE = null "
186 + " , FILE_CATEGORY = $" + SdcArtifactHandlerConstants.FILE_CATEGORY + VNF_TYPE_QUERY_STR
187 + SdcArtifactHandlerConstants.VNF_TYPE + " , ACTION = null " + " , ARTIFACT_TYPE = null "
188 + " , ARTIFACT_NAME = $" + SdcArtifactHandlerConstants.ARTIFACT_NAME;
190 key = INSERT_INTO_QUERY_STR + SdcArtifactHandlerConstants.DB_SDC_REFERENCE + " set VNFC_TYPE = $"
191 + SdcArtifactHandlerConstants.VNFC_TYPE + " , FILE_CATEGORY = $"
192 + SdcArtifactHandlerConstants.FILE_CATEGORY + VNF_TYPE_QUERY_STR
193 + SdcArtifactHandlerConstants.VNF_TYPE + ACTION_QUERY_STR + SdcArtifactHandlerConstants.ACTION
194 + " , ARTIFACT_TYPE = $" + SdcArtifactHandlerConstants.ARTIFACT_TYPE + " , ARTIFACT_NAME = $"
195 + SdcArtifactHandlerConstants.ARTIFACT_NAME;
198 if (serviceLogic != null) {
199 log.info("Insert Key: " + key);
200 status = serviceLogic.save("SQL", false, false, key, null, null, context);
201 if (status.toString().equals(FAILURE_PARAM)) {
202 throw new SvcLogicException("Error While processing sdc_reference table ");
207 public boolean isArtifactUpdateRequired(SvcLogicContext context, String db) throws DBException {
208 return isArtifactUpdateRequired( context, db, null);
211 public boolean isArtifactUpdateRequired(SvcLogicContext context, String db, String modelId)
214 log.info("Checking if Update required for this data");
216 log.info("ACTION=" + context.getAttribute(SdcArtifactHandlerConstants.ACTION));
217 log.info("VNFC_TYPE=" + context.getAttribute(SdcArtifactHandlerConstants.VNFC_TYPE));
218 log.info("VNFC_INSTANCE=" + context.getAttribute(SdcArtifactHandlerConstants.VNFC_INSTANCE));
219 log.info("VM_INSTANCE=" + context.getAttribute(SdcArtifactHandlerConstants.VM_INSTANCE));
220 log.info("VNF_TYPE=" + context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE));
222 //Check for templates
223 //if templates are present - there might be multiple records, so validate
224 if( db.equals(SdcArtifactHandlerConstants.DB_SDC_REFERENCE) && StringUtils.isNotBlank(modelId)) {
225 log.info("ModelId is sent!!");
226 String queryPart = createQueryListForTemplateIds(modelId);
227 log.info("Querypart is = " + queryPart);
228 if (isUpdateRequiredForTemplates(queryPart, context, db)) {
229 log.info("Update is Required!!");
232 log.info("Insert is Required!!");
239 whereClause = WHERE_VNF_TYPE_QUERY_STR + SdcArtifactHandlerConstants.VNF_TYPE;
240 whereClause = resolveWhereClause(context, db, whereClause);
242 String key = "select COUNT(*) from " + db + whereClause;
243 log.info("SELECT String : " + key);
244 status = serviceLogic.query("SQL", false, null, key, null, null, context);
245 checkForFailure(db, status);
246 String count = context.getAttribute("COUNT(*)");
247 log.info("Number of row Returned : " + count + ": " + status + ":");
248 return tryAddCountAttribute(context, count);
251 } catch (SvcLogicException e) {
252 throw new DBException("An error occurred while checking for artifact update", e);
256 private void checkForFailure(String db, QueryStatus status) throws SvcLogicException {
257 if (status.toString().equals(FAILURE_PARAM)) {
258 throw new SvcLogicException("Error while reading data from " + db);
262 private boolean validate(String db) {
263 return db != null && serviceLogic != null;
266 private boolean keyExists(PropertiesConfiguration conf, String property) {
267 if (conf.subset(property) != null) {
268 if (conf.containsKey(property)) {
269 log.info("Key Exists for property" + property + "in southbound.properties file");
273 log.info("Key Does not exists and need to add the key for property" + property
274 + "in southbound.properties file");
279 private boolean tryAddCountAttribute(SvcLogicContext context, String count) {
280 if (count != null && Integer.parseInt(count) > 0) {
281 context.setAttribute(count, null);
289 private String resolveWhereClause(SvcLogicContext context, String db, String whereClause) {
291 if (hasValidAttributes(context, db)) {
292 return whereClause + AND_FILE_CAT_QUERY_STR + SdcArtifactHandlerConstants.FILE_CATEGORY;
293 } else if (db.equals(SdcArtifactHandlerConstants.DB_SDC_REFERENCE)) {
294 return whereClause + AND_VNFC_TYPE_QUERY_STR + SdcArtifactHandlerConstants.VNFC_TYPE
295 + AND_FILE_CAT_QUERY_STR + SdcArtifactHandlerConstants.FILE_CATEGORY + AND_ACTION_QUERY_STR
296 + SdcArtifactHandlerConstants.ACTION;
297 } else if (db.equals(SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE)) {
298 return " where PROTOCOL = $" + SdcArtifactHandlerConstants.DEVICE_PROTOCOL;
299 } else if (db.equals(SdcArtifactHandlerConstants.DB_DEVICE_AUTHENTICATION)) {
300 log.info(" DB validation for Device authentication " + whereClause + " AND PROTOCOL = $"
301 + SdcArtifactHandlerConstants.DEVICE_PROTOCOL + " AND ACTION = $"
302 + SdcArtifactHandlerConstants.ACTION);
303 return whereClause + " AND PROTOCOL = $" + SdcArtifactHandlerConstants.DEVICE_PROTOCOL
304 + " AND ACTION = $" + SdcArtifactHandlerConstants.ACTION;
305 } else if (db.equals(SdcArtifactHandlerConstants.DB_CONFIG_ACTION_DG)) {
306 return whereClause + AND_ACTION_QUERY_STR + SdcArtifactHandlerConstants.ACTION;
307 } else if (db.equals(SdcArtifactHandlerConstants.DB_VNFC_REFERENCE)) {
308 return whereClause + AND_ACTION_QUERY_STR + SdcArtifactHandlerConstants.ACTION
309 + AND_VNFC_TYPE_QUERY_STR + SdcArtifactHandlerConstants.VNFC_TYPE + " and VNFC_INSTANCE = $"
310 + SdcArtifactHandlerConstants.VNFC_INSTANCE + " and VM_INSTANCE = $"
311 + SdcArtifactHandlerConstants.VM_INSTANCE;
317 private boolean hasValidAttributes(SvcLogicContext context, String db) {
318 return db.equals(SdcArtifactHandlerConstants.DB_SDC_REFERENCE)
319 && context.getAttribute(SdcArtifactHandlerConstants.FILE_CATEGORY)
320 .equals(SdcArtifactHandlerConstants.CAPABILITY)
321 && context.getAttribute(SdcArtifactHandlerConstants.ACTION) == null;
324 public void processDeviceInterfaceProtocol(SvcLogicContext context, boolean isUpdate) throws SvcLogicException {
325 log.info("Starting DB operation for Device Interface Protocol " + isUpdate);
329 key = UPDATE_QUERY_STR + SdcArtifactHandlerConstants.DB_DEVICE_INTERFACE_PROTOCOL + " set PROTOCOL = $"
330 + SdcArtifactHandlerConstants.DEVICE_PROTOCOL + " , DG_RPC = 'getDeviceRunningConfig' "
331 + " , MODULE = 'APPC' " + WHERE_VNF_TYPE_QUERY_STR + SdcArtifactHandlerConstants.VNF_TYPE;
334 INSERT_INTO_QUERY_STR + SdcArtifactHandlerConstants.DB_DEVICE_INTERFACE_PROTOCOL + " set VNF_TYPE = $"
335 + SdcArtifactHandlerConstants.VNF_TYPE + " , PROTOCOL = $"
336 + SdcArtifactHandlerConstants.DEVICE_PROTOCOL + " , DG_RPC = 'getDeviceRunningConfig' "
337 + " , MODULE = 'APPC' ";
340 if (serviceLogic != null && context != null) {
342 status = serviceLogic.save("SQL", false, false, key, null, null, context);
343 if (status.toString().equals(FAILURE_PARAM)) {
344 throw new SvcLogicException("Error While processing DEVICE_INTERFACE_PROTOCOL table ");
349 public void processDeviceAuthentication(SvcLogicContext context, boolean isUpdate)
352 String fn = "DBService.processDeviceAuthentication";
353 log.info(fn + "Starting DB operation for Device Authentication " + isUpdate);
354 String port = context.getAttribute(SdcArtifactHandlerConstants.PORT_NUMBER);
355 String user = context.getAttribute(SdcArtifactHandlerConstants.USER_NAME);
356 String protocol = context.getAttribute(SdcArtifactHandlerConstants.DEVICE_PROTOCOL);
357 String action = context.getAttribute(SdcArtifactHandlerConstants.ACTION);
358 String vnftype = context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE);
360 if (StringUtils.isBlank(port)) {
363 if (StringUtils.isBlank(user)) {
366 if (isInvalidInput(SdcArtifactHandlerConstants.DEVICE_PROTOCOL, SdcArtifactHandlerConstants.ACTION,
367 SdcArtifactHandlerConstants.VNF_TYPE)) {
368 throw new SvcLogicException(
369 "Error While processing reference File as few or all of parameters VNF_TYPE,PROTOCOL,ACTION are missing ");
372 log.info("Starting DB operation for Device authentication " + isUpdate);
373 log.info("credentials"+user + "user" + "port" + port +"protocol" + protocol + "action" + action + "vnftype" + vnftype);
377 key = UPDATE_QUERY_STR + SdcArtifactHandlerConstants.DB_DEVICE_AUTHENTICATION + " set USER_NAME = '"
378 + user + "' , PORT_NUMBER = " + port + "";
379 if (context.getAttributeKeySet().contains(SdcArtifactHandlerConstants.URL)) {
380 String url = context.getAttribute(SdcArtifactHandlerConstants.URL);
381 if (StringUtils.isBlank(url)) {
384 key = key + ", URL = '" + url + "' ";
386 key = key + WHERE_VNF_TYPE_QUERY_STR + SdcArtifactHandlerConstants.VNF_TYPE + " AND PROTOCOL = $"
387 + SdcArtifactHandlerConstants.DEVICE_PROTOCOL + " AND ACTION = $"
388 + SdcArtifactHandlerConstants.ACTION;
390 key = "insert into DEVICE_AUTHENTICATION set VNF_TYPE = '" + vnftype + "' , PROTOCOL = '" + protocol
391 + "' , " + "ACTION = '" + action + "' , USER_NAME = '" + user + "' , PORT_NUMBER = '" + port
393 if (context.getAttributeKeySet().contains(SdcArtifactHandlerConstants.URL)) {
394 String url = context.getAttribute(SdcArtifactHandlerConstants.URL);
395 if (StringUtils.isBlank(url)) {
398 key = key + ", URL = '" + url + "' ";
402 log.info("Query forDevice authentication " + key);
403 if (serviceLogic != null && context != null) {
405 status = serviceLogic.save("SQL", false, false, key, null, null, context);
406 if (status.toString().equals(FAILURE_PARAM)) {
407 throw new SvcLogicException("Error While processing DEVICE_AUTHENTICATION table ");
411 } catch (SvcLogicException e) {
413 throw new DBException("An error occurred when processing device authentication", e);
417 private boolean isInvalidInput(String protocol, String action, String vnfType) {
418 return isInvalid(vnfType) && isInvalid(action) && isInvalid(protocol);
421 private boolean isInvalid(String str) {
422 return (str == null) || ("".equals(str));
425 public void processVnfcReference(SvcLogicContext context, boolean isUpdate) throws SvcLogicException {
426 String fn = "DBService.processVnfcReference";
427 log.info(fn + "Starting DB operation for Vnfc Reference " + isUpdate);
431 if (context.getAttribute(SdcArtifactHandlerConstants.VM_INSTANCE) != null) {
432 vmInstance = Integer.parseInt(context.getAttribute(SdcArtifactHandlerConstants.VM_INSTANCE));
435 int vnfcInstance = -1;
436 if (context.getAttribute(SdcArtifactHandlerConstants.VNFC_INSTANCE) != null) {
437 vnfcInstance = Integer.parseInt(context.getAttribute(SdcArtifactHandlerConstants.VNFC_INSTANCE));
442 key = UPDATE_QUERY_STR + SdcArtifactHandlerConstants.DB_VNFC_REFERENCE + " set VM_INSTANCE = " + vmInstance
443 + " , VNFC_INSTANCE = " + vnfcInstance + " , VNFC_TYPE = $" + SdcArtifactHandlerConstants.VNFC_TYPE
444 + " , VNFC_FUNCTION_CODE = $" + SdcArtifactHandlerConstants.VNFC_FUNCTION_CODE
445 + " , GROUP_NOTATION_TYPE = $" + SdcArtifactHandlerConstants.GROUP_NOTATION_TYPE
446 + " , GROUP_NOTATION_VALUE = $" + SdcArtifactHandlerConstants.GROUP_NOTATION_VALUE
447 + " , IPADDRESS_V4_OAM_VIP = $" + SdcArtifactHandlerConstants.IPADDRESS_V4_OAM_VIP
448 + WHERE_VNF_TYPE_QUERY_STR + SdcArtifactHandlerConstants.VNF_TYPE + AND_ACTION_QUERY_STR
449 + SdcArtifactHandlerConstants.ACTION + AND_VNFC_TYPE_QUERY_STR + SdcArtifactHandlerConstants.VNFC_TYPE
450 + " and VNFC_INSTANCE = $" + SdcArtifactHandlerConstants.VNFC_INSTANCE + " and VM_INSTANCE = $"
451 + SdcArtifactHandlerConstants.VM_INSTANCE;
453 key = INSERT_INTO_QUERY_STR + SdcArtifactHandlerConstants.DB_VNFC_REFERENCE + " set VNF_TYPE = $"
454 + SdcArtifactHandlerConstants.VNF_TYPE + ACTION_QUERY_STR + SdcArtifactHandlerConstants.ACTION
455 + " , VM_INSTANCE = $" + SdcArtifactHandlerConstants.VM_INSTANCE + " , VNFC_INSTANCE = $"
456 + SdcArtifactHandlerConstants.VNFC_INSTANCE + " , VNFC_TYPE = $"
457 + SdcArtifactHandlerConstants.VNFC_TYPE + " , VNFC_FUNCTION_CODE = $"
458 + SdcArtifactHandlerConstants.VNFC_FUNCTION_CODE + " , TEMPLATE_ID = $"
459 + SdcArtifactHandlerConstants.TEMPLATE_ID + " , GROUP_NOTATION_TYPE = $"
460 + SdcArtifactHandlerConstants.GROUP_NOTATION_TYPE + " , IPADDRESS_V4_OAM_VIP = $"
461 + SdcArtifactHandlerConstants.IPADDRESS_V4_OAM_VIP + " , GROUP_NOTATION_VALUE = $"
462 + SdcArtifactHandlerConstants.GROUP_NOTATION_VALUE;
465 if (serviceLogic != null) {
466 status = serviceLogic.save("SQL", false, false, key, null, null, context);
467 if (status.toString().equals(FAILURE_PARAM)) {
468 throw new SvcLogicException("Error While processing VNFC_REFERENCE table ");
473 public void processDownloadDgReference(SvcLogicContext context, boolean isUpdate)
474 throws SvcLogicException {
475 String fn = "DBService.processDownloadDgReference";
476 log.info(fn + "Starting DB operation for Download DG Reference " + isUpdate);
478 QueryStatus status = null;
482 UPDATE_QUERY_STR + SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE + SET_DOWNLOAD_CONFIG_QUERY_STR
483 + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " where PROTOCOL = $"
484 + SdcArtifactHandlerConstants.DEVICE_PROTOCOL;
486 key = INSERT_INTO_QUERY_STR + SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE
487 + SET_DOWNLOAD_CONFIG_QUERY_STR
488 + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " , PROTOCOL = $"
489 + SdcArtifactHandlerConstants.DEVICE_PROTOCOL;
492 if (serviceLogic != null && context != null) {
493 status = serviceLogic.save("SQL", false, false, key, null, null, context);
495 if ((status == null) || status.toString().equals(FAILURE_PARAM)) {
496 throw new SvcLogicException("Error While processing DOWNLOAD_DG_REFERENCE table ");
500 public void processConfigActionDg(SvcLogicContext context, boolean isUpdate) throws SvcLogicException {
501 String fn = "DBService.processConfigActionDg";
502 log.info(fn + "Starting DB operation for Config DG Action " + isUpdate);
504 QueryStatus status = null;
506 if (context.getAttribute(SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE) != null
507 && context.getAttribute(SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE).length() > 0) {
509 key = UPDATE_QUERY_STR + SdcArtifactHandlerConstants.DB_CONFIG_ACTION_DG + SET_DOWNLOAD_CONFIG_QUERY_STR
510 + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " where ACTION = $"
511 + SdcArtifactHandlerConstants.ACTION + AND_VNF_TYPE_QUERY_STR
512 + SdcArtifactHandlerConstants.VNF_TYPE;
514 key = INSERT_INTO_QUERY_STR + SdcArtifactHandlerConstants.DB_CONFIG_ACTION_DG
515 + SET_DOWNLOAD_CONFIG_QUERY_STR
516 + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + ACTION_QUERY_STR
517 + SdcArtifactHandlerConstants.ACTION + VNF_TYPE_QUERY_STR + SdcArtifactHandlerConstants.VNF_TYPE;
520 if (serviceLogic != null) {
521 status = serviceLogic.save("SQL", false, false, key, null, null, context);
523 if ((status == null) || status.toString().equals(FAILURE_PARAM)) {
524 throw new SvcLogicException("Error While processing Configure DG Action table ");
527 log.info("No Update required for Config DG Action");
532 public String getModelDataInformationbyArtifactName(String artifactName) throws SvcLogicException {
533 String fn = "DBService.getVnfData";
534 SvcLogicContext con = new SvcLogicContext();
538 "select VNF_TYPE, VNFC_TYPE, ACTION, FILE_CATEGORY, ARTIFACT_TYPE from ASDC_REFERENCE where ARTIFACT_NAME = "
541 if (serviceLogic != null) {
542 log.info(fn + "select Key: " + key);
543 status = serviceLogic.query("SQL", false, null, key, null, null, con);
544 if (status.toString().equals(FAILURE_PARAM)) {
545 throw new SvcLogicException("Error While processing is ArtifactUpdateRequiredforPD table ");
549 log.info(fn + "Vnf_received :" + con.getAttribute("VNF_TYPE"));
551 return con.getAttribute("VNF_TYPE");
554 public void updateYangContents(SvcLogicContext context, String artifactId, String yangContents)
555 throws SvcLogicException {
556 String fn = "DBService.updateYangContents";
557 log.info(fn + "Starting DB operation for updateYangContents");
559 QueryStatus status = null;
561 key = "update ASDC_ARTIFACTS " + " set ARTIFACT_CONTENT = '" + yangContents + "'"
562 + " where ASDC_ARTIFACTS_ID = " + artifactId;
564 if (serviceLogic != null && context != null) {
565 status = serviceLogic.save("SQL", false, false, key, null, null, context);
567 if ((status == null) || status.toString().equals(FAILURE_PARAM)) {
568 throw new SvcLogicException("Error While processing Configure DG Action table ");
574 public void insertProtocolReference(SvcLogicContext context, String vnfType, String protocol, String action,
575 String actionLevel, String template) throws SvcLogicException {
576 String fn = "DBService.insertProtocolReference";
577 log.info(fn + "Starting DB operation for insertProtocolReference");
579 QueryStatus status = null;
581 key = "insert into PROTOCOL_REFERENCE (ACTION, VNF_TYPE, PROTOCOL, UPDATED_DATE, TEMPLATE, ACTION_LEVEL)"
582 + " values (" + "'" + action + "', '" + vnfType + "', '" + protocol + "', now(),'" + template + "', '"
583 + actionLevel + "')";
585 if (serviceLogic != null && context != null) {
586 status = serviceLogic.save("SQL", false, false, key, null, null, context);
588 if ((status == null) || status.toString().equals(FAILURE_PARAM)) {
589 throw new SvcLogicException("Error While processing insertProtocolReference ");
594 public boolean isProtocolReferenceUpdateRequired(SvcLogicContext context, String vnfType, String protocol,
595 String action, String actionLevel, String template) throws SvcLogicException {
596 SvcLogicContext localContext = new SvcLogicContext();
597 String fn = "DBService.isProtocolReferenceUpdateRequired";
598 log.info(fn + "Starting DB operation for isProtocolReferenceUpdateRequired");
600 String key = "select COUNT(*) from PROTOCOL_REFERENCE where ACTION='" + action + "' and ACTION_LEVEL='" + actionLevel
601 + "' and VNF_TYPE='" + vnfType + "'";
602 serviceLogic.query("SQL", false, null, key, null, null, localContext);
604 String countStr = localContext.getAttribute("COUNT(*)");
605 int count = Integer.parseInt(countStr);
609 public void updateProtocolReference(SvcLogicContext context, String vnfType, String protocol, String action,
610 String actionLevel, String template) throws SvcLogicException {
612 String fn = "DBService.isProtocolReferenceUpdateRequired";
613 log.info(fn + "Starting DB operation for isProtocolReferenceUpdateRequired");
617 key = "update PROTOCOL_REFERENCE set UPDATED_DATE=now(), template='" + template + "', protocol ='" + protocol
618 + "' where ACTION='" + action + "' and ACTION_LEVEL='" + actionLevel + "' and VNF_TYPE='" + vnfType
620 status = serviceLogic.save("SQL", false, false, key, null, null, context);
621 if (status == QueryStatus.FAILURE) {
622 log.info("updateProtocolReference:: Error updating protocol reference");
623 throw new SvcLogicException("Error - updating PROTOCOL_REFERENCE_TABLE in updateProtocolReference");
627 public String getDownLoadDGReference(SvcLogicContext context) throws DBException {
630 String fn = "DBService.setDownLoadDGReference";
631 String downloadConfigDg;
632 log.info(fn + "Setting Download DG Reference from DB");
635 String protocol = context.getAttribute(SdcArtifactHandlerConstants.DEVICE_PROTOCOL);
636 if (StringUtils.isBlank(protocol)) {
637 log.info(fn + " :: Protocol is Blank!! Returning without querying DB");
638 throw new ConfigurationException(fn + ":: Protocol is Blank!! Returning without querying DB");
640 key = "select download_config_dg from " + SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE
641 + " where protocol = '" + protocol + "'";
642 SvcLogicContext localContext = new SvcLogicContext();
643 status = serviceLogic.query("SQL", false, null, key, null, null, localContext);
644 if (status == QueryStatus.FAILURE) {
645 log.info(fn + ":: Error retrieving download_config_dg");
646 throw new SvcLogicException("Error retrieving download_config_dg");
648 if (status == QueryStatus.NOT_FOUND) {
649 log.info(fn + ":: NOT_FOUND! No data found for download_config_dg!!");
650 throw new SvcLogicException(fn + ":: NOT_FOUND! No data found for download_config_dg!");
652 downloadConfigDg = localContext.getAttribute("download-config-dg");
653 log.info(fn + "download_config_dg::" + downloadConfigDg);
654 return downloadConfigDg;
655 } catch (SvcLogicException | ConfigurationException e) {
656 throw new DBException("An error occurred when getting DG reference", e);
660 public void cleanUpVnfcReferencesForVnf(SvcLogicContext context) throws SvcLogicException {
662 String key1 = "delete from " + SdcArtifactHandlerConstants.DB_VNFC_REFERENCE + " where action = $"
663 + SdcArtifactHandlerConstants.ACTION + " and vnf_type = $" + SdcArtifactHandlerConstants.VNF_TYPE;
664 log.debug("Action : " + context.getAttribute(SdcArtifactHandlerConstants.ACTION));
665 log.debug("vnfType: " + context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE));
667 log.info("cleanUpVnfcReferencesForVnf()::Query:" + key1);
668 if (serviceLogic != null) {
669 status = serviceLogic.save("SQL", false, false, key1, null, null, context);
670 if (status.toString().equals(FAILURE_PARAM)) {
671 log.debug("Error deleting from VNFC_REFERENCE table");
672 throw new SvcLogicException("Error While processing VNFC_REFERENCE table ");
675 } catch (Exception e) {
676 log.debug("Error deleting from VNFC_REFERENCE table : "
677 + context.getAttribute(SdcArtifactHandlerConstants.ACTION) + " and "
678 + context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE), e);
683 public boolean isUpdateRequiredForTemplates(String queryPart, SvcLogicContext context, String db) throws DBException {
685 log.info("Checking if Update required for this data");
687 log.info("ACTION=" + context.getAttribute(SdcArtifactHandlerConstants.ACTION));
688 log.info("VNF_TYPE=" + context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE));
692 whereClause = WHERE_VNF_TYPE_QUERY_STR + SdcArtifactHandlerConstants.VNF_TYPE ;
693 whereClause = resolveWhereClause(context, db, whereClause);
694 whereClause += queryPart;
696 if (!db.equals(SdcArtifactHandlerConstants.DB_DEVICE_AUTHENTICATION)) {
697 String key = "select COUNT(*) from " + db + whereClause;
698 log.info("SELECT String : " + key);
699 status = serviceLogic.query("SQL", false, null, key, null, null, context);
700 checkForFailure(db, status);
701 String count = context.getAttribute("COUNT(*)");
702 log.info("Number of row Returned : " + count + ": " + status + ":");
703 return tryAddCountAttribute(context, count);
706 log.info("Problems validating DB and/or Context ");
709 } catch (SvcLogicException e) {
710 throw new DBException("An error occurred while checking for artifact update", e);
714 public String createQueryListForTemplateIds(String modelId) {
715 String queryPart = " AND ARTIFACT_NAME like '%_" + modelId + ".%'";