Coverage for appc-artifact-handler package
[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-2018 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 import org.apache.commons.configuration.ConfigurationException;
31 import org.apache.commons.configuration.PropertiesConfiguration;
32 import org.apache.commons.lang.StringUtils;
33 import org.onap.appc.artifact.handler.utils.SdcArtifactHandlerConstants;
34 import org.onap.ccsdk.sli.adaptors.resource.sql.SqlResource;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
37 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
38 import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
39
40 public class DBService {
41
42     private static final EELFLogger log = EELFManager.getInstance().getLogger(DBService.class);
43     private static final String FAILURE_PARAM = "FAILURE";
44     private static final String RECEIVED_AS = "Internal Version received as1 : ";
45     private static final String SET_DOWNLOAD_CONFIG_QUERY_STR = " set DOWNLOAD_CONFIG_DG = $";
46     private static final String WHERE_VNF_TYPE_QUERY_STR = " where VNF_TYPE = $";
47     private static final String ACTION_QUERY_STR = " , ACTION = $";
48     private static final String VNF_TYPE_QUERY_STR = " , VNF_TYPE = $";
49     private static final String INSERT_INTO_QUERY_STR = "insert into ";
50     private static final String AND_ACTION_QUERY_STR = " and ACTION = $";
51     private static final String AND_FILE_CAT_QUERY_STR = " and FILE_CATEGORY = $";
52     private static final String AND_VNF_TYPE_QUERY_STR = " and VNF_TYPE = $";
53     private static final String UPDATE_QUERY_STR = "update ";
54     private static final String AND_VNFC_TYPE_QUERY_STR = " and VNFC_TYPE = $";
55
56     private SvcLogicResource serviceLogic;
57     private static DBService dgGeneralDBService = null;
58
59     private DBService() {
60         if (serviceLogic == null) {
61             serviceLogic = new SqlResource();
62         }
63     }
64
65     protected DBService(SqlResource svcLogic) {
66         if (serviceLogic == null) {
67             serviceLogic = svcLogic;
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 (serviceLogic != null && ctx != null) {
83             String key = "select max(internal_version) as maximum from ASDC_ARTIFACTS  WHERE ARTIFACT_NAME = '"
84                 + artifactName + "'";
85             log.info("Getting internal Version :" + key);
86             status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
87             if (status.toString().equals(FAILURE_PARAM)) {
88                 throw new SvcLogicException("Error - getting internal Artifact Number");
89             }
90             artifactInternalVersion = ctx.getAttribute("maximum");
91             log.info("Internal Version received as : " + artifactInternalVersion);
92             log.info(RECEIVED_AS + ctx.getAttribute("max(internal_version)"));
93             log.info(RECEIVED_AS + ctx.getAttribute("max"));
94             log.info(RECEIVED_AS + ctx.getAttribute("internal_version"));
95             log.info(RECEIVED_AS + ctx.getAttributeKeySet().toString());
96         }
97         return artifactInternalVersion;
98     }
99
100     public String getArtifactID(SvcLogicContext ctx, String artifactName) throws SvcLogicException {
101         QueryStatus status;
102         String artifactID = null;
103         if (serviceLogic != null && ctx != null) {
104             String key = "select max(ASDC_ARTIFACTS_ID) as id from ASDC_ARTIFACTS  WHERE ARTIFACT_NAME = '"
105                 + artifactName + "'";
106             log.info("Getting Artifact ID String :" + key);
107             status = serviceLogic.query("SQL", false, null, key, null, null, ctx);
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 (serviceLogic != 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    = " + intversion
128                 + "," + " ARTIFACT_NAME       =  $artifact-name ," + " ARTIFACT_CONTENT    =  $artifact-contents ";
129
130             status = serviceLogic.save("SQL", false, false, key, null, null, 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 (serviceLogic != 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 = serviceLogic.save("SQL", false, false, key, null, prefix, 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_QUERY_STR + SdcArtifactHandlerConstants.DB_SDC_REFERENCE + "  set ARTIFACT_NAME = $"
169                 + SdcArtifactHandlerConstants.ARTIFACT_NAME + " where " + "FILE_CATEGORY = $"
170                 + SdcArtifactHandlerConstants.FILE_CATEGORY + AND_VNF_TYPE_QUERY_STR
171                 + SdcArtifactHandlerConstants.VNF_TYPE;
172         } else if (isUpdate) {
173             key = UPDATE_QUERY_STR + SdcArtifactHandlerConstants.DB_SDC_REFERENCE + "  set ARTIFACT_NAME = $"
174                 + SdcArtifactHandlerConstants.ARTIFACT_NAME + " where VNFC_TYPE = $"
175                 + SdcArtifactHandlerConstants.VNFC_TYPE + AND_FILE_CAT_QUERY_STR
176                 + SdcArtifactHandlerConstants.FILE_CATEGORY + AND_ACTION_QUERY_STR + SdcArtifactHandlerConstants.ACTION
177                 + AND_VNF_TYPE_QUERY_STR + SdcArtifactHandlerConstants.VNF_TYPE;
178             if (StringUtils.isNotBlank(modelId)) {
179                 key += createQueryListForTemplateIds(modelId);
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_QUERY_STR + SdcArtifactHandlerConstants.DB_SDC_REFERENCE + " set VNFC_TYPE = null "
186                     + " , FILE_CATEGORY = $" + SdcArtifactHandlerConstants.FILE_CATEGORY + VNF_TYPE_QUERY_STR
187                     + SdcArtifactHandlerConstants.VNF_TYPE + " , ACTION = null " + " , ARTIFACT_TYPE = null "
188                     + " , ARTIFACT_NAME = $" + SdcArtifactHandlerConstants.ARTIFACT_NAME;
189             } else {
190                 key = INSERT_INTO_QUERY_STR + SdcArtifactHandlerConstants.DB_SDC_REFERENCE + " set VNFC_TYPE = $"
191                     + SdcArtifactHandlerConstants.VNFC_TYPE + " , FILE_CATEGORY = $"
192                     + SdcArtifactHandlerConstants.FILE_CATEGORY + VNF_TYPE_QUERY_STR
193                     + SdcArtifactHandlerConstants.VNF_TYPE + ACTION_QUERY_STR + SdcArtifactHandlerConstants.ACTION
194                     + " , ARTIFACT_TYPE = $" + SdcArtifactHandlerConstants.ARTIFACT_TYPE + " , ARTIFACT_NAME = $"
195                     + SdcArtifactHandlerConstants.ARTIFACT_NAME;
196             }
197         }
198         if (serviceLogic != null) {
199             log.info("Insert Key: " + key);
200             status = serviceLogic.save("SQL", false, false, key, null, null, 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);
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_QUERY_STR + 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 = serviceLogic.query("SQL", false, null, key, null, null, 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 && serviceLogic != 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_CAT_QUERY_STR + SdcArtifactHandlerConstants.FILE_CATEGORY;
293             } else if (db.equals(SdcArtifactHandlerConstants.DB_SDC_REFERENCE)) {
294                 return whereClause + AND_VNFC_TYPE_QUERY_STR + SdcArtifactHandlerConstants.VNFC_TYPE
295                     + AND_FILE_CAT_QUERY_STR + SdcArtifactHandlerConstants.FILE_CATEGORY + AND_ACTION_QUERY_STR
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_QUERY_STR + SdcArtifactHandlerConstants.ACTION;
307             } else if (db.equals(SdcArtifactHandlerConstants.DB_VNFC_REFERENCE)) {
308                 return whereClause + AND_ACTION_QUERY_STR + SdcArtifactHandlerConstants.ACTION
309                     + AND_VNFC_TYPE_QUERY_STR + 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_QUERY_STR + SdcArtifactHandlerConstants.DB_DEVICE_INTERFACE_PROTOCOL + " set PROTOCOL = $"
330                 + SdcArtifactHandlerConstants.DEVICE_PROTOCOL + " , DG_RPC = 'getDeviceRunningConfig' "
331                 + " , MODULE = 'APPC' " + WHERE_VNF_TYPE_QUERY_STR + SdcArtifactHandlerConstants.VNF_TYPE;
332         } else {
333             key =
334                 INSERT_INTO_QUERY_STR + 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 (serviceLogic != null && context != null) {
341
342             status = serviceLogic.save("SQL", false, false, key, null, null, 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             }
363             if (StringUtils.isBlank(user)) {
364                 user = "";
365             }
366             if (isInvalidInput(SdcArtifactHandlerConstants.DEVICE_PROTOCOL, SdcArtifactHandlerConstants.ACTION,
367                         SdcArtifactHandlerConstants.VNF_TYPE)) {
368                 throw new SvcLogicException(
369                     "Error While processing reference File as few or all of parameters VNF_TYPE,PROTOCOL,ACTION are missing ");
370             }
371
372             log.info("Starting DB operation for Device authentication " + isUpdate);
373             log.info("credentials"+user + "user" + "port" + port +"protocol"+protocol+"action"+action+"vnftype"+vnftype);
374             String key;
375             QueryStatus status;
376             if (isUpdate) {
377                 key = UPDATE_QUERY_STR + SdcArtifactHandlerConstants.DB_DEVICE_AUTHENTICATION + " set USER_NAME = '"
378                         + user + "' , PORT_NUMBER = " + port + "";
379                 if (context.getAttributeKeySet().contains(SdcArtifactHandlerConstants.URL)) {
380                     String url = context.getAttribute(SdcArtifactHandlerConstants.URL);
381                     if (StringUtils.isBlank(url)) {
382                         url = "" ;
383                     }
384                     key = key + ", URL = '" + url + "' ";
385                 }
386                 key = key + WHERE_VNF_TYPE_QUERY_STR + SdcArtifactHandlerConstants.VNF_TYPE + "  AND PROTOCOL = $"
387                         + SdcArtifactHandlerConstants.DEVICE_PROTOCOL + " AND  ACTION = $"
388                         + SdcArtifactHandlerConstants.ACTION;
389             } else {
390                 key = "insert into DEVICE_AUTHENTICATION set VNF_TYPE = '" + vnftype + "' , PROTOCOL = '" + protocol
391                         + "' , " + "ACTION = '" + action + "' , USER_NAME = '" + user + "' , PORT_NUMBER = '" + port
392                         + "'";
393                 if (context.getAttributeKeySet().contains(SdcArtifactHandlerConstants.URL)) {
394                     String url = context.getAttribute(SdcArtifactHandlerConstants.URL);
395                     if (StringUtils.isBlank(url)) {
396                         url = "";
397                     }
398                     key = key + ", URL = '" + url + "' ";
399                 }
400             }
401
402             log.info("Query forDevice authentication  " + key);
403             if (serviceLogic != null && context != null) {
404
405                 status = serviceLogic.save("SQL", false, false, key, null, null, context);
406                 if (status.toString().equals(FAILURE_PARAM)) {
407                     throw new SvcLogicException("Error While processing DEVICE_AUTHENTICATION table ");
408                 }
409             }
410
411         } catch (SvcLogicException e) {
412
413             throw new DBException("An error occurred when processing device authentication", e);
414         }
415     }
416
417     private boolean isInvalidInput(String protocol, String action, String vnfType) {
418         return isInvalid(vnfType) && isInvalid(action) && isInvalid(protocol);
419     }
420
421     private boolean isInvalid(String str) {
422         return (str == null) || ("".equals(str));
423     }
424
425     public void processVnfcReference(SvcLogicContext context, boolean isUpdate) throws SvcLogicException {
426         String fn = "DBService.processVnfcReference";
427         log.info(fn + "Starting DB operation for Vnfc Reference " + isUpdate);
428         String key;
429
430         int vmInstance = -1;
431         if (context.getAttribute(SdcArtifactHandlerConstants.VM_INSTANCE) != null) {
432             vmInstance = Integer.parseInt(context.getAttribute(SdcArtifactHandlerConstants.VM_INSTANCE));
433         }
434
435         int vnfcInstance = -1;
436         if (context.getAttribute(SdcArtifactHandlerConstants.VNFC_INSTANCE) != null) {
437             vnfcInstance = Integer.parseInt(context.getAttribute(SdcArtifactHandlerConstants.VNFC_INSTANCE));
438         }
439
440         QueryStatus status;
441         if (isUpdate) {
442             key = UPDATE_QUERY_STR + SdcArtifactHandlerConstants.DB_VNFC_REFERENCE + " set VM_INSTANCE = " + vmInstance
443                 + " , VNFC_INSTANCE = " + vnfcInstance + " , VNFC_TYPE = $" + SdcArtifactHandlerConstants.VNFC_TYPE
444                 + " , VNFC_FUNCTION_CODE = $" + SdcArtifactHandlerConstants.VNFC_FUNCTION_CODE
445                 + " , GROUP_NOTATION_TYPE = $" + SdcArtifactHandlerConstants.GROUP_NOTATION_TYPE
446                 + " , GROUP_NOTATION_VALUE = $" + SdcArtifactHandlerConstants.GROUP_NOTATION_VALUE
447                 + " , IPADDRESS_V4_OAM_VIP = $" + SdcArtifactHandlerConstants.IPADDRESS_V4_OAM_VIP
448                 + WHERE_VNF_TYPE_QUERY_STR + SdcArtifactHandlerConstants.VNF_TYPE + AND_ACTION_QUERY_STR
449                 + SdcArtifactHandlerConstants.ACTION + AND_VNFC_TYPE_QUERY_STR + SdcArtifactHandlerConstants.VNFC_TYPE
450                 + " and VNFC_INSTANCE = $" + SdcArtifactHandlerConstants.VNFC_INSTANCE + " and VM_INSTANCE = $"
451                 + SdcArtifactHandlerConstants.VM_INSTANCE;
452         } else {
453             key = INSERT_INTO_QUERY_STR + SdcArtifactHandlerConstants.DB_VNFC_REFERENCE + " set  VNF_TYPE = $"
454                 + SdcArtifactHandlerConstants.VNF_TYPE + ACTION_QUERY_STR + SdcArtifactHandlerConstants.ACTION
455                 + " , VM_INSTANCE = $" + SdcArtifactHandlerConstants.VM_INSTANCE + " , VNFC_INSTANCE = $"
456                 + SdcArtifactHandlerConstants.VNFC_INSTANCE + " , VNFC_TYPE = $"
457                 + SdcArtifactHandlerConstants.VNFC_TYPE + " , VNFC_FUNCTION_CODE = $"
458                 + SdcArtifactHandlerConstants.VNFC_FUNCTION_CODE + " , TEMPLATE_ID = $"
459                 + SdcArtifactHandlerConstants.TEMPLATE_ID + " , GROUP_NOTATION_TYPE = $"
460                 + SdcArtifactHandlerConstants.GROUP_NOTATION_TYPE + " , IPADDRESS_V4_OAM_VIP = $"
461                 + SdcArtifactHandlerConstants.IPADDRESS_V4_OAM_VIP + " , GROUP_NOTATION_VALUE = $"
462                 + SdcArtifactHandlerConstants.GROUP_NOTATION_VALUE;
463         }
464
465         if (serviceLogic != null) {
466             status = serviceLogic.save("SQL", false, false, key, null, null, context);
467             if (status.toString().equals(FAILURE_PARAM)) {
468                 throw new SvcLogicException("Error While processing VNFC_REFERENCE table ");
469             }
470         }
471     }
472
473     public void processDownloadDgReference(SvcLogicContext context, boolean isUpdate)
474         throws SvcLogicException {
475         String fn = "DBService.processDownloadDgReference";
476         log.info(fn + "Starting DB operation for Download DG Reference " + isUpdate);
477         String key;
478         QueryStatus status = null;
479
480         if (isUpdate) {
481             key =
482                 UPDATE_QUERY_STR + SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE + SET_DOWNLOAD_CONFIG_QUERY_STR
483                     + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " where PROTOCOL = $"
484                     + SdcArtifactHandlerConstants.DEVICE_PROTOCOL;
485         } else {
486             key = INSERT_INTO_QUERY_STR + SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE
487                 + SET_DOWNLOAD_CONFIG_QUERY_STR
488                 + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " , PROTOCOL = $"
489                 + SdcArtifactHandlerConstants.DEVICE_PROTOCOL;
490         }
491
492         if (serviceLogic != null && context != null) {
493             status = serviceLogic.save("SQL", false, false, key, null, null, context);
494         }
495         if ((status == null) || status.toString().equals(FAILURE_PARAM)) {
496             throw new SvcLogicException("Error While processing DOWNLOAD_DG_REFERENCE table ");
497         }
498     }
499
500     public void processConfigActionDg(SvcLogicContext context, boolean isUpdate) throws SvcLogicException {
501         String fn = "DBService.processConfigActionDg";
502         log.info(fn + "Starting DB operation for Config DG Action " + isUpdate);
503         String key;
504         QueryStatus status = null;
505
506         if (context.getAttribute(SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE) != null
507             && context.getAttribute(SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE).length() > 0) {
508             if (isUpdate) {
509                 key = UPDATE_QUERY_STR + SdcArtifactHandlerConstants.DB_CONFIG_ACTION_DG + SET_DOWNLOAD_CONFIG_QUERY_STR
510                     + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + " where ACTION = $"
511                     + SdcArtifactHandlerConstants.ACTION + AND_VNF_TYPE_QUERY_STR
512                     + SdcArtifactHandlerConstants.VNF_TYPE;
513             } else {
514                 key = INSERT_INTO_QUERY_STR + SdcArtifactHandlerConstants.DB_CONFIG_ACTION_DG
515                     + SET_DOWNLOAD_CONFIG_QUERY_STR
516                     + SdcArtifactHandlerConstants.DOWNLOAD_DG_REFERENCE + ACTION_QUERY_STR
517                     + SdcArtifactHandlerConstants.ACTION + VNF_TYPE_QUERY_STR + SdcArtifactHandlerConstants.VNF_TYPE;
518             }
519
520             if (serviceLogic != null) {
521                 status = serviceLogic.save("SQL", false, false, key, null, null, context);
522             }
523             if ((status == null) || status.toString().equals(FAILURE_PARAM)) {
524                 throw new SvcLogicException("Error While processing Configure DG Action table ");
525             }
526         } else {
527             log.info("No Update required for Config DG Action");
528         }
529
530     }
531
532     public String getModelDataInformationbyArtifactName(String artifactName) throws SvcLogicException {
533         String fn = "DBService.getVnfData";
534         SvcLogicContext con = new SvcLogicContext();
535         String key;
536         QueryStatus status;
537         key =
538             "select VNF_TYPE, VNFC_TYPE, ACTION, FILE_CATEGORY, ARTIFACT_TYPE from ASDC_REFERENCE where  ARTIFACT_NAME = "
539                 + artifactName;
540
541         if (serviceLogic != null) {
542             log.info(fn + "select Key: " + key);
543             status = serviceLogic.query("SQL", false, null, key, null, null, con);
544             if (status.toString().equals(FAILURE_PARAM)) {
545                 throw new SvcLogicException("Error While processing is ArtifactUpdateRequiredforPD table ");
546             }
547
548         }
549         log.info(fn + "Vnf_received :" + con.getAttribute("VNF_TYPE"));
550
551         return con.getAttribute("VNF_TYPE");
552     }
553
554     public void updateYangContents(SvcLogicContext context, String artifactId, String yangContents)
555         throws SvcLogicException {
556         String fn = "DBService.updateYangContents";
557         log.info(fn + "Starting DB operation for  updateYangContents");
558         String key;
559         QueryStatus status = null;
560
561         key = "update ASDC_ARTIFACTS " + " set ARTIFACT_CONTENT = '" + yangContents + "'"
562             + " where ASDC_ARTIFACTS_ID = " + artifactId;
563
564         if (serviceLogic != null && context != null) {
565             status = serviceLogic.save("SQL", false, false, key, null, null, context);
566         }
567         if ((status == null) || status.toString().equals(FAILURE_PARAM)) {
568             throw new SvcLogicException("Error While processing Configure DG Action table ");
569         }
570
571     }
572
573
574     public void insertProtocolReference(SvcLogicContext context, String vnfType, String protocol, String action,
575         String actionLevel, String template) throws SvcLogicException {
576         String fn = "DBService.insertProtocolReference";
577         log.info(fn + "Starting DB operation for  insertProtocolReference");
578         String key;
579         QueryStatus status = null;
580
581         key = "insert into PROTOCOL_REFERENCE (ACTION, VNF_TYPE, PROTOCOL, UPDATED_DATE, TEMPLATE, ACTION_LEVEL)"
582             + " values  (" + "'" + action + "', '" + vnfType + "', '" + protocol + "', now(),'" + template + "', '"
583             + actionLevel + "')";
584
585         if (serviceLogic != null && context != null) {
586             status = serviceLogic.save("SQL", false, false, key, null, null, context);
587         }
588         if ((status == null) || status.toString().equals(FAILURE_PARAM)) {
589             throw new SvcLogicException("Error While processing insertProtocolReference ");
590         }
591
592     }
593
594     public boolean isProtocolReferenceUpdateRequired(SvcLogicContext context, String vnfType, String protocol,
595         String action, String actionLevel, String template) throws SvcLogicException {
596         SvcLogicContext localContext = new SvcLogicContext();
597         String fn = "DBService.isProtocolReferenceUpdateRequired";
598         log.info(fn + "Starting DB operation for  isProtocolReferenceUpdateRequired");
599
600         String key = "select COUNT(*) from PROTOCOL_REFERENCE where ACTION='" + action + "' and ACTION_LEVEL='" + actionLevel
601             + "' and VNF_TYPE='" + vnfType + "'";
602         serviceLogic.query("SQL", false, null, key, null, null, localContext);
603
604         String countStr = localContext.getAttribute("COUNT(*)");
605         int count = Integer.parseInt(countStr);
606         return count > 0;
607     }
608
609     public void updateProtocolReference(SvcLogicContext context, String vnfType, String protocol, String action,
610         String actionLevel, String template) throws SvcLogicException {
611
612         String fn = "DBService.isProtocolReferenceUpdateRequired";
613         log.info(fn + "Starting DB operation for  isProtocolReferenceUpdateRequired");
614         String key;
615         QueryStatus status;
616
617         key = "update PROTOCOL_REFERENCE set UPDATED_DATE=now(), template='" + template + "', protocol ='" + protocol
618             + "' where ACTION='" + action + "' and ACTION_LEVEL='" + actionLevel + "' and VNF_TYPE='" + vnfType
619             + "'";
620         status = serviceLogic.save("SQL", false, false, key, null, null, context);
621         if (status == QueryStatus.FAILURE) {
622             log.info("updateProtocolReference:: Error updating protocol reference");
623             throw new SvcLogicException("Error - updating PROTOCOL_REFERENCE_TABLE in updateProtocolReference");
624         }
625     }
626
627     public String getDownLoadDGReference(SvcLogicContext context) throws DBException {
628         try {
629
630             String fn = "DBService.setDownLoadDGReference";
631             String downloadConfigDg;
632             log.info(fn + "Setting Download DG Reference from DB");
633             String key;
634             QueryStatus status;
635             String protocol = context.getAttribute(SdcArtifactHandlerConstants.DEVICE_PROTOCOL);
636             if (StringUtils.isBlank(protocol)) {
637                 log.info(fn + " :: Protocol is Blank!! Returning without querying DB");
638                 throw new ConfigurationException(fn + ":: Protocol is Blank!! Returning without querying DB");
639             }
640             key = "select download_config_dg from " + SdcArtifactHandlerConstants.DB_DOWNLOAD_DG_REFERENCE
641                 + " where protocol = '" + protocol + "'";
642             SvcLogicContext localContext = new SvcLogicContext();
643             status = serviceLogic.query("SQL", false, null, key, null, null, localContext);
644             if (status == QueryStatus.FAILURE) {
645                 log.info(fn + ":: Error retrieving download_config_dg");
646                 throw new SvcLogicException("Error retrieving download_config_dg");
647             }
648             if (status == QueryStatus.NOT_FOUND) {
649                 log.info(fn + ":: NOT_FOUND! No data found for download_config_dg!!");
650                 throw new SvcLogicException(fn + ":: NOT_FOUND! No data found for download_config_dg!");
651             }
652             downloadConfigDg = localContext.getAttribute("download-config-dg");
653             log.info(fn + "download_config_dg::" + downloadConfigDg);
654             return downloadConfigDg;
655         } catch (SvcLogicException | ConfigurationException e) {
656             throw new DBException("An error occurred when getting DG reference", e);
657         }
658     }
659
660     public void cleanUpVnfcReferencesForVnf(SvcLogicContext context) throws SvcLogicException {
661         try {
662             String key1 = "delete from " + SdcArtifactHandlerConstants.DB_VNFC_REFERENCE + " where action = $"
663                 + SdcArtifactHandlerConstants.ACTION + " and vnf_type = $" + SdcArtifactHandlerConstants.VNF_TYPE;
664             log.debug("Action : " + context.getAttribute(SdcArtifactHandlerConstants.ACTION));
665             log.debug("vnfType: " + context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE));
666             QueryStatus status;
667             log.info("cleanUpVnfcReferencesForVnf()::Query:" + key1);
668             if (serviceLogic != null) {
669                 status = serviceLogic.save("SQL", false, false, key1, null, null, context);
670                 if (status.toString().equals(FAILURE_PARAM)) {
671                     log.debug("Error deleting from VNFC_REFERENCE table");
672                     throw new SvcLogicException("Error While processing VNFC_REFERENCE table ");
673                 }
674             }
675         } catch (Exception e) {
676             log.debug("Error deleting from VNFC_REFERENCE table  : "
677                 + context.getAttribute(SdcArtifactHandlerConstants.ACTION) + " and "
678                 + context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE), e);
679         }
680     }
681
682
683     public boolean isUpdateRequiredForTemplates(String queryPart, SvcLogicContext context, String db) throws DBException {
684         try {
685             log.info("Checking if Update required for this data");
686             log.info("db" + db);
687             log.info("ACTION=" + context.getAttribute(SdcArtifactHandlerConstants.ACTION));
688             log.info("VNF_TYPE=" + context.getAttribute(SdcArtifactHandlerConstants.VNF_TYPE));
689             log.info("");
690             String whereClause;
691             QueryStatus status;
692             whereClause = WHERE_VNF_TYPE_QUERY_STR + SdcArtifactHandlerConstants.VNF_TYPE ;
693             whereClause = resolveWhereClause(context, db, whereClause);
694             whereClause += queryPart;
695             if (validate(db)) {
696                 if (!db.equals(SdcArtifactHandlerConstants.DB_DEVICE_AUTHENTICATION)) {
697                     String key = "select COUNT(*) from " + db + whereClause;
698                     log.info("SELECT String : " + key);
699                     status = serviceLogic.query("SQL", false, null, key, null, null, context);
700                     checkForFailure(db, status);
701                     String count = context.getAttribute("COUNT(*)");
702                     log.info("Number of row Returned : " + count + ": " + status + ":");
703                     return tryAddCountAttribute(context, count);
704                 }
705             }
706             log.info("Problems validating DB and/or Context ");
707             return false;
708
709         } catch (SvcLogicException e) {
710             throw new DBException("An error occurred while checking for artifact update", e);
711         }
712     }
713
714     public String createQueryListForTemplateIds(String modelId) {
715         String queryPart = " AND ARTIFACT_NAME like '%_" + modelId+".%'";
716         return queryPart;
717     }
718 }