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 log.info("Getting Artifact ID String :" + key);
108 status = dblib.query(key, ctx, arguments);
109 if (status.toString().equals(FAILURE_PARAM)) {
110 throw new SvcLogicException("Error - getting Artifact ID from database");
112 artifactID = ctx.getAttribute("id");
113 log.info("SDC_ARTIFACTS_ID received as : " + ctx.getAttribute("id"));
118 public QueryStatus saveArtifacts(SvcLogicContext ctx, int intversion) throws SvcLogicException {
119 QueryStatus status = null;
120 if (dblib != null && ctx != null) {
121 String key = "INSERT INTO ASDC_ARTIFACTS " + "SET SERVICE_UUID = $service-uuid , "
122 + " DISTRIBUTION_ID = $distribution-id ," + " SERVICE_NAME = $service-name ,"
123 + " SERVICE_DESCRIPTION = $service-description ," + " RESOURCE_UUID = $resource-uuid ,"
124 + " RESOURCE_INSTANCE_NAME = $resource-instance-name ," + " RESOURCE_NAME = $resource-name ,"
125 + " RESOURCE_VERSION = $resource-version ," + " RESOURCE_TYPE = $resource-type ,"
126 + " ARTIFACT_UUID = $artifact-uuid ," + " ARTIFACT_TYPE = $artifact-type ,"
127 + " ARTIFACT_VERSION = $artifact-version ,"
128 + " ARTIFACT_DESCRIPTION = $artifact-description ," + " INTERNAL_VERSION = $internal-version"
129 + " ," + " ARTIFACT_NAME = $artifact-name ," + " ARTIFACT_CONTENT = $artifact-contents ";
130 ctx.setAttribute("internal-version", Integer.toString(intversion));
131 status = dblib.save(key, ctx);
132 if (status.toString().equals(FAILURE_PARAM)) {
133 throw new SvcLogicException("Error While processing storing Artifact: "
134 + ctx.getAttribute(SdcArtifactHandlerConstants.ARTIFACT_NAME));
140 public QueryStatus logData(SvcLogicContext ctx, String prefix) throws SvcLogicException {
141 QueryStatus status = null;
142 if (dblib != null && ctx != null) {
143 String key = "INSERT INTO CONFIG_TRANSACTION_LOG " + " SET request_id = $request-id , "
144 + " message_type = $log-message-type , " + " message = $log-message ;";
145 status = dblib.save(key, ctx);
146 if (status.toString().equals(FAILURE_PARAM)) {
147 throw new SvcLogicException("Error while logging data");
154 public void processConfigureActionDg(SvcLogicContext context, boolean isUpdate) {
155 log.info("Update Parameter for SDC Reference " + isUpdate);
156 //TODO implement this method
159 public void processSdcReferences(SvcLogicContext context, boolean isUpdate) throws SvcLogicException {
160 processSdcReferences(context, isUpdate, null);
163 public void processSdcReferences(SvcLogicContext context, boolean isUpdate, String modelId) throws SvcLogicException {
166 if (isUpdate && context.getAttribute(SdcArtifactHandlerConstants.FILE_CATEGORY)
167 .equals(SdcArtifactHandlerConstants.CAPABILITY)) {
168 log.info("Updating capability artifact in ASDC_REFERENCE");
169 key = "update " + SdcArtifactHandlerConstants.DB_SDC_REFERENCE + " set ARTIFACT_NAME = $"
170 + SdcArtifactHandlerConstants.ARTIFACT_NAME + " where " + "FILE_CATEGORY = $"
171 + SdcArtifactHandlerConstants.FILE_CATEGORY + " and VNF_TYPE = $"
172 + SdcArtifactHandlerConstants.VNF_TYPE;
173 } else if (isUpdate) {
174 key = "update " + SdcArtifactHandlerConstants.DB_SDC_REFERENCE + " set ARTIFACT_NAME = $"
175 + SdcArtifactHandlerConstants.ARTIFACT_NAME + " where VNFC_TYPE = $"
176 + SdcArtifactHandlerConstants.VNFC_TYPE + " and FILE_CATEGORY = $"
177 + SdcArtifactHandlerConstants.FILE_CATEGORY + " and ACTION = $" + SdcArtifactHandlerConstants.ACTION
178 + " and VNF_TYPE = $" + SdcArtifactHandlerConstants.VNF_TYPE;
179 if (StringUtils.isNotBlank(modelId)) {
180 key += createQueryListForTemplateIds(modelId, context);
183 if (context.getAttribute(SdcArtifactHandlerConstants.FILE_CATEGORY)
184 .equals(SdcArtifactHandlerConstants.CAPABILITY)) {
185 log.info("Inserting new record for capability artifact in ASDC_REFERENCE");
186 key = "insert into " + SdcArtifactHandlerConstants.DB_SDC_REFERENCE + " set VNFC_TYPE = null "
187 + " , FILE_CATEGORY = $" + SdcArtifactHandlerConstants.FILE_CATEGORY + " , VNF_TYPE = $"
188 + SdcArtifactHandlerConstants.VNF_TYPE + " , ACTION = null " + " , ARTIFACT_TYPE = null "
189 + " , ARTIFACT_NAME = $" + SdcArtifactHandlerConstants.ARTIFACT_NAME;
191 key = "insert into " + SdcArtifactHandlerConstants.DB_SDC_REFERENCE + " set VNFC_TYPE = $"
192 + SdcArtifactHandlerConstants.VNFC_TYPE + " , FILE_CATEGORY = $"
193 + SdcArtifactHandlerConstants.FILE_CATEGORY + " , VNF_TYPE = $"
194 + SdcArtifactHandlerConstants.VNF_TYPE + " , ACTION = $" + SdcArtifactHandlerConstants.ACTION
195 + " , ARTIFACT_TYPE = $" + SdcArtifactHandlerConstants.ARTIFACT_TYPE + " , ARTIFACT_NAME = $"
196 + SdcArtifactHandlerConstants.ARTIFACT_NAME;
200 log.info("Insert Key: " + key);
201 status = dblib.save(key, context);
202 if (status.toString().equals(FAILURE_PARAM)) {
203 throw new SvcLogicException("Error While processing sdc_reference table ");
208 public boolean isArtifactUpdateRequired(SvcLogicContext context, String db) throws DBException {
209 return isArtifactUpdateRequired( context, db, null);
212 public boolean isArtifactUpdateRequired(SvcLogicContext context, String db, String modelId)
215 log.info("Checking if Update required for this data");
217 log.info("ACTION=" + context.getAttribute(SdcArtifactHandlerConstants.ACTION));
218 log.info("VNFC_TYPE=" + context.getAttribute(SdcArtifactHandlerConstants.VNFC_TYPE));
219 log.info("VNFC_INSTANCE=" + context.getAttribute(SdcArtifactHandlerConstants.VNFC_INSTANCE));
220 log.info("VM_INSTANCE=" + context.getAttribute(SdcArtifactHandlerConstants.VM_INSTANCE));
221 log.info("VNF_TYPE=" + context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE));
223 //Check for templates
224 //if templates are present - there might be multiple records, so validate
225 if( db.equals(SdcArtifactHandlerConstants.DB_SDC_REFERENCE) && StringUtils.isNotBlank(modelId)) {
226 log.info("ModelId is sent!!");
227 String queryPart = createQueryListForTemplateIds(modelId, context);
228 log.info("Querypart is = " + queryPart);
229 if (isUpdateRequiredForTemplates(queryPart, context, db)) {
230 log.info("Update is Required!!");
233 log.info("Insert is Required!!");
240 whereClause = " where VNF_TYPE = $" + SdcArtifactHandlerConstants.VNF_TYPE;
241 whereClause = resolveWhereClause(context, db, whereClause);
243 String key = "select COUNT(*) from " + db + whereClause;
244 log.info("SELECT String : " + key);
245 status = dblib.query(key, context);
246 checkForFailure(db, status);
247 String count = context.getAttribute("COUNT(*)");
248 log.info("Number of row Returned : " + count + ": " + status + ":");
249 return tryAddCountAttribute(context, count);
252 } catch (SvcLogicException e) {
253 throw new DBException("An error occurred while checking for artifact update", e);
257 private void checkForFailure(String db, QueryStatus status) throws SvcLogicException {
258 if (status.toString().equals(FAILURE_PARAM)) {
259 throw new SvcLogicException("Error while reading data from " + db);
263 private boolean validate(String db) {
264 return db != null && dblib != null;
267 private boolean keyExists(PropertiesConfiguration conf, String property) {
268 if (conf.subset(property) != null) {
269 if (conf.containsKey(property)) {
270 log.info("Key Exists for property" + property + "in southbound.properties file");
274 log.info("Key Does not exists and need to add the key for property" + property
275 + "in southbound.properties file");
280 private boolean tryAddCountAttribute(SvcLogicContext context, String count) {
281 if (count != null && Integer.parseInt(count) > 0) {
282 context.setAttribute(count, null);
290 private String resolveWhereClause(SvcLogicContext context, String db, String whereClause) {
292 if (hasValidAttributes(context, db)) {
293 return whereClause + " and FILE_CATEGORY = $" + SdcArtifactHandlerConstants.FILE_CATEGORY;
294 } else if (db.equals(SdcArtifactHandlerConstants.DB_SDC_REFERENCE)) {
295 return whereClause + " and VNFC_TYPE = $" + SdcArtifactHandlerConstants.VNFC_TYPE
296 + " and FILE_CATEGORY = $" + SdcArtifactHandlerConstants.FILE_CATEGORY + " and ACTION = $"
297 + SdcArtifactHandlerConstants.ACTION;
298 } else if (db.equals(SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE)) {
299 return " where PROTOCOL = $" + SdcArtifactHandlerConstants.DEVICE_PROTOCOL;
300 } else if (db.equals(SdcArtifactHandlerConstants.DB_DEVICE_AUTHENTICATION)) {
301 log.info(" DB validation for Device authentication " + whereClause + " AND PROTOCOL = $"
302 + SdcArtifactHandlerConstants.DEVICE_PROTOCOL + " AND ACTION = $"
303 + SdcArtifactHandlerConstants.ACTION);
304 return whereClause + " AND PROTOCOL = $" + SdcArtifactHandlerConstants.DEVICE_PROTOCOL
305 + " AND ACTION = $" + SdcArtifactHandlerConstants.ACTION;
306 } else if (db.equals(SdcArtifactHandlerConstants.DB_CONFIG_ACTION_DG)) {
307 return whereClause + " and ACTION = $" + SdcArtifactHandlerConstants.ACTION;
308 } else if (db.equals(SdcArtifactHandlerConstants.DB_VNFC_REFERENCE)) {
309 return whereClause + " and ACTION = $" + SdcArtifactHandlerConstants.ACTION
310 + " and VNFC_TYPE = $" + SdcArtifactHandlerConstants.VNFC_TYPE + " and VNFC_INSTANCE = $"
311 + SdcArtifactHandlerConstants.VNFC_INSTANCE + " and VM_INSTANCE = $"
312 + SdcArtifactHandlerConstants.VM_INSTANCE;
318 private boolean hasValidAttributes(SvcLogicContext context, String db) {
319 return db.equals(SdcArtifactHandlerConstants.DB_SDC_REFERENCE)
320 && context.getAttribute(SdcArtifactHandlerConstants.FILE_CATEGORY)
321 .equals(SdcArtifactHandlerConstants.CAPABILITY)
322 && context.getAttribute(SdcArtifactHandlerConstants.ACTION) == null;
325 public void processDeviceInterfaceProtocol(SvcLogicContext context, boolean isUpdate) throws SvcLogicException {
326 log.info("Starting DB operation for Device Interface Protocol " + isUpdate);
330 key = "update " + SdcArtifactHandlerConstants.DB_DEVICE_INTERFACE_PROTOCOL + " set PROTOCOL = $"
331 + SdcArtifactHandlerConstants.DEVICE_PROTOCOL + " , DG_RPC = 'getDeviceRunningConfig' "
332 + " , MODULE = 'APPC' " + " where VNF_TYPE = $" + SdcArtifactHandlerConstants.VNF_TYPE;
335 "insert into " + SdcArtifactHandlerConstants.DB_DEVICE_INTERFACE_PROTOCOL + " set VNF_TYPE = $"
336 + SdcArtifactHandlerConstants.VNF_TYPE + " , PROTOCOL = $"
337 + SdcArtifactHandlerConstants.DEVICE_PROTOCOL + " , DG_RPC = 'getDeviceRunningConfig' "
338 + " , MODULE = 'APPC' ";
341 if (dblib != null && context != null) {
343 status = dblib.save(key, context);
344 if (status.toString().equals(FAILURE_PARAM)) {
345 throw new SvcLogicException("Error While processing DEVICE_INTERFACE_PROTOCOL table ");
350 public void processDeviceAuthentication(SvcLogicContext context, boolean isUpdate)
353 String fn = "DBService.processDeviceAuthentication";
354 log.info(fn + "Starting DB operation for Device Authentication " + isUpdate);
355 String port = context.getAttribute(SdcArtifactHandlerConstants.PORT_NUMBER);
356 String user = context.getAttribute(SdcArtifactHandlerConstants.USER_NAME);
357 String protocol = context.getAttribute(SdcArtifactHandlerConstants.DEVICE_PROTOCOL);
358 String action = context.getAttribute(SdcArtifactHandlerConstants.ACTION);
359 String vnftype = context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE);
361 if (StringUtils.isBlank(port)) {
363 context.setAttribute(SdcArtifactHandlerConstants.PORT_NUMBER, port);
365 if (StringUtils.isBlank(user)) {
367 context.setAttribute(SdcArtifactHandlerConstants.USER_NAME, user);
369 if (isInvalidInput(SdcArtifactHandlerConstants.DEVICE_PROTOCOL, SdcArtifactHandlerConstants.ACTION,
370 SdcArtifactHandlerConstants.VNF_TYPE)) {
371 throw new SvcLogicException(
372 "Error While processing reference File as few or all of parameters VNF_TYPE,PROTOCOL,ACTION are missing ");
375 log.info("Starting DB operation for Device authentication " + isUpdate);
376 log.info("credentials"+user + "user" + "port" + port +"protocol" + protocol + "action" + action + "vnftype" + vnftype);
380 key = "update " + SdcArtifactHandlerConstants.DB_DEVICE_AUTHENTICATION + " set USER_NAME = $"
381 + SdcArtifactHandlerConstants.USER_NAME
382 + " , PORT_NUMBER = $" + SdcArtifactHandlerConstants.PORT_NUMBER + "";
383 if (context.getAttributeKeySet().contains(SdcArtifactHandlerConstants.URL)) {
384 String url = context.getAttribute(SdcArtifactHandlerConstants.URL);
385 if (StringUtils.isBlank(url)) {
387 context.setAttribute(SdcArtifactHandlerConstants.URL, url);
389 key = key + " , URL = $" + SdcArtifactHandlerConstants.URL + " ";
391 key = key + " where VNF_TYPE = $" + SdcArtifactHandlerConstants.VNF_TYPE + " AND PROTOCOL = $"
392 + SdcArtifactHandlerConstants.DEVICE_PROTOCOL + " AND ACTION = $"
393 + SdcArtifactHandlerConstants.ACTION;
395 key = "insert into DEVICE_AUTHENTICATION set VNF_TYPE = $" + SdcArtifactHandlerConstants.VNF_TYPE
396 + " , PROTOCOL = $" + SdcArtifactHandlerConstants.DEVICE_PROTOCOL
397 + " , " + "ACTION = $" + SdcArtifactHandlerConstants.ACTION
398 + " , USER_NAME = $" + SdcArtifactHandlerConstants.USER_NAME
399 + " , PORT_NUMBER = $" + SdcArtifactHandlerConstants.PORT_NUMBER;
400 if (context.getAttributeKeySet().contains(SdcArtifactHandlerConstants.URL)) {
401 String url = context.getAttribute(SdcArtifactHandlerConstants.URL);
402 if (StringUtils.isBlank(url)) {
404 context.setAttribute(SdcArtifactHandlerConstants.URL, url);
406 key = key + " , URL = $" + SdcArtifactHandlerConstants.URL + " ";
410 log.info("Query forDevice authentication " + key);
411 if (dblib != null && context != null) {
413 status = dblib.save(key, context);
414 if (status.toString().equals(FAILURE_PARAM)) {
415 throw new SvcLogicException("Error While processing DEVICE_AUTHENTICATION table ");
419 } catch (SvcLogicException e) {
421 throw new DBException("An error occurred when processing device authentication", e);
425 private boolean isInvalidInput(String protocol, String action, String vnfType) {
426 return isInvalid(vnfType) && isInvalid(action) && isInvalid(protocol);
429 private boolean isInvalid(String str) {
430 return (str == null) || ("".equals(str));
433 public void processVnfcReference(SvcLogicContext context, boolean isUpdate) throws SvcLogicException {
434 String fn = "DBService.processVnfcReference";
435 log.info(fn + "Starting DB operation for Vnfc Reference " + isUpdate);
439 if (context.getAttribute(SdcArtifactHandlerConstants.VM_INSTANCE) != null) {
440 vmInstance = Integer.parseInt(context.getAttribute(SdcArtifactHandlerConstants.VM_INSTANCE));
443 int vnfcInstance = -1;
444 if (context.getAttribute(SdcArtifactHandlerConstants.VNFC_INSTANCE) != null) {
445 vnfcInstance = Integer.parseInt(context.getAttribute(SdcArtifactHandlerConstants.VNFC_INSTANCE));
450 key = "update " + SdcArtifactHandlerConstants.DB_VNFC_REFERENCE + " set VM_INSTANCE = " + vmInstance
451 + " , VNFC_INSTANCE = " + vnfcInstance + " , VNFC_TYPE = $" + SdcArtifactHandlerConstants.VNFC_TYPE
452 + " , VNFC_FUNCTION_CODE = $" + SdcArtifactHandlerConstants.VNFC_FUNCTION_CODE
453 + " , GROUP_NOTATION_TYPE = $" + SdcArtifactHandlerConstants.GROUP_NOTATION_TYPE
454 + " , GROUP_NOTATION_VALUE = $" + SdcArtifactHandlerConstants.GROUP_NOTATION_VALUE
455 + " , IPADDRESS_V4_OAM_VIP = $" + SdcArtifactHandlerConstants.IPADDRESS_V4_OAM_VIP
456 + " where VNF_TYPE = $" + SdcArtifactHandlerConstants.VNF_TYPE + " and ACTION = $"
457 + SdcArtifactHandlerConstants.ACTION + " and VNFC_TYPE = $" + SdcArtifactHandlerConstants.VNFC_TYPE
458 + " and VNFC_INSTANCE = $" + SdcArtifactHandlerConstants.VNFC_INSTANCE + " and VM_INSTANCE = $"
459 + SdcArtifactHandlerConstants.VM_INSTANCE;
461 key = "insert into " + SdcArtifactHandlerConstants.DB_VNFC_REFERENCE + " set VNF_TYPE = $"
462 + SdcArtifactHandlerConstants.VNF_TYPE + " , ACTION = $" + SdcArtifactHandlerConstants.ACTION
463 + " , VM_INSTANCE = $" + SdcArtifactHandlerConstants.VM_INSTANCE + " , VNFC_INSTANCE = $"
464 + SdcArtifactHandlerConstants.VNFC_INSTANCE + " , VNFC_TYPE = $"
465 + SdcArtifactHandlerConstants.VNFC_TYPE + " , VNFC_FUNCTION_CODE = $"
466 + SdcArtifactHandlerConstants.VNFC_FUNCTION_CODE + " , TEMPLATE_ID = $"
467 + SdcArtifactHandlerConstants.TEMPLATE_ID + " , GROUP_NOTATION_TYPE = $"
468 + SdcArtifactHandlerConstants.GROUP_NOTATION_TYPE + " , IPADDRESS_V4_OAM_VIP = $"
469 + SdcArtifactHandlerConstants.IPADDRESS_V4_OAM_VIP + " , GROUP_NOTATION_VALUE = $"
470 + SdcArtifactHandlerConstants.GROUP_NOTATION_VALUE;
474 status = dblib.save(key, context);
475 if (status.toString().equals(FAILURE_PARAM)) {
476 throw new SvcLogicException("Error While processing VNFC_REFERENCE table ");
481 public void processDownloadDgReference(SvcLogicContext context, boolean isUpdate)
482 throws SvcLogicException {
483 String fn = "DBService.processDownloadDgReference";
484 log.info(fn + "Starting DB operation for Download DG Reference " + isUpdate);
486 QueryStatus status = null;
490 "update " + SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE + " set DOWNLOAD_CONFIG_DG = $"
491 + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " where PROTOCOL = $"
492 + SdcArtifactHandlerConstants.DEVICE_PROTOCOL;
494 key = "insert into " + SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE
495 + " set DOWNLOAD_CONFIG_DG = $"
496 + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " , PROTOCOL = $"
497 + SdcArtifactHandlerConstants.DEVICE_PROTOCOL;
500 if (dblib != null && context != null) {
501 status = dblib.save(key, context);
503 if ((status == null) || status.toString().equals(FAILURE_PARAM)) {
504 throw new SvcLogicException("Error While processing DOWNLOAD_DG_REFERENCE table ");
508 public void processConfigActionDg(SvcLogicContext context, boolean isUpdate) throws SvcLogicException {
509 String fn = "DBService.processConfigActionDg";
510 log.info(fn + "Starting DB operation for Config DG Action " + isUpdate);
512 QueryStatus status = null;
514 if (context.getAttribute(SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE) != null
515 && context.getAttribute(SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE).length() > 0) {
517 key = "update " + SdcArtifactHandlerConstants.DB_CONFIG_ACTION_DG + " set DOWNLOAD_CONFIG_DG = $"
518 + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " where ACTION = $"
519 + SdcArtifactHandlerConstants.ACTION + " and VNF_TYPE = $"
520 + SdcArtifactHandlerConstants.VNF_TYPE;
522 key = "insert into " + SdcArtifactHandlerConstants.DB_CONFIG_ACTION_DG
523 + " set DOWNLOAD_CONFIG_DG = $"
524 + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " , ACTION = $"
525 + SdcArtifactHandlerConstants.ACTION + " , VNF_TYPE = $" + SdcArtifactHandlerConstants.VNF_TYPE;
529 status = dblib.save(key, context);
531 if ((status == null) || status.toString().equals(FAILURE_PARAM)) {
532 throw new SvcLogicException("Error While processing Configure DG Action table ");
535 log.info("No Update required for Config DG Action");
540 public String getModelDataInformationbyArtifactName(String artifactName) throws SvcLogicException {
541 String fn = "DBService.getVnfData";
542 SvcLogicContext con = new SvcLogicContext();
546 "select VNF_TYPE, VNFC_TYPE, ACTION, FILE_CATEGORY, ARTIFACT_TYPE from ASDC_REFERENCE where ARTIFACT_NAME = ?";
547 ArrayList<String> arguments = new ArrayList<String>();
548 arguments.add(artifactName);
550 log.info(fn + "select Key: " + key);
551 status = dblib.query(key, con, arguments);
552 if (status.toString().equals(FAILURE_PARAM)) {
553 throw new SvcLogicException("Error While processing is ArtifactUpdateRequiredforPD table ");
557 log.info(fn + "Vnf_received :" + con.getAttribute("VNF_TYPE"));
559 return con.getAttribute("VNF_TYPE");
562 public void updateYangContents(SvcLogicContext context, String artifactId, String yangContents)
563 throws SvcLogicException {
564 String fn = "DBService.updateYangContents";
565 log.info(fn + "Starting DB operation for updateYangContents");
567 QueryStatus status = null;
569 key = "update ASDC_ARTIFACTS " + " set ARTIFACT_CONTENT = ?"
570 + " where ASDC_ARTIFACTS_ID = ?";
571 ArrayList<String> arguments = new ArrayList<String>();
572 arguments.add(yangContents);
573 arguments.add(artifactId);
575 if (dblib != null && context != null) {
576 status = dblib.save(key, context, arguments);
578 if ((status == null) || status.toString().equals(FAILURE_PARAM)) {
579 throw new SvcLogicException("Error While processing Configure DG Action table ");
585 public void insertProtocolReference(SvcLogicContext context, String vnfType, String protocol, String action,
586 String actionLevel, String template) throws SvcLogicException {
587 String fn = "DBService.insertProtocolReference";
588 log.info(fn + "Starting DB operation for insertProtocolReference");
590 QueryStatus status = null;
592 key = "insert into PROTOCOL_REFERENCE (ACTION, VNF_TYPE, PROTOCOL, UPDATED_DATE, TEMPLATE, ACTION_LEVEL)"
593 + " values (" + "?, ?, ?, now(), ?, "
595 ArrayList<String> arguments = new ArrayList<String>();
596 arguments.add(action);
597 arguments.add(vnfType);
598 arguments.add(protocol);
599 arguments.add(template);
600 arguments.add(actionLevel);
602 if (dblib != null && context != null) {
603 status = dblib.save(key, context, arguments);
605 if ((status == null) || status.toString().equals(FAILURE_PARAM)) {
606 throw new SvcLogicException("Error While processing insertProtocolReference ");
611 public boolean isProtocolReferenceUpdateRequired(SvcLogicContext context, String vnfType, String protocol,
612 String action, String actionLevel, String template) throws SvcLogicException {
613 SvcLogicContext localContext = new SvcLogicContext();
614 String fn = "DBService.isProtocolReferenceUpdateRequired";
615 log.info(fn + "Starting DB operation for isProtocolReferenceUpdateRequired");
617 String key = "select COUNT(*) from PROTOCOL_REFERENCE where ACTION=? and ACTION_LEVEL=?"
619 ArrayList<String> arguments = new ArrayList<String>();
620 arguments.add(action);
621 arguments.add(actionLevel);
622 arguments.add(vnfType);
623 dblib.query(key, localContext, arguments);
625 String countStr = localContext.getAttribute("COUNT(*)");
626 int count = Integer.parseInt(countStr);
630 public void updateProtocolReference(SvcLogicContext context, String vnfType, String protocol, String action,
631 String actionLevel, String template) throws SvcLogicException {
633 String fn = "DBService.isProtocolReferenceUpdateRequired";
634 log.info(fn + "Starting DB operation for isProtocolReferenceUpdateRequired");
638 key = "update PROTOCOL_REFERENCE set UPDATED_DATE=now(), template=?, protocol =?"
639 + " where ACTION=? and ACTION_LEVEL=? and VNF_TYPE=?";
640 ArrayList<String> arguments = new ArrayList<String>();
641 arguments.add(template);
642 arguments.add(protocol);
643 arguments.add(action);
644 arguments.add(actionLevel);
645 arguments.add(vnfType);
646 status = dblib.save(key, context, arguments);
647 if (status == QueryStatus.FAILURE) {
648 log.info("updateProtocolReference:: Error updating protocol reference");
649 throw new SvcLogicException("Error - updating PROTOCOL_REFERENCE_TABLE in updateProtocolReference");
653 public String getDownLoadDGReference(SvcLogicContext context) throws DBException {
656 String fn = "DBService.setDownLoadDGReference";
657 String downloadConfigDg;
658 log.info(fn + "Setting Download DG Reference from DB");
661 String protocol = context.getAttribute(SdcArtifactHandlerConstants.DEVICE_PROTOCOL);
662 if (StringUtils.isBlank(protocol)) {
663 log.info(fn + " :: Protocol is Blank!! Returning without querying DB");
664 throw new ConfigurationException(fn + ":: Protocol is Blank!! Returning without querying DB");
666 key = "select download_config_dg from " + SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE
667 + " where protocol = ?";
668 ArrayList<String> arguments = new ArrayList<String>();
669 arguments.add(protocol);
670 SvcLogicContext localContext = new SvcLogicContext();
671 status = dblib.query(key, localContext, arguments);
672 if (status == QueryStatus.FAILURE) {
673 log.info(fn + ":: Error retrieving download_config_dg");
674 throw new SvcLogicException("Error retrieving download_config_dg");
676 if (status == QueryStatus.NOT_FOUND) {
677 log.info(fn + ":: NOT_FOUND! No data found for download_config_dg!!");
678 throw new SvcLogicException(fn + ":: NOT_FOUND! No data found for download_config_dg!");
680 downloadConfigDg = localContext.getAttribute("download-config-dg");
681 log.info(fn + "download_config_dg::" + downloadConfigDg);
682 return downloadConfigDg;
683 } catch (SvcLogicException | ConfigurationException e) {
684 throw new DBException("An error occurred when getting DG reference", e);
688 public void cleanUpVnfcReferencesForVnf(SvcLogicContext context) throws SvcLogicException {
690 String key1 = "delete from " + SdcArtifactHandlerConstants.DB_VNFC_REFERENCE + " where action = $"
691 + SdcArtifactHandlerConstants.ACTION + " and vnf_type = $" + SdcArtifactHandlerConstants.VNF_TYPE;
692 log.debug("Action : " + context.getAttribute(SdcArtifactHandlerConstants.ACTION));
693 log.debug("vnfType: " + context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE));
695 log.info("cleanUpVnfcReferencesForVnf()::Query:" + key1);
697 status = dblib.save(key1, context);
698 if (status.toString().equals(FAILURE_PARAM)) {
699 log.debug("Error deleting from VNFC_REFERENCE table");
700 throw new SvcLogicException("Error While processing VNFC_REFERENCE table ");
703 } catch (Exception e) {
704 log.debug("Error deleting from VNFC_REFERENCE table : "
705 + context.getAttribute(SdcArtifactHandlerConstants.ACTION) + " and "
706 + context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE), e);
711 public boolean isUpdateRequiredForTemplates(String queryPart, SvcLogicContext context, String db) throws DBException {
713 log.info("Checking if Update required for this data");
715 log.info("ACTION=" + context.getAttribute(SdcArtifactHandlerConstants.ACTION));
716 log.info("VNF_TYPE=" + context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE));
720 whereClause = " where VNF_TYPE = $" + SdcArtifactHandlerConstants.VNF_TYPE ;
721 whereClause = resolveWhereClause(context, db, whereClause);
722 whereClause += queryPart;
724 if (!db.equals(SdcArtifactHandlerConstants.DB_DEVICE_AUTHENTICATION)) {
725 String key = "select COUNT(*) from " + db + whereClause;
726 log.info("SELECT String : " + key);
727 status = dblib.query(key, context);
728 checkForFailure(db, status);
729 String count = context.getAttribute("COUNT(*)");
730 log.info("Number of row Returned : " + count + ": " + status + ":");
731 return tryAddCountAttribute(context, count);
734 log.info("Problems validating DB and/or Context ");
737 } catch (SvcLogicException e) {
738 throw new DBException("An error occurred while checking for artifact update", e);
742 public String createQueryListForTemplateIds(String modelId, SvcLogicContext context) {
743 String parameter = modelId.replace("%", "!%").replace("_","!_").replace("[","![").replace("]", "!]").replace("!","!!");
744 parameter = "%_" + parameter + ".%";
745 context.setAttribute("model-id", parameter);
746 String queryPart = " AND ARTIFACT_NAME like $model-id ";