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