8e0e71ad0498d0ec4b220cf41409d7228eef9838
[appc.git] / appc-inbound / appc-artifact-handler / provider / src / main / java / org / onap / appc / artifact / handler / dbservices / DBService.java
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") ) {
386             if (url != null && !url.isEmpty()) {
387                 conf.setProperty(property + "." + "url", url);
388             }
389         } else {
390             conf.addProperty(property + "." + "url", url);
391         }
392     }
393
394     private void resolvePortAction(String port, String property, PropertiesConfiguration conf) {
395         if (conf.containsKey(property + "." + "port")) {
396             if (port != null && !port.isEmpty()) {
397                 conf.setProperty(property + "." + "port", port);
398             }
399         } else {
400             conf.addProperty(property + "." + "port", port);
401         }
402     }
403
404     private void resolveUserAction(boolean isUpdate, String user, String property, PropertiesConfiguration conf) {
405         if (conf.containsKey(property + "." + "user")) {
406             if (user != null && !user.isEmpty()) {
407                 conf.setProperty(property + "." + "user", user);
408             }
409         } else {
410             conf.addProperty(property + "." + "user", user);
411         }
412     }
413
414     private boolean isInvalidInput(String protocol, String action, String vnfType) {
415         return isInvalid(vnfType) && isInvalid(action) && isInvalid(protocol);
416     }
417
418     private boolean isInvalid(String str) {
419         return (str == null) || ("".equals(str));
420     }
421
422     public void processVnfcReference(SvcLogicContext context, boolean isUpdate) throws SvcLogicException {
423         String fn = "DBService.processVnfcReference";
424         log.info(fn + "Starting DB operation for Vnfc Reference " + isUpdate);
425         String key;
426
427         int vmInstance = -1;
428         if (context.getAttribute(SdcArtifactHandlerConstants.VM_INSTANCE) != null) {
429             vmInstance = Integer.parseInt(context.getAttribute(SdcArtifactHandlerConstants.VM_INSTANCE));
430         }
431
432         int vnfcInstance = -1;
433         if (context.getAttribute(SdcArtifactHandlerConstants.VNFC_INSTANCE) != null) {
434             vnfcInstance = Integer.parseInt(context.getAttribute(SdcArtifactHandlerConstants.VNFC_INSTANCE));
435         }
436
437         QueryStatus status;
438         if (isUpdate) {
439             key = UPDATE_QUERY_STR + SdcArtifactHandlerConstants.DB_VNFC_REFERENCE + " set VM_INSTANCE = " + vmInstance
440                 + " , VNFC_INSTANCE = " + vnfcInstance + " , VNFC_TYPE = $" + SdcArtifactHandlerConstants.VNFC_TYPE
441                 + " , VNFC_FUNCTION_CODE = $" + SdcArtifactHandlerConstants.VNFC_FUNCTION_CODE
442                 + " , GROUP_NOTATION_TYPE = $" + SdcArtifactHandlerConstants.GROUP_NOTATION_TYPE
443                 + " , GROUP_NOTATION_VALUE = $" + SdcArtifactHandlerConstants.GROUP_NOTATION_VALUE
444                 + " , IPADDRESS_V4_OAM_VIP = $" + SdcArtifactHandlerConstants.IPADDRESS_V4_OAM_VIP
445                 + WHERE_VNF_TYPE_QUERY_STR + SdcArtifactHandlerConstants.VNF_TYPE + AND_ACTION_QUERY_STR
446                 + SdcArtifactHandlerConstants.ACTION + AND_VNFC_TYPE_QUERY_STR + SdcArtifactHandlerConstants.VNFC_TYPE
447                 + " and VNFC_INSTANCE = $" + SdcArtifactHandlerConstants.VNFC_INSTANCE + " and VM_INSTANCE = $"
448                 + SdcArtifactHandlerConstants.VM_INSTANCE;
449         } else {
450             key = INSERT_INTO_QUERY_STR + SdcArtifactHandlerConstants.DB_VNFC_REFERENCE + " set  VNF_TYPE = $"
451                 + SdcArtifactHandlerConstants.VNF_TYPE + ACTION_QUERY_STR + SdcArtifactHandlerConstants.ACTION
452                 + " , VM_INSTANCE = $" + SdcArtifactHandlerConstants.VM_INSTANCE + " , VNFC_INSTANCE = $"
453                 + SdcArtifactHandlerConstants.VNFC_INSTANCE + " , VNFC_TYPE = $"
454                 + SdcArtifactHandlerConstants.VNFC_TYPE + " , VNFC_FUNCTION_CODE = $"
455                 + SdcArtifactHandlerConstants.VNFC_FUNCTION_CODE + " , TEMPLATE_ID = $"
456                 + SdcArtifactHandlerConstants.TEMPLATE_ID + " , GROUP_NOTATION_TYPE = $"
457                 + SdcArtifactHandlerConstants.GROUP_NOTATION_TYPE + " , IPADDRESS_V4_OAM_VIP = $"
458                 + SdcArtifactHandlerConstants.IPADDRESS_V4_OAM_VIP + " , GROUP_NOTATION_VALUE = $"
459                 + SdcArtifactHandlerConstants.GROUP_NOTATION_VALUE;
460         }
461
462         if (serviceLogic != null) {
463             status = serviceLogic.save("SQL", false, false, key, null, null, context);
464             if (status.toString().equals(FAILURE_PARAM)) {
465                 throw new SvcLogicException("Error While processing VNFC_REFERENCE table ");
466             }
467         }
468     }
469
470     public void processDownloadDgReference(SvcLogicContext context, boolean isUpdate)
471         throws SvcLogicException {
472         String fn = "DBService.processDownloadDgReference";
473         log.info(fn + "Starting DB operation for Download DG Reference " + isUpdate);
474         String key;
475         QueryStatus status = null;
476
477         if (isUpdate) {
478             key =
479                 UPDATE_QUERY_STR + SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE + SET_DOWNLOAD_CONFIG_QUERY_STR
480                     + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " where PROTOCOL = $"
481                     + SdcArtifactHandlerConstants.DEVICE_PROTOCOL;
482         } else {
483             key = INSERT_INTO_QUERY_STR + SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE
484                 + SET_DOWNLOAD_CONFIG_QUERY_STR
485                 + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " , PROTOCOL = $"
486                 + SdcArtifactHandlerConstants.DEVICE_PROTOCOL;
487         }
488
489         if (serviceLogic != null && context != null) {
490             status = serviceLogic.save("SQL", false, false, key, null, null, context);
491         }
492         if ((status == null) || status.toString().equals(FAILURE_PARAM)) {
493             throw new SvcLogicException("Error While processing DOWNLOAD_DG_REFERENCE table ");
494         }
495     }
496
497     public void processConfigActionDg(SvcLogicContext context, boolean isUpdate) throws SvcLogicException {
498         String fn = "DBService.processConfigActionDg";
499         log.info(fn + "Starting DB operation for Config DG Action " + isUpdate);
500         String key;
501         QueryStatus status = null;
502
503         if (context.getAttribute(SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE) != null
504             && context.getAttribute(SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE).length() > 0) {
505             if (isUpdate) {
506                 key = UPDATE_QUERY_STR + SdcArtifactHandlerConstants.DB_CONFIG_ACTION_DG + SET_DOWNLOAD_CONFIG_QUERY_STR
507                     + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " where ACTION = $"
508                     + SdcArtifactHandlerConstants.ACTION + AND_VNF_TYPE_QUERY_STR
509                     + SdcArtifactHandlerConstants.VNF_TYPE;
510             } else {
511                 key = INSERT_INTO_QUERY_STR + SdcArtifactHandlerConstants.DB_CONFIG_ACTION_DG
512                     + SET_DOWNLOAD_CONFIG_QUERY_STR
513                     + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + ACTION_QUERY_STR
514                     + SdcArtifactHandlerConstants.ACTION + VNF_TYPE_QUERY_STR + SdcArtifactHandlerConstants.VNF_TYPE;
515             }
516
517             if (serviceLogic != null) {
518                 status = serviceLogic.save("SQL", false, false, key, null, null, context);
519             }
520             if ((status == null) || status.toString().equals(FAILURE_PARAM)) {
521                 throw new SvcLogicException("Error While processing Configure DG Action table ");
522             }
523         } else {
524             log.info("No Update required for Config DG Action");
525         }
526
527     }
528
529     public String getModelDataInformationbyArtifactName(String artifactName) throws SvcLogicException {
530         String fn = "DBService.getVnfData";
531         SvcLogicContext con = new SvcLogicContext();
532         String key;
533         QueryStatus status;
534         key =
535             "select VNF_TYPE, VNFC_TYPE, ACTION, FILE_CATEGORY, ARTIFACT_TYPE from ASDC_REFERENCE where  ARTIFACT_NAME = "
536                 + artifactName;
537
538         if (serviceLogic != null) {
539             log.info(fn + "select Key: " + key);
540             status = serviceLogic.query("SQL", false, null, key, null, null, con);
541             if (status.toString().equals(FAILURE_PARAM)) {
542                 throw new SvcLogicException("Error While processing is ArtifactUpdateRequiredforPD table ");
543             }
544
545         }
546         log.info(fn + "Vnf_received :" + con.getAttribute("VNF_TYPE"));
547
548         return con.getAttribute("VNF_TYPE");
549     }
550
551     public void updateYangContents(SvcLogicContext context, String artifactId, String yangContents)
552         throws SvcLogicException {
553         String fn = "DBService.updateYangContents";
554         log.info(fn + "Starting DB operation for  updateYangContents");
555         String key;
556         QueryStatus status = null;
557
558         key = "update ASDC_ARTIFACTS " + " set ARTIFACT_CONTENT = '" + yangContents + "'"
559             + " where ASDC_ARTIFACTS_ID = " + artifactId;
560
561         if (serviceLogic != null && context != null) {
562             status = serviceLogic.save("SQL", false, false, key, null, null, context);
563         }
564         if ((status == null) || status.toString().equals(FAILURE_PARAM)) {
565             throw new SvcLogicException("Error While processing Configure DG Action table ");
566         }
567
568     }
569
570
571     public void insertProtocolReference(SvcLogicContext context, String vnfType, String protocol, String action,
572         String actionLevel, String template) throws SvcLogicException {
573         String fn = "DBService.insertProtocolReference";
574         log.info(fn + "Starting DB operation for  insertProtocolReference");
575         String key;
576         QueryStatus status = null;
577
578         key = "insert into PROTOCOL_REFERENCE (ACTION, VNF_TYPE, PROTOCOL, UPDATED_DATE, TEMPLATE, ACTION_LEVEL)"
579             + " values  (" + "'" + action + "', '" + vnfType + "', '" + protocol + "', now(),'" + template + "', '"
580             + actionLevel + "')";
581
582         if (serviceLogic != null && context != null) {
583             status = serviceLogic.save("SQL", false, false, key, null, null, context);
584         }
585         if ((status == null) || status.toString().equals(FAILURE_PARAM)) {
586             throw new SvcLogicException("Error While processing insertProtocolReference ");
587         }
588
589     }
590
591     public boolean isProtocolReferenceUpdateRequired(SvcLogicContext context, String vnfType, String protocol,
592         String action, String actionLevel, String template) throws SvcLogicException {
593         SvcLogicContext localContext = new SvcLogicContext();
594         String fn = "DBService.isProtocolReferenceUpdateRequired";
595         log.info(fn + "Starting DB operation for  isProtocolReferenceUpdateRequired");
596
597         String key = "select COUNT(*) from PROTOCOL_REFERENCE where ACTION='" + action + "' and ACTION_LEVEL='" + actionLevel
598             + "' and VNF_TYPE='" + vnfType + "'";
599         serviceLogic.query("SQL", false, null, key, null, null, localContext);
600
601         String countStr = localContext.getAttribute("COUNT(*)");
602         int count = Integer.parseInt(countStr);
603         return count > 0;
604     }
605
606     public void updateProtocolReference(SvcLogicContext context, String vnfType, String protocol, String action,
607         String actionLevel, String template) throws SvcLogicException {
608
609         String fn = "DBService.isProtocolReferenceUpdateRequired";
610         log.info(fn + "Starting DB operation for  isProtocolReferenceUpdateRequired");
611         String key;
612         QueryStatus status;
613
614         key = "update PROTOCOL_REFERENCE set UPDATED_DATE=now(), template='" + template + "', protocol ='" + protocol
615             + "' where ACTION='" + action + "' and ACTION_LEVEL='" + actionLevel + "' and VNF_TYPE='" + vnfType
616             + "'";
617         status = serviceLogic.save("SQL", false, false, key, null, null, context);
618         if (status == QueryStatus.FAILURE) {
619             log.info("updateProtocolReference:: Error updating protocol reference");
620             throw new SvcLogicException("Error - updating PROTOCOL_REFERENCE_TABLE in updateProtocolReference");
621         }
622     }
623
624     public String getDownLoadDGReference(SvcLogicContext context) throws DBException {
625         try {
626
627             String fn = "DBService.setDownLoadDGReference";
628             String downloadConfigDg;
629             log.info(fn + "Setting Download DG Reference from DB");
630             String key;
631             QueryStatus status;
632             String protocol = context.getAttribute(SdcArtifactHandlerConstants.DEVICE_PROTOCOL);
633             if (StringUtils.isBlank(protocol)) {
634                 log.info(fn + " :: Protocol is Blank!! Returning without querying DB");
635                 throw new ConfigurationException(fn + ":: Protocol is Blank!! Returning without querying DB");
636             }
637             key = "select download_config_dg from " + SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE
638                 + " where protocol = '" + protocol + "'";
639             SvcLogicContext localContext = new SvcLogicContext();
640             status = serviceLogic.query("SQL", false, null, key, null, null, localContext);
641             if (status == QueryStatus.FAILURE) {
642                 log.info(fn + ":: Error retrieving download_config_dg");
643                 throw new SvcLogicException("Error retrieving download_config_dg");
644             }
645             if (status == QueryStatus.NOT_FOUND) {
646                 log.info(fn + ":: NOT_FOUND! No data found for download_config_dg!!");
647                 throw new SvcLogicException(fn + ":: NOT_FOUND! No data found for download_config_dg!");
648             }
649             downloadConfigDg = localContext.getAttribute("download-config-dg");
650             log.info(fn + "download_config_dg::" + downloadConfigDg);
651             return downloadConfigDg;
652         } catch (SvcLogicException | ConfigurationException e) {
653             throw new DBException("An error occurred when getting DG reference", e);
654         }
655     }
656
657     public void cleanUpVnfcReferencesForVnf(SvcLogicContext context) throws SvcLogicException {
658         try {
659             String key1 = "delete from " + SdcArtifactHandlerConstants.DB_VNFC_REFERENCE + " where action = $"
660                 + SdcArtifactHandlerConstants.ACTION + " and vnf_type = $" + SdcArtifactHandlerConstants.VNF_TYPE;
661             log.debug("Action : " + context.getAttribute(SdcArtifactHandlerConstants.ACTION));
662             log.debug("vnfType: " + context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE));
663             QueryStatus status;
664             log.info("cleanUpVnfcReferencesForVnf()::Query:" + key1);
665             if (serviceLogic != null) {
666                 status = serviceLogic.save("SQL", false, false, key1, null, null, context);
667                 if (status.toString().equals(FAILURE_PARAM)) {
668                     log.debug("Error deleting from VNFC_REFERENCE table");
669                     throw new SvcLogicException("Error While processing VNFC_REFERENCE table ");
670                 }
671             }
672         } catch (Exception e) {
673             log.debug("Error deleting from VNFC_REFERENCE table  : "
674                 + context.getAttribute(SdcArtifactHandlerConstants.ACTION) + " and "
675                 + context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE), e);
676         }
677     }
678 }