2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 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;
31 import java.util.ArrayList;
33 import org.apache.commons.configuration.ConfigurationException;
34 import org.apache.commons.configuration.PropertiesConfiguration;
35 import org.apache.commons.lang.StringUtils;
36 import org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants;
37 import org.onap.ccsdk.sli.core.dblib.DbLibService;
38 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
39 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
40 import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
42 public class DBService {
44 private static final EELFLogger log = EELFManager.getInstance().getLogger(DBService.class);
45 private static final String FAILURE_PARAM = "FAILURE";
46 private static final String RECEIVED_AS = "Internal Version received as1 : ";
48 private DbLibServiceQueries dblib;
49 private static DBService dgGeneralDBService = null;
53 dblib = new DbLibServiceQueries();
57 protected DBService(DbLibService dbLibService) {
59 dblib = new DbLibServiceQueries(dbLibService);
63 protected DBService(DbLibServiceQueries dbLibServiceQueries) {
65 dblib = dbLibServiceQueries;
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 (dblib != null && ctx != null) {
81 String key = "select max(internal_version) as maximum from ASDC_ARTIFACTS WHERE ARTIFACT_NAME = ?";
82 ArrayList<String> arguments = new ArrayList<>();
83 arguments.add(artifactName);
84 log.info("Getting internal Version :" + key);
85 status = dblib.query(key, ctx, arguments);
86 if (status.toString().equals(FAILURE_PARAM)) {
87 throw new SvcLogicException("Error - getting internal Artifact Number");
89 artifactInternalVersion = ctx.getAttribute("maximum");
90 log.info("Internal Version received as : " + artifactInternalVersion);
91 log.info(RECEIVED_AS + ctx.getAttribute("max(internal_version)"));
92 log.info(RECEIVED_AS + ctx.getAttribute("max"));
93 log.info(RECEIVED_AS + ctx.getAttribute("internal_version"));
94 log.info(RECEIVED_AS + ctx.getAttributeKeySet().toString());
96 return artifactInternalVersion;
99 public String getArtifactID(SvcLogicContext ctx, String artifactName) throws SvcLogicException {
101 String artifactID = null;
102 if (dblib != null && ctx != null) {
103 String key = "select max(ASDC_ARTIFACTS_ID) as id from ASDC_ARTIFACTS WHERE ARTIFACT_NAME = ?";
104 ArrayList<String> arguments = new ArrayList<>();
105 arguments.add(artifactName);
106 log.info("Getting Artifact ID String :" + key);
107 status = dblib.query(key, ctx, arguments);
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 (dblib != 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 = $internal-version"
128 + " ," + " ARTIFACT_NAME = $artifact-name ," + " ARTIFACT_CONTENT = $artifact-contents ";
129 ctx.setAttribute("internal-version", Integer.toString(intversion));
130 status = dblib.save(key, 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 (dblib != 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 = dblib.save(key, 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 " + SdcArtifactHandlerConstants.DB_SDC_REFERENCE + " set ARTIFACT_NAME = $"
169 + SdcArtifactHandlerConstants.ARTIFACT_NAME + " where " + "FILE_CATEGORY = $"
170 + SdcArtifactHandlerConstants.FILE_CATEGORY + " and VNF_TYPE = $"
171 + SdcArtifactHandlerConstants.VNF_TYPE;
172 } else if (isUpdate) {
173 key = "update " + SdcArtifactHandlerConstants.DB_SDC_REFERENCE + " set ARTIFACT_NAME = $"
174 + SdcArtifactHandlerConstants.ARTIFACT_NAME + " where VNFC_TYPE = $"
175 + SdcArtifactHandlerConstants.VNFC_TYPE + " and FILE_CATEGORY = $"
176 + SdcArtifactHandlerConstants.FILE_CATEGORY + " and ACTION = $" + SdcArtifactHandlerConstants.ACTION
177 + " and VNF_TYPE = $" + SdcArtifactHandlerConstants.VNF_TYPE;
178 if (StringUtils.isNotBlank(modelId)) {
179 key += createQueryListForTemplateIds(modelId, context);
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 " + SdcArtifactHandlerConstants.DB_SDC_REFERENCE + " set VNFC_TYPE = null "
186 + " , FILE_CATEGORY = $" + SdcArtifactHandlerConstants.FILE_CATEGORY + " , VNF_TYPE = $"
187 + SdcArtifactHandlerConstants.VNF_TYPE + " , ACTION = null " + " , ARTIFACT_TYPE = null "
188 + " , ARTIFACT_NAME = $" + SdcArtifactHandlerConstants.ARTIFACT_NAME;
190 key = "insert into " + SdcArtifactHandlerConstants.DB_SDC_REFERENCE + " set VNFC_TYPE = $"
191 + SdcArtifactHandlerConstants.VNFC_TYPE + " , FILE_CATEGORY = $"
192 + SdcArtifactHandlerConstants.FILE_CATEGORY + " , VNF_TYPE = $"
193 + SdcArtifactHandlerConstants.VNF_TYPE + " , ACTION = $" + SdcArtifactHandlerConstants.ACTION
194 + " , ARTIFACT_TYPE = $" + SdcArtifactHandlerConstants.ARTIFACT_TYPE + " , ARTIFACT_NAME = $"
195 + SdcArtifactHandlerConstants.ARTIFACT_NAME;
199 log.info("Insert Key: " + key);
200 status = dblib.save(key, 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, context);
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 = $" + 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 = dblib.query(key, 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 && dblib != 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_CATEGORY = $" + SdcArtifactHandlerConstants.FILE_CATEGORY;
293 } else if (db.equals(SdcArtifactHandlerConstants.DB_SDC_REFERENCE)) {
294 return whereClause + " and VNFC_TYPE = $" + SdcArtifactHandlerConstants.VNFC_TYPE
295 + " and FILE_CATEGORY = $" + SdcArtifactHandlerConstants.FILE_CATEGORY + " and ACTION = $"
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 = $" + SdcArtifactHandlerConstants.ACTION;
307 } else if (db.equals(SdcArtifactHandlerConstants.DB_VNFC_REFERENCE)) {
308 return whereClause + " and ACTION = $" + SdcArtifactHandlerConstants.ACTION
309 + " and VNFC_TYPE = $" + 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 " + SdcArtifactHandlerConstants.DB_DEVICE_INTERFACE_PROTOCOL + " set PROTOCOL = $"
330 + SdcArtifactHandlerConstants.DEVICE_PROTOCOL + " , DG_RPC = 'getDeviceRunningConfig' "
331 + " , MODULE = 'APPC' " + " where VNF_TYPE = $" + SdcArtifactHandlerConstants.VNF_TYPE;
334 "insert into " + 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 (dblib != null && context != null) {
342 status = dblib.save(key, 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)) {
362 context.setAttribute(SdcArtifactHandlerConstants.PORT_NUMBER, port);
364 if (StringUtils.isBlank(user)) {
366 context.setAttribute(SdcArtifactHandlerConstants.USER_NAME, user);
368 if (isInvalidInput(SdcArtifactHandlerConstants.DEVICE_PROTOCOL, SdcArtifactHandlerConstants.ACTION,
369 SdcArtifactHandlerConstants.VNF_TYPE)) {
370 throw new SvcLogicException(
371 "Error While processing reference File as few or all of parameters VNF_TYPE,PROTOCOL,ACTION are missing ");
374 log.info("Starting DB operation for Device authentication " + isUpdate);
375 log.info("credentials"+user + "user" + "port" + port +"protocol" + protocol + "action" + action + "vnftype" + vnftype);
379 key = "update " + SdcArtifactHandlerConstants.DB_DEVICE_AUTHENTICATION + " set USER_NAME = $"
380 + SdcArtifactHandlerConstants.USER_NAME
381 + " , PORT_NUMBER = $" + SdcArtifactHandlerConstants.PORT_NUMBER + "";
382 if (context.getAttributeKeySet().contains(SdcArtifactHandlerConstants.URL)) {
383 String url = context.getAttribute(SdcArtifactHandlerConstants.URL);
384 if (StringUtils.isBlank(url)) {
386 context.setAttribute(SdcArtifactHandlerConstants.URL, url);
388 key = key + " , URL = $" + SdcArtifactHandlerConstants.URL + " ";
390 key = key + " where VNF_TYPE = $" + SdcArtifactHandlerConstants.VNF_TYPE + " AND PROTOCOL = $"
391 + SdcArtifactHandlerConstants.DEVICE_PROTOCOL + " AND ACTION = $"
392 + SdcArtifactHandlerConstants.ACTION;
394 key = "insert into DEVICE_AUTHENTICATION set VNF_TYPE = $" + SdcArtifactHandlerConstants.VNF_TYPE
395 + " , PROTOCOL = $" + SdcArtifactHandlerConstants.DEVICE_PROTOCOL
396 + " , " + "ACTION = $" + SdcArtifactHandlerConstants.ACTION
397 + " , USER_NAME = $" + SdcArtifactHandlerConstants.USER_NAME
398 + " , PORT_NUMBER = $" + SdcArtifactHandlerConstants.PORT_NUMBER;
399 if (context.getAttributeKeySet().contains(SdcArtifactHandlerConstants.URL)) {
400 String url = context.getAttribute(SdcArtifactHandlerConstants.URL);
401 if (StringUtils.isBlank(url)) {
403 context.setAttribute(SdcArtifactHandlerConstants.URL, url);
405 key = key + " , URL = $" + SdcArtifactHandlerConstants.URL + " ";
409 log.info("Query forDevice authentication " + key);
410 if (dblib != null && context != null) {
412 status = dblib.save(key, context);
413 if (status.toString().equals(FAILURE_PARAM)) {
414 throw new SvcLogicException("Error While processing DEVICE_AUTHENTICATION table ");
418 } catch (SvcLogicException e) {
420 throw new DBException("An error occurred when processing device authentication", e);
424 private boolean isInvalidInput(String protocol, String action, String vnfType) {
425 return isInvalid(vnfType) && isInvalid(action) && isInvalid(protocol);
428 private boolean isInvalid(String str) {
429 return (str == null) || ("".equals(str));
432 public void processVnfcReference(SvcLogicContext context, boolean isUpdate) throws SvcLogicException {
433 String fn = "DBService.processVnfcReference";
434 log.info(fn + "Starting DB operation for Vnfc Reference " + isUpdate);
438 if (context.getAttribute(SdcArtifactHandlerConstants.VM_INSTANCE) != null) {
439 vmInstance = Integer.parseInt(context.getAttribute(SdcArtifactHandlerConstants.VM_INSTANCE));
442 int vnfcInstance = -1;
443 if (context.getAttribute(SdcArtifactHandlerConstants.VNFC_INSTANCE) != null) {
444 vnfcInstance = Integer.parseInt(context.getAttribute(SdcArtifactHandlerConstants.VNFC_INSTANCE));
449 key = "update " + SdcArtifactHandlerConstants.DB_VNFC_REFERENCE + " set VM_INSTANCE = " + vmInstance
450 + " , VNFC_INSTANCE = " + vnfcInstance + " , VNFC_TYPE = $" + SdcArtifactHandlerConstants.VNFC_TYPE
451 + " , VNFC_FUNCTION_CODE = $" + SdcArtifactHandlerConstants.VNFC_FUNCTION_CODE
452 + " , GROUP_NOTATION_TYPE = $" + SdcArtifactHandlerConstants.GROUP_NOTATION_TYPE
453 + " , GROUP_NOTATION_VALUE = $" + SdcArtifactHandlerConstants.GROUP_NOTATION_VALUE
454 + " , IPADDRESS_V4_OAM_VIP = $" + SdcArtifactHandlerConstants.IPADDRESS_V4_OAM_VIP
455 + " where VNF_TYPE = $" + SdcArtifactHandlerConstants.VNF_TYPE + " and ACTION = $"
456 + SdcArtifactHandlerConstants.ACTION + " and VNFC_TYPE = $" + SdcArtifactHandlerConstants.VNFC_TYPE
457 + " and VNFC_INSTANCE = $" + SdcArtifactHandlerConstants.VNFC_INSTANCE + " and VM_INSTANCE = $"
458 + SdcArtifactHandlerConstants.VM_INSTANCE;
460 key = "insert into " + SdcArtifactHandlerConstants.DB_VNFC_REFERENCE + " set VNF_TYPE = $"
461 + SdcArtifactHandlerConstants.VNF_TYPE + " , ACTION = $" + SdcArtifactHandlerConstants.ACTION
462 + " , VM_INSTANCE = $" + SdcArtifactHandlerConstants.VM_INSTANCE + " , VNFC_INSTANCE = $"
463 + SdcArtifactHandlerConstants.VNFC_INSTANCE + " , VNFC_TYPE = $"
464 + SdcArtifactHandlerConstants.VNFC_TYPE + " , VNFC_FUNCTION_CODE = $"
465 + SdcArtifactHandlerConstants.VNFC_FUNCTION_CODE + " , TEMPLATE_ID = $"
466 + SdcArtifactHandlerConstants.TEMPLATE_ID + " , GROUP_NOTATION_TYPE = $"
467 + SdcArtifactHandlerConstants.GROUP_NOTATION_TYPE + " , IPADDRESS_V4_OAM_VIP = $"
468 + SdcArtifactHandlerConstants.IPADDRESS_V4_OAM_VIP + " , GROUP_NOTATION_VALUE = $"
469 + SdcArtifactHandlerConstants.GROUP_NOTATION_VALUE;
473 status = dblib.save(key, context);
474 if (status.toString().equals(FAILURE_PARAM)) {
475 throw new SvcLogicException("Error While processing VNFC_REFERENCE table ");
480 public void processDownloadDgReference(SvcLogicContext context, boolean isUpdate)
481 throws SvcLogicException {
482 String fn = "DBService.processDownloadDgReference";
483 log.info(fn + "Starting DB operation for Download DG Reference " + isUpdate);
485 QueryStatus status = null;
489 "update " + SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE + " set DOWNLOAD_CONFIG_DG = $"
490 + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " where PROTOCOL = $"
491 + SdcArtifactHandlerConstants.DEVICE_PROTOCOL;
493 key = "insert into " + SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE
494 + " set DOWNLOAD_CONFIG_DG = $"
495 + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " , PROTOCOL = $"
496 + SdcArtifactHandlerConstants.DEVICE_PROTOCOL;
499 if (dblib != null && context != null) {
500 status = dblib.save(key, context);
502 if ((status == null) || status.toString().equals(FAILURE_PARAM)) {
503 throw new SvcLogicException("Error While processing DOWNLOAD_DG_REFERENCE table ");
507 public void processConfigActionDg(SvcLogicContext context, boolean isUpdate) throws SvcLogicException {
508 String fn = "DBService.processConfigActionDg";
509 log.info(fn + "Starting DB operation for Config DG Action " + isUpdate);
511 QueryStatus status = null;
513 if (context.getAttribute(SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE) != null
514 && context.getAttribute(SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE).length() > 0) {
516 key = "update " + SdcArtifactHandlerConstants.DB_CONFIG_ACTION_DG + " set DOWNLOAD_CONFIG_DG = $"
517 + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " where ACTION = $"
518 + SdcArtifactHandlerConstants.ACTION + " and VNF_TYPE = $"
519 + SdcArtifactHandlerConstants.VNF_TYPE;
521 key = "insert into " + SdcArtifactHandlerConstants.DB_CONFIG_ACTION_DG
522 + " set DOWNLOAD_CONFIG_DG = $"
523 + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " , ACTION = $"
524 + SdcArtifactHandlerConstants.ACTION + " , VNF_TYPE = $" + SdcArtifactHandlerConstants.VNF_TYPE;
528 status = dblib.save(key, context);
530 if ((status == null) || status.toString().equals(FAILURE_PARAM)) {
531 throw new SvcLogicException("Error While processing Configure DG Action table ");
534 log.info("No Update required for Config DG Action");
539 public String getModelDataInformationbyArtifactName(String artifactName) throws SvcLogicException {
540 String fn = "DBService.getVnfData";
541 SvcLogicContext con = new SvcLogicContext();
545 "select VNF_TYPE, VNFC_TYPE, ACTION, FILE_CATEGORY, ARTIFACT_TYPE from ASDC_REFERENCE where ARTIFACT_NAME = ?";
546 ArrayList<String> arguments = new ArrayList<String>();
547 arguments.add(artifactName);
549 log.info(fn + "select Key: " + key);
550 status = dblib.query(key, con, arguments);
551 if (status.toString().equals(FAILURE_PARAM)) {
552 throw new SvcLogicException("Error While processing is ArtifactUpdateRequiredforPD table ");
556 log.info(fn + "Vnf_received :" + con.getAttribute("VNF_TYPE"));
558 return con.getAttribute("VNF_TYPE");
561 public void updateYangContents(SvcLogicContext context, String artifactId, String yangContents)
562 throws SvcLogicException {
563 String fn = "DBService.updateYangContents";
564 log.info(fn + "Starting DB operation for updateYangContents");
566 QueryStatus status = null;
568 key = "update ASDC_ARTIFACTS " + " set ARTIFACT_CONTENT = ?"
569 + " where ASDC_ARTIFACTS_ID = ?";
570 ArrayList<String> arguments = new ArrayList<String>();
571 arguments.add(yangContents);
572 arguments.add(artifactId);
574 if (dblib != null && context != null) {
575 status = dblib.save(key, context, arguments);
577 if ((status == null) || status.toString().equals(FAILURE_PARAM)) {
578 throw new SvcLogicException("Error While processing Configure DG Action table ");
584 public void insertProtocolReference(SvcLogicContext context, String vnfType, String protocol, String action,
585 String actionLevel, String template) throws SvcLogicException {
586 String fn = "DBService.insertProtocolReference";
587 log.info(fn + "Starting DB operation for insertProtocolReference");
589 QueryStatus status = null;
591 key = "insert into PROTOCOL_REFERENCE (ACTION, VNF_TYPE, PROTOCOL, UPDATED_DATE, TEMPLATE, ACTION_LEVEL)"
592 + " values (" + "?, ?, ?, now(), ?, "
594 ArrayList<String> arguments = new ArrayList<String>();
595 arguments.add(action);
596 arguments.add(vnfType);
597 arguments.add(protocol);
598 arguments.add(template);
599 arguments.add(actionLevel);
601 if (dblib != null && context != null) {
602 status = dblib.save(key, context, arguments);
604 if ((status == null) || status.toString().equals(FAILURE_PARAM)) {
605 throw new SvcLogicException("Error While processing insertProtocolReference ");
610 public boolean isProtocolReferenceUpdateRequired(SvcLogicContext context, String vnfType, String protocol,
611 String action, String actionLevel, String template) throws SvcLogicException {
612 SvcLogicContext localContext = new SvcLogicContext();
613 String fn = "DBService.isProtocolReferenceUpdateRequired";
614 log.info(fn + "Starting DB operation for isProtocolReferenceUpdateRequired");
616 String key = "select COUNT(*) from PROTOCOL_REFERENCE where ACTION=? and ACTION_LEVEL=?"
618 ArrayList<String> arguments = new ArrayList<String>();
619 arguments.add(action);
620 arguments.add(actionLevel);
621 arguments.add(vnfType);
622 dblib.query(key, localContext, arguments);
624 String countStr = localContext.getAttribute("COUNT(*)");
625 int count = Integer.parseInt(countStr);
629 public void updateProtocolReference(SvcLogicContext context, String vnfType, String protocol, String action,
630 String actionLevel, String template) throws SvcLogicException {
632 String fn = "DBService.isProtocolReferenceUpdateRequired";
633 log.info(fn + "Starting DB operation for isProtocolReferenceUpdateRequired");
637 key = "update PROTOCOL_REFERENCE set UPDATED_DATE=now(), template=?, protocol =?"
638 + " where ACTION=? and ACTION_LEVEL=? and VNF_TYPE=?";
639 ArrayList<String> arguments = new ArrayList<String>();
640 arguments.add(template);
641 arguments.add(protocol);
642 arguments.add(action);
643 arguments.add(actionLevel);
644 arguments.add(vnfType);
645 status = dblib.save(key, context, arguments);
646 if (status == QueryStatus.FAILURE) {
647 log.info("updateProtocolReference:: Error updating protocol reference");
648 throw new SvcLogicException("Error - updating PROTOCOL_REFERENCE_TABLE in updateProtocolReference");
652 public String getDownLoadDGReference(SvcLogicContext context) throws DBException {
655 String fn = "DBService.setDownLoadDGReference";
656 String downloadConfigDg;
657 log.info(fn + "Setting Download DG Reference from DB");
660 String protocol = context.getAttribute(SdcArtifactHandlerConstants.DEVICE_PROTOCOL);
661 if (StringUtils.isBlank(protocol)) {
662 log.info(fn + " :: Protocol is Blank!! Returning without querying DB");
663 throw new ConfigurationException(fn + ":: Protocol is Blank!! Returning without querying DB");
665 key = "select download_config_dg from " + SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE
666 + " where protocol = ?";
667 ArrayList<String> arguments = new ArrayList<String>();
668 arguments.add(protocol);
669 SvcLogicContext localContext = new SvcLogicContext();
670 status = dblib.query(key, localContext, arguments);
671 if (status == QueryStatus.FAILURE) {
672 log.info(fn + ":: Error retrieving download_config_dg");
673 throw new SvcLogicException("Error retrieving download_config_dg");
675 if (status == QueryStatus.NOT_FOUND) {
676 log.info(fn + ":: NOT_FOUND! No data found for download_config_dg!!");
677 throw new SvcLogicException(fn + ":: NOT_FOUND! No data found for download_config_dg!");
679 downloadConfigDg = localContext.getAttribute("download-config-dg");
680 log.info(fn + "download_config_dg::" + downloadConfigDg);
681 return downloadConfigDg;
682 } catch (SvcLogicException | ConfigurationException e) {
683 throw new DBException("An error occurred when getting DG reference", e);
687 public void cleanUpVnfcReferencesForVnf(SvcLogicContext context) throws SvcLogicException {
689 String key1 = "delete from " + SdcArtifactHandlerConstants.DB_VNFC_REFERENCE + " where action = $"
690 + SdcArtifactHandlerConstants.ACTION + " and vnf_type = $" + SdcArtifactHandlerConstants.VNF_TYPE;
691 log.debug("Action : " + context.getAttribute(SdcArtifactHandlerConstants.ACTION));
692 log.debug("vnfType: " + context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE));
694 log.info("cleanUpVnfcReferencesForVnf()::Query:" + key1);
696 status = dblib.save(key1, context);
697 if (status.toString().equals(FAILURE_PARAM)) {
698 log.debug("Error deleting from VNFC_REFERENCE table");
699 throw new SvcLogicException("Error While processing VNFC_REFERENCE table ");
702 } catch (Exception e) {
703 log.debug("Error deleting from VNFC_REFERENCE table : "
704 + context.getAttribute(SdcArtifactHandlerConstants.ACTION) + " and "
705 + context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE), e);
710 public boolean isUpdateRequiredForTemplates(String queryPart, SvcLogicContext context, String db) throws DBException {
712 log.info("Checking if Update required for this data");
714 log.info("ACTION=" + context.getAttribute(SdcArtifactHandlerConstants.ACTION));
715 log.info("VNF_TYPE=" + context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE));
719 whereClause = " where VNF_TYPE = $" + SdcArtifactHandlerConstants.VNF_TYPE ;
720 whereClause = resolveWhereClause(context, db, whereClause);
721 whereClause += queryPart;
723 if (!db.equals(SdcArtifactHandlerConstants.DB_DEVICE_AUTHENTICATION)) {
724 String key = "select COUNT(*) from " + db + whereClause;
725 log.info("SELECT String : " + key);
726 status = dblib.query(key, context);
727 checkForFailure(db, status);
728 String count = context.getAttribute("COUNT(*)");
729 log.info("Number of row Returned : " + count + ": " + status + ":");
730 return tryAddCountAttribute(context, count);
733 log.info("Problems validating DB and/or Context ");
736 } catch (SvcLogicException e) {
737 throw new DBException("An error occurred while checking for artifact update", e);
741 public String createQueryListForTemplateIds(String modelId, SvcLogicContext context) {
742 String parameter = modelId.replace("%", "!%").replace("_","!_").replace("[","![").replace("]", "!]").replace("!","!!");
743 parameter = "%_" + parameter + ".%";
744 context.setAttribute("model-id", parameter);
745 String queryPart = " AND ARTIFACT_NAME like $model-id ";