Artifact Handler Updates
[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(System.getenv("APPC_CONFIG_DIR")+"/appc_southbound.properties");
263           String property ="";
264             if (StringUtils.isNotBlank(vnf_type)) {
265
266             if (StringUtils.isNotBlank(protocol)) {
267                if (StringUtils.isNotBlank(action)) {
268                   property = vnf_type + "." + protocol + "." + action;
269                }
270             }
271             }
272             if (conf.subset(property )!=null) {
273                if (conf.containsKey(property )) {
274                   log.info("Key Exists for property"+property +"in southbound.properties file");
275                   return true;
276                }
277             } else
278                log.info("Key Doesnot exists and need to add the key  for property"+property +"in southbound.properties file");
279                 return false;
280         }
281         return false;
282     }
283
284     public void processDeviceInterfaceProtocol(SvcLogicContext context, boolean isUpdate) throws SvcLogicException {
285         String fn = "DBService.processDeviceInterfaceProtocol";
286         log.info("Starting DB operation for Device Interface Protocol " + isUpdate);
287         String key = "";
288         QueryStatus status = null;
289         if (isUpdate)
290             key = "update " + SdcArtifactHandlerConstants.DB_DEVICE_INTERFACE_PROTOCOL + " set PROTOCOL = $"
291                     + SdcArtifactHandlerConstants.DEVICE_PROTOCOL + " , DG_RPC = 'getDeviceRunningConfig' "
292                     + " , MODULE = 'APPC' " + " where VNF_TYPE = $" + SdcArtifactHandlerConstants.VNF_TYPE;
293         else
294             key = "insert into " + SdcArtifactHandlerConstants.DB_DEVICE_INTERFACE_PROTOCOL + " set  VNF_TYPE = $"
295                     + SdcArtifactHandlerConstants.VNF_TYPE + " , PROTOCOL = $"
296                     + SdcArtifactHandlerConstants.DEVICE_PROTOCOL + " , DG_RPC = 'getDeviceRunningConfig' "
297                     + " , MODULE = 'APPC' ";
298
299         if (serviceLogic != null && context != null) {
300
301             status = serviceLogic.save("SQL", false, false, key, null, null, context);
302             if (status.toString().equals("FAILURE"))
303                 throw new SvcLogicException("Error While processing DEVICE_INTERFACE_PROTOCOL table ");
304         }
305
306     }
307
308     public void processDeviceAuthentication(SvcLogicContext context, boolean isUpdate) throws SvcLogicException, ConfigurationException {
309         String fn = "DBService.processDeviceAuthentication";
310         log.info(fn + "Starting DB operation for Device Authentication " + isUpdate);
311         String protocol = context.getAttribute(SdcArtifactHandlerConstants.DEVICE_PROTOCOL);
312       String action = context.getAttribute(SdcArtifactHandlerConstants.ACTION);
313       String vnf_type = context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE);
314       String url = context.getAttribute(SdcArtifactHandlerConstants.URL);
315       String port = context.getAttribute(SdcArtifactHandlerConstants.PORT_NUMBER);
316       String user = context.getAttribute(SdcArtifactHandlerConstants.USER_NAME);
317       String property = vnf_type + "." +protocol + "." + action;
318       log.info("property :"+property);
319       if (StringUtils.isBlank(url)) {
320          url = "";
321       }
322       if (StringUtils.isBlank(port) ) {
323          port = "";
324       }
325       if (StringUtils.isBlank(user)) {
326          user = "";
327       }
328       if (((vnf_type == null) || ("".equals(vnf_type))) && ((action == null) || ("".equals(action)))
329             && ((protocol == null) || ("".equals(protocol))))
330          throw new SvcLogicException(
331                "Error While processing refernce File as few or all of parameters VNF_TYPE,PROTOCOL,ACTION are missing ");
332          PropertiesConfiguration conf = new PropertiesConfiguration(System.getenv("APPC_CONFIG_DIR")+"/appc_southbound.properties");
333          log.info("is Updating to southbound  properties : "+isUpdate);
334             if (conf.containsKey(property + "." + "user")) {
335                if(user!=null && !user.isEmpty())
336                conf.setProperty(property + "." + "user", user);
337                } else {
338                                  log.info("is Adding to southbound.properties"+isUpdate);
339
340                conf.addProperty(property + "." + "user", user);
341             }
342
343             if (conf.containsKey(property + "." + "port")) {
344                if (port != null && !port.isEmpty())
345                   conf.setProperty(property + "." + "port", port);
346             } else {
347                   conf.addProperty(property + "." + "port", port);
348             }
349             if (conf.containsKey(property + "." + "password")) {
350             } else {
351                conf.addProperty(property + "." + "password", "");
352             }
353             if (conf.containsKey(property + "." + "url")) {
354                if (url != null && !url.isEmpty())
355                   conf.setProperty(property + "." + "url", url);
356
357             } else {
358
359                conf.addProperty(property + "." + "url", url);
360             }
361             log.info("About to save to properties file");
362              conf.save();
363             log.info("saved to properties file");
364    }
365
366     public void processVnfcReference(SvcLogicContext context, boolean isUpdate) throws SvcLogicException {
367         String fn = "DBService.processVnfcReference";
368         log.info(fn + "Starting DB operation for Vnfc Reference " + isUpdate);
369         String key = "";
370         int vm_instance = -1;
371         if (context.getAttribute(SdcArtifactHandlerConstants.VM_INSTANCE) != null)
372             vm_instance = Integer.parseInt(context.getAttribute(SdcArtifactHandlerConstants.VM_INSTANCE));
373         int vnfc_instance = -1;
374         if (context.getAttribute(SdcArtifactHandlerConstants.VNFC_INSTANCE) != null)
375             vnfc_instance = Integer.parseInt(context.getAttribute(SdcArtifactHandlerConstants.VNFC_INSTANCE));
376         QueryStatus status = null;
377         if (isUpdate)
378             key = "update " + SdcArtifactHandlerConstants.DB_VNFC_REFERENCE + " set VM_INSTANCE = " + vm_instance
379                     + " , VNFC_INSTANCE = " + vnfc_instance + " , VNFC_TYPE = $" + SdcArtifactHandlerConstants.VNFC_TYPE
380                     + " , VNFC_FUNCTION_CODE = $" + SdcArtifactHandlerConstants.VNFC_FUNCTION_CODE
381                     + " , GROUP_NOTATION_TYPE = $" + SdcArtifactHandlerConstants.GROUP_NOTATION_TYPE
382                     + " , GROUP_NOTATION_VALUE = $" + SdcArtifactHandlerConstants.GROUP_NOTATION_VALUE
383                     + " , IPADDRESS_V4_OAM_VIP = $" + SdcArtifactHandlerConstants.IPADDRESS_V4_OAM_VIP
384                     + " where VNF_TYPE = $" + SdcArtifactHandlerConstants.VNF_TYPE + " and ACTION = $"
385                     + SdcArtifactHandlerConstants.ACTION + " and VNFC_TYPE = $" + SdcArtifactHandlerConstants.VNFC_TYPE
386                     + " and VNFC_INSTANCE = $" + SdcArtifactHandlerConstants.VNFC_INSTANCE + " and VM_INSTANCE = $"
387                     + SdcArtifactHandlerConstants.VM_INSTANCE;
388         else
389             key = "insert into " + SdcArtifactHandlerConstants.DB_VNFC_REFERENCE + " set  VNF_TYPE = $"
390                     + SdcArtifactHandlerConstants.VNF_TYPE + " , ACTION = $" + SdcArtifactHandlerConstants.ACTION
391                     + " , VM_INSTANCE = $" + SdcArtifactHandlerConstants.VM_INSTANCE + " , VNFC_INSTANCE = $"
392                     + SdcArtifactHandlerConstants.VNFC_INSTANCE + " , VNFC_TYPE = $"
393                     + SdcArtifactHandlerConstants.VNFC_TYPE + " , VNFC_FUNCTION_CODE = $"
394                     + SdcArtifactHandlerConstants.VNFC_FUNCTION_CODE + " , GROUP_NOTATION_TYPE = $"
395                     + SdcArtifactHandlerConstants.GROUP_NOTATION_TYPE + " , IPADDRESS_V4_OAM_VIP = $"
396                     + SdcArtifactHandlerConstants.IPADDRESS_V4_OAM_VIP + " , GROUP_NOTATION_VALUE = $"
397                     + SdcArtifactHandlerConstants.GROUP_NOTATION_VALUE;
398
399         if (serviceLogic != null && context != null) {
400             status = serviceLogic.save("SQL", false, false, key, null, null, context);
401             if (status.toString().equals("FAILURE"))
402                 throw new SvcLogicException("Error While processing VNFC_REFERENCE table ");
403         }
404     }
405
406     public void processDownloadDgReference(SvcLogicContext context, boolean isUpdate)
407             throws SvcLogicException, SQLException {
408         String fn = "DBService.processDownloadDgReference";
409         log.info(fn + "Starting DB operation for Download DG Reference " + isUpdate);
410         String key = "";
411         QueryStatus status = null;
412
413         if (isUpdate)
414             key = "update " + SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE + " set DOWNLOAD_CONFIG_DG = $"
415                     + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " where PROTOCOL = $"
416                     + SdcArtifactHandlerConstants.DEVICE_PROTOCOL;
417         else
418             key = "insert into " + SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE + " set DOWNLOAD_CONFIG_DG = $"
419                     + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " , PROTOCOL = $"
420                     + SdcArtifactHandlerConstants.DEVICE_PROTOCOL;
421
422         if (serviceLogic != null && context != null)
423             status = serviceLogic.save("SQL", false, false, key, null, null, context);
424         if ((status == null) || status.toString().equals("FAILURE"))
425             throw new SvcLogicException("Error While processing DOWNLOAD_DG_REFERENCE table ");
426     }
427
428     public void processConfigActionDg(SvcLogicContext context, boolean isUpdate) throws SvcLogicException {
429         String fn = "DBService.processConfigActionDg";
430         log.info(fn + "Starting DB operation for Config DG Action " + isUpdate);
431         String key = "";
432         QueryStatus status = null;
433
434         if (context.getAttribute(SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE) != null
435                 && context.getAttribute(SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE).length() > 0) {
436             if (isUpdate)
437                 key = "update " + SdcArtifactHandlerConstants.DB_CONFIG_ACTION_DG + " set DOWNLOAD_CONFIG_DG = $"
438                         + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " where ACTION = $"
439                         + SdcArtifactHandlerConstants.ACTION + " and VNF_TYPE = $"
440                         + SdcArtifactHandlerConstants.VNF_TYPE;
441             else
442                 key = "insert into " + SdcArtifactHandlerConstants.DB_CONFIG_ACTION_DG + " set DOWNLOAD_CONFIG_DG = $"
443                         + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " , ACTION = $"
444                         + SdcArtifactHandlerConstants.ACTION + " , VNF_TYPE = $" + SdcArtifactHandlerConstants.VNF_TYPE;
445
446             if (serviceLogic != null && context != null)
447                 status = serviceLogic.save("SQL", false, false, key, null, null, context);
448             if ((status == null) || status.toString().equals("FAILURE"))
449                 throw new SvcLogicException("Error While processing Configure DG Action table ");
450         } else
451             log.info("No Update required for Config DG Action");
452
453     }
454
455     public String getModelDataInformationbyArtifactName(String artifact_name) throws SvcLogicException {
456         String fn = "DBService.getVnfData";
457         String key = "";
458         SvcLogicContext con = new SvcLogicContext();
459         HashMap<String, String> modelData = new HashMap<String, String>();
460         QueryStatus status = null;
461         key = "select VNF_TYPE, VNFC_TYPE, ACTION, FILE_CATEGORY, ARTIFACT_TYPE from ASDC_REFERENCE where  ARTIFACT_NAME = "
462                 + artifact_name;
463
464         if (serviceLogic != null && con != null) {
465             log.info(fn + "select Key: " + key);
466             status = serviceLogic.query("SQL", false, null, key, null, null, con);
467             if (status.toString().equals("FAILURE"))
468                 throw new SvcLogicException("Error While processing is ArtifactUpdateRequiredforPD table ");
469
470         }
471
472         log.info(fn + "Vnf_received :" + con.getAttribute("VNF_TYPE"));
473
474         return con.getAttribute("VNF_TYPE");
475
476     }
477
478     public void updateYangContents(SvcLogicContext context, String artifactId, String yangContents)
479             throws SvcLogicException {
480         String fn = "DBService.updateYangContents";
481         log.info(fn + "Starting DB operation for  updateYangContents");
482         String key = "";
483         QueryStatus status = null;
484
485         key = "update ASDC_ARTIFACTS " + " set ARTIFACT_CONTENT = '" + yangContents + "'"
486                 + " where ASDC_ARTIFACTS_ID = " + artifactId;
487
488         if (serviceLogic != null && context != null)
489             status = serviceLogic.save("SQL", false, false, key, null, null, context);
490         if ((status == null) || status.toString().equals("FAILURE"))
491             throw new SvcLogicException("Error While processing Configure DG Action table ");
492
493     }
494
495
496     public void insertProtocolReference(SvcLogicContext context, String vnfType, String protocol, String action,
497             String action_level, String template) throws SvcLogicException {
498         String fn = "DBService.insertProtocolReference";
499         log.info(fn + "Starting DB operation for  insertProtocolReference");
500         String key = "";
501         QueryStatus status = null;
502
503         key = "insert into PROTOCOL_REFERENCE (ACTION, VNF_TYPE, PROTOCOL, UPDATED_DATE, TEMPLATE, ACTION_LEVEL)"
504                 + " values  (" + "'" + action + "', '" + vnfType + "', '" + protocol + "', now(),'" + template + "', '"
505                 + action_level + "')";
506
507         if (serviceLogic != null && context != null)
508             status = serviceLogic.save("SQL", false, false, key, null, null, context);
509         if ((status == null) || status.toString().equals("FAILURE"))
510             throw new SvcLogicException("Error While processing insertProtocolReference ");
511
512     }
513
514     public boolean isProtocolReferenceUpdateRequired(SvcLogicContext context, String vnfType, String protocol,
515              String action, String action_level, String template) throws SvcLogicException {
516         SvcLogicContext localContext = new SvcLogicContext();
517         String fn = "DBService.isProtocolReferenceUpdateRequired";
518         log.info(fn + "Starting DB operation for  isProtocolReferenceUpdateRequired");
519         String key = "";
520         QueryStatus status = null;
521
522         key = "select COUNT(*) from PROTOCOL_REFERENCE where ACTION='" + action + "' and ACTION_LEVEL='" + action_level
523                 + "' and VNF_TYPE='" + vnfType + "'";
524         status = serviceLogic.query("SQL", false, null, key, null, null, localContext);
525         String countStr = localContext.getAttribute("COUNT(*)");
526         int count = Integer.parseInt(countStr);
527         if (count > 0)
528             return true;
529         else
530             return false;
531     }
532
533     public void updateProtocolReference(SvcLogicContext context, String vnfType, String protocol, String action,
534                 String action_level, String template) throws SvcLogicException {
535
536         String fn = "DBService.isProtocolReferenceUpdateRequired";
537         log.info(fn + "Starting DB operation for  isProtocolReferenceUpdateRequired");
538         String key = "";
539         QueryStatus status = null;
540
541         key = "update PROTOCOL_REFERENCE set UPDATED_DATE=now(), template='" + template + "' where ACTION='" + action
542                 + "' and ACTION_LEVEL='" + action_level + "' and VNF_TYPE='" + vnfType + "'";
543         status = serviceLogic.save("SQL", false, false, key, null, null, context);
544         if (status == QueryStatus.FAILURE) {
545             log.info("updateProtocolReference:: Error updating protocol reference");
546             throw new SvcLogicException("Error - updating PROTOCOL_REFERENCE_TABLE in updateProtocolReference");
547         }
548         return;
549     }
550
551     public String getDownLoadDGReference(SvcLogicContext context) throws Exception {
552         String fn = "DBService.setDownLoadDGReference";
553         String downloadConfigDg = null;
554         log.info(fn + "Setting Download DG Reference from DB");
555         String key = "";
556         QueryStatus status = null;
557         String protocol = context.getAttribute(SdcArtifactHandlerConstants.DEVICE_PROTOCOL);
558         if (StringUtils.isBlank(protocol)) {
559             log.info(fn + " :: Protocol is Blank!! Returning without querying DB");
560             throw new Exception(fn+":: Protocol is Blank!! Returning without querying DB");
561         }
562         key = "select download_config_dg from " + SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE
563                 + " where protocol = '" + protocol + "'";
564         SvcLogicContext localContext = new SvcLogicContext();
565         status = serviceLogic.query("SQL", false, null, key, null, null, localContext);
566         if (status == QueryStatus.FAILURE) {
567             log.info(fn + ":: Error retrieving download_config_dg");
568             throw new SvcLogicException("Error retrieving download_config_dg");
569         }
570         if (status == QueryStatus.NOT_FOUND) {
571             log.info(fn + ":: NOT_FOUND! No data found for download_config_dg!!");
572             throw new Exception(fn + ":: NOT_FOUND! No data found for download_config_dg!");
573         }
574         downloadConfigDg = localContext.getAttribute("download-config-dg");
575         log.info(fn + "download_config_dg::" + downloadConfigDg);
576         return downloadConfigDg;
577     }
578
579     public void cleanUpVnfcReferencesForVnf(SvcLogicContext context) throws SvcLogicException {
580        try{
581    String key1 = "delete from " + SdcArtifactHandlerConstants.DB_VNFC_REFERENCE
582                  + " where action = $"+ SdcArtifactHandlerConstants.ACTION + " and vnf_type = $" + SdcArtifactHandlerConstants.VNF_TYPE;
583    log.debug("Action : " + context.getAttribute(SdcArtifactHandlerConstants.ACTION));
584    log.debug("vnfType: "+ context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE));
585    QueryStatus status = null;
586    log.info("cleanUpVnfcReferencesForVnf()::Query:" + key1);
587    if (serviceLogic != null && context != null) {
588       status = serviceLogic.save("SQL", false, false, key1, null, null, context);
589       if (status.toString().equals("FAILURE")) {
590          log.debug("Error deleting from VNFC_REFERENCE table");
591          throw new SvcLogicException("Error While processing VNFC_REFERENCE table ");
592          }
593       status = null;
594       }
595    }catch(Exception e){
596       log.debug("Error deleting from VNFC_REFERENCE table  : "+context.getAttribute(SdcArtifactHandlerConstants.ACTION) +" and " +context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE));
597       }
598    }
599 }