a337f098969386a7b8411d21e1f59fad010431a6
[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
37 import java.sql.SQLException;
38 import java.util.HashMap;
39 import org.apache.commons.lang.StringUtils;
40 import org.apache.commons.configuration.PropertiesConfiguration;
41 import  org.apache.commons.configuration.ConfigurationException;
42
43 public class DBService {
44
45     private static final EELFLogger log = EELFManager.getInstance().getLogger(DBService.class);
46     private SvcLogicResource serviceLogic;
47     private static DBService dgGeneralDBService = null;
48
49     public static DBService initialise() {
50         if (dgGeneralDBService == null) {
51             dgGeneralDBService = new DBService();
52         }
53         return dgGeneralDBService;
54     }
55
56     private DBService() {
57         if (serviceLogic == null) {
58             serviceLogic = new SqlResource();
59         }
60     }
61
62     protected DBService(SqlResource svcLogic) {
63         if (serviceLogic == null) {
64             serviceLogic = svcLogic;
65         }
66     }
67
68     public String getInternalVersionNumber(SvcLogicContext ctx, String artifactName, String prefix)
69             throws SvcLogicException {
70         String fn = "DBService.getInternalVersionNumber";
71         QueryStatus status = null;
72         String artifactInternalVersion = null;
73         if (serviceLogic != null && ctx != null) {
74             String key = "select max(internal_version) as maximum from ASDC_ARTIFACTS  WHERE ARTIFACT_NAME = '"
75                     + artifactName + "'";
76             log.info("Getting internal Versoin :" + key);
77             status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
78             if (status.toString().equals("FAILURE"))
79                 throw new SvcLogicException("Error - getting internal Artifact Number");
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             artifactID = ctx.getAttribute("id");
102             log.info("SDC_ARTIFACTS_ID received as : " + ctx.getAttribute("id"));
103         }
104         return artifactID;
105     }
106
107     public QueryStatus saveArtifacts(SvcLogicContext ctx, int intversion) throws SvcLogicException {
108         String fn = "DBService.saveArtifacts";
109         QueryStatus status = null;
110         if (serviceLogic != null && ctx != null) {
111             String key = "INSERT INTO ASDC_ARTIFACTS " + "SET SERVICE_UUID    =  $service-uuid , "
112                     + " DISTRIBUTION_ID    =  $distribution-id ," + " SERVICE_NAME    =  $service-name ,"
113                     + " SERVICE_DESCRIPTION    =  $service-description ," + " RESOURCE_UUID    = $resource-uuid ,"
114                     + " RESOURCE_INSTANCE_NAME    = $resource-instance-name ," + " RESOURCE_NAME    = $resource-name ,"
115                     + " RESOURCE_VERSION    = $resource-version ," + " RESOURCE_TYPE    = $resource-type ,"
116                     + " ARTIFACT_UUID    = $artifact-uuid ," + " ARTIFACT_TYPE    = $artifact-type ,"
117                     + " ARTIFACT_VERSION    = $artifact-version ," + " ARTIFACT_DESCRIPTION    = $artifact-description ,"
118                     + " INTERNAL_VERSION    = " + intversion + "," + " ARTIFACT_NAME       =  $artifact-name ,"
119                     + " ARTIFACT_CONTENT    =  $artifact-contents ";
120
121             status = serviceLogic.save("SQL", false, false, key, null, null, ctx);
122             if (status.toString().equals("FAILURE"))
123                 throw new SvcLogicException("Error While processing storing Artifact: "
124                         + ctx.getAttribute(SdcArtifactHandlerConstants.ARTIFACT_NAME));
125         }
126         return status;
127
128     }
129
130     public QueryStatus logData(SvcLogicContext ctx, String prefix) throws SvcLogicException {
131         String fn = "DBService.saveReferenceData";
132         QueryStatus status = null;
133         if (serviceLogic != null && ctx != null) {
134             String key = "INSERT INTO CONFIG_TRANSACTION_LOG " + " SET request_id = $request-id , "
135                     + " message_type = $log-message-type , " + " message = $log-message ;";
136             status = serviceLogic.save("SQL", false, false, key, null, prefix, ctx);
137             if (status.toString().equals("FAILURE"))
138                 throw new SvcLogicException("Error while loging data");
139
140         }
141         return status;
142     }
143
144     public void processConfigureActionDg(SvcLogicContext context, boolean isUpdate) {
145         String fn = "DBService.processConfigureActionDg";
146         log.info("Update Parameter for SDC Reference " + isUpdate);
147         String key = "";
148         QueryStatus status = null;
149         if (isUpdate)
150             ;
151     }
152
153     public void processSdcReferences(SvcLogicContext context, boolean isUpdate) throws SvcLogicException {
154         String fn = "DBService.processSdcReferences";
155         String key = "";
156         QueryStatus status = null;
157
158         if (isUpdate && context.getAttribute(SdcArtifactHandlerConstants.FILE_CATEGORY).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 = $"
169                     + SdcArtifactHandlerConstants.ACTION + " and VNF_TYPE = $" + SdcArtifactHandlerConstants.VNF_TYPE;
170
171         else {
172             if (context.getAttribute(SdcArtifactHandlerConstants.FILE_CATEGORY).equals(SdcArtifactHandlerConstants.CAPABILITY)) {
173       log.info("Inserting new record for capability artifact in ASDC_REFERENCE");
174                 key = "insert into " + SdcArtifactHandlerConstants.DB_SDC_REFERENCE + " set VNFC_TYPE = null "
175                         + " , FILE_CATEGORY = $" + SdcArtifactHandlerConstants.FILE_CATEGORY + " , VNF_TYPE = $"
176                         + SdcArtifactHandlerConstants.VNF_TYPE + " , ACTION = null " + " , ARTIFACT_TYPE = null "
177                         + " , ARTIFACT_NAME = $" + SdcArtifactHandlerConstants.ARTIFACT_NAME;
178             } else {
179                 key = "insert into " + SdcArtifactHandlerConstants.DB_SDC_REFERENCE + " set VNFC_TYPE = $"
180                         + SdcArtifactHandlerConstants.VNFC_TYPE + " , FILE_CATEGORY = $"
181                         + SdcArtifactHandlerConstants.FILE_CATEGORY + " , VNF_TYPE = $"
182                         + SdcArtifactHandlerConstants.VNF_TYPE + " , ACTION = $" + SdcArtifactHandlerConstants.ACTION
183                         + " , ARTIFACT_TYPE = $" + SdcArtifactHandlerConstants.ARTIFACT_TYPE + " , ARTIFACT_NAME = $"
184                         + SdcArtifactHandlerConstants.ARTIFACT_NAME;
185             }
186         }
187         if (serviceLogic != null && context != null) {
188             log.info("Insert Key: " + key);
189             status = serviceLogic.save("SQL", false, false, key, null, null, context);
190             if (status.toString().equals("FAILURE"))
191                 throw new SvcLogicException("Error While processing sdc_reference table ");
192         }
193     }
194
195     public boolean isArtifactUpdateRequired(SvcLogicContext context, String db) throws SvcLogicException, SQLException, ConfigurationException {
196         String fn = "DBService.isArtifactUpdateRequired";
197         log.info("Checking if Update required for this data");
198
199         log.info("db" + db);
200         log.info("ACTION=" + context.getAttribute(SdcArtifactHandlerConstants.ACTION));
201         log.info("VNFC_TYPE=" + context.getAttribute(SdcArtifactHandlerConstants.VNFC_TYPE));
202         log.info("VNFC_INSTANCE=" + context.getAttribute(SdcArtifactHandlerConstants.VNFC_INSTANCE));
203         log.info("VM_INSTANCE=" + context.getAttribute(SdcArtifactHandlerConstants.VM_INSTANCE));
204         log.info("VNF_TYPE=" + context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE));
205         String whereClause = "";
206
207         QueryStatus status = null;
208         whereClause = " where VNF_TYPE = $" + SdcArtifactHandlerConstants.VNF_TYPE;
209
210         if (db != null) {
211             if (db.equals(SdcArtifactHandlerConstants.DB_SDC_REFERENCE)
212                     && context.getAttribute(SdcArtifactHandlerConstants.FILE_CATEGORY)
213                     .equals(SdcArtifactHandlerConstants.CAPABILITY)
214                     && context.getAttribute(SdcArtifactHandlerConstants.ACTION) == null) {
215                 whereClause = whereClause + " and FILE_CATEGORY = $" + SdcArtifactHandlerConstants.FILE_CATEGORY;
216             }
217
218             else if (db.equals(SdcArtifactHandlerConstants.DB_SDC_REFERENCE)) {
219                 whereClause = whereClause + " and VNFC_TYPE = $" + SdcArtifactHandlerConstants.VNFC_TYPE
220                         + " and FILE_CATEGORY = $" + SdcArtifactHandlerConstants.FILE_CATEGORY + " and ACTION = $"
221                         + SdcArtifactHandlerConstants.ACTION;
222             }
223
224             else if (db.equals(SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE)) {
225                 whereClause = " where PROTOCOL = $" + SdcArtifactHandlerConstants.DEVICE_PROTOCOL;
226             } else if (db.equals(SdcArtifactHandlerConstants.DB_CONFIG_ACTION_DG)) {
227                 whereClause = whereClause + " and ACTION = $" + SdcArtifactHandlerConstants.ACTION;
228             } else if (db.equals(SdcArtifactHandlerConstants.DB_VNFC_REFERENCE)) {
229                 int vm_instance = -1;
230                 if (context.getAttribute(SdcArtifactHandlerConstants.VM_INSTANCE) != null)
231                     vm_instance = Integer.parseInt(context.getAttribute(SdcArtifactHandlerConstants.VM_INSTANCE));
232                 int vnfc_instance = -1;
233                 if (context.getAttribute(SdcArtifactHandlerConstants.VNFC_INSTANCE) != null)
234                     vnfc_instance = Integer.parseInt(context.getAttribute(SdcArtifactHandlerConstants.VNFC_INSTANCE));
235                 whereClause = whereClause + " and ACTION = $" + SdcArtifactHandlerConstants.ACTION + " and VNFC_TYPE = $"
236                         + SdcArtifactHandlerConstants.VNFC_TYPE + " and VNFC_INSTANCE = $"
237                         + SdcArtifactHandlerConstants.VNFC_INSTANCE + " and VM_INSTANCE = $"
238                         + SdcArtifactHandlerConstants.VM_INSTANCE;
239
240             }
241         }
242
243         if (!db.equals(SdcArtifactHandlerConstants.DB_DEVICE_AUTHENTICATION) && serviceLogic != null && context != null) {
244             String key = "select COUNT(*) from " + db + whereClause;
245             log.info("SELECT String : " + key);
246             status = serviceLogic.query("SQL", false, null, key, null, null, context);
247             if (status.toString().equals("FAILURE")) {
248                 throw new SvcLogicException("Error while reading data from " + db);
249             }
250             String count = context.getAttribute("COUNT(*)");
251             log.info("Number of row Returned : " + count + ": " + status + ":");
252             if (count != null && Integer.parseInt(count) > 0) {
253                 context.setAttribute(count, null);
254                 return true;
255             } else
256                 return false;
257         }
258        if (db.equals(SdcArtifactHandlerConstants.DB_DEVICE_AUTHENTICATION) && serviceLogic != null && context != null) {
259              log.info("Check for update or insert for properties file");
260           String protocol = context.getAttribute(SdcArtifactHandlerConstants.DEVICE_PROTOCOL);
261           String action = context.getAttribute(SdcArtifactHandlerConstants.ACTION);
262           String vnf_type = context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE);
263           PropertiesConfiguration conf = new PropertiesConfiguration(System.getenv("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(System.getenv("APPC_CONFIG_DIR")+"/appc_southbound.properties");
334          log.info("is Updating to southbound  properties : "+isUpdate);
335             if (conf.containsKey(property + "." + "user")) {
336                if(user!=null && !user.isEmpty())
337                conf.setProperty(property + "." + "user", user);
338                } else {
339                                  log.info("is Adding to southbound.properties"+isUpdate);
340
341                conf.addProperty(property + "." + "user", user);
342             }
343
344             if (conf.containsKey(property + "." + "port")) {
345                if (port != null && !port.isEmpty())
346                   conf.setProperty(property + "." + "port", port);
347             } else {
348                   conf.addProperty(property + "." + "port", port);
349             }
350             if (conf.containsKey(property + "." + "password")) {
351             } else {
352                conf.addProperty(property + "." + "password", "");
353             }
354             if (conf.containsKey(property + "." + "url")) {
355                if (url != null && !url.isEmpty())
356                   conf.setProperty(property + "." + "url", url);
357
358             } else {
359
360                conf.addProperty(property + "." + "url", url);
361             }
362             log.info("About to save to properties file");
363              conf.save();
364             log.info("saved to properties file");
365    }
366
367     public void processVnfcReference(SvcLogicContext context, boolean isUpdate) throws SvcLogicException {
368         String fn = "DBService.processVnfcReference";
369         log.info(fn + "Starting DB operation for Vnfc Reference " + isUpdate);
370         String key = "";
371         int vm_instance = -1;
372         if (context.getAttribute(SdcArtifactHandlerConstants.VM_INSTANCE) != null)
373             vm_instance = Integer.parseInt(context.getAttribute(SdcArtifactHandlerConstants.VM_INSTANCE));
374         int vnfc_instance = -1;
375         if (context.getAttribute(SdcArtifactHandlerConstants.VNFC_INSTANCE) != null)
376             vnfc_instance = Integer.parseInt(context.getAttribute(SdcArtifactHandlerConstants.VNFC_INSTANCE));
377         QueryStatus status = null;
378         if (isUpdate)
379             key = "update " + SdcArtifactHandlerConstants.DB_VNFC_REFERENCE + " set VM_INSTANCE = " + vm_instance
380                     + " , VNFC_INSTANCE = " + vnfc_instance + " , VNFC_TYPE = $" + SdcArtifactHandlerConstants.VNFC_TYPE
381                     + " , VNFC_FUNCTION_CODE = $" + SdcArtifactHandlerConstants.VNFC_FUNCTION_CODE
382                     + " , GROUP_NOTATION_TYPE = $" + SdcArtifactHandlerConstants.GROUP_NOTATION_TYPE
383                     + " , GROUP_NOTATION_VALUE = $" + SdcArtifactHandlerConstants.GROUP_NOTATION_VALUE
384                     + " , IPADDRESS_V4_OAM_VIP = $" + SdcArtifactHandlerConstants.IPADDRESS_V4_OAM_VIP
385                     + " where VNF_TYPE = $" + SdcArtifactHandlerConstants.VNF_TYPE + " and ACTION = $"
386                     + SdcArtifactHandlerConstants.ACTION + " and VNFC_TYPE = $" + SdcArtifactHandlerConstants.VNFC_TYPE
387                     + " and VNFC_INSTANCE = $" + SdcArtifactHandlerConstants.VNFC_INSTANCE + " and VM_INSTANCE = $"
388                     + SdcArtifactHandlerConstants.VM_INSTANCE;
389         else
390             key = "insert into " + SdcArtifactHandlerConstants.DB_VNFC_REFERENCE + " set  VNF_TYPE = $"
391                     + SdcArtifactHandlerConstants.VNF_TYPE + " , ACTION = $" + SdcArtifactHandlerConstants.ACTION
392                     + " , VM_INSTANCE = $" + SdcArtifactHandlerConstants.VM_INSTANCE + " , VNFC_INSTANCE = $"
393                     + SdcArtifactHandlerConstants.VNFC_INSTANCE + " , VNFC_TYPE = $"
394                     + SdcArtifactHandlerConstants.VNFC_TYPE + " , VNFC_FUNCTION_CODE = $"
395                     + SdcArtifactHandlerConstants.VNFC_FUNCTION_CODE + " , GROUP_NOTATION_TYPE = $"
396                     + SdcArtifactHandlerConstants.GROUP_NOTATION_TYPE + " , IPADDRESS_V4_OAM_VIP = $"
397                     + SdcArtifactHandlerConstants.IPADDRESS_V4_OAM_VIP + " , GROUP_NOTATION_VALUE = $"
398                     + SdcArtifactHandlerConstants.GROUP_NOTATION_VALUE;
399
400         if (serviceLogic != null && context != null) {
401             status = serviceLogic.save("SQL", false, false, key, null, null, context);
402             if (status.toString().equals("FAILURE"))
403                 throw new SvcLogicException("Error While processing VNFC_REFERENCE table ");
404         }
405     }
406
407     public void processDownloadDgReference(SvcLogicContext context, boolean isUpdate)
408             throws SvcLogicException, SQLException {
409         String fn = "DBService.processDownloadDgReference";
410         log.info(fn + "Starting DB operation for Download DG Reference " + isUpdate);
411         String key = "";
412         QueryStatus status = null;
413
414         if (isUpdate)
415             key = "update " + SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE + " set DOWNLOAD_CONFIG_DG = $"
416                     + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " where PROTOCOL = $"
417                     + SdcArtifactHandlerConstants.DEVICE_PROTOCOL;
418         else
419             key = "insert into " + SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE + " set DOWNLOAD_CONFIG_DG = $"
420                     + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " , PROTOCOL = $"
421                     + SdcArtifactHandlerConstants.DEVICE_PROTOCOL;
422
423         if (serviceLogic != null && context != null)
424             status = serviceLogic.save("SQL", false, false, key, null, null, context);
425         if ((status == null) || status.toString().equals("FAILURE"))
426             throw new SvcLogicException("Error While processing DOWNLOAD_DG_REFERENCE table ");
427     }
428
429     public void processConfigActionDg(SvcLogicContext context, boolean isUpdate) throws SvcLogicException {
430         String fn = "DBService.processConfigActionDg";
431         log.info(fn + "Starting DB operation for Config DG Action " + isUpdate);
432         String key = "";
433         QueryStatus status = null;
434
435         if (context.getAttribute(SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE) != null
436                 && context.getAttribute(SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE).length() > 0) {
437             if (isUpdate)
438                 key = "update " + SdcArtifactHandlerConstants.DB_CONFIG_ACTION_DG + " set DOWNLOAD_CONFIG_DG = $"
439                         + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " where ACTION = $"
440                         + SdcArtifactHandlerConstants.ACTION + " and VNF_TYPE = $"
441                         + SdcArtifactHandlerConstants.VNF_TYPE;
442             else
443                 key = "insert into " + SdcArtifactHandlerConstants.DB_CONFIG_ACTION_DG + " set DOWNLOAD_CONFIG_DG = $"
444                         + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " , ACTION = $"
445                         + SdcArtifactHandlerConstants.ACTION + " , VNF_TYPE = $" + SdcArtifactHandlerConstants.VNF_TYPE;
446
447             if (serviceLogic != null && context != null)
448                 status = serviceLogic.save("SQL", false, false, key, null, null, context);
449             if ((status == null) || status.toString().equals("FAILURE"))
450                 throw new SvcLogicException("Error While processing Configure DG Action table ");
451         } else
452             log.info("No Update required for Config DG Action");
453
454     }
455
456     public String getModelDataInformationbyArtifactName(String artifact_name) throws SvcLogicException {
457         String fn = "DBService.getVnfData";
458         String key = "";
459         SvcLogicContext con = new SvcLogicContext();
460         HashMap<String, String> modelData = new HashMap<String, String>();
461         QueryStatus status = null;
462         key = "select VNF_TYPE, VNFC_TYPE, ACTION, FILE_CATEGORY, ARTIFACT_TYPE from ASDC_REFERENCE where  ARTIFACT_NAME = "
463                 + artifact_name;
464
465         if (serviceLogic != null && con != null) {
466             log.info(fn + "select Key: " + key);
467             status = serviceLogic.query("SQL", false, null, key, null, null, con);
468             if (status.toString().equals("FAILURE"))
469                 throw new SvcLogicException("Error While processing is ArtifactUpdateRequiredforPD table ");
470
471         }
472
473         log.info(fn + "Vnf_received :" + con.getAttribute("VNF_TYPE"));
474
475         return con.getAttribute("VNF_TYPE");
476
477     }
478
479     public void updateYangContents(SvcLogicContext context, String artifactId, String yangContents)
480             throws SvcLogicException {
481         String fn = "DBService.updateYangContents";
482         log.info(fn + "Starting DB operation for  updateYangContents");
483         String key = "";
484         QueryStatus status = null;
485
486         key = "update ASDC_ARTIFACTS " + " set ARTIFACT_CONTENT = '" + yangContents + "'"
487                 + " where ASDC_ARTIFACTS_ID = " + artifactId;
488
489         if (serviceLogic != null && context != null)
490             status = serviceLogic.save("SQL", false, false, key, null, null, context);
491         if ((status == null) || status.toString().equals("FAILURE"))
492             throw new SvcLogicException("Error While processing Configure DG Action table ");
493
494     }
495
496
497     public void insertProtocolReference(SvcLogicContext context, String vnfType, String protocol, String action,
498             String action_level, String template) throws SvcLogicException {
499         String fn = "DBService.insertProtocolReference";
500         log.info(fn + "Starting DB operation for  insertProtocolReference");
501         String key = "";
502         QueryStatus status = null;
503
504         key = "insert into PROTOCOL_REFERENCE (ACTION, VNF_TYPE, PROTOCOL, UPDATED_DATE, TEMPLATE, ACTION_LEVEL)"
505                 + " values  (" + "'" + action + "', '" + vnfType + "', '" + protocol + "', now(),'" + template + "', '"
506                 + action_level + "')";
507
508         if (serviceLogic != null && context != null)
509             status = serviceLogic.save("SQL", false, false, key, null, null, context);
510         if ((status == null) || status.toString().equals("FAILURE"))
511             throw new SvcLogicException("Error While processing insertProtocolReference ");
512
513     }
514
515     public boolean isProtocolReferenceUpdateRequired(SvcLogicContext context, String vnfType, String protocol,
516              String action, String action_level, String template) throws SvcLogicException {
517         SvcLogicContext localContext = new SvcLogicContext();
518         String fn = "DBService.isProtocolReferenceUpdateRequired";
519         log.info(fn + "Starting DB operation for  isProtocolReferenceUpdateRequired");
520         String key = "";
521         QueryStatus status = null;
522
523         key = "select COUNT(*) from PROTOCOL_REFERENCE where ACTION='" + action + "' and ACTION_LEVEL='" + action_level
524                 + "' and VNF_TYPE='" + vnfType + "'";
525         status = serviceLogic.query("SQL", false, null, key, null, null, localContext);
526         String countStr = localContext.getAttribute("COUNT(*)");
527         int count = Integer.parseInt(countStr);
528         if (count > 0)
529             return true;
530         else
531             return false;
532     }
533
534     public void updateProtocolReference(SvcLogicContext context, String vnfType, String protocol, String action,
535                 String action_level, String template) throws SvcLogicException {
536
537         String fn = "DBService.isProtocolReferenceUpdateRequired";
538         log.info(fn + "Starting DB operation for  isProtocolReferenceUpdateRequired");
539         String key = "";
540         QueryStatus status = null;
541
542         key = "update PROTOCOL_REFERENCE set UPDATED_DATE=now(), template='" + template + "' where ACTION='" + action
543                 + "' and ACTION_LEVEL='" + action_level + "' and VNF_TYPE='" + vnfType + "'";
544         status = serviceLogic.save("SQL", false, false, key, null, null, context);
545         if (status == QueryStatus.FAILURE) {
546             log.info("updateProtocolReference:: Error updating protocol reference");
547             throw new SvcLogicException("Error - updating PROTOCOL_REFERENCE_TABLE in updateProtocolReference");
548         }
549         return;
550     }
551
552     public String getDownLoadDGReference(SvcLogicContext context) throws SvcLogicException, ConfigurationException {
553         String fn = "DBService.setDownLoadDGReference";
554         String downloadConfigDg = null;
555         log.info(fn + "Setting Download DG Reference from DB");
556         String key = "";
557         QueryStatus status = null;
558         String protocol = context.getAttribute(SdcArtifactHandlerConstants.DEVICE_PROTOCOL);
559         if (StringUtils.isBlank(protocol)) {
560             log.info(fn + " :: Protocol is Blank!! Returning without querying DB");
561             throw new ConfigurationException(fn+":: Protocol is Blank!! Returning without querying DB");
562         }
563         key = "select download_config_dg from " + SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE
564                 + " where protocol = '" + protocol + "'";
565         SvcLogicContext localContext = new SvcLogicContext();
566         status = serviceLogic.query("SQL", false, null, key, null, null, localContext);
567         if (status == QueryStatus.FAILURE) {
568             log.info(fn + ":: Error retrieving download_config_dg");
569             throw new SvcLogicException("Error retrieving download_config_dg");
570         }
571         if (status == QueryStatus.NOT_FOUND) {
572             log.info(fn + ":: NOT_FOUND! No data found for download_config_dg!!");
573             throw new SvcLogicException(fn + ":: NOT_FOUND! No data found for download_config_dg!");
574         }
575         downloadConfigDg = localContext.getAttribute("download-config-dg");
576         log.info(fn + "download_config_dg::" + downloadConfigDg);
577         return downloadConfigDg;
578     }
579
580     public void cleanUpVnfcReferencesForVnf(SvcLogicContext context) throws SvcLogicException {
581        try{
582    String key1 = "delete from " + SdcArtifactHandlerConstants.DB_VNFC_REFERENCE
583                  + " where action = $"+ SdcArtifactHandlerConstants.ACTION + " and vnf_type = $" + SdcArtifactHandlerConstants.VNF_TYPE;
584    log.debug("Action : " + context.getAttribute(SdcArtifactHandlerConstants.ACTION));
585    log.debug("vnfType: "+ context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE));
586    QueryStatus status = null;
587    log.info("cleanUpVnfcReferencesForVnf()::Query:" + key1);
588    if (serviceLogic != null && context != null) {
589       status = serviceLogic.save("SQL", false, false, key1, null, null, context);
590       if (status.toString().equals("FAILURE")) {
591          log.debug("Error deleting from VNFC_REFERENCE table");
592          throw new SvcLogicException("Error While processing VNFC_REFERENCE table ");
593          }
594       status = null;
595       }
596    }catch(Exception e){
597       log.debug("Error deleting from VNFC_REFERENCE table  : "+context.getAttribute(SdcArtifactHandlerConstants.ACTION) +" and " +context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE));
598       }
599    }
600 }