ef11895a982ae2b3354610d5f0ff4f2d293cdda5
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.artifact.handler.dbservices;
26
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import org.apache.commons.configuration.ConfigurationException;
30 import org.apache.commons.configuration.PropertiesConfiguration;
31 import org.apache.commons.lang.StringUtils;
32 import org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants;
33 import org.onap.ccsdk.sli.adaptors.resource.sql.SqlResource;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
37 import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
38
39 public class DBService {
40
41     private static final EELFLogger log = EELFManager.getInstance().getLogger(DBService.class);
42     private static final String FAILURE_PARAM = "FAILURE";
43     private static final String RECEIVED_AS = "Internal Version received as1 : ";
44     private static final String SET_DOWNLOAD_CONFIG_QUERY_STR = " set DOWNLOAD_CONFIG_DG = $";
45     private static final String WHERE_VNF_TYPE_QUERY_STR = " where VNF_TYPE = $";
46     private static final String ACTION_QUERY_STR = " , ACTION = $";
47     private static final String VNF_TYPE_QUERY_STR = " , VNF_TYPE = $";
48     private static final String INSERT_INTO_QUERY_STR = "insert into ";
49     private static final String AND_ACTION_QUERY_STR = " and ACTION = $";
50     private static final String AND_FILE_CAT_QUERY_STR = " and FILE_CATEGORY = $";
51     private static final String AND_VNF_TYPE_QUERY_STR = " and VNF_TYPE = $";
52     private static final String UPDATE_QUERY_STR = "update ";
53     private static final String AND_VNFC_TYPE_QUERY_STR = " and VNFC_TYPE = $";
54
55     private SvcLogicResource serviceLogic;
56     private static DBService dgGeneralDBService = null;
57
58     private DBService() {
59         if (serviceLogic == null) {
60             serviceLogic = new SqlResource();
61         }
62     }
63
64     protected DBService(SqlResource svcLogic) {
65         if (serviceLogic == null) {
66             serviceLogic = svcLogic;
67         }
68     }
69
70     public static DBService initialise() {
71         if (dgGeneralDBService == null) {
72             dgGeneralDBService = new DBService();
73         }
74         return dgGeneralDBService;
75     }
76
77     public String getInternalVersionNumber(SvcLogicContext ctx, String artifactName, String prefix)
78         throws SvcLogicException {
79         QueryStatus status;
80         String artifactInternalVersion = null;
81         if (serviceLogic != null && ctx != null) {
82             String key = "select max(internal_version) as maximum from ASDC_ARTIFACTS  WHERE ARTIFACT_NAME = '"
83                 + artifactName + "'";
84             log.info("Getting internal Versoin :" + key);
85             status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
86             if (status.toString().equals(FAILURE_PARAM)) {
87                 throw new SvcLogicException("Error - getting internal Artifact Number");
88             }
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());
95         }
96         return artifactInternalVersion;
97     }
98
99     public String getArtifactID(SvcLogicContext ctx, String artifactName) throws SvcLogicException {
100         QueryStatus status;
101         String artifactID = null;
102         if (serviceLogic != null && ctx != null) {
103             String key = "select max(ASDC_ARTIFACTS_ID) as id from ASDC_ARTIFACTS  WHERE ARTIFACT_NAME = '"
104                 + artifactName + "'";
105             log.info("Getting Artifact ID String :" + key);
106             status = serviceLogic.query("SQL", false, null, key, null, null, ctx);
107             if (status.toString().equals(FAILURE_PARAM)) {
108                 throw new SvcLogicException("Error - getting  Artifact ID from database");
109             }
110             artifactID = ctx.getAttribute("id");
111             log.info("SDC_ARTIFACTS_ID received as : " + ctx.getAttribute("id"));
112         }
113         return artifactID;
114     }
115
116     public QueryStatus saveArtifacts(SvcLogicContext ctx, int intversion) throws SvcLogicException {
117         QueryStatus status = null;
118         if (serviceLogic != null && ctx != null) {
119             String key = "INSERT INTO ASDC_ARTIFACTS " + "SET SERVICE_UUID    =  $service-uuid , "
120                 + " DISTRIBUTION_ID    =  $distribution-id ," + " SERVICE_NAME    =  $service-name ,"
121                 + " SERVICE_DESCRIPTION    =  $service-description ," + " RESOURCE_UUID    = $resource-uuid ,"
122                 + " RESOURCE_INSTANCE_NAME    = $resource-instance-name ," + " RESOURCE_NAME    = $resource-name ,"
123                 + " RESOURCE_VERSION    = $resource-version ," + " RESOURCE_TYPE    = $resource-type ,"
124                 + " ARTIFACT_UUID    = $artifact-uuid ," + " ARTIFACT_TYPE    = $artifact-type ,"
125                 + " ARTIFACT_VERSION    = $artifact-version ,"
126                 + " ARTIFACT_DESCRIPTION    = $artifact-description ," + " INTERNAL_VERSION    = " + intversion
127                 + "," + " ARTIFACT_NAME       =  $artifact-name ," + " ARTIFACT_CONTENT    =  $artifact-contents ";
128
129             status = serviceLogic.save("SQL", false, false, key, null, null, ctx);
130             if (status.toString().equals(FAILURE_PARAM)) {
131                 throw new SvcLogicException("Error While processing storing Artifact: "
132                     + ctx.getAttribute(SdcArtifactHandlerConstants.ARTIFACT_NAME));
133             }
134         }
135         return status;
136     }
137
138     public QueryStatus logData(SvcLogicContext ctx, String prefix) throws SvcLogicException {
139         QueryStatus status = null;
140         if (serviceLogic != null && ctx != null) {
141             String key = "INSERT INTO CONFIG_TRANSACTION_LOG " + " SET request_id = $request-id , "
142                 + " message_type = $log-message-type , " + " message = $log-message ;";
143             status = serviceLogic.save("SQL", false, false, key, null, prefix, ctx);
144             if (status.toString().equals(FAILURE_PARAM)) {
145                 throw new SvcLogicException("Error while loging data");
146             }
147
148         }
149         return status;
150     }
151
152     public void processConfigureActionDg(SvcLogicContext context, boolean isUpdate) {
153         log.info("Update Parameter for SDC Reference " + isUpdate);
154         //TODO implement this method
155     }
156
157     public void processSdcReferences(SvcLogicContext context, boolean isUpdate) throws SvcLogicException {
158         String key;
159         QueryStatus status;
160
161         if (isUpdate && context.getAttribute(SdcArtifactHandlerConstants.FILE_CATEGORY)
162             .equals(SdcArtifactHandlerConstants.CAPABILITY)) {
163             log.info("Updating capability artifact in ASDC_REFERENCE");
164             key = UPDATE_QUERY_STR + SdcArtifactHandlerConstants.DB_SDC_REFERENCE + "  set ARTIFACT_NAME = $"
165                 + SdcArtifactHandlerConstants.ARTIFACT_NAME + " where " + "FILE_CATEGORY = $"
166                 + SdcArtifactHandlerConstants.FILE_CATEGORY + AND_VNF_TYPE_QUERY_STR
167                 + SdcArtifactHandlerConstants.VNF_TYPE;
168         } else if (isUpdate) {
169             key = UPDATE_QUERY_STR + SdcArtifactHandlerConstants.DB_SDC_REFERENCE + "  set ARTIFACT_NAME = $"
170                 + SdcArtifactHandlerConstants.ARTIFACT_NAME + " where VNFC_TYPE = $"
171                 + SdcArtifactHandlerConstants.VNFC_TYPE + AND_FILE_CAT_QUERY_STR
172                 + SdcArtifactHandlerConstants.FILE_CATEGORY + AND_ACTION_QUERY_STR + SdcArtifactHandlerConstants.ACTION
173                 + AND_VNF_TYPE_QUERY_STR + SdcArtifactHandlerConstants.VNF_TYPE;
174         } else {
175             if (context.getAttribute(SdcArtifactHandlerConstants.FILE_CATEGORY)
176                 .equals(SdcArtifactHandlerConstants.CAPABILITY)) {
177                 log.info("Inserting new record for capability artifact in ASDC_REFERENCE");
178                 key = INSERT_INTO_QUERY_STR + SdcArtifactHandlerConstants.DB_SDC_REFERENCE + " set VNFC_TYPE = null "
179                     + " , FILE_CATEGORY = $" + SdcArtifactHandlerConstants.FILE_CATEGORY + VNF_TYPE_QUERY_STR
180                     + SdcArtifactHandlerConstants.VNF_TYPE + " , ACTION = null " + " , ARTIFACT_TYPE = null "
181                     + " , ARTIFACT_NAME = $" + SdcArtifactHandlerConstants.ARTIFACT_NAME;
182             } else {
183                 key = INSERT_INTO_QUERY_STR + SdcArtifactHandlerConstants.DB_SDC_REFERENCE + " set VNFC_TYPE = $"
184                     + SdcArtifactHandlerConstants.VNFC_TYPE + " , FILE_CATEGORY = $"
185                     + SdcArtifactHandlerConstants.FILE_CATEGORY + VNF_TYPE_QUERY_STR
186                     + SdcArtifactHandlerConstants.VNF_TYPE + ACTION_QUERY_STR + SdcArtifactHandlerConstants.ACTION
187                     + " , ARTIFACT_TYPE = $" + SdcArtifactHandlerConstants.ARTIFACT_TYPE + " , ARTIFACT_NAME = $"
188                     + SdcArtifactHandlerConstants.ARTIFACT_NAME;
189             }
190         }
191         if (serviceLogic != null) {
192             log.info("Insert Key: " + key);
193             status = serviceLogic.save("SQL", false, false, key, null, null, context);
194             if (status.toString().equals(FAILURE_PARAM)) {
195                 throw new SvcLogicException("Error While processing sdc_reference table ");
196             }
197         }
198     }
199
200     public boolean isArtifactUpdateRequired(SvcLogicContext context, String db)
201         throws DBException {
202         try {
203             log.info("Checking if Update required for this data");
204             log.info("db" + db);
205             log.info("ACTION=" + context.getAttribute(SdcArtifactHandlerConstants.ACTION));
206             log.info("VNFC_TYPE=" + context.getAttribute(SdcArtifactHandlerConstants.VNFC_TYPE));
207             log.info("VNFC_INSTANCE=" + context.getAttribute(SdcArtifactHandlerConstants.VNFC_INSTANCE));
208             log.info("VM_INSTANCE=" + context.getAttribute(SdcArtifactHandlerConstants.VM_INSTANCE));
209             log.info("VNF_TYPE=" + context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE));
210             String whereClause;
211             QueryStatus status;
212             whereClause = WHERE_VNF_TYPE_QUERY_STR + SdcArtifactHandlerConstants.VNF_TYPE;
213             whereClause = resolveWhereClause(context, db, whereClause);
214             if (validate(db)) {
215                 if (!db.equals(SdcArtifactHandlerConstants.DB_DEVICE_AUTHENTICATION)) {
216                     String key = "select COUNT(*) from " + db + whereClause;
217                     log.info("SELECT String : " + key);
218                     status = serviceLogic.query("SQL", false, null, key, null, null, context);
219                     checkForFailure(db, status);
220                     String count = context.getAttribute("COUNT(*)");
221                     log.info("Number of row Returned : " + count + ": " + status + ":");
222                     return tryAddCountAttribute(context, count);
223                 } else {
224                     log.info("Check for update or insert for properties file");
225                     String protocol = context.getAttribute(SdcArtifactHandlerConstants.DEVICE_PROTOCOL);
226                     String action = context.getAttribute(SdcArtifactHandlerConstants.ACTION);
227                     String vnfType = context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE);
228                     PropertiesConfiguration conf = new PropertiesConfiguration(
229                         System.getenv("APPC_CONFIG_DIR") + "/appc_southbound.properties");
230                     String property = tryCreatePropertyStr(protocol, action, vnfType);
231                     return keyExists(conf, property);
232                 }
233             }
234             return false;
235         } catch (SvcLogicException | ConfigurationException e) {
236             throw new DBException("An error occurred while checking for artifact update", e);
237         }
238     }
239
240     private void checkForFailure(String db, QueryStatus status) throws SvcLogicException {
241         if (status.toString().equals(FAILURE_PARAM)) {
242             throw new SvcLogicException("Error while reading data from " + db);
243         }
244     }
245
246     private boolean validate(String db) {
247         return db != null && serviceLogic != null;
248     }
249
250     private boolean keyExists(PropertiesConfiguration conf, String property) {
251         if (conf.subset(property) != null) {
252             if (conf.containsKey(property)) {
253                 log.info("Key Exists for property" + property + "in southbound.properties file");
254                 return true;
255             }
256         } else {
257             log.info("Key Does not exists and need to add the key  for property" + property
258                 + "in southbound.properties file");
259         }
260         return false;
261     }
262
263     private boolean tryAddCountAttribute(SvcLogicContext context, String count) {
264         if (count != null && Integer.parseInt(count) > 0) {
265             context.setAttribute(count, null);
266             return true;
267         } else {
268             return false;
269         }
270     }
271
272     private String tryCreatePropertyStr(String protocol, String action, String vnfType) {
273
274         if (StringUtils.isNotBlank(vnfType) && StringUtils.isNotBlank(protocol) && StringUtils
275             .isNotBlank(action)) {
276             return vnfType + "." + protocol + "." + action;
277         }
278         return "";
279     }
280
281     private String resolveWhereClause(SvcLogicContext context, String db, String whereClause) {
282         if (db != null) {
283             if (hasValidAttributes(context, db)) {
284                 return whereClause + AND_FILE_CAT_QUERY_STR + SdcArtifactHandlerConstants.FILE_CATEGORY;
285             } else if (db.equals(SdcArtifactHandlerConstants.DB_SDC_REFERENCE)) {
286                 return whereClause + AND_VNFC_TYPE_QUERY_STR + SdcArtifactHandlerConstants.VNFC_TYPE
287                     + AND_FILE_CAT_QUERY_STR + SdcArtifactHandlerConstants.FILE_CATEGORY + AND_ACTION_QUERY_STR
288                     + SdcArtifactHandlerConstants.ACTION;
289             } else if (db.equals(SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE)) {
290                 return " where PROTOCOL = $" + SdcArtifactHandlerConstants.DEVICE_PROTOCOL;
291             } else if (db.equals(SdcArtifactHandlerConstants.DB_CONFIG_ACTION_DG)) {
292                 return whereClause + AND_ACTION_QUERY_STR + SdcArtifactHandlerConstants.ACTION;
293             } else if (db.equals(SdcArtifactHandlerConstants.DB_VNFC_REFERENCE)) {
294                 return whereClause + AND_ACTION_QUERY_STR + SdcArtifactHandlerConstants.ACTION
295                     + AND_VNFC_TYPE_QUERY_STR + SdcArtifactHandlerConstants.VNFC_TYPE + " and VNFC_INSTANCE = $"
296                     + SdcArtifactHandlerConstants.VNFC_INSTANCE + " and VM_INSTANCE = $"
297                     + SdcArtifactHandlerConstants.VM_INSTANCE;
298             }
299         }
300         return whereClause;
301     }
302
303     private boolean hasValidAttributes(SvcLogicContext context, String db) {
304         return db.equals(SdcArtifactHandlerConstants.DB_SDC_REFERENCE)
305             && context.getAttribute(SdcArtifactHandlerConstants.FILE_CATEGORY)
306             .equals(SdcArtifactHandlerConstants.CAPABILITY)
307             && context.getAttribute(SdcArtifactHandlerConstants.ACTION) == null;
308     }
309
310     public void processDeviceInterfaceProtocol(SvcLogicContext context, boolean isUpdate) throws SvcLogicException {
311         log.info("Starting DB operation for Device Interface Protocol " + isUpdate);
312         String key;
313         QueryStatus status;
314         if (isUpdate) {
315             key = UPDATE_QUERY_STR + SdcArtifactHandlerConstants.DB_DEVICE_INTERFACE_PROTOCOL + " set PROTOCOL = $"
316                 + SdcArtifactHandlerConstants.DEVICE_PROTOCOL + " , DG_RPC = 'getDeviceRunningConfig' "
317                 + " , MODULE = 'APPC' " + WHERE_VNF_TYPE_QUERY_STR + SdcArtifactHandlerConstants.VNF_TYPE;
318         } else {
319             key =
320                 INSERT_INTO_QUERY_STR + SdcArtifactHandlerConstants.DB_DEVICE_INTERFACE_PROTOCOL + " set  VNF_TYPE = $"
321                     + SdcArtifactHandlerConstants.VNF_TYPE + " , PROTOCOL = $"
322                     + SdcArtifactHandlerConstants.DEVICE_PROTOCOL + " , DG_RPC = 'getDeviceRunningConfig' "
323                     + " , MODULE = 'APPC' ";
324         }
325
326         if (serviceLogic != null && context != null) {
327
328             status = serviceLogic.save("SQL", false, false, key, null, null, context);
329             if (status.toString().equals(FAILURE_PARAM)) {
330                 throw new SvcLogicException("Error While processing DEVICE_INTERFACE_PROTOCOL table ");
331             }
332         }
333     }
334
335     public void processDeviceAuthentication(SvcLogicContext context, boolean isUpdate)
336         throws DBException {
337         try {
338             String fn = "DBService.processDeviceAuthentication";
339             log.info(fn + "Starting DB operation for Device Authentication " + isUpdate);
340             String protocol = context.getAttribute(SdcArtifactHandlerConstants.DEVICE_PROTOCOL);
341             String action = context.getAttribute(SdcArtifactHandlerConstants.ACTION);
342             String vnfType = context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE);
343             String url = context.getAttribute(SdcArtifactHandlerConstants.URL);
344             String port = context.getAttribute(SdcArtifactHandlerConstants.PORT_NUMBER);
345             String user = context.getAttribute(SdcArtifactHandlerConstants.USER_NAME);
346             String property = vnfType + "." + protocol + "." + action;
347             log.info("property :" + property);
348             if (StringUtils.isBlank(url)) {
349                 url = "";
350             }
351             if (StringUtils.isBlank(port)) {
352                 port = "";
353             }
354             if (StringUtils.isBlank(user)) {
355                 user = "";
356             }
357             if (isInvalidInput(protocol, action, vnfType)) {
358                 throw new SvcLogicException(
359                     "Error While processing reference File as few or all of parameters VNF_TYPE,PROTOCOL,ACTION are missing ");
360             }
361             PropertiesConfiguration conf =
362                 new PropertiesConfiguration(System.getenv("APPC_CONFIG_DIR") + "/appc_southbound.properties");
363             log.info("is Updating to southbound  properties : " + isUpdate);
364
365             resolveUserAction(isUpdate, user, property, conf);
366             resolvePortAction(port, property, conf);
367             resolveUrlAction(url, property, conf);
368             tryAddPasswordProperty(property, conf);
369
370             log.info("About to save to properties file");
371             conf.save();
372             log.info("saved to properties file");
373         } catch (SvcLogicException | ConfigurationException e) {
374             throw new DBException("An error occurred when processing device authentication", e);
375         }
376     }
377
378     private void tryAddPasswordProperty(String property, PropertiesConfiguration conf) {
379         if (!conf.containsKey(property + "." + "password")) {
380             conf.addProperty(property + "." + "password", "");
381         }
382     }
383
384     private void resolveUrlAction(String url, String property, PropertiesConfiguration conf) {
385         if (conf.containsKey(property + "." + "url") && !url.isEmpty()) {
386             conf.setProperty(property + "." + "url", url);
387         } else {
388             conf.addProperty(property + "." + "url", url);
389         }
390     }
391
392     private void resolvePortAction(String port, String property, PropertiesConfiguration conf) {
393         if (conf.containsKey(property + "." + "port") && !port.isEmpty()) {
394             conf.setProperty(property + "." + "port", port);
395         } else {
396             conf.addProperty(property + "." + "port", port);
397         }
398     }
399
400     private void resolveUserAction(boolean isUpdate, String user, String property, PropertiesConfiguration conf) {
401         if (conf.containsKey(property + "." + "user") && !user.isEmpty()) {
402             conf.setProperty(property + "." + "user", user);
403         } else {
404             log.info("is Adding to southbound.properties" + isUpdate);
405             conf.addProperty(property + "." + "user", user);
406         }
407     }
408
409     private boolean isInvalidInput(String protocol, String action, String vnfType) {
410         return isInvalid(vnfType) && isInvalid(action) && isInvalid(protocol);
411     }
412
413     private boolean isInvalid(String str) {
414         return (str == null) || ("".equals(str));
415     }
416
417     public void processVnfcReference(SvcLogicContext context, boolean isUpdate) throws SvcLogicException {
418         String fn = "DBService.processVnfcReference";
419         log.info(fn + "Starting DB operation for Vnfc Reference " + isUpdate);
420         String key;
421
422         int vmInstance = -1;
423         if (context.getAttribute(SdcArtifactHandlerConstants.VM_INSTANCE) != null) {
424             vmInstance = Integer.parseInt(context.getAttribute(SdcArtifactHandlerConstants.VM_INSTANCE));
425         }
426
427         int vnfcInstance = -1;
428         if (context.getAttribute(SdcArtifactHandlerConstants.VNFC_INSTANCE) != null) {
429             vnfcInstance = Integer.parseInt(context.getAttribute(SdcArtifactHandlerConstants.VNFC_INSTANCE));
430         }
431
432         QueryStatus status;
433         if (isUpdate) {
434             key = UPDATE_QUERY_STR + SdcArtifactHandlerConstants.DB_VNFC_REFERENCE + " set VM_INSTANCE = " + vmInstance
435                 + " , VNFC_INSTANCE = " + vnfcInstance + " , VNFC_TYPE = $" + SdcArtifactHandlerConstants.VNFC_TYPE
436                 + " , VNFC_FUNCTION_CODE = $" + SdcArtifactHandlerConstants.VNFC_FUNCTION_CODE
437                 + " , GROUP_NOTATION_TYPE = $" + SdcArtifactHandlerConstants.GROUP_NOTATION_TYPE
438                 + " , GROUP_NOTATION_VALUE = $" + SdcArtifactHandlerConstants.GROUP_NOTATION_VALUE
439                 + " , IPADDRESS_V4_OAM_VIP = $" + SdcArtifactHandlerConstants.IPADDRESS_V4_OAM_VIP
440                 + WHERE_VNF_TYPE_QUERY_STR + SdcArtifactHandlerConstants.VNF_TYPE + AND_ACTION_QUERY_STR
441                 + SdcArtifactHandlerConstants.ACTION + AND_VNFC_TYPE_QUERY_STR + SdcArtifactHandlerConstants.VNFC_TYPE
442                 + " and VNFC_INSTANCE = $" + SdcArtifactHandlerConstants.VNFC_INSTANCE + " and VM_INSTANCE = $"
443                 + SdcArtifactHandlerConstants.VM_INSTANCE;
444         } else {
445             key = INSERT_INTO_QUERY_STR + SdcArtifactHandlerConstants.DB_VNFC_REFERENCE + " set  VNF_TYPE = $"
446                 + SdcArtifactHandlerConstants.VNF_TYPE + ACTION_QUERY_STR + SdcArtifactHandlerConstants.ACTION
447                 + " , VM_INSTANCE = $" + SdcArtifactHandlerConstants.VM_INSTANCE + " , VNFC_INSTANCE = $"
448                 + SdcArtifactHandlerConstants.VNFC_INSTANCE + " , VNFC_TYPE = $"
449                 + SdcArtifactHandlerConstants.VNFC_TYPE + " , VNFC_FUNCTION_CODE = $"
450                 + SdcArtifactHandlerConstants.VNFC_FUNCTION_CODE + " , TEMPLATE_ID = $"
451                 + SdcArtifactHandlerConstants.TEMPLATE_ID + " , GROUP_NOTATION_TYPE = $"
452                 + SdcArtifactHandlerConstants.GROUP_NOTATION_TYPE + " , IPADDRESS_V4_OAM_VIP = $"
453                 + SdcArtifactHandlerConstants.IPADDRESS_V4_OAM_VIP + " , GROUP_NOTATION_VALUE = $"
454                 + SdcArtifactHandlerConstants.GROUP_NOTATION_VALUE;
455         }
456
457         if (serviceLogic != null) {
458             status = serviceLogic.save("SQL", false, false, key, null, null, context);
459             if (status.toString().equals(FAILURE_PARAM)) {
460                 throw new SvcLogicException("Error While processing VNFC_REFERENCE table ");
461             }
462         }
463     }
464
465     public void processDownloadDgReference(SvcLogicContext context, boolean isUpdate)
466         throws SvcLogicException {
467         String fn = "DBService.processDownloadDgReference";
468         log.info(fn + "Starting DB operation for Download DG Reference " + isUpdate);
469         String key;
470         QueryStatus status = null;
471
472         if (isUpdate) {
473             key =
474                 UPDATE_QUERY_STR + SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE + SET_DOWNLOAD_CONFIG_QUERY_STR
475                     + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " where PROTOCOL = $"
476                     + SdcArtifactHandlerConstants.DEVICE_PROTOCOL;
477         } else {
478             key = INSERT_INTO_QUERY_STR + SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE
479                 + SET_DOWNLOAD_CONFIG_QUERY_STR
480                 + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " , PROTOCOL = $"
481                 + SdcArtifactHandlerConstants.DEVICE_PROTOCOL;
482         }
483
484         if (serviceLogic != null && context != null) {
485             status = serviceLogic.save("SQL", false, false, key, null, null, context);
486         }
487         if ((status == null) || status.toString().equals(FAILURE_PARAM)) {
488             throw new SvcLogicException("Error While processing DOWNLOAD_DG_REFERENCE table ");
489         }
490     }
491
492     public void processConfigActionDg(SvcLogicContext context, boolean isUpdate) throws SvcLogicException {
493         String fn = "DBService.processConfigActionDg";
494         log.info(fn + "Starting DB operation for Config DG Action " + isUpdate);
495         String key;
496         QueryStatus status = null;
497
498         if (context.getAttribute(SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE) != null
499             && context.getAttribute(SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE).length() > 0) {
500             if (isUpdate) {
501                 key = UPDATE_QUERY_STR + SdcArtifactHandlerConstants.DB_CONFIG_ACTION_DG + SET_DOWNLOAD_CONFIG_QUERY_STR
502                     + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " where ACTION = $"
503                     + SdcArtifactHandlerConstants.ACTION + AND_VNF_TYPE_QUERY_STR
504                     + SdcArtifactHandlerConstants.VNF_TYPE;
505             } else {
506                 key = INSERT_INTO_QUERY_STR + SdcArtifactHandlerConstants.DB_CONFIG_ACTION_DG
507                     + SET_DOWNLOAD_CONFIG_QUERY_STR
508                     + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + ACTION_QUERY_STR
509                     + SdcArtifactHandlerConstants.ACTION + VNF_TYPE_QUERY_STR + SdcArtifactHandlerConstants.VNF_TYPE;
510             }
511
512             if (serviceLogic != null) {
513                 status = serviceLogic.save("SQL", false, false, key, null, null, context);
514             }
515             if ((status == null) || status.toString().equals(FAILURE_PARAM)) {
516                 throw new SvcLogicException("Error While processing Configure DG Action table ");
517             }
518         } else {
519             log.info("No Update required for Config DG Action");
520         }
521
522     }
523
524     public String getModelDataInformationbyArtifactName(String artifactName) throws SvcLogicException {
525         String fn = "DBService.getVnfData";
526         SvcLogicContext con = new SvcLogicContext();
527         String key;
528         QueryStatus status;
529         key =
530             "select VNF_TYPE, VNFC_TYPE, ACTION, FILE_CATEGORY, ARTIFACT_TYPE from ASDC_REFERENCE where  ARTIFACT_NAME = "
531                 + artifactName;
532
533         if (serviceLogic != null) {
534             log.info(fn + "select Key: " + key);
535             status = serviceLogic.query("SQL", false, null, key, null, null, con);
536             if (status.toString().equals(FAILURE_PARAM)) {
537                 throw new SvcLogicException("Error While processing is ArtifactUpdateRequiredforPD table ");
538             }
539
540         }
541         log.info(fn + "Vnf_received :" + con.getAttribute("VNF_TYPE"));
542
543         return con.getAttribute("VNF_TYPE");
544     }
545
546     public void updateYangContents(SvcLogicContext context, String artifactId, String yangContents)
547         throws SvcLogicException {
548         String fn = "DBService.updateYangContents";
549         log.info(fn + "Starting DB operation for  updateYangContents");
550         String key;
551         QueryStatus status = null;
552
553         key = "update ASDC_ARTIFACTS " + " set ARTIFACT_CONTENT = '" + yangContents + "'"
554             + " where ASDC_ARTIFACTS_ID = " + artifactId;
555
556         if (serviceLogic != null && context != null) {
557             status = serviceLogic.save("SQL", false, false, key, null, null, context);
558         }
559         if ((status == null) || status.toString().equals(FAILURE_PARAM)) {
560             throw new SvcLogicException("Error While processing Configure DG Action table ");
561         }
562
563     }
564
565
566     public void insertProtocolReference(SvcLogicContext context, String vnfType, String protocol, String action,
567         String actionLevel, String template) throws SvcLogicException {
568         String fn = "DBService.insertProtocolReference";
569         log.info(fn + "Starting DB operation for  insertProtocolReference");
570         String key;
571         QueryStatus status = null;
572
573         key = "insert into PROTOCOL_REFERENCE (ACTION, VNF_TYPE, PROTOCOL, UPDATED_DATE, TEMPLATE, ACTION_LEVEL)"
574             + " values  (" + "'" + action + "', '" + vnfType + "', '" + protocol + "', now(),'" + template + "', '"
575             + actionLevel + "')";
576
577         if (serviceLogic != null && context != null) {
578             status = serviceLogic.save("SQL", false, false, key, null, null, context);
579         }
580         if ((status == null) || status.toString().equals(FAILURE_PARAM)) {
581             throw new SvcLogicException("Error While processing insertProtocolReference ");
582         }
583
584     }
585
586     public boolean isProtocolReferenceUpdateRequired(SvcLogicContext context, String vnfType, String protocol,
587         String action, String actionLevel, String template) throws SvcLogicException {
588         SvcLogicContext localContext = new SvcLogicContext();
589         String fn = "DBService.isProtocolReferenceUpdateRequired";
590         log.info(fn + "Starting DB operation for  isProtocolReferenceUpdateRequired");
591
592         String key = "select COUNT(*) from PROTOCOL_REFERENCE where ACTION='" + action + "' and ACTION_LEVEL='" + actionLevel
593             + "' and VNF_TYPE='" + vnfType + "'";
594         serviceLogic.query("SQL", false, null, key, null, null, localContext);
595
596         String countStr = localContext.getAttribute("COUNT(*)");
597         int count = Integer.parseInt(countStr);
598         return count > 0;
599     }
600
601     public void updateProtocolReference(SvcLogicContext context, String vnfType, String protocol, String action,
602         String actionLevel, String template) throws SvcLogicException {
603
604         String fn = "DBService.isProtocolReferenceUpdateRequired";
605         log.info(fn + "Starting DB operation for  isProtocolReferenceUpdateRequired");
606         String key;
607         QueryStatus status;
608
609         key = "update PROTOCOL_REFERENCE set UPDATED_DATE=now(), template='" + template + "', protocol ='" + protocol
610             + "' where ACTION='" + action + "' and ACTION_LEVEL='" + actionLevel + "' and VNF_TYPE='" + vnfType
611             + "'";
612         status = serviceLogic.save("SQL", false, false, key, null, null, context);
613         if (status == QueryStatus.FAILURE) {
614             log.info("updateProtocolReference:: Error updating protocol reference");
615             throw new SvcLogicException("Error - updating PROTOCOL_REFERENCE_TABLE in updateProtocolReference");
616         }
617     }
618
619     public String getDownLoadDGReference(SvcLogicContext context) throws DBException {
620         try {
621
622             String fn = "DBService.setDownLoadDGReference";
623             String downloadConfigDg;
624             log.info(fn + "Setting Download DG Reference from DB");
625             String key;
626             QueryStatus status;
627             String protocol = context.getAttribute(SdcArtifactHandlerConstants.DEVICE_PROTOCOL);
628             if (StringUtils.isBlank(protocol)) {
629                 log.info(fn + " :: Protocol is Blank!! Returning without querying DB");
630                 throw new ConfigurationException(fn + ":: Protocol is Blank!! Returning without querying DB");
631             }
632             key = "select download_config_dg from " + SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE
633                 + " where protocol = '" + protocol + "'";
634             SvcLogicContext localContext = new SvcLogicContext();
635             status = serviceLogic.query("SQL", false, null, key, null, null, localContext);
636             if (status == QueryStatus.FAILURE) {
637                 log.info(fn + ":: Error retrieving download_config_dg");
638                 throw new SvcLogicException("Error retrieving download_config_dg");
639             }
640             if (status == QueryStatus.NOT_FOUND) {
641                 log.info(fn + ":: NOT_FOUND! No data found for download_config_dg!!");
642                 throw new SvcLogicException(fn + ":: NOT_FOUND! No data found for download_config_dg!");
643             }
644             downloadConfigDg = localContext.getAttribute("download-config-dg");
645             log.info(fn + "download_config_dg::" + downloadConfigDg);
646             return downloadConfigDg;
647         } catch (SvcLogicException | ConfigurationException e) {
648             throw new DBException("An error occurred when getting DG reference", e);
649         }
650     }
651
652     public void cleanUpVnfcReferencesForVnf(SvcLogicContext context) throws SvcLogicException {
653         try {
654             String key1 = "delete from " + SdcArtifactHandlerConstants.DB_VNFC_REFERENCE + " where action = $"
655                 + SdcArtifactHandlerConstants.ACTION + " and vnf_type = $" + SdcArtifactHandlerConstants.VNF_TYPE;
656             log.debug("Action : " + context.getAttribute(SdcArtifactHandlerConstants.ACTION));
657             log.debug("vnfType: " + context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE));
658             QueryStatus status;
659             log.info("cleanUpVnfcReferencesForVnf()::Query:" + key1);
660             if (serviceLogic != null) {
661                 status = serviceLogic.save("SQL", false, false, key1, null, null, context);
662                 if (status.toString().equals(FAILURE_PARAM)) {
663                     log.debug("Error deleting from VNFC_REFERENCE table");
664                     throw new SvcLogicException("Error While processing VNFC_REFERENCE table ");
665                 }
666             }
667         } catch (Exception e) {
668             log.debug("Error deleting from VNFC_REFERENCE table  : "
669                 + context.getAttribute(SdcArtifactHandlerConstants.ACTION) + " and "
670                 + context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE), e);
671         }
672     }
673 }