bd2f6dfffaca57d89c4d3bb8c1fa9110435f35c6
[appc.git] / appc-inbound / appc-artifact-handler / provider / src / main / java / org / onap / appc / artifact / handler / dbservices / DBService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.artifact.handler.dbservices;
26
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import org.onap.ccsdk.sli.adaptors.resource.sql.SqlResource;
30 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
31 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
34 import org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants;
35
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 ," + " ARTIFACT_DESCRIPTION    = $artifact-description ,"
117                     + " INTERNAL_VERSION    = " + intversion + "," + " ARTIFACT_NAME       =  $artifact-name ,"
118                     + " 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).equals(SdcArtifactHandlerConstants.CAPABILITY)) {
158             log.info("Updating capability artifact in ASDC_REFERENCE");
159             key = "update " + SdcArtifactHandlerConstants.DB_SDC_REFERENCE + "  set ARTIFACT_NAME = $"
160                     + SdcArtifactHandlerConstants.ARTIFACT_NAME + " where " + "FILE_CATEGORY = $"
161                     + SdcArtifactHandlerConstants.FILE_CATEGORY + " and VNF_TYPE = $"
162                     + SdcArtifactHandlerConstants.VNF_TYPE ;
163         } else if (isUpdate)
164             key = "update " + SdcArtifactHandlerConstants.DB_SDC_REFERENCE + "  set ARTIFACT_NAME = $"
165                     + SdcArtifactHandlerConstants.ARTIFACT_NAME + " where VNFC_TYPE = $"
166                     + SdcArtifactHandlerConstants.VNFC_TYPE + " and FILE_CATEGORY = $"
167                     + SdcArtifactHandlerConstants.FILE_CATEGORY + " and ACTION = $"
168                     + SdcArtifactHandlerConstants.ACTION + " and VNF_TYPE = $" + SdcArtifactHandlerConstants.VNF_TYPE;
169
170         else {
171             if (context.getAttribute(SdcArtifactHandlerConstants.FILE_CATEGORY).equals(SdcArtifactHandlerConstants.CAPABILITY)) {
172       log.info("Inserting new record for capability artifact in ASDC_REFERENCE");
173                 key = "insert into " + SdcArtifactHandlerConstants.DB_SDC_REFERENCE + " set VNFC_TYPE = null "
174                         + " , FILE_CATEGORY = $" + SdcArtifactHandlerConstants.FILE_CATEGORY + " , VNF_TYPE = $"
175                         + SdcArtifactHandlerConstants.VNF_TYPE + " , ACTION = null " + " , ARTIFACT_TYPE = null "
176                         + " , ARTIFACT_NAME = $" + SdcArtifactHandlerConstants.ARTIFACT_NAME;
177             } else {
178                 key = "insert into " + SdcArtifactHandlerConstants.DB_SDC_REFERENCE + " set VNFC_TYPE = $"
179                         + SdcArtifactHandlerConstants.VNFC_TYPE + " , FILE_CATEGORY = $"
180                         + SdcArtifactHandlerConstants.FILE_CATEGORY + " , VNF_TYPE = $"
181                         + SdcArtifactHandlerConstants.VNF_TYPE + " , ACTION = $" + SdcArtifactHandlerConstants.ACTION
182                         + " , ARTIFACT_TYPE = $" + SdcArtifactHandlerConstants.ARTIFACT_TYPE + " , ARTIFACT_NAME = $"
183                         + SdcArtifactHandlerConstants.ARTIFACT_NAME;
184             }
185         }
186         if (serviceLogic != null && context != null) {
187             log.info("Insert Key: " + key);
188             status = serviceLogic.save("SQL", false, false, key, null, null, context);
189             if (status.toString().equals("FAILURE"))
190                 throw new SvcLogicException("Error While processing sdc_reference table ");
191         }
192     }
193
194     public boolean isArtifactUpdateRequired(SvcLogicContext context, String db) throws SvcLogicException, SQLException, ConfigurationException {
195         String fn = "DBService.isArtifactUpdateRequired";
196         log.info("Checking if Update required for this data");
197
198         log.info("db" + db);
199         log.info("ACTION=" + context.getAttribute(SdcArtifactHandlerConstants.ACTION));
200         log.info("VNFC_TYPE=" + context.getAttribute(SdcArtifactHandlerConstants.VNFC_TYPE));
201         log.info("VNFC_INSTANCE=" + context.getAttribute(SdcArtifactHandlerConstants.VNFC_INSTANCE));
202         log.info("VM_INSTANCE=" + context.getAttribute(SdcArtifactHandlerConstants.VM_INSTANCE));
203         log.info("VNF_TYPE=" + context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE));
204         String whereClause = "";
205
206         QueryStatus status = null;
207         whereClause = " where VNF_TYPE = $" + SdcArtifactHandlerConstants.VNF_TYPE;
208
209         if (db != null) {
210             if (db.equals(SdcArtifactHandlerConstants.DB_SDC_REFERENCE)
211                     && context.getAttribute(SdcArtifactHandlerConstants.FILE_CATEGORY)
212                     .equals(SdcArtifactHandlerConstants.CAPABILITY)
213                     && context.getAttribute(SdcArtifactHandlerConstants.ACTION) == null) {
214                 whereClause = whereClause + " and FILE_CATEGORY = $" + SdcArtifactHandlerConstants.FILE_CATEGORY;
215             }
216
217             else if (db.equals(SdcArtifactHandlerConstants.DB_SDC_REFERENCE)) {
218                 whereClause = whereClause + " and VNFC_TYPE = $" + SdcArtifactHandlerConstants.VNFC_TYPE
219                         + " and FILE_CATEGORY = $" + SdcArtifactHandlerConstants.FILE_CATEGORY + " and ACTION = $"
220                         + SdcArtifactHandlerConstants.ACTION;
221             }
222
223             else if (db.equals(SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE)) {
224                 whereClause = " where PROTOCOL = $" + SdcArtifactHandlerConstants.DEVICE_PROTOCOL;
225             } else if (db.equals(SdcArtifactHandlerConstants.DB_CONFIG_ACTION_DG)) {
226                 whereClause = whereClause + " and ACTION = $" + SdcArtifactHandlerConstants.ACTION;
227             } else if (db.equals(SdcArtifactHandlerConstants.DB_VNFC_REFERENCE)) {
228                 int vm_instance = -1;
229                 if (context.getAttribute(SdcArtifactHandlerConstants.VM_INSTANCE) != null)
230                     vm_instance = Integer.parseInt(context.getAttribute(SdcArtifactHandlerConstants.VM_INSTANCE));
231                 int vnfc_instance = -1;
232                 if (context.getAttribute(SdcArtifactHandlerConstants.VNFC_INSTANCE) != null)
233                     vnfc_instance = Integer.parseInt(context.getAttribute(SdcArtifactHandlerConstants.VNFC_INSTANCE));
234                 whereClause = whereClause + " and ACTION = $" + SdcArtifactHandlerConstants.ACTION + " and VNFC_TYPE = $"
235                         + SdcArtifactHandlerConstants.VNFC_TYPE + " and VNFC_INSTANCE = $"
236                         + SdcArtifactHandlerConstants.VNFC_INSTANCE + " and VM_INSTANCE = $"
237                         + SdcArtifactHandlerConstants.VM_INSTANCE;
238
239             }
240         }
241
242         if (!db.equals(SdcArtifactHandlerConstants.DB_DEVICE_AUTHENTICATION) && serviceLogic != null && context != null) {
243             String key = "select COUNT(*) from " + db + whereClause;
244             log.info("SELECT String : " + key);
245             status = serviceLogic.query("SQL", false, null, key, null, null, context);
246             if (status.toString().equals("FAILURE")) {
247                 throw new SvcLogicException("Error while reading data from " + db);
248             }
249             String count = context.getAttribute("COUNT(*)");
250             log.info("Number of row Returned : " + count + ": " + status + ":");
251             if (count != null && Integer.parseInt(count) > 0) {
252                 context.setAttribute(count, null);
253                 return true;
254             } else
255                 return false;
256         }
257        if (db.equals(SdcArtifactHandlerConstants.DB_DEVICE_AUTHENTICATION) && serviceLogic != null && context != null) {
258              log.info("Check for update or insert for properties file");
259           String protocol = context.getAttribute(SdcArtifactHandlerConstants.DEVICE_PROTOCOL);
260           String action = context.getAttribute(SdcArtifactHandlerConstants.ACTION);
261           String vnf_type = context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE);
262           PropertiesConfiguration conf = new PropertiesConfiguration(
263                 SdcArtifactHandlerConstants.APPC_CONFIG_DIR + "/appc_southbound.properties");
264           String property ="";
265             if (StringUtils.isNotBlank(vnf_type)) {
266
267             if (StringUtils.isNotBlank(protocol)) {
268                if (StringUtils.isNotBlank(action)) {
269                   property = vnf_type + "." + protocol + "." + action;
270                }
271             }
272             }
273             if (conf.subset(property )!=null) {
274                if (conf.containsKey(property )) {
275                   log.info("Key Exists for property"+property +"in southbound.properties file");
276                   return true;
277                }
278             } else
279                log.info("Key Doesnot exists and need to add the key  for property"+property +"in southbound.properties file");
280                 return false;
281         }
282         return false;
283     }
284
285     public void processDeviceInterfaceProtocol(SvcLogicContext context, boolean isUpdate) throws SvcLogicException {
286         String fn = "DBService.processDeviceInterfaceProtocol";
287         log.info("Starting DB operation for Device Interface Protocol " + isUpdate);
288         String key = "";
289         QueryStatus status = null;
290         if (isUpdate)
291             key = "update " + SdcArtifactHandlerConstants.DB_DEVICE_INTERFACE_PROTOCOL + " set PROTOCOL = $"
292                     + SdcArtifactHandlerConstants.DEVICE_PROTOCOL + " , DG_RPC = 'getDeviceRunningConfig' "
293                     + " , MODULE = 'APPC' " + " where VNF_TYPE = $" + SdcArtifactHandlerConstants.VNF_TYPE;
294         else
295             key = "insert into " + SdcArtifactHandlerConstants.DB_DEVICE_INTERFACE_PROTOCOL + " set  VNF_TYPE = $"
296                     + SdcArtifactHandlerConstants.VNF_TYPE + " , PROTOCOL = $"
297                     + SdcArtifactHandlerConstants.DEVICE_PROTOCOL + " , DG_RPC = 'getDeviceRunningConfig' "
298                     + " , MODULE = 'APPC' ";
299
300         if (serviceLogic != null && context != null) {
301
302             status = serviceLogic.save("SQL", false, false, key, null, null, context);
303             if (status.toString().equals("FAILURE"))
304                 throw new SvcLogicException("Error While processing DEVICE_INTERFACE_PROTOCOL table ");
305         }
306
307     }
308
309     public void processDeviceAuthentication(SvcLogicContext context, boolean isUpdate) throws SvcLogicException, ConfigurationException {
310         String fn = "DBService.processDeviceAuthentication";
311         log.info(fn + "Starting DB operation for Device Authentication " + isUpdate);
312         String protocol = context.getAttribute(SdcArtifactHandlerConstants.DEVICE_PROTOCOL);
313       String action = context.getAttribute(SdcArtifactHandlerConstants.ACTION);
314       String vnf_type = context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE);
315       String url = context.getAttribute(SdcArtifactHandlerConstants.URL);
316       String port = context.getAttribute(SdcArtifactHandlerConstants.PORT_NUMBER);
317       String user = context.getAttribute(SdcArtifactHandlerConstants.USER_NAME);
318       String property = vnf_type + "." +protocol + "." + action;
319       log.info("property :"+property);
320       if (StringUtils.isBlank(url)) {
321          url = "";
322       }
323       if (StringUtils.isBlank(port) ) {
324          port = "";
325       }
326       if (StringUtils.isBlank(user)) {
327          user = "";
328       }
329       if (((vnf_type == null) || ("".equals(vnf_type))) && ((action == null) || ("".equals(action)))
330             && ((protocol == null) || ("".equals(protocol))))
331          throw new SvcLogicException(
332                "Error While processing refernce File as few or all of parameters VNF_TYPE,PROTOCOL,ACTION are missing ");
333       PropertiesConfiguration conf = new PropertiesConfiguration(
334             SdcArtifactHandlerConstants.APPC_CONFIG_DIR + "/appc_southbound.properties");
335          log.info("is Updating to southbound  properties : "+isUpdate);
336             if (conf.containsKey(property + "." + "user")) {
337                if(user!=null && !user.isEmpty())
338                conf.setProperty(property + "." + "user", user);
339                } else {
340                                  log.info("is Adding to southbound.properties"+isUpdate);
341
342                conf.addProperty(property + "." + "user", user);
343             }
344
345             if (conf.containsKey(property + "." + "port")) {
346                if (port != null && !port.isEmpty())
347                   conf.setProperty(property + "." + "port", port);
348             } else {
349                   conf.addProperty(property + "." + "port", port);
350             }
351             if (conf.containsKey(property + "." + "password")) {
352             } else {
353                conf.addProperty(property + "." + "password", "");
354             }
355             if (conf.containsKey(property + "." + "url")) {
356                if (url != null && !url.isEmpty())
357                   conf.setProperty(property + "." + "url", url);
358
359             } else {
360
361                conf.addProperty(property + "." + "url", url);
362             }
363             log.info("About to save to properties file");
364              conf.save();
365             log.info("saved to properties file");
366    }
367
368     public void processVnfcReference(SvcLogicContext context, boolean isUpdate) throws SvcLogicException {
369         String fn = "DBService.processVnfcReference";
370         log.info(fn + "Starting DB operation for Vnfc Reference " + isUpdate);
371         String key = "";
372         int vm_instance = -1;
373         if (context.getAttribute(SdcArtifactHandlerConstants.VM_INSTANCE) != null)
374             vm_instance = Integer.parseInt(context.getAttribute(SdcArtifactHandlerConstants.VM_INSTANCE));
375         int vnfc_instance = -1;
376         if (context.getAttribute(SdcArtifactHandlerConstants.VNFC_INSTANCE) != null)
377             vnfc_instance = Integer.parseInt(context.getAttribute(SdcArtifactHandlerConstants.VNFC_INSTANCE));
378         QueryStatus status = null;
379         if (isUpdate)
380             key = "update " + SdcArtifactHandlerConstants.DB_VNFC_REFERENCE + " set VM_INSTANCE = " + vm_instance
381                     + " , VNFC_INSTANCE = " + vnfc_instance + " , VNFC_TYPE = $" + SdcArtifactHandlerConstants.VNFC_TYPE
382                     + " , VNFC_FUNCTION_CODE = $" + SdcArtifactHandlerConstants.VNFC_FUNCTION_CODE
383                     + " , GROUP_NOTATION_TYPE = $" + SdcArtifactHandlerConstants.GROUP_NOTATION_TYPE
384                     + " , GROUP_NOTATION_VALUE = $" + SdcArtifactHandlerConstants.GROUP_NOTATION_VALUE
385                     + " , IPADDRESS_V4_OAM_VIP = $" + SdcArtifactHandlerConstants.IPADDRESS_V4_OAM_VIP
386                     + " where VNF_TYPE = $" + SdcArtifactHandlerConstants.VNF_TYPE + " and ACTION = $"
387                     + SdcArtifactHandlerConstants.ACTION + " and VNFC_TYPE = $" + SdcArtifactHandlerConstants.VNFC_TYPE
388                     + " and VNFC_INSTANCE = $" + SdcArtifactHandlerConstants.VNFC_INSTANCE + " and VM_INSTANCE = $"
389                     + SdcArtifactHandlerConstants.VM_INSTANCE;
390         else
391             key = "insert into " + SdcArtifactHandlerConstants.DB_VNFC_REFERENCE + " set  VNF_TYPE = $"
392                     + SdcArtifactHandlerConstants.VNF_TYPE + " , ACTION = $" + SdcArtifactHandlerConstants.ACTION
393                     + " , VM_INSTANCE = $" + SdcArtifactHandlerConstants.VM_INSTANCE + " , VNFC_INSTANCE = $"
394                     + SdcArtifactHandlerConstants.VNFC_INSTANCE + " , VNFC_TYPE = $"
395                     + SdcArtifactHandlerConstants.VNFC_TYPE + " , VNFC_FUNCTION_CODE = $"
396                     + SdcArtifactHandlerConstants.VNFC_FUNCTION_CODE + " , GROUP_NOTATION_TYPE = $"
397                     + SdcArtifactHandlerConstants.GROUP_NOTATION_TYPE + " , IPADDRESS_V4_OAM_VIP = $"
398                     + SdcArtifactHandlerConstants.IPADDRESS_V4_OAM_VIP + " , GROUP_NOTATION_VALUE = $"
399                     + SdcArtifactHandlerConstants.GROUP_NOTATION_VALUE;
400
401         if (serviceLogic != null && context != null) {
402             status = serviceLogic.save("SQL", false, false, key, null, null, context);
403             if (status.toString().equals("FAILURE"))
404                 throw new SvcLogicException("Error While processing VNFC_REFERENCE table ");
405         }
406     }
407
408     public void processDownloadDgReference(SvcLogicContext context, boolean isUpdate)
409             throws SvcLogicException, SQLException {
410         String fn = "DBService.processDownloadDgReference";
411         log.info(fn + "Starting DB operation for Download DG Reference " + isUpdate);
412         String key = "";
413         QueryStatus status = null;
414
415         if (isUpdate)
416             key = "update " + SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE + " set DOWNLOAD_CONFIG_DG = $"
417                     + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " where PROTOCOL = $"
418                     + SdcArtifactHandlerConstants.DEVICE_PROTOCOL;
419         else
420             key = "insert into " + SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE + " set DOWNLOAD_CONFIG_DG = $"
421                     + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " , PROTOCOL = $"
422                     + SdcArtifactHandlerConstants.DEVICE_PROTOCOL;
423
424         if (serviceLogic != null && context != null)
425             status = serviceLogic.save("SQL", false, false, key, null, null, context);
426         if ((status == null) || status.toString().equals("FAILURE"))
427             throw new SvcLogicException("Error While processing DOWNLOAD_DG_REFERENCE table ");
428     }
429
430     public void processConfigActionDg(SvcLogicContext context, boolean isUpdate) throws SvcLogicException {
431         String fn = "DBService.processConfigActionDg";
432         log.info(fn + "Starting DB operation for Config DG Action " + isUpdate);
433         String key = "";
434         QueryStatus status = null;
435
436         if (context.getAttribute(SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE) != null
437                 && context.getAttribute(SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE).length() > 0) {
438             if (isUpdate)
439                 key = "update " + SdcArtifactHandlerConstants.DB_CONFIG_ACTION_DG + " set DOWNLOAD_CONFIG_DG = $"
440                         + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " where ACTION = $"
441                         + SdcArtifactHandlerConstants.ACTION + " and VNF_TYPE = $"
442                         + SdcArtifactHandlerConstants.VNF_TYPE;
443             else
444                 key = "insert into " + SdcArtifactHandlerConstants.DB_CONFIG_ACTION_DG + " set DOWNLOAD_CONFIG_DG = $"
445                         + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " , ACTION = $"
446                         + SdcArtifactHandlerConstants.ACTION + " , VNF_TYPE = $" + SdcArtifactHandlerConstants.VNF_TYPE;
447
448             if (serviceLogic != null && context != null)
449                 status = serviceLogic.save("SQL", false, false, key, null, null, context);
450             if ((status == null) || status.toString().equals("FAILURE"))
451                 throw new SvcLogicException("Error While processing Configure DG Action table ");
452         } else
453             log.info("No Update required for Config DG Action");
454
455     }
456
457     public String getModelDataInformationbyArtifactName(String artifact_name) throws SvcLogicException {
458         String fn = "DBService.getVnfData";
459         String key = "";
460         SvcLogicContext con = new SvcLogicContext();
461         HashMap<String, String> modelData = new HashMap<String, String>();
462         QueryStatus status = null;
463         key = "select VNF_TYPE, VNFC_TYPE, ACTION, FILE_CATEGORY, ARTIFACT_TYPE from ASDC_REFERENCE where  ARTIFACT_NAME = "
464                 + artifact_name;
465
466         if (serviceLogic != null && con != null) {
467             log.info(fn + "select Key: " + key);
468             status = serviceLogic.query("SQL", false, null, key, null, null, con);
469             if (status.toString().equals("FAILURE"))
470                 throw new SvcLogicException("Error While processing is ArtifactUpdateRequiredforPD table ");
471
472         }
473
474         log.info(fn + "Vnf_received :" + con.getAttribute("VNF_TYPE"));
475
476         return con.getAttribute("VNF_TYPE");
477
478     }
479
480     public void updateYangContents(SvcLogicContext context, String artifactId, String yangContents)
481             throws SvcLogicException {
482         String fn = "DBService.updateYangContents";
483         log.info(fn + "Starting DB operation for  updateYangContents");
484         String key = "";
485         QueryStatus status = null;
486
487         key = "update ASDC_ARTIFACTS " + " set ARTIFACT_CONTENT = '" + yangContents + "'"
488                 + " where ASDC_ARTIFACTS_ID = " + artifactId;
489
490         if (serviceLogic != null && context != null)
491             status = serviceLogic.save("SQL", false, false, key, null, null, context);
492         if ((status == null) || status.toString().equals("FAILURE"))
493             throw new SvcLogicException("Error While processing Configure DG Action table ");
494
495     }
496
497
498     public void insertProtocolReference(SvcLogicContext context, String vnfType, String protocol, String action,
499             String action_level, String template) throws SvcLogicException {
500         String fn = "DBService.insertProtocolReference";
501         log.info(fn + "Starting DB operation for  insertProtocolReference");
502         String key = "";
503         QueryStatus status = null;
504
505         key = "insert into PROTOCOL_REFERENCE (ACTION, VNF_TYPE, PROTOCOL, UPDATED_DATE, TEMPLATE, ACTION_LEVEL)"
506                 + " values  (" + "'" + action + "', '" + vnfType + "', '" + protocol + "', now(),'" + template + "', '"
507                 + action_level + "')";
508
509         if (serviceLogic != null && context != null)
510             status = serviceLogic.save("SQL", false, false, key, null, null, context);
511         if ((status == null) || status.toString().equals("FAILURE"))
512             throw new SvcLogicException("Error While processing insertProtocolReference ");
513
514     }
515
516     public boolean isProtocolReferenceUpdateRequired(SvcLogicContext context, String vnfType, String protocol,
517              String action, String action_level, String template) throws SvcLogicException {
518         SvcLogicContext localContext = new SvcLogicContext();
519         String fn = "DBService.isProtocolReferenceUpdateRequired";
520         log.info(fn + "Starting DB operation for  isProtocolReferenceUpdateRequired");
521         String key = "";
522         QueryStatus status = null;
523
524         key = "select COUNT(*) from PROTOCOL_REFERENCE where ACTION='" + action + "' and ACTION_LEVEL='" + action_level
525                 + "' and VNF_TYPE='" + vnfType + "'";
526         status = serviceLogic.query("SQL", false, null, key, null, null, localContext);
527         String countStr = localContext.getAttribute("COUNT(*)");
528         int count = Integer.parseInt(countStr);
529         if (count > 0)
530             return true;
531         else
532             return false;
533     }
534
535     public void updateProtocolReference(SvcLogicContext context, String vnfType, String protocol, String action,
536                 String action_level, String template) throws SvcLogicException {
537
538         String fn = "DBService.isProtocolReferenceUpdateRequired";
539         log.info(fn + "Starting DB operation for  isProtocolReferenceUpdateRequired");
540         String key = "";
541         QueryStatus status = null;
542
543         key = "update PROTOCOL_REFERENCE set UPDATED_DATE=now(), template='" + template + "' where ACTION='" + action
544                 + "' and ACTION_LEVEL='" + action_level + "' and VNF_TYPE='" + vnfType + "'";
545         status = serviceLogic.save("SQL", false, false, key, null, null, context);
546         if (status == QueryStatus.FAILURE) {
547             log.info("updateProtocolReference:: Error updating protocol reference");
548             throw new SvcLogicException("Error - updating PROTOCOL_REFERENCE_TABLE in updateProtocolReference");
549         }
550         return;
551     }
552
553     public String getDownLoadDGReference(SvcLogicContext context) throws Exception {
554         String fn = "DBService.setDownLoadDGReference";
555         String downloadConfigDg = null;
556         log.info(fn + "Setting Download DG Reference from DB");
557         String key = "";
558         QueryStatus status = null;
559         String protocol = context.getAttribute(SdcArtifactHandlerConstants.DEVICE_PROTOCOL);
560         if (StringUtils.isBlank(protocol)) {
561             log.info(fn + " :: Protocol is Blank!! Returning without querying DB");
562             throw new Exception(fn+":: Protocol is Blank!! Returning without querying DB");
563         }
564         key = "select download_config_dg from " + SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE
565                 + " where protocol = '" + protocol + "'";
566         SvcLogicContext localContext = new SvcLogicContext();
567         status = serviceLogic.query("SQL", false, null, key, null, null, localContext);
568         if (status == QueryStatus.FAILURE) {
569             log.info(fn + ":: Error retrieving download_config_dg");
570             throw new SvcLogicException("Error retrieving download_config_dg");
571         }
572         if (status == QueryStatus.NOT_FOUND) {
573             log.info(fn + ":: NOT_FOUND! No data found for download_config_dg!!");
574             throw new Exception(fn + ":: NOT_FOUND! No data found for download_config_dg!");
575         }
576         downloadConfigDg = localContext.getAttribute("download-config-dg");
577         log.info(fn + "download_config_dg::" + downloadConfigDg);
578         return downloadConfigDg;
579     }
580
581     public void cleanUpVnfcReferencesForVnf(SvcLogicContext context) throws SvcLogicException {
582        try{
583    String key1 = "delete from " + SdcArtifactHandlerConstants.DB_VNFC_REFERENCE
584                  + " where action = $"+ SdcArtifactHandlerConstants.ACTION + " and vnf_type = $" + SdcArtifactHandlerConstants.VNF_TYPE;
585    log.debug("Action : " + context.getAttribute(SdcArtifactHandlerConstants.ACTION));
586    log.debug("vnfType: "+ context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE));
587    QueryStatus status = null;
588    log.info("cleanUpVnfcReferencesForVnf()::Query:" + key1);
589    if (serviceLogic != null && context != null) {
590       status = serviceLogic.save("SQL", false, false, key1, null, null, context);
591       if (status.toString().equals("FAILURE")) {
592          log.debug("Error deleting from VNFC_REFERENCE table");
593          throw new SvcLogicException("Error While processing VNFC_REFERENCE table ");
594          }
595       status = null;
596       }
597    }catch(Exception e){
598       log.debug("Error deleting from VNFC_REFERENCE table  : "+context.getAttribute(SdcArtifactHandlerConstants.ACTION) +" and " +context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE));
599       }
600    }
601 }