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