471b0b019b835d8412e655b5de543e60d87fac68
[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-2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * ================================================================================
9  * Modifications Copyright (C) 2019 Ericsson
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  *
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.artifact.handler.dbservices;
27
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
30
31 import java.util.ArrayList;
32
33 import org.apache.commons.configuration.ConfigurationException;
34 import org.apache.commons.configuration.PropertiesConfiguration;
35 import org.apache.commons.lang.StringUtils;
36 import org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants;
37 import org.onap.ccsdk.sli.adaptors.resource.sql.SqlResource;
38 import org.onap.ccsdk.sli.core.dblib.DbLibService;
39 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
40 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
41 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
42 import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
43
44 public class DBService {
45
46     private static final EELFLogger log = EELFManager.getInstance().getLogger(DBService.class);
47     private static final String FAILURE_PARAM = "FAILURE";
48     private static final String RECEIVED_AS = "Internal Version received as1 : ";
49
50     private DbLibServiceQueries dblib;
51     private static DBService dgGeneralDBService = null;
52
53     private DBService() {
54         if (dblib == null) {
55             dblib = new DbLibServiceQueries();
56         }
57     }
58
59     protected DBService(DbLibService dbLibService) {
60         if (dblib == null) {
61             dblib = new DbLibServiceQueries(dbLibService);
62         }
63     }
64     
65     protected DBService(DbLibServiceQueries dbLibServiceQueries) {
66         if (dblib == null) {
67             dblib = dbLibServiceQueries;
68         }
69     }
70
71     public static DBService initialise() {
72         if (dgGeneralDBService == null) {
73             dgGeneralDBService = new DBService();
74         }
75         return dgGeneralDBService;
76     }
77
78     public String getInternalVersionNumber(SvcLogicContext ctx, String artifactName, String prefix)
79         throws SvcLogicException {
80         QueryStatus status;
81         String artifactInternalVersion = null;
82         if (dblib != null && ctx != null) {
83             String key = "select max(internal_version) as maximum from ASDC_ARTIFACTS  WHERE ARTIFACT_NAME = ?";
84             ArrayList<String> arguments = new ArrayList<>();
85             arguments.add(artifactName);
86             log.info("Getting internal Version :" + key);
87             status = dblib.query(key, ctx, arguments);
88             if (status.toString().equals(FAILURE_PARAM)) {
89                 throw new SvcLogicException("Error - getting internal Artifact Number");
90             }
91             artifactInternalVersion = ctx.getAttribute("maximum");
92             log.info("Internal Version received as : " + artifactInternalVersion);
93             log.info(RECEIVED_AS + ctx.getAttribute("max(internal_version)"));
94             log.info(RECEIVED_AS + ctx.getAttribute("max"));
95             log.info(RECEIVED_AS + ctx.getAttribute("internal_version"));
96             log.info(RECEIVED_AS + ctx.getAttributeKeySet().toString());
97         }
98         return artifactInternalVersion;
99     }
100
101     public String getArtifactID(SvcLogicContext ctx, String artifactName) throws SvcLogicException {
102         QueryStatus status;
103         String artifactID = null;
104         if (dblib != null && ctx != null) {
105             String key = "select max(ASDC_ARTIFACTS_ID) as id from ASDC_ARTIFACTS  WHERE ARTIFACT_NAME = ?";
106             ArrayList<String> arguments = new ArrayList<>();
107             log.info("Getting Artifact ID String :" + key);
108             status = dblib.query(key, ctx, arguments);
109             if (status.toString().equals(FAILURE_PARAM)) {
110                 throw new SvcLogicException("Error - getting  Artifact ID from database");
111             }
112             artifactID = ctx.getAttribute("id");
113             log.info("SDC_ARTIFACTS_ID received as : " + ctx.getAttribute("id"));
114         }
115         return artifactID;
116     }
117
118     public QueryStatus saveArtifacts(SvcLogicContext ctx, int intversion) throws SvcLogicException {
119         QueryStatus status = null;
120         if (dblib != null && ctx != null) {
121             String key = "INSERT INTO ASDC_ARTIFACTS " + "SET SERVICE_UUID    =  $service-uuid , "
122                 + " DISTRIBUTION_ID    =  $distribution-id ," + " SERVICE_NAME    =  $service-name ,"
123                 + " SERVICE_DESCRIPTION    =  $service-description ," + " RESOURCE_UUID    = $resource-uuid ,"
124                 + " RESOURCE_INSTANCE_NAME    = $resource-instance-name ," + " RESOURCE_NAME    = $resource-name ,"
125                 + " RESOURCE_VERSION    = $resource-version ," + " RESOURCE_TYPE    = $resource-type ,"
126                 + " ARTIFACT_UUID    = $artifact-uuid ," + " ARTIFACT_TYPE    = $artifact-type ,"
127                 + " ARTIFACT_VERSION    = $artifact-version ,"
128                 + " ARTIFACT_DESCRIPTION    = $artifact-description ," + " INTERNAL_VERSION    = $internal-version"
129                 + " ," + " ARTIFACT_NAME       =  $artifact-name ," + " ARTIFACT_CONTENT    =  $artifact-contents ";
130             ctx.setAttribute("internal-version", Integer.toString(intversion));
131             status = dblib.save(key, ctx);
132             if (status.toString().equals(FAILURE_PARAM)) {
133                 throw new SvcLogicException("Error While processing storing Artifact: "
134                     + ctx.getAttribute(SdcArtifactHandlerConstants.ARTIFACT_NAME));
135             }
136         }
137         return status;
138     }
139
140     public QueryStatus logData(SvcLogicContext ctx, String prefix) throws SvcLogicException {
141         QueryStatus status = null;
142         if (dblib != null && ctx != null) {
143             String key = "INSERT INTO CONFIG_TRANSACTION_LOG " + " SET request_id = $request-id , "
144                 + " message_type = $log-message-type , " + " message = $log-message ;";
145             status = dblib.save(key, ctx);
146             if (status.toString().equals(FAILURE_PARAM)) {
147                 throw new SvcLogicException("Error while logging data");
148             }
149
150         }
151         return status;
152     }
153
154     public void processConfigureActionDg(SvcLogicContext context, boolean isUpdate) {
155         log.info("Update Parameter for SDC Reference " + isUpdate);
156         //TODO implement this method
157     }
158
159     public void processSdcReferences(SvcLogicContext context, boolean isUpdate) throws SvcLogicException {
160         processSdcReferences(context, isUpdate, null);
161     }
162
163     public void processSdcReferences(SvcLogicContext context, boolean isUpdate, String modelId) throws SvcLogicException {
164         String key;
165         QueryStatus status;
166          if (isUpdate && context.getAttribute(SdcArtifactHandlerConstants.FILE_CATEGORY)
167             .equals(SdcArtifactHandlerConstants.CAPABILITY)) {
168             log.info("Updating capability artifact in ASDC_REFERENCE");
169             key = "update " + SdcArtifactHandlerConstants.DB_SDC_REFERENCE + "  set ARTIFACT_NAME = $"
170                 + SdcArtifactHandlerConstants.ARTIFACT_NAME + " where " + "FILE_CATEGORY = $"
171                 + SdcArtifactHandlerConstants.FILE_CATEGORY + " and VNF_TYPE = $"
172                 + SdcArtifactHandlerConstants.VNF_TYPE;
173         } else if (isUpdate) {
174             key = "update " + SdcArtifactHandlerConstants.DB_SDC_REFERENCE + "  set ARTIFACT_NAME = $"
175                 + SdcArtifactHandlerConstants.ARTIFACT_NAME + " where VNFC_TYPE = $"
176                 + SdcArtifactHandlerConstants.VNFC_TYPE + " and FILE_CATEGORY = $"
177                 + SdcArtifactHandlerConstants.FILE_CATEGORY + " and ACTION = $" + SdcArtifactHandlerConstants.ACTION
178                 + " and VNF_TYPE = $" + SdcArtifactHandlerConstants.VNF_TYPE;
179             if (StringUtils.isNotBlank(modelId)) {
180                 key += createQueryListForTemplateIds(modelId, context);
181             }
182         } else {
183             if (context.getAttribute(SdcArtifactHandlerConstants.FILE_CATEGORY)
184                 .equals(SdcArtifactHandlerConstants.CAPABILITY)) {
185                 log.info("Inserting new record for capability artifact in ASDC_REFERENCE");
186                 key = "insert into " + SdcArtifactHandlerConstants.DB_SDC_REFERENCE + " set VNFC_TYPE = null "
187                     + " , FILE_CATEGORY = $" + SdcArtifactHandlerConstants.FILE_CATEGORY + " , VNF_TYPE = $"
188                     + SdcArtifactHandlerConstants.VNF_TYPE + " , ACTION = null " + " , ARTIFACT_TYPE = null "
189                     + " , ARTIFACT_NAME = $" + SdcArtifactHandlerConstants.ARTIFACT_NAME;
190             } else {
191                 key = "insert into " + SdcArtifactHandlerConstants.DB_SDC_REFERENCE + " set VNFC_TYPE = $"
192                     + SdcArtifactHandlerConstants.VNFC_TYPE + " , FILE_CATEGORY = $"
193                     + SdcArtifactHandlerConstants.FILE_CATEGORY + " , VNF_TYPE = $"
194                     + SdcArtifactHandlerConstants.VNF_TYPE + " , ACTION = $" + SdcArtifactHandlerConstants.ACTION
195                     + " , ARTIFACT_TYPE = $" + SdcArtifactHandlerConstants.ARTIFACT_TYPE + " , ARTIFACT_NAME = $"
196                     + SdcArtifactHandlerConstants.ARTIFACT_NAME;
197             }
198         }
199         if (dblib != null) {
200             log.info("Insert Key: " + key);
201             status = dblib.save(key, context);
202             if (status.toString().equals(FAILURE_PARAM)) {
203                 throw new SvcLogicException("Error While processing sdc_reference table ");
204             }
205         }
206     }
207
208     public boolean isArtifactUpdateRequired(SvcLogicContext context, String db) throws DBException {
209         return isArtifactUpdateRequired( context,  db, null);
210     }
211
212     public boolean isArtifactUpdateRequired(SvcLogicContext context, String db, String modelId)
213         throws DBException {
214         try {
215             log.info("Checking if Update required for this data");
216             log.info("db" + db);
217             log.info("ACTION=" + context.getAttribute(SdcArtifactHandlerConstants.ACTION));
218             log.info("VNFC_TYPE=" + context.getAttribute(SdcArtifactHandlerConstants.VNFC_TYPE));
219             log.info("VNFC_INSTANCE=" + context.getAttribute(SdcArtifactHandlerConstants.VNFC_INSTANCE));
220             log.info("VM_INSTANCE=" + context.getAttribute(SdcArtifactHandlerConstants.VM_INSTANCE));
221             log.info("VNF_TYPE=" + context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE));
222
223             //Check for templates
224             //if templates are present - there might be multiple records, so validate
225             if( db.equals(SdcArtifactHandlerConstants.DB_SDC_REFERENCE) && StringUtils.isNotBlank(modelId)) {
226                 log.info("ModelId is sent!!");
227                   String queryPart = createQueryListForTemplateIds(modelId, context);
228                   log.info("Querypart is = " + queryPart);
229                    if (isUpdateRequiredForTemplates(queryPart, context, db)) {
230                        log.info("Update is Required!!");
231                     return true;
232                    } else {
233                        log.info("Insert is Required!!");
234                        return false;
235                    }
236             }
237
238             String whereClause;
239             QueryStatus status;
240             whereClause = " where VNF_TYPE = $" + SdcArtifactHandlerConstants.VNF_TYPE;
241             whereClause = resolveWhereClause(context, db, whereClause);
242             if (validate(db)) {
243                     String key = "select COUNT(*) from " + db + whereClause;
244                     log.info("SELECT String : " + key);
245                     status = dblib.query(key, context);
246                     checkForFailure(db, status);
247                     String count = context.getAttribute("COUNT(*)");
248                     log.info("Number of row Returned : " + count + ": " + status + ":");
249                     return tryAddCountAttribute(context, count);
250             }
251             return false;
252         } catch (SvcLogicException e) {
253             throw new DBException("An error occurred while checking for artifact update", e);
254         }
255     }
256
257     private void checkForFailure(String db, QueryStatus status) throws SvcLogicException {
258         if (status.toString().equals(FAILURE_PARAM)) {
259             throw new SvcLogicException("Error while reading data from " + db);
260         }
261     }
262
263     private boolean validate(String db) {
264         return db != null && dblib != null;
265     }
266
267     private boolean keyExists(PropertiesConfiguration conf, String property) {
268         if (conf.subset(property) != null) {
269             if (conf.containsKey(property)) {
270                 log.info("Key Exists for property" + property + "in southbound.properties file");
271                 return true;
272             }
273         } else {
274             log.info("Key Does not exists and need to add the key  for property" + property
275                 + "in southbound.properties file");
276         }
277         return false;
278     }
279
280     private boolean tryAddCountAttribute(SvcLogicContext context, String count) {
281         if (count != null && Integer.parseInt(count) > 0) {
282             context.setAttribute(count, null);
283             return true;
284         } else {
285             return false;
286         }
287     }
288
289
290     private String resolveWhereClause(SvcLogicContext context, String db, String whereClause) {
291         if (db != null) {
292             if (hasValidAttributes(context, db)) {
293                 return whereClause + " and FILE_CATEGORY = $" + SdcArtifactHandlerConstants.FILE_CATEGORY;
294             } else if (db.equals(SdcArtifactHandlerConstants.DB_SDC_REFERENCE)) {
295                 return whereClause + " and VNFC_TYPE = $" + SdcArtifactHandlerConstants.VNFC_TYPE
296                     + " and FILE_CATEGORY = $" + SdcArtifactHandlerConstants.FILE_CATEGORY + " and ACTION = $"
297                     + SdcArtifactHandlerConstants.ACTION;
298             } else if (db.equals(SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE)) {
299                 return " where PROTOCOL = $" + SdcArtifactHandlerConstants.DEVICE_PROTOCOL;
300             } else if (db.equals(SdcArtifactHandlerConstants.DB_DEVICE_AUTHENTICATION)) {
301                 log.info(" DB validation for Device authentication " + whereClause + " AND  PROTOCOL = $"
302                              + SdcArtifactHandlerConstants.DEVICE_PROTOCOL + " AND ACTION = $"
303                              + SdcArtifactHandlerConstants.ACTION);
304                 return whereClause + " AND  PROTOCOL = $" + SdcArtifactHandlerConstants.DEVICE_PROTOCOL
305                              + " AND ACTION = $" + SdcArtifactHandlerConstants.ACTION;
306             } else if (db.equals(SdcArtifactHandlerConstants.DB_CONFIG_ACTION_DG)) {
307                 return whereClause + " and ACTION = $" + SdcArtifactHandlerConstants.ACTION;
308             } else if (db.equals(SdcArtifactHandlerConstants.DB_VNFC_REFERENCE)) {
309                 return whereClause + " and ACTION = $" + SdcArtifactHandlerConstants.ACTION
310                     + " and VNFC_TYPE = $" + SdcArtifactHandlerConstants.VNFC_TYPE + " and VNFC_INSTANCE = $"
311                     + SdcArtifactHandlerConstants.VNFC_INSTANCE + " and VM_INSTANCE = $"
312                     + SdcArtifactHandlerConstants.VM_INSTANCE;
313             }
314         }
315         return whereClause;
316     }
317
318     private boolean hasValidAttributes(SvcLogicContext context, String db) {
319         return db.equals(SdcArtifactHandlerConstants.DB_SDC_REFERENCE)
320             && context.getAttribute(SdcArtifactHandlerConstants.FILE_CATEGORY)
321             .equals(SdcArtifactHandlerConstants.CAPABILITY)
322             && context.getAttribute(SdcArtifactHandlerConstants.ACTION) == null;
323     }
324
325     public void processDeviceInterfaceProtocol(SvcLogicContext context, boolean isUpdate) throws SvcLogicException {
326         log.info("Starting DB operation for Device Interface Protocol " + isUpdate);
327         String key;
328         QueryStatus status;
329         if (isUpdate) {
330             key = "update " + SdcArtifactHandlerConstants.DB_DEVICE_INTERFACE_PROTOCOL + " set PROTOCOL = $"
331                 + SdcArtifactHandlerConstants.DEVICE_PROTOCOL + " , DG_RPC = 'getDeviceRunningConfig' "
332                 + " , MODULE = 'APPC' " + " where VNF_TYPE = $" + SdcArtifactHandlerConstants.VNF_TYPE;
333         } else {
334             key =
335                 "insert into " + SdcArtifactHandlerConstants.DB_DEVICE_INTERFACE_PROTOCOL + " set  VNF_TYPE = $"
336                     + SdcArtifactHandlerConstants.VNF_TYPE + " , PROTOCOL = $"
337                     + SdcArtifactHandlerConstants.DEVICE_PROTOCOL + " , DG_RPC = 'getDeviceRunningConfig' "
338                     + " , MODULE = 'APPC' ";
339         }
340
341         if (dblib != null && context != null) {
342
343             status = dblib.save(key, context);
344             if (status.toString().equals(FAILURE_PARAM)) {
345                 throw new SvcLogicException("Error While processing DEVICE_INTERFACE_PROTOCOL table ");
346             }
347         }
348     }
349
350     public void processDeviceAuthentication(SvcLogicContext context, boolean isUpdate)
351         throws DBException {
352         try {
353             String fn = "DBService.processDeviceAuthentication";
354             log.info(fn + "Starting DB operation for Device Authentication " + isUpdate);
355             String port = context.getAttribute(SdcArtifactHandlerConstants.PORT_NUMBER);
356             String user = context.getAttribute(SdcArtifactHandlerConstants.USER_NAME);
357             String protocol = context.getAttribute(SdcArtifactHandlerConstants.DEVICE_PROTOCOL);
358             String action = context.getAttribute(SdcArtifactHandlerConstants.ACTION);
359             String vnftype = context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE);
360
361             if (StringUtils.isBlank(port)) {
362                 port = "0";
363                 context.setAttribute(SdcArtifactHandlerConstants.PORT_NUMBER, port);
364             }
365             if (StringUtils.isBlank(user)) {
366                 user = "";
367                 context.setAttribute(SdcArtifactHandlerConstants.USER_NAME, user);
368             }
369             if (isInvalidInput(SdcArtifactHandlerConstants.DEVICE_PROTOCOL, SdcArtifactHandlerConstants.ACTION,
370                         SdcArtifactHandlerConstants.VNF_TYPE)) {
371                 throw new SvcLogicException(
372                     "Error While processing reference File as few or all of parameters VNF_TYPE,PROTOCOL,ACTION are missing ");
373             }
374
375             log.info("Starting DB operation for Device authentication " + isUpdate);
376             log.info("credentials"+user + "user" + "port" + port +"protocol" + protocol + "action" + action + "vnftype" + vnftype);
377             String key;
378             QueryStatus status;
379             if (isUpdate) {
380                 key = "update " + SdcArtifactHandlerConstants.DB_DEVICE_AUTHENTICATION + " set USER_NAME = $"
381                         + SdcArtifactHandlerConstants.USER_NAME
382                         + " , PORT_NUMBER = $" + SdcArtifactHandlerConstants.PORT_NUMBER + "";
383                 if (context.getAttributeKeySet().contains(SdcArtifactHandlerConstants.URL)) {
384                     String url = context.getAttribute(SdcArtifactHandlerConstants.URL);
385                     if (StringUtils.isBlank(url)) {
386                         url = "" ;
387                         context.setAttribute(SdcArtifactHandlerConstants.URL, url);
388                     }
389                     key = key + " , URL = $" + SdcArtifactHandlerConstants.URL + " ";
390                 }
391                 key = key + " where VNF_TYPE = $" + SdcArtifactHandlerConstants.VNF_TYPE + "  AND PROTOCOL = $"
392                         + SdcArtifactHandlerConstants.DEVICE_PROTOCOL + " AND  ACTION = $"
393                         + SdcArtifactHandlerConstants.ACTION;
394             } else {
395                 key = "insert into DEVICE_AUTHENTICATION set VNF_TYPE = $" + SdcArtifactHandlerConstants.VNF_TYPE
396                         + " , PROTOCOL = $" + SdcArtifactHandlerConstants.DEVICE_PROTOCOL
397                         + " , " + "ACTION = $" + SdcArtifactHandlerConstants.ACTION
398                         + " , USER_NAME = $" + SdcArtifactHandlerConstants.USER_NAME
399                         + " , PORT_NUMBER = $" + SdcArtifactHandlerConstants.PORT_NUMBER;
400                 if (context.getAttributeKeySet().contains(SdcArtifactHandlerConstants.URL)) {
401                     String url = context.getAttribute(SdcArtifactHandlerConstants.URL);
402                     if (StringUtils.isBlank(url)) {
403                         url = "";
404                         context.setAttribute(SdcArtifactHandlerConstants.URL, url);
405                     }
406                     key = key + " , URL = $" + SdcArtifactHandlerConstants.URL + " ";
407                 }
408             }
409
410             log.info("Query forDevice authentication  " + key);
411             if (dblib != null && context != null) {
412
413                 status = dblib.save(key, context);
414                 if (status.toString().equals(FAILURE_PARAM)) {
415                     throw new SvcLogicException("Error While processing DEVICE_AUTHENTICATION table ");
416                 }
417             }
418
419         } catch (SvcLogicException e) {
420
421             throw new DBException("An error occurred when processing device authentication", e);
422         }
423     }
424
425     private boolean isInvalidInput(String protocol, String action, String vnfType) {
426         return isInvalid(vnfType) && isInvalid(action) && isInvalid(protocol);
427     }
428
429     private boolean isInvalid(String str) {
430         return (str == null) || ("".equals(str));
431     }
432
433     public void processVnfcReference(SvcLogicContext context, boolean isUpdate) throws SvcLogicException {
434         String fn = "DBService.processVnfcReference";
435         log.info(fn + "Starting DB operation for Vnfc Reference " + isUpdate);
436         String key;
437
438         int vmInstance = -1;
439         if (context.getAttribute(SdcArtifactHandlerConstants.VM_INSTANCE) != null) {
440             vmInstance = Integer.parseInt(context.getAttribute(SdcArtifactHandlerConstants.VM_INSTANCE));
441         }
442
443         int vnfcInstance = -1;
444         if (context.getAttribute(SdcArtifactHandlerConstants.VNFC_INSTANCE) != null) {
445             vnfcInstance = Integer.parseInt(context.getAttribute(SdcArtifactHandlerConstants.VNFC_INSTANCE));
446         }
447
448         QueryStatus status;
449         if (isUpdate) {
450             key = "update " + SdcArtifactHandlerConstants.DB_VNFC_REFERENCE + " set VM_INSTANCE = " + vmInstance
451                 + " , VNFC_INSTANCE = " + vnfcInstance + " , VNFC_TYPE = $" + SdcArtifactHandlerConstants.VNFC_TYPE
452                 + " , VNFC_FUNCTION_CODE = $" + SdcArtifactHandlerConstants.VNFC_FUNCTION_CODE
453                 + " , GROUP_NOTATION_TYPE = $" + SdcArtifactHandlerConstants.GROUP_NOTATION_TYPE
454                 + " , GROUP_NOTATION_VALUE = $" + SdcArtifactHandlerConstants.GROUP_NOTATION_VALUE
455                 + " , IPADDRESS_V4_OAM_VIP = $" + SdcArtifactHandlerConstants.IPADDRESS_V4_OAM_VIP
456                 + " where VNF_TYPE = $" + SdcArtifactHandlerConstants.VNF_TYPE + " and ACTION = $"
457                 + SdcArtifactHandlerConstants.ACTION + " and VNFC_TYPE = $" + SdcArtifactHandlerConstants.VNFC_TYPE
458                 + " and VNFC_INSTANCE = $" + SdcArtifactHandlerConstants.VNFC_INSTANCE + " and VM_INSTANCE = $"
459                 + SdcArtifactHandlerConstants.VM_INSTANCE;
460         } else {
461             key = "insert into " + SdcArtifactHandlerConstants.DB_VNFC_REFERENCE + " set  VNF_TYPE = $"
462                 + SdcArtifactHandlerConstants.VNF_TYPE + " , ACTION = $" + SdcArtifactHandlerConstants.ACTION
463                 + " , VM_INSTANCE = $" + SdcArtifactHandlerConstants.VM_INSTANCE + " , VNFC_INSTANCE = $"
464                 + SdcArtifactHandlerConstants.VNFC_INSTANCE + " , VNFC_TYPE = $"
465                 + SdcArtifactHandlerConstants.VNFC_TYPE + " , VNFC_FUNCTION_CODE = $"
466                 + SdcArtifactHandlerConstants.VNFC_FUNCTION_CODE + " , TEMPLATE_ID = $"
467                 + SdcArtifactHandlerConstants.TEMPLATE_ID + " , GROUP_NOTATION_TYPE = $"
468                 + SdcArtifactHandlerConstants.GROUP_NOTATION_TYPE + " , IPADDRESS_V4_OAM_VIP = $"
469                 + SdcArtifactHandlerConstants.IPADDRESS_V4_OAM_VIP + " , GROUP_NOTATION_VALUE = $"
470                 + SdcArtifactHandlerConstants.GROUP_NOTATION_VALUE;
471         }
472
473         if (dblib != null) {
474             status = dblib.save(key, context);
475             if (status.toString().equals(FAILURE_PARAM)) {
476                 throw new SvcLogicException("Error While processing VNFC_REFERENCE table ");
477             }
478         }
479     }
480
481     public void processDownloadDgReference(SvcLogicContext context, boolean isUpdate)
482         throws SvcLogicException {
483         String fn = "DBService.processDownloadDgReference";
484         log.info(fn + "Starting DB operation for Download DG Reference " + isUpdate);
485         String key;
486         QueryStatus status = null;
487
488         if (isUpdate) {
489             key =
490                 "update " + SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE + " set DOWNLOAD_CONFIG_DG = $"
491                     + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " where PROTOCOL = $"
492                     + SdcArtifactHandlerConstants.DEVICE_PROTOCOL;
493         } else {
494             key = "insert into " + SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE
495                 + " set DOWNLOAD_CONFIG_DG = $"
496                 + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " , PROTOCOL = $"
497                 + SdcArtifactHandlerConstants.DEVICE_PROTOCOL;
498         }
499
500         if (dblib != null && context != null) {
501             status = dblib.save(key, context);
502         }
503         if ((status == null) || status.toString().equals(FAILURE_PARAM)) {
504             throw new SvcLogicException("Error While processing DOWNLOAD_DG_REFERENCE table ");
505         }
506     }
507
508     public void processConfigActionDg(SvcLogicContext context, boolean isUpdate) throws SvcLogicException {
509         String fn = "DBService.processConfigActionDg";
510         log.info(fn + "Starting DB operation for Config DG Action " + isUpdate);
511         String key;
512         QueryStatus status = null;
513
514         if (context.getAttribute(SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE) != null
515             && context.getAttribute(SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE).length() > 0) {
516             if (isUpdate) {
517                 key = "update " + SdcArtifactHandlerConstants.DB_CONFIG_ACTION_DG + " set DOWNLOAD_CONFIG_DG = $"
518                     + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " where ACTION = $"
519                     + SdcArtifactHandlerConstants.ACTION + " and VNF_TYPE = $"
520                     + SdcArtifactHandlerConstants.VNF_TYPE;
521             } else {
522                 key = "insert into " + SdcArtifactHandlerConstants.DB_CONFIG_ACTION_DG
523                     + " set DOWNLOAD_CONFIG_DG = $"
524                     + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " , ACTION = $"
525                     + SdcArtifactHandlerConstants.ACTION + " , VNF_TYPE = $" + SdcArtifactHandlerConstants.VNF_TYPE;
526             }
527
528             if (dblib != null) {
529                 status = dblib.save(key, context);
530             }
531             if ((status == null) || status.toString().equals(FAILURE_PARAM)) {
532                 throw new SvcLogicException("Error While processing Configure DG Action table ");
533             }
534         } else {
535             log.info("No Update required for Config DG Action");
536         }
537
538     }
539
540     public String getModelDataInformationbyArtifactName(String artifactName) throws SvcLogicException {
541         String fn = "DBService.getVnfData";
542         SvcLogicContext con = new SvcLogicContext();
543         String key;
544         QueryStatus status;
545         key =
546             "select VNF_TYPE, VNFC_TYPE, ACTION, FILE_CATEGORY, ARTIFACT_TYPE from ASDC_REFERENCE where  ARTIFACT_NAME = ?";
547         ArrayList<String> arguments = new ArrayList<String>();
548         arguments.add(artifactName);
549         if (dblib != null) {
550             log.info(fn + "select Key: " + key);
551             status = dblib.query(key, con, arguments);
552             if (status.toString().equals(FAILURE_PARAM)) {
553                 throw new SvcLogicException("Error While processing is ArtifactUpdateRequiredforPD table ");
554             }
555
556         }
557         log.info(fn + "Vnf_received :" + con.getAttribute("VNF_TYPE"));
558
559         return con.getAttribute("VNF_TYPE");
560     }
561
562     public void updateYangContents(SvcLogicContext context, String artifactId, String yangContents)
563         throws SvcLogicException {
564         String fn = "DBService.updateYangContents";
565         log.info(fn + "Starting DB operation for  updateYangContents");
566         String key;
567         QueryStatus status = null;
568
569         key = "update ASDC_ARTIFACTS " + " set ARTIFACT_CONTENT = ?"
570             + " where ASDC_ARTIFACTS_ID = ?";
571         ArrayList<String> arguments = new ArrayList<String>();
572         arguments.add(yangContents);
573         arguments.add(artifactId);
574
575         if (dblib != null && context != null) {
576             status = dblib.save(key, context, arguments);
577         }
578         if ((status == null) || status.toString().equals(FAILURE_PARAM)) {
579             throw new SvcLogicException("Error While processing Configure DG Action table ");
580         }
581
582     }
583
584
585     public void insertProtocolReference(SvcLogicContext context, String vnfType, String protocol, String action,
586         String actionLevel, String template) throws SvcLogicException {
587         String fn = "DBService.insertProtocolReference";
588         log.info(fn + "Starting DB operation for  insertProtocolReference");
589         String key;
590         QueryStatus status = null;
591
592         key = "insert into PROTOCOL_REFERENCE (ACTION, VNF_TYPE, PROTOCOL, UPDATED_DATE, TEMPLATE, ACTION_LEVEL)"
593             + " values  (" + "?, ?, ?, now(), ?, "
594             + "?)";
595         ArrayList<String> arguments = new ArrayList<String>();
596         arguments.add(action);
597         arguments.add(vnfType);
598         arguments.add(protocol);
599         arguments.add(template);
600         arguments.add(actionLevel);
601
602         if (dblib != null && context != null) {
603             status = dblib.save(key, context, arguments);
604         }
605         if ((status == null) || status.toString().equals(FAILURE_PARAM)) {
606             throw new SvcLogicException("Error While processing insertProtocolReference ");
607         }
608
609     }
610
611     public boolean isProtocolReferenceUpdateRequired(SvcLogicContext context, String vnfType, String protocol,
612         String action, String actionLevel, String template) throws SvcLogicException {
613         SvcLogicContext localContext = new SvcLogicContext();
614         String fn = "DBService.isProtocolReferenceUpdateRequired";
615         log.info(fn + "Starting DB operation for  isProtocolReferenceUpdateRequired");
616
617         String key = "select COUNT(*) from PROTOCOL_REFERENCE where ACTION=? and ACTION_LEVEL=?"
618             + " and VNF_TYPE=?";
619         ArrayList<String> arguments = new ArrayList<String>();
620         arguments.add(action);
621         arguments.add(actionLevel);
622         arguments.add(vnfType);
623         dblib.query(key, localContext, arguments);
624
625         String countStr = localContext.getAttribute("COUNT(*)");
626         int count = Integer.parseInt(countStr);
627         return count > 0;
628     }
629
630     public void updateProtocolReference(SvcLogicContext context, String vnfType, String protocol, String action,
631         String actionLevel, String template) throws SvcLogicException {
632
633         String fn = "DBService.isProtocolReferenceUpdateRequired";
634         log.info(fn + "Starting DB operation for  isProtocolReferenceUpdateRequired");
635         String key;
636         QueryStatus status;
637
638         key = "update PROTOCOL_REFERENCE set UPDATED_DATE=now(), template=?, protocol =?"
639             + " where ACTION=? and ACTION_LEVEL=? and VNF_TYPE=?";
640         ArrayList<String> arguments = new ArrayList<String>();
641         arguments.add(template);
642         arguments.add(protocol);
643         arguments.add(action);
644         arguments.add(actionLevel);
645         arguments.add(vnfType);
646         status = dblib.save(key, context, arguments);
647         if (status == QueryStatus.FAILURE) {
648             log.info("updateProtocolReference:: Error updating protocol reference");
649             throw new SvcLogicException("Error - updating PROTOCOL_REFERENCE_TABLE in updateProtocolReference");
650         }
651     }
652
653     public String getDownLoadDGReference(SvcLogicContext context) throws DBException {
654         try {
655
656             String fn = "DBService.setDownLoadDGReference";
657             String downloadConfigDg;
658             log.info(fn + "Setting Download DG Reference from DB");
659             String key;
660             QueryStatus status;
661             String protocol = context.getAttribute(SdcArtifactHandlerConstants.DEVICE_PROTOCOL);
662             if (StringUtils.isBlank(protocol)) {
663                 log.info(fn + " :: Protocol is Blank!! Returning without querying DB");
664                 throw new ConfigurationException(fn + ":: Protocol is Blank!! Returning without querying DB");
665             }
666             key = "select download_config_dg from " + SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE
667                 + " where protocol = ?";
668             ArrayList<String> arguments = new ArrayList<String>();
669             arguments.add(protocol);
670             SvcLogicContext localContext = new SvcLogicContext();
671             status = dblib.query(key, localContext, arguments);
672             if (status == QueryStatus.FAILURE) {
673                 log.info(fn + ":: Error retrieving download_config_dg");
674                 throw new SvcLogicException("Error retrieving download_config_dg");
675             }
676             if (status == QueryStatus.NOT_FOUND) {
677                 log.info(fn + ":: NOT_FOUND! No data found for download_config_dg!!");
678                 throw new SvcLogicException(fn + ":: NOT_FOUND! No data found for download_config_dg!");
679             }
680             downloadConfigDg = localContext.getAttribute("download-config-dg");
681             log.info(fn + "download_config_dg::" + downloadConfigDg);
682             return downloadConfigDg;
683         } catch (SvcLogicException | ConfigurationException e) {
684             throw new DBException("An error occurred when getting DG reference", e);
685         }
686     }
687
688     public void cleanUpVnfcReferencesForVnf(SvcLogicContext context) throws SvcLogicException {
689         try {
690             String key1 = "delete from " + SdcArtifactHandlerConstants.DB_VNFC_REFERENCE + " where action = $"
691                 + SdcArtifactHandlerConstants.ACTION + " and vnf_type = $" + SdcArtifactHandlerConstants.VNF_TYPE;
692             log.debug("Action : " + context.getAttribute(SdcArtifactHandlerConstants.ACTION));
693             log.debug("vnfType: " + context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE));
694             QueryStatus status;
695             log.info("cleanUpVnfcReferencesForVnf()::Query:" + key1);
696             if (dblib != null) {
697                 status = dblib.save(key1, context);
698                 if (status.toString().equals(FAILURE_PARAM)) {
699                     log.debug("Error deleting from VNFC_REFERENCE table");
700                     throw new SvcLogicException("Error While processing VNFC_REFERENCE table ");
701                 }
702             }
703         } catch (Exception e) {
704             log.debug("Error deleting from VNFC_REFERENCE table  : "
705                 + context.getAttribute(SdcArtifactHandlerConstants.ACTION) + " and "
706                 + context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE), e);
707         }
708     }
709
710
711     public boolean isUpdateRequiredForTemplates(String queryPart, SvcLogicContext context, String db) throws DBException {
712         try {
713             log.info("Checking if Update required for this data");
714             log.info("db" + db);
715             log.info("ACTION=" + context.getAttribute(SdcArtifactHandlerConstants.ACTION));
716             log.info("VNF_TYPE=" + context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE));
717             log.info("");
718             String whereClause;
719             QueryStatus status;
720             whereClause = " where VNF_TYPE = $" + SdcArtifactHandlerConstants.VNF_TYPE ;
721             whereClause = resolveWhereClause(context, db, whereClause);
722             whereClause += queryPart;
723             if (validate(db)) {
724                 if (!db.equals(SdcArtifactHandlerConstants.DB_DEVICE_AUTHENTICATION)) {
725                     String key = "select COUNT(*) from " + db + whereClause;
726                     log.info("SELECT String : " + key);
727                     status = dblib.query(key, context);
728                     checkForFailure(db, status);
729                     String count = context.getAttribute("COUNT(*)");
730                     log.info("Number of row Returned : " + count + ": " + status + ":");
731                     return tryAddCountAttribute(context, count);
732                 }
733             }
734             log.info("Problems validating DB and/or Context ");
735             return false;
736
737         } catch (SvcLogicException e) {
738             throw new DBException("An error occurred while checking for artifact update", e);
739         }
740     }
741
742     public String createQueryListForTemplateIds(String modelId, SvcLogicContext context) {
743         String parameter = modelId.replace("%", "!%").replace("_","!_").replace("[","![").replace("]", "!]").replace("!","!!");
744         parameter = "%_" + parameter + ".%";
745         context.setAttribute("model-id", parameter);
746         String queryPart = " AND ARTIFACT_NAME like $model-id ";
747         return queryPart;
748     }
749 }