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.adaptors.resource.sql.SqlResource;
38 import org.onap.ccsdk.sli.core.dblib.DbLibService;
39 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
40 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
41 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
42 import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
44 public class DBService {
46 private static final EELFLogger log = EELFManager.getInstance().getLogger(DBService.class);
47 private static final String FAILURE_PARAM = "FAILURE";
48 private static final String RECEIVED_AS = "Internal Version received as1 : ";
50 private DbLibServiceQueries dblib;
51 private static DBService dgGeneralDBService = null;
55 dblib = new DbLibServiceQueries();
59 protected DBService(DbLibService dbLibService) {
61 dblib = new DbLibServiceQueries(dbLibService);
65 protected DBService(DbLibServiceQueries dbLibServiceQueries) {
67 dblib = dbLibServiceQueries;
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 (dblib != null && ctx != null) {
83 String key = "select max(internal_version) as maximum from ASDC_ARTIFACTS WHERE ARTIFACT_NAME = ?";
84 ArrayList<String> arguments = new ArrayList<>();
85 arguments.add(artifactName);
86 log.info("Getting internal Version :" + key);
87 status = dblib.query(key, ctx, arguments);
88 if (status.toString().equals(FAILURE_PARAM)) {
89 throw new SvcLogicException("Error - getting internal Artifact Number");
91 artifactInternalVersion = ctx.getAttribute("maximum");
92 log.info("Internal Version received as : " + artifactInternalVersion);
93 log.info(RECEIVED_AS + ctx.getAttribute("max(internal_version)"));
94 log.info(RECEIVED_AS + ctx.getAttribute("max"));
95 log.info(RECEIVED_AS + ctx.getAttribute("internal_version"));
96 log.info(RECEIVED_AS + ctx.getAttributeKeySet().toString());
98 return artifactInternalVersion;
101 public String getArtifactID(SvcLogicContext ctx, String artifactName) throws SvcLogicException {
103 String artifactID = null;
104 if (dblib != null && ctx != null) {
105 String key = "select max(ASDC_ARTIFACTS_ID) as id from ASDC_ARTIFACTS WHERE ARTIFACT_NAME = ?";
106 ArrayList<String> arguments = new ArrayList<>();
107 arguments.add(artifactName);
108 log.info("Getting Artifact ID String :" + key);
109 status = dblib.query(key, ctx, arguments);
110 if (status.toString().equals(FAILURE_PARAM)) {
111 throw new SvcLogicException("Error - getting Artifact ID from database");
113 artifactID = ctx.getAttribute("id");
114 log.info("SDC_ARTIFACTS_ID received as : " + ctx.getAttribute("id"));
119 public QueryStatus saveArtifacts(SvcLogicContext ctx, int intversion) throws SvcLogicException {
120 QueryStatus status = null;
121 if (dblib != null && ctx != null) {
122 String key = "INSERT INTO ASDC_ARTIFACTS " + "SET SERVICE_UUID = $service-uuid , "
123 + " DISTRIBUTION_ID = $distribution-id ," + " SERVICE_NAME = $service-name ,"
124 + " SERVICE_DESCRIPTION = $service-description ," + " RESOURCE_UUID = $resource-uuid ,"
125 + " RESOURCE_INSTANCE_NAME = $resource-instance-name ," + " RESOURCE_NAME = $resource-name ,"
126 + " RESOURCE_VERSION = $resource-version ," + " RESOURCE_TYPE = $resource-type ,"
127 + " ARTIFACT_UUID = $artifact-uuid ," + " ARTIFACT_TYPE = $artifact-type ,"
128 + " ARTIFACT_VERSION = $artifact-version ,"
129 + " ARTIFACT_DESCRIPTION = $artifact-description ," + " INTERNAL_VERSION = $internal-version"
130 + " ," + " ARTIFACT_NAME = $artifact-name ," + " ARTIFACT_CONTENT = $artifact-contents ";
131 ctx.setAttribute("internal-version", Integer.toString(intversion));
132 status = dblib.save(key, ctx);
133 if (status.toString().equals(FAILURE_PARAM)) {
134 throw new SvcLogicException("Error While processing storing Artifact: "
135 + ctx.getAttribute(SdcArtifactHandlerConstants.ARTIFACT_NAME));
141 public QueryStatus logData(SvcLogicContext ctx, String prefix) throws SvcLogicException {
142 QueryStatus status = null;
143 if (dblib != null && ctx != null) {
144 String key = "INSERT INTO CONFIG_TRANSACTION_LOG " + " SET request_id = $request-id , "
145 + " message_type = $log-message-type , " + " message = $log-message ;";
146 status = dblib.save(key, ctx);
147 if (status.toString().equals(FAILURE_PARAM)) {
148 throw new SvcLogicException("Error while logging data");
155 public void processConfigureActionDg(SvcLogicContext context, boolean isUpdate) {
156 log.info("Update Parameter for SDC Reference " + isUpdate);
157 //TODO implement this method
160 public void processSdcReferences(SvcLogicContext context, boolean isUpdate) throws SvcLogicException {
161 processSdcReferences(context, isUpdate, null);
164 public void processSdcReferences(SvcLogicContext context, boolean isUpdate, String modelId) throws SvcLogicException {
167 if (isUpdate && context.getAttribute(SdcArtifactHandlerConstants.FILE_CATEGORY)
168 .equals(SdcArtifactHandlerConstants.CAPABILITY)) {
169 log.info("Updating capability artifact in ASDC_REFERENCE");
170 key = "update " + SdcArtifactHandlerConstants.DB_SDC_REFERENCE + " set ARTIFACT_NAME = $"
171 + SdcArtifactHandlerConstants.ARTIFACT_NAME + " where " + "FILE_CATEGORY = $"
172 + SdcArtifactHandlerConstants.FILE_CATEGORY + " and VNF_TYPE = $"
173 + SdcArtifactHandlerConstants.VNF_TYPE;
174 } else if (isUpdate) {
175 key = "update " + SdcArtifactHandlerConstants.DB_SDC_REFERENCE + " set ARTIFACT_NAME = $"
176 + SdcArtifactHandlerConstants.ARTIFACT_NAME + " where VNFC_TYPE = $"
177 + SdcArtifactHandlerConstants.VNFC_TYPE + " and FILE_CATEGORY = $"
178 + SdcArtifactHandlerConstants.FILE_CATEGORY + " and ACTION = $" + SdcArtifactHandlerConstants.ACTION
179 + " and VNF_TYPE = $" + SdcArtifactHandlerConstants.VNF_TYPE;
180 if (StringUtils.isNotBlank(modelId)) {
181 key += createQueryListForTemplateIds(modelId, context);
184 if (context.getAttribute(SdcArtifactHandlerConstants.FILE_CATEGORY)
185 .equals(SdcArtifactHandlerConstants.CAPABILITY)) {
186 log.info("Inserting new record for capability artifact in ASDC_REFERENCE");
187 key = "insert into " + SdcArtifactHandlerConstants.DB_SDC_REFERENCE + " set VNFC_TYPE = null "
188 + " , FILE_CATEGORY = $" + SdcArtifactHandlerConstants.FILE_CATEGORY + " , VNF_TYPE = $"
189 + SdcArtifactHandlerConstants.VNF_TYPE + " , ACTION = null " + " , ARTIFACT_TYPE = null "
190 + " , ARTIFACT_NAME = $" + SdcArtifactHandlerConstants.ARTIFACT_NAME;
192 key = "insert into " + SdcArtifactHandlerConstants.DB_SDC_REFERENCE + " set VNFC_TYPE = $"
193 + SdcArtifactHandlerConstants.VNFC_TYPE + " , FILE_CATEGORY = $"
194 + SdcArtifactHandlerConstants.FILE_CATEGORY + " , VNF_TYPE = $"
195 + SdcArtifactHandlerConstants.VNF_TYPE + " , ACTION = $" + SdcArtifactHandlerConstants.ACTION
196 + " , ARTIFACT_TYPE = $" + SdcArtifactHandlerConstants.ARTIFACT_TYPE + " , ARTIFACT_NAME = $"
197 + SdcArtifactHandlerConstants.ARTIFACT_NAME;
201 log.info("Insert Key: " + key);
202 status = dblib.save(key, context);
203 if (status.toString().equals(FAILURE_PARAM)) {
204 throw new SvcLogicException("Error While processing sdc_reference table ");
209 public boolean isArtifactUpdateRequired(SvcLogicContext context, String db) throws DBException {
210 return isArtifactUpdateRequired( context, db, null);
213 public boolean isArtifactUpdateRequired(SvcLogicContext context, String db, String modelId)
216 log.info("Checking if Update required for this data");
218 log.info("ACTION=" + context.getAttribute(SdcArtifactHandlerConstants.ACTION));
219 log.info("VNFC_TYPE=" + context.getAttribute(SdcArtifactHandlerConstants.VNFC_TYPE));
220 log.info("VNFC_INSTANCE=" + context.getAttribute(SdcArtifactHandlerConstants.VNFC_INSTANCE));
221 log.info("VM_INSTANCE=" + context.getAttribute(SdcArtifactHandlerConstants.VM_INSTANCE));
222 log.info("VNF_TYPE=" + context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE));
224 //Check for templates
225 //if templates are present - there might be multiple records, so validate
226 if( db.equals(SdcArtifactHandlerConstants.DB_SDC_REFERENCE) && StringUtils.isNotBlank(modelId)) {
227 log.info("ModelId is sent!!");
228 String queryPart = createQueryListForTemplateIds(modelId, context);
229 log.info("Querypart is = " + queryPart);
230 if (isUpdateRequiredForTemplates(queryPart, context, db)) {
231 log.info("Update is Required!!");
234 log.info("Insert is Required!!");
241 whereClause = " where VNF_TYPE = $" + SdcArtifactHandlerConstants.VNF_TYPE;
242 whereClause = resolveWhereClause(context, db, whereClause);
244 String key = "select COUNT(*) from " + db + whereClause;
245 log.info("SELECT String : " + key);
246 status = dblib.query(key, context);
247 checkForFailure(db, status);
248 String count = context.getAttribute("COUNT(*)");
249 log.info("Number of row Returned : " + count + ": " + status + ":");
250 return tryAddCountAttribute(context, count);
253 } catch (SvcLogicException e) {
254 throw new DBException("An error occurred while checking for artifact update", e);
258 private void checkForFailure(String db, QueryStatus status) throws SvcLogicException {
259 if (status.toString().equals(FAILURE_PARAM)) {
260 throw new SvcLogicException("Error while reading data from " + db);
264 private boolean validate(String db) {
265 return db != null && dblib != null;
268 private boolean keyExists(PropertiesConfiguration conf, String property) {
269 if (conf.subset(property) != null) {
270 if (conf.containsKey(property)) {
271 log.info("Key Exists for property" + property + "in southbound.properties file");
275 log.info("Key Does not exists and need to add the key for property" + property
276 + "in southbound.properties file");
281 private boolean tryAddCountAttribute(SvcLogicContext context, String count) {
282 if (count != null && Integer.parseInt(count) > 0) {
283 context.setAttribute(count, null);
291 private String resolveWhereClause(SvcLogicContext context, String db, String whereClause) {
293 if (hasValidAttributes(context, db)) {
294 return whereClause + " and FILE_CATEGORY = $" + SdcArtifactHandlerConstants.FILE_CATEGORY;
295 } else if (db.equals(SdcArtifactHandlerConstants.DB_SDC_REFERENCE)) {
296 return whereClause + " and VNFC_TYPE = $" + SdcArtifactHandlerConstants.VNFC_TYPE
297 + " and FILE_CATEGORY = $" + SdcArtifactHandlerConstants.FILE_CATEGORY + " and ACTION = $"
298 + SdcArtifactHandlerConstants.ACTION;
299 } else if (db.equals(SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE)) {
300 return " where PROTOCOL = $" + SdcArtifactHandlerConstants.DEVICE_PROTOCOL;
301 } else if (db.equals(SdcArtifactHandlerConstants.DB_DEVICE_AUTHENTICATION)) {
302 log.info(" DB validation for Device authentication " + whereClause + " AND PROTOCOL = $"
303 + SdcArtifactHandlerConstants.DEVICE_PROTOCOL + " AND ACTION = $"
304 + SdcArtifactHandlerConstants.ACTION);
305 return whereClause + " AND PROTOCOL = $" + SdcArtifactHandlerConstants.DEVICE_PROTOCOL
306 + " AND ACTION = $" + SdcArtifactHandlerConstants.ACTION;
307 } else if (db.equals(SdcArtifactHandlerConstants.DB_CONFIG_ACTION_DG)) {
308 return whereClause + " and ACTION = $" + SdcArtifactHandlerConstants.ACTION;
309 } else if (db.equals(SdcArtifactHandlerConstants.DB_VNFC_REFERENCE)) {
310 return whereClause + " and ACTION = $" + SdcArtifactHandlerConstants.ACTION
311 + " and VNFC_TYPE = $" + SdcArtifactHandlerConstants.VNFC_TYPE + " and VNFC_INSTANCE = $"
312 + SdcArtifactHandlerConstants.VNFC_INSTANCE + " and VM_INSTANCE = $"
313 + SdcArtifactHandlerConstants.VM_INSTANCE;
319 private boolean hasValidAttributes(SvcLogicContext context, String db) {
320 return db.equals(SdcArtifactHandlerConstants.DB_SDC_REFERENCE)
321 && context.getAttribute(SdcArtifactHandlerConstants.FILE_CATEGORY)
322 .equals(SdcArtifactHandlerConstants.CAPABILITY)
323 && context.getAttribute(SdcArtifactHandlerConstants.ACTION) == null;
326 public void processDeviceInterfaceProtocol(SvcLogicContext context, boolean isUpdate) throws SvcLogicException {
327 log.info("Starting DB operation for Device Interface Protocol " + isUpdate);
331 key = "update " + SdcArtifactHandlerConstants.DB_DEVICE_INTERFACE_PROTOCOL + " set PROTOCOL = $"
332 + SdcArtifactHandlerConstants.DEVICE_PROTOCOL + " , DG_RPC = 'getDeviceRunningConfig' "
333 + " , MODULE = 'APPC' " + " where VNF_TYPE = $" + SdcArtifactHandlerConstants.VNF_TYPE;
336 "insert into " + SdcArtifactHandlerConstants.DB_DEVICE_INTERFACE_PROTOCOL + " set VNF_TYPE = $"
337 + SdcArtifactHandlerConstants.VNF_TYPE + " , PROTOCOL = $"
338 + SdcArtifactHandlerConstants.DEVICE_PROTOCOL + " , DG_RPC = 'getDeviceRunningConfig' "
339 + " , MODULE = 'APPC' ";
342 if (dblib != null && context != null) {
344 status = dblib.save(key, context);
345 if (status.toString().equals(FAILURE_PARAM)) {
346 throw new SvcLogicException("Error While processing DEVICE_INTERFACE_PROTOCOL table ");
351 public void processDeviceAuthentication(SvcLogicContext context, boolean isUpdate)
354 String fn = "DBService.processDeviceAuthentication";
355 log.info(fn + "Starting DB operation for Device Authentication " + isUpdate);
356 String port = context.getAttribute(SdcArtifactHandlerConstants.PORT_NUMBER);
357 String user = context.getAttribute(SdcArtifactHandlerConstants.USER_NAME);
358 String protocol = context.getAttribute(SdcArtifactHandlerConstants.DEVICE_PROTOCOL);
359 String action = context.getAttribute(SdcArtifactHandlerConstants.ACTION);
360 String vnftype = context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE);
362 if (StringUtils.isBlank(port)) {
364 context.setAttribute(SdcArtifactHandlerConstants.PORT_NUMBER, port);
366 if (StringUtils.isBlank(user)) {
368 context.setAttribute(SdcArtifactHandlerConstants.USER_NAME, user);
370 if (isInvalidInput(SdcArtifactHandlerConstants.DEVICE_PROTOCOL, SdcArtifactHandlerConstants.ACTION,
371 SdcArtifactHandlerConstants.VNF_TYPE)) {
372 throw new SvcLogicException(
373 "Error While processing reference File as few or all of parameters VNF_TYPE,PROTOCOL,ACTION are missing ");
376 log.info("Starting DB operation for Device authentication " + isUpdate);
377 log.info("credentials"+user + "user" + "port" + port +"protocol" + protocol + "action" + action + "vnftype" + vnftype);
381 key = "update " + SdcArtifactHandlerConstants.DB_DEVICE_AUTHENTICATION + " set USER_NAME = $"
382 + SdcArtifactHandlerConstants.USER_NAME
383 + " , PORT_NUMBER = $" + SdcArtifactHandlerConstants.PORT_NUMBER + "";
384 if (context.getAttributeKeySet().contains(SdcArtifactHandlerConstants.URL)) {
385 String url = context.getAttribute(SdcArtifactHandlerConstants.URL);
386 if (StringUtils.isBlank(url)) {
388 context.setAttribute(SdcArtifactHandlerConstants.URL, url);
390 key = key + " , URL = $" + SdcArtifactHandlerConstants.URL + " ";
392 key = key + " where VNF_TYPE = $" + SdcArtifactHandlerConstants.VNF_TYPE + " AND PROTOCOL = $"
393 + SdcArtifactHandlerConstants.DEVICE_PROTOCOL + " AND ACTION = $"
394 + SdcArtifactHandlerConstants.ACTION;
396 key = "insert into DEVICE_AUTHENTICATION set VNF_TYPE = $" + SdcArtifactHandlerConstants.VNF_TYPE
397 + " , PROTOCOL = $" + SdcArtifactHandlerConstants.DEVICE_PROTOCOL
398 + " , " + "ACTION = $" + SdcArtifactHandlerConstants.ACTION
399 + " , USER_NAME = $" + SdcArtifactHandlerConstants.USER_NAME
400 + " , PORT_NUMBER = $" + SdcArtifactHandlerConstants.PORT_NUMBER;
401 if (context.getAttributeKeySet().contains(SdcArtifactHandlerConstants.URL)) {
402 String url = context.getAttribute(SdcArtifactHandlerConstants.URL);
403 if (StringUtils.isBlank(url)) {
405 context.setAttribute(SdcArtifactHandlerConstants.URL, url);
407 key = key + " , URL = $" + SdcArtifactHandlerConstants.URL + " ";
411 log.info("Query forDevice authentication " + key);
412 if (dblib != null && context != null) {
414 status = dblib.save(key, context);
415 if (status.toString().equals(FAILURE_PARAM)) {
416 throw new SvcLogicException("Error While processing DEVICE_AUTHENTICATION table ");
420 } catch (SvcLogicException e) {
422 throw new DBException("An error occurred when processing device authentication", e);
426 private boolean isInvalidInput(String protocol, String action, String vnfType) {
427 return isInvalid(vnfType) && isInvalid(action) && isInvalid(protocol);
430 private boolean isInvalid(String str) {
431 return (str == null) || ("".equals(str));
434 public void processVnfcReference(SvcLogicContext context, boolean isUpdate) throws SvcLogicException {
435 String fn = "DBService.processVnfcReference";
436 log.info(fn + "Starting DB operation for Vnfc Reference " + isUpdate);
440 if (context.getAttribute(SdcArtifactHandlerConstants.VM_INSTANCE) != null) {
441 vmInstance = Integer.parseInt(context.getAttribute(SdcArtifactHandlerConstants.VM_INSTANCE));
444 int vnfcInstance = -1;
445 if (context.getAttribute(SdcArtifactHandlerConstants.VNFC_INSTANCE) != null) {
446 vnfcInstance = Integer.parseInt(context.getAttribute(SdcArtifactHandlerConstants.VNFC_INSTANCE));
451 key = "update " + SdcArtifactHandlerConstants.DB_VNFC_REFERENCE + " set VM_INSTANCE = " + vmInstance
452 + " , VNFC_INSTANCE = " + vnfcInstance + " , VNFC_TYPE = $" + SdcArtifactHandlerConstants.VNFC_TYPE
453 + " , VNFC_FUNCTION_CODE = $" + SdcArtifactHandlerConstants.VNFC_FUNCTION_CODE
454 + " , GROUP_NOTATION_TYPE = $" + SdcArtifactHandlerConstants.GROUP_NOTATION_TYPE
455 + " , GROUP_NOTATION_VALUE = $" + SdcArtifactHandlerConstants.GROUP_NOTATION_VALUE
456 + " , IPADDRESS_V4_OAM_VIP = $" + SdcArtifactHandlerConstants.IPADDRESS_V4_OAM_VIP
457 + " where VNF_TYPE = $" + SdcArtifactHandlerConstants.VNF_TYPE + " and ACTION = $"
458 + SdcArtifactHandlerConstants.ACTION + " and VNFC_TYPE = $" + SdcArtifactHandlerConstants.VNFC_TYPE
459 + " and VNFC_INSTANCE = $" + SdcArtifactHandlerConstants.VNFC_INSTANCE + " and VM_INSTANCE = $"
460 + SdcArtifactHandlerConstants.VM_INSTANCE;
462 key = "insert into " + SdcArtifactHandlerConstants.DB_VNFC_REFERENCE + " set VNF_TYPE = $"
463 + SdcArtifactHandlerConstants.VNF_TYPE + " , ACTION = $" + SdcArtifactHandlerConstants.ACTION
464 + " , VM_INSTANCE = $" + SdcArtifactHandlerConstants.VM_INSTANCE + " , VNFC_INSTANCE = $"
465 + SdcArtifactHandlerConstants.VNFC_INSTANCE + " , VNFC_TYPE = $"
466 + SdcArtifactHandlerConstants.VNFC_TYPE + " , VNFC_FUNCTION_CODE = $"
467 + SdcArtifactHandlerConstants.VNFC_FUNCTION_CODE + " , TEMPLATE_ID = $"
468 + SdcArtifactHandlerConstants.TEMPLATE_ID + " , GROUP_NOTATION_TYPE = $"
469 + SdcArtifactHandlerConstants.GROUP_NOTATION_TYPE + " , IPADDRESS_V4_OAM_VIP = $"
470 + SdcArtifactHandlerConstants.IPADDRESS_V4_OAM_VIP + " , GROUP_NOTATION_VALUE = $"
471 + SdcArtifactHandlerConstants.GROUP_NOTATION_VALUE;
475 status = dblib.save(key, context);
476 if (status.toString().equals(FAILURE_PARAM)) {
477 throw new SvcLogicException("Error While processing VNFC_REFERENCE table ");
482 public void processDownloadDgReference(SvcLogicContext context, boolean isUpdate)
483 throws SvcLogicException {
484 String fn = "DBService.processDownloadDgReference";
485 log.info(fn + "Starting DB operation for Download DG Reference " + isUpdate);
487 QueryStatus status = null;
491 "update " + SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE + " set DOWNLOAD_CONFIG_DG = $"
492 + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " where PROTOCOL = $"
493 + SdcArtifactHandlerConstants.DEVICE_PROTOCOL;
495 key = "insert into " + SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE
496 + " set DOWNLOAD_CONFIG_DG = $"
497 + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " , PROTOCOL = $"
498 + SdcArtifactHandlerConstants.DEVICE_PROTOCOL;
501 if (dblib != null && context != null) {
502 status = dblib.save(key, context);
504 if ((status == null) || status.toString().equals(FAILURE_PARAM)) {
505 throw new SvcLogicException("Error While processing DOWNLOAD_DG_REFERENCE table ");
509 public void processConfigActionDg(SvcLogicContext context, boolean isUpdate) throws SvcLogicException {
510 String fn = "DBService.processConfigActionDg";
511 log.info(fn + "Starting DB operation for Config DG Action " + isUpdate);
513 QueryStatus status = null;
515 if (context.getAttribute(SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE) != null
516 && context.getAttribute(SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE).length() > 0) {
518 key = "update " + SdcArtifactHandlerConstants.DB_CONFIG_ACTION_DG + " set DOWNLOAD_CONFIG_DG = $"
519 + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " where ACTION = $"
520 + SdcArtifactHandlerConstants.ACTION + " and VNF_TYPE = $"
521 + SdcArtifactHandlerConstants.VNF_TYPE;
523 key = "insert into " + SdcArtifactHandlerConstants.DB_CONFIG_ACTION_DG
524 + " set DOWNLOAD_CONFIG_DG = $"
525 + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " , ACTION = $"
526 + SdcArtifactHandlerConstants.ACTION + " , VNF_TYPE = $" + SdcArtifactHandlerConstants.VNF_TYPE;
530 status = dblib.save(key, context);
532 if ((status == null) || status.toString().equals(FAILURE_PARAM)) {
533 throw new SvcLogicException("Error While processing Configure DG Action table ");
536 log.info("No Update required for Config DG Action");
541 public String getModelDataInformationbyArtifactName(String artifactName) throws SvcLogicException {
542 String fn = "DBService.getVnfData";
543 SvcLogicContext con = new SvcLogicContext();
547 "select VNF_TYPE, VNFC_TYPE, ACTION, FILE_CATEGORY, ARTIFACT_TYPE from ASDC_REFERENCE where ARTIFACT_NAME = ?";
548 ArrayList<String> arguments = new ArrayList<String>();
549 arguments.add(artifactName);
551 log.info(fn + "select Key: " + key);
552 status = dblib.query(key, con, arguments);
553 if (status.toString().equals(FAILURE_PARAM)) {
554 throw new SvcLogicException("Error While processing is ArtifactUpdateRequiredforPD table ");
558 log.info(fn + "Vnf_received :" + con.getAttribute("VNF_TYPE"));
560 return con.getAttribute("VNF_TYPE");
563 public void updateYangContents(SvcLogicContext context, String artifactId, String yangContents)
564 throws SvcLogicException {
565 String fn = "DBService.updateYangContents";
566 log.info(fn + "Starting DB operation for updateYangContents");
568 QueryStatus status = null;
570 key = "update ASDC_ARTIFACTS " + " set ARTIFACT_CONTENT = ?"
571 + " where ASDC_ARTIFACTS_ID = ?";
572 ArrayList<String> arguments = new ArrayList<String>();
573 arguments.add(yangContents);
574 arguments.add(artifactId);
576 if (dblib != null && context != null) {
577 status = dblib.save(key, context, arguments);
579 if ((status == null) || status.toString().equals(FAILURE_PARAM)) {
580 throw new SvcLogicException("Error While processing Configure DG Action table ");
586 public void insertProtocolReference(SvcLogicContext context, String vnfType, String protocol, String action,
587 String actionLevel, String template) throws SvcLogicException {
588 String fn = "DBService.insertProtocolReference";
589 log.info(fn + "Starting DB operation for insertProtocolReference");
591 QueryStatus status = null;
593 key = "insert into PROTOCOL_REFERENCE (ACTION, VNF_TYPE, PROTOCOL, UPDATED_DATE, TEMPLATE, ACTION_LEVEL)"
594 + " values (" + "?, ?, ?, now(), ?, "
596 ArrayList<String> arguments = new ArrayList<String>();
597 arguments.add(action);
598 arguments.add(vnfType);
599 arguments.add(protocol);
600 arguments.add(template);
601 arguments.add(actionLevel);
603 if (dblib != null && context != null) {
604 status = dblib.save(key, context, arguments);
606 if ((status == null) || status.toString().equals(FAILURE_PARAM)) {
607 throw new SvcLogicException("Error While processing insertProtocolReference ");
612 public boolean isProtocolReferenceUpdateRequired(SvcLogicContext context, String vnfType, String protocol,
613 String action, String actionLevel, String template) throws SvcLogicException {
614 SvcLogicContext localContext = new SvcLogicContext();
615 String fn = "DBService.isProtocolReferenceUpdateRequired";
616 log.info(fn + "Starting DB operation for isProtocolReferenceUpdateRequired");
618 String key = "select COUNT(*) from PROTOCOL_REFERENCE where ACTION=? and ACTION_LEVEL=?"
620 ArrayList<String> arguments = new ArrayList<String>();
621 arguments.add(action);
622 arguments.add(actionLevel);
623 arguments.add(vnfType);
624 dblib.query(key, localContext, arguments);
626 String countStr = localContext.getAttribute("COUNT(*)");
627 int count = Integer.parseInt(countStr);
631 public void updateProtocolReference(SvcLogicContext context, String vnfType, String protocol, String action,
632 String actionLevel, String template) throws SvcLogicException {
634 String fn = "DBService.isProtocolReferenceUpdateRequired";
635 log.info(fn + "Starting DB operation for isProtocolReferenceUpdateRequired");
639 key = "update PROTOCOL_REFERENCE set UPDATED_DATE=now(), template=?, protocol =?"
640 + " where ACTION=? and ACTION_LEVEL=? and VNF_TYPE=?";
641 ArrayList<String> arguments = new ArrayList<String>();
642 arguments.add(template);
643 arguments.add(protocol);
644 arguments.add(action);
645 arguments.add(actionLevel);
646 arguments.add(vnfType);
647 status = dblib.save(key, context, arguments);
648 if (status == QueryStatus.FAILURE) {
649 log.info("updateProtocolReference:: Error updating protocol reference");
650 throw new SvcLogicException("Error - updating PROTOCOL_REFERENCE_TABLE in updateProtocolReference");
654 public String getDownLoadDGReference(SvcLogicContext context) throws DBException {
657 String fn = "DBService.setDownLoadDGReference";
658 String downloadConfigDg;
659 log.info(fn + "Setting Download DG Reference from DB");
662 String protocol = context.getAttribute(SdcArtifactHandlerConstants.DEVICE_PROTOCOL);
663 if (StringUtils.isBlank(protocol)) {
664 log.info(fn + " :: Protocol is Blank!! Returning without querying DB");
665 throw new ConfigurationException(fn + ":: Protocol is Blank!! Returning without querying DB");
667 key = "select download_config_dg from " + SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE
668 + " where protocol = ?";
669 ArrayList<String> arguments = new ArrayList<String>();
670 arguments.add(protocol);
671 SvcLogicContext localContext = new SvcLogicContext();
672 status = dblib.query(key, localContext, arguments);
673 if (status == QueryStatus.FAILURE) {
674 log.info(fn + ":: Error retrieving download_config_dg");
675 throw new SvcLogicException("Error retrieving download_config_dg");
677 if (status == QueryStatus.NOT_FOUND) {
678 log.info(fn + ":: NOT_FOUND! No data found for download_config_dg!!");
679 throw new SvcLogicException(fn + ":: NOT_FOUND! No data found for download_config_dg!");
681 downloadConfigDg = localContext.getAttribute("download-config-dg");
682 log.info(fn + "download_config_dg::" + downloadConfigDg);
683 return downloadConfigDg;
684 } catch (SvcLogicException | ConfigurationException e) {
685 throw new DBException("An error occurred when getting DG reference", e);
689 public void cleanUpVnfcReferencesForVnf(SvcLogicContext context) throws SvcLogicException {
691 String key1 = "delete from " + SdcArtifactHandlerConstants.DB_VNFC_REFERENCE + " where action = $"
692 + SdcArtifactHandlerConstants.ACTION + " and vnf_type = $" + SdcArtifactHandlerConstants.VNF_TYPE;
693 log.debug("Action : " + context.getAttribute(SdcArtifactHandlerConstants.ACTION));
694 log.debug("vnfType: " + context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE));
696 log.info("cleanUpVnfcReferencesForVnf()::Query:" + key1);
698 status = dblib.save(key1, context);
699 if (status.toString().equals(FAILURE_PARAM)) {
700 log.debug("Error deleting from VNFC_REFERENCE table");
701 throw new SvcLogicException("Error While processing VNFC_REFERENCE table ");
704 } catch (Exception e) {
705 log.debug("Error deleting from VNFC_REFERENCE table : "
706 + context.getAttribute(SdcArtifactHandlerConstants.ACTION) + " and "
707 + context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE), e);
712 public boolean isUpdateRequiredForTemplates(String queryPart, SvcLogicContext context, String db) throws DBException {
714 log.info("Checking if Update required for this data");
716 log.info("ACTION=" + context.getAttribute(SdcArtifactHandlerConstants.ACTION));
717 log.info("VNF_TYPE=" + context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE));
721 whereClause = " where VNF_TYPE = $" + SdcArtifactHandlerConstants.VNF_TYPE ;
722 whereClause = resolveWhereClause(context, db, whereClause);
723 whereClause += queryPart;
725 if (!db.equals(SdcArtifactHandlerConstants.DB_DEVICE_AUTHENTICATION)) {
726 String key = "select COUNT(*) from " + db + whereClause;
727 log.info("SELECT String : " + key);
728 status = dblib.query(key, context);
729 checkForFailure(db, status);
730 String count = context.getAttribute("COUNT(*)");
731 log.info("Number of row Returned : " + count + ": " + status + ":");
732 return tryAddCountAttribute(context, count);
735 log.info("Problems validating DB and/or Context ");
738 } catch (SvcLogicException e) {
739 throw new DBException("An error occurred while checking for artifact update", e);
743 public String createQueryListForTemplateIds(String modelId, SvcLogicContext context) {
744 String parameter = modelId.replace("%", "!%").replace("_","!_").replace("[","![").replace("]", "!]").replace("!","!!");
745 parameter = "%_" + parameter + ".%";
746 context.setAttribute("model-id", parameter);
747 String queryPart = " AND ARTIFACT_NAME like $model-id ";