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