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