9b2d80d3fddd520448375fc741d8aed10d456569
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.artifact.handler.dbservices;
26
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import 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         String templateId="";
392
393         if (context.getAttribute(SdcArtifactHandlerConstants.VM_INSTANCE) != null) {
394             vm_instance = Integer.parseInt(context.getAttribute(SdcArtifactHandlerConstants.VM_INSTANCE));
395         }
396         int vnfc_instance = -1;
397         if (context.getAttribute(SdcArtifactHandlerConstants.VNFC_INSTANCE) != null) {
398             vnfc_instance = Integer.parseInt(context.getAttribute(SdcArtifactHandlerConstants.VNFC_INSTANCE));
399         } 
400         if (context.getAttribute(SdcArtifactHandlerConstants.TEMPLATE_ID) != null) {
401             templateId = context.getAttribute(SdcArtifactHandlerConstants.TEMPLATE_ID);
402         }
403
404         QueryStatus status = null;
405         if (isUpdate) {
406             key = "update " + SdcArtifactHandlerConstants.DB_VNFC_REFERENCE + " set VM_INSTANCE = " + vm_instance
407                 + " , VNFC_INSTANCE = " + vnfc_instance + " , VNFC_TYPE = $" + SdcArtifactHandlerConstants.VNFC_TYPE
408                 + " , VNFC_FUNCTION_CODE = $" + SdcArtifactHandlerConstants.VNFC_FUNCTION_CODE
409                 + " , GROUP_NOTATION_TYPE = $" + SdcArtifactHandlerConstants.GROUP_NOTATION_TYPE
410                 + " , GROUP_NOTATION_VALUE = $" + SdcArtifactHandlerConstants.GROUP_NOTATION_VALUE
411                 + " , IPADDRESS_V4_OAM_VIP = $" + SdcArtifactHandlerConstants.IPADDRESS_V4_OAM_VIP
412                 + " where VNF_TYPE = $" + SdcArtifactHandlerConstants.VNF_TYPE + " and ACTION = $"
413                 + SdcArtifactHandlerConstants.ACTION + " and VNFC_TYPE = $" + SdcArtifactHandlerConstants.VNFC_TYPE
414                 + " and VNFC_INSTANCE = $" + SdcArtifactHandlerConstants.VNFC_INSTANCE + " and VM_INSTANCE = $"
415                 + SdcArtifactHandlerConstants.VM_INSTANCE;
416         } else {
417             key = "insert into " + SdcArtifactHandlerConstants.DB_VNFC_REFERENCE + " set  VNF_TYPE = $"
418                 + SdcArtifactHandlerConstants.VNF_TYPE + " , ACTION = $" + SdcArtifactHandlerConstants.ACTION
419                 + " , VM_INSTANCE = $" + SdcArtifactHandlerConstants.VM_INSTANCE + " , VNFC_INSTANCE = $"
420                 + SdcArtifactHandlerConstants.VNFC_INSTANCE + " , VNFC_TYPE = $"
421                 + SdcArtifactHandlerConstants.VNFC_TYPE + " , VNFC_FUNCTION_CODE = $"
422                 + SdcArtifactHandlerConstants.VNFC_FUNCTION_CODE + " , TEMPLATE_ID = $"
423                 + SdcArtifactHandlerConstants.TEMPLATE_ID + " , GROUP_NOTATION_TYPE = $"
424                 + SdcArtifactHandlerConstants.GROUP_NOTATION_TYPE + " , IPADDRESS_V4_OAM_VIP = $"
425                 + SdcArtifactHandlerConstants.IPADDRESS_V4_OAM_VIP + " , GROUP_NOTATION_VALUE = $"
426                 + SdcArtifactHandlerConstants.GROUP_NOTATION_VALUE;
427         }
428
429         if (serviceLogic != null && context != null) {
430             status = serviceLogic.save("SQL", false, false, key, null, null, context);
431             if (status.toString().equals("FAILURE")) {
432                 throw new SvcLogicException("Error While processing VNFC_REFERENCE table ");
433             }
434         }
435     }
436
437     public void processDownloadDgReference(SvcLogicContext context, boolean isUpdate)
438         throws SvcLogicException, SQLException {
439         String fn = "DBService.processDownloadDgReference";
440         log.info(fn + "Starting DB operation for Download DG Reference " + isUpdate);
441         String key = "";
442         QueryStatus status = null;
443
444         if (isUpdate) {
445             key = "update " + SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE + " set DOWNLOAD_CONFIG_DG = $"
446                 + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " where PROTOCOL = $"
447                 + SdcArtifactHandlerConstants.DEVICE_PROTOCOL;
448         } else {
449             key = "insert into " + SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE + " set DOWNLOAD_CONFIG_DG = $"
450                 + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " , PROTOCOL = $"
451                 + SdcArtifactHandlerConstants.DEVICE_PROTOCOL;
452         }
453
454         if (serviceLogic != null && context != null) {
455             status = serviceLogic.save("SQL", false, false, key, null, null, context);
456         }
457         if ((status == null) || status.toString().equals("FAILURE")) {
458             throw new SvcLogicException("Error While processing DOWNLOAD_DG_REFERENCE table ");
459         }
460     }
461
462     public void processConfigActionDg(SvcLogicContext context, boolean isUpdate) throws SvcLogicException {
463         String fn = "DBService.processConfigActionDg";
464         log.info(fn + "Starting DB operation for Config DG Action " + isUpdate);
465         String key = "";
466         QueryStatus status = null;
467
468         if (context.getAttribute(SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE) != null
469             && context.getAttribute(SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE).length() > 0) {
470             if (isUpdate) {
471                 key = "update " + SdcArtifactHandlerConstants.DB_CONFIG_ACTION_DG + " set DOWNLOAD_CONFIG_DG = $"
472                     + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " where ACTION = $"
473                     + SdcArtifactHandlerConstants.ACTION + " and VNF_TYPE = $"
474                     + SdcArtifactHandlerConstants.VNF_TYPE;
475             } else {
476                 key = "insert into " + SdcArtifactHandlerConstants.DB_CONFIG_ACTION_DG + " set DOWNLOAD_CONFIG_DG = $"
477                     + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " , ACTION = $"
478                     + SdcArtifactHandlerConstants.ACTION + " , VNF_TYPE = $" + SdcArtifactHandlerConstants.VNF_TYPE;
479             }
480
481             if (serviceLogic != null && context != null) {
482                 status = serviceLogic.save("SQL", false, false, key, null, null, context);
483             }
484             if ((status == null) || status.toString().equals("FAILURE")) {
485                 throw new SvcLogicException("Error While processing Configure DG Action table ");
486             }
487         } else {
488             log.info("No Update required for Config DG Action");
489         }
490
491     }
492
493     public String getModelDataInformationbyArtifactName(String artifact_name) throws SvcLogicException {
494         String fn = "DBService.getVnfData";
495         String key = "";
496         SvcLogicContext con = new SvcLogicContext();
497         HashMap<String, String> modelData = new HashMap<String, String>();
498         QueryStatus status = null;
499         key =
500             "select VNF_TYPE, VNFC_TYPE, ACTION, FILE_CATEGORY, ARTIFACT_TYPE from ASDC_REFERENCE where  ARTIFACT_NAME = "
501                 + artifact_name;
502
503         if (serviceLogic != null && con != null) {
504             log.info(fn + "select Key: " + key);
505             status = serviceLogic.query("SQL", false, null, key, null, null, con);
506             if (status.toString().equals("FAILURE")) {
507                 throw new SvcLogicException("Error While processing is ArtifactUpdateRequiredforPD table ");
508             }
509
510         }
511
512         log.info(fn + "Vnf_received :" + con.getAttribute("VNF_TYPE"));
513
514         return con.getAttribute("VNF_TYPE");
515
516     }
517
518     public void updateYangContents(SvcLogicContext context, String artifactId, String yangContents)
519         throws SvcLogicException {
520         String fn = "DBService.updateYangContents";
521         log.info(fn + "Starting DB operation for  updateYangContents");
522         String key = "";
523         QueryStatus status = null;
524
525         key = "update ASDC_ARTIFACTS " + " set ARTIFACT_CONTENT = '" + yangContents + "'"
526             + " where ASDC_ARTIFACTS_ID = " + artifactId;
527
528         if (serviceLogic != null && context != null) {
529             status = serviceLogic.save("SQL", false, false, key, null, null, context);
530         }
531         if ((status == null) || status.toString().equals("FAILURE")) {
532             throw new SvcLogicException("Error While processing Configure DG Action table ");
533         }
534
535     }
536
537
538     public void insertProtocolReference(SvcLogicContext context, String vnfType, String protocol, String action,
539         String action_level, String template) throws SvcLogicException {
540         String fn = "DBService.insertProtocolReference";
541         log.info(fn + "Starting DB operation for  insertProtocolReference");
542         String key = "";
543         QueryStatus status = null;
544
545         key = "insert into PROTOCOL_REFERENCE (ACTION, VNF_TYPE, PROTOCOL, UPDATED_DATE, TEMPLATE, ACTION_LEVEL)"
546             + " values  (" + "'" + action + "', '" + vnfType + "', '" + protocol + "', now(),'" + template + "', '"
547             + action_level + "')";
548
549         if (serviceLogic != null && context != null) {
550             status = serviceLogic.save("SQL", false, false, key, null, null, context);
551         }
552         if ((status == null) || status.toString().equals("FAILURE")) {
553             throw new SvcLogicException("Error While processing insertProtocolReference ");
554         }
555
556     }
557
558     public boolean isProtocolReferenceUpdateRequired(SvcLogicContext context, String vnfType, String protocol,
559         String action, String action_level, String template) throws SvcLogicException {
560         SvcLogicContext localContext = new SvcLogicContext();
561         String fn = "DBService.isProtocolReferenceUpdateRequired";
562         log.info(fn + "Starting DB operation for  isProtocolReferenceUpdateRequired");
563         String key = "";
564         QueryStatus status = null;
565
566         key = "select COUNT(*) from PROTOCOL_REFERENCE where ACTION='" + action + "' and ACTION_LEVEL='" + action_level
567             + "' and VNF_TYPE='" + vnfType + "'";
568         status = serviceLogic.query("SQL", false, null, key, null, null, localContext);
569         String countStr = localContext.getAttribute("COUNT(*)");
570         int count = Integer.parseInt(countStr);
571         if (count > 0) {
572             return true;
573         } else {
574             return false;
575         }
576     }
577
578     public void updateProtocolReference(SvcLogicContext context, String vnfType, String protocol, String action,
579         String action_level, String template) throws SvcLogicException {
580
581         String fn = "DBService.isProtocolReferenceUpdateRequired";
582         log.info(fn + "Starting DB operation for  isProtocolReferenceUpdateRequired");
583         String key = "";
584         QueryStatus status = null;
585
586         key = "update PROTOCOL_REFERENCE set UPDATED_DATE=now(), template='" + template + "', protocol ='" + protocol
587             + "' where ACTION='" + action + "' and ACTION_LEVEL='" + action_level + "' and VNF_TYPE='" + vnfType
588             + "'";
589         status = serviceLogic.save("SQL", false, false, key, null, null, context);
590         if (status == QueryStatus.FAILURE) {
591             log.info("updateProtocolReference:: Error updating protocol reference");
592             throw new SvcLogicException("Error - updating PROTOCOL_REFERENCE_TABLE in updateProtocolReference");
593         }
594         return;
595     }
596
597     public String getDownLoadDGReference(SvcLogicContext context) throws SvcLogicException, ConfigurationException {
598         String fn = "DBService.setDownLoadDGReference";
599         String downloadConfigDg = null;
600         log.info(fn + "Setting Download DG Reference from DB");
601         String key = "";
602         QueryStatus status = null;
603         String protocol = context.getAttribute(SdcArtifactHandlerConstants.DEVICE_PROTOCOL);
604         if (StringUtils.isBlank(protocol)) {
605             log.info(fn + " :: Protocol is Blank!! Returning without querying DB");
606             throw new ConfigurationException(fn + ":: Protocol is Blank!! Returning without querying DB");
607         }
608         key = "select download_config_dg from " + SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE
609             + " where protocol = '" + protocol + "'";
610         SvcLogicContext localContext = new SvcLogicContext();
611         status = serviceLogic.query("SQL", false, null, key, null, null, localContext);
612         if (status == QueryStatus.FAILURE) {
613             log.info(fn + ":: Error retrieving download_config_dg");
614             throw new SvcLogicException("Error retrieving download_config_dg");
615         }
616         if (status == QueryStatus.NOT_FOUND) {
617             log.info(fn + ":: NOT_FOUND! No data found for download_config_dg!!");
618             throw new SvcLogicException(fn + ":: NOT_FOUND! No data found for download_config_dg!");
619         }
620         downloadConfigDg = localContext.getAttribute("download-config-dg");
621         log.info(fn + "download_config_dg::" + downloadConfigDg);
622         return downloadConfigDg;
623     }
624
625     public void cleanUpVnfcReferencesForVnf(SvcLogicContext context) throws SvcLogicException {
626         try {
627             String key1 = "delete from " + SdcArtifactHandlerConstants.DB_VNFC_REFERENCE + " where action = $"
628                 + SdcArtifactHandlerConstants.ACTION + " and vnf_type = $" + SdcArtifactHandlerConstants.VNF_TYPE;
629             log.debug("Action : " + context.getAttribute(SdcArtifactHandlerConstants.ACTION));
630             log.debug("vnfType: " + context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE));
631             QueryStatus status = null;
632             log.info("cleanUpVnfcReferencesForVnf()::Query:" + key1);
633             if (serviceLogic != null && context != null) {
634                 status = serviceLogic.save("SQL", false, false, key1, null, null, context);
635                 if (status.toString().equals("FAILURE")) {
636                     log.debug("Error deleting from VNFC_REFERENCE table");
637                     throw new SvcLogicException("Error While processing VNFC_REFERENCE table ");
638                 }
639                 status = null;
640             }
641         } catch (Exception e) {
642             log.debug("Error deleting from VNFC_REFERENCE table  : "
643                 + context.getAttribute(SdcArtifactHandlerConstants.ACTION) + " and "
644                 + context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE));
645         }
646     }
647 }