Prepared statements for DG services
[appc.git] / appc-config / appc-data-services / provider / src / main / java / org / onap / appc / data / services / db / DGGeneralDBService.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 IBM.
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 package org.onap.appc.data.services.db;
26
27 import java.util.Set;
28
29 import org.apache.commons.lang3.StringUtils;
30 import org.onap.ccsdk.sli.core.dblib.DbLibService;
31 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
35 import org.onap.ccsdk.sli.adaptors.resource.sql.SqlResource;
36 import com.att.eelf.configuration.EELFLogger;
37 import com.att.eelf.configuration.EELFManager;
38
39 public class DGGeneralDBService {
40
41     private static final EELFLogger log = EELFManager.getInstance().getLogger(DGGeneralDBService.class);
42     //private SvcLogicResource serviceLogic;
43     private DbLibServiceQueries dblib;
44     private static DGGeneralDBService dgGeneralDBService = null;
45
46     public static DGGeneralDBService initialise() {
47
48         if (dgGeneralDBService == null) {
49             dgGeneralDBService = new DGGeneralDBService();
50         }
51         return dgGeneralDBService;
52     }
53
54     private DGGeneralDBService() {
55         if (dblib == null) {
56             dblib = new DbLibServiceQueries();
57         }
58     }
59
60     protected DGGeneralDBService(DbLibService dbLibService) {
61         if (dblib == null) {
62             dblib = new DbLibServiceQueries(dbLibService);
63         }
64     }
65     
66     protected DGGeneralDBService(DbLibServiceQueries dbLibServiceQueries) {
67         if (dblib == null) {
68             dblib = dbLibServiceQueries;
69         }
70     }
71
72     public QueryStatus getDeviceProtocolByVnfType(SvcLogicContext ctx, String prefix) throws SvcLogicException {
73         QueryStatus status = null;
74         if (dblib != null && ctx != null) {
75             String key = "SELECT * FROM DEVICE_INTERFACE_PROTOCOL WHERE vnf_type = $vnf-type ;";
76             status = dblib.query(key, prefix, ctx);
77         }
78         return status;
79     }
80
81     public QueryStatus getDeviceAuthenticationByVnfType(SvcLogicContext ctx, String prefix) throws SvcLogicException {
82         QueryStatus status = null;
83         if (dblib != null && ctx != null) {
84             String key = "SELECT * FROM DEVICE_AUTHENTICATION WHERE vnf_type = $vnf-type ;";
85             status = dblib.query(key, prefix, ctx);
86
87         }
88         return status;
89     }
90
91     public QueryStatus getConfigFileReferenceByVnfType(SvcLogicContext ctx, String prefix) throws SvcLogicException {
92         QueryStatus status = null;
93         if (dblib != null && ctx != null) {
94             String key = "SELECT * FROM CONFIG_FILE_REFERENCE WHERE vnf_type = $vnf-type ;";
95             status = dblib.query(key, prefix, ctx);
96         }
97         return status;
98     }
99
100     public QueryStatus getConfigFileReferenceByFileTypeNVnfType(SvcLogicContext ctx, String prefix, String fileType)
101             throws SvcLogicException {
102         QueryStatus status = null;
103         if (dblib != null && ctx != null) {
104             ctx.setAttribute("file-type", fileType);
105             String key = "SELECT * FROM CONFIG_FILE_REFERENCE  WHERE file_type = $file-type"
106                     + " and vnf_type = $vnf-type ;";
107             status = dblib.query(key, prefix, ctx);
108         }
109         return status;
110     }
111
112     public QueryStatus getTemplate(SvcLogicContext ctx, String prefix, String fileCategory) throws SvcLogicException {
113         QueryStatus status = null;
114         if (dblib != null && ctx != null) {
115             ctx.setAttribute("file-category", fileCategory);
116             String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
117                     + " FROM ASDC_ARTIFACTS "
118                     + " WHERE asdc_artifacts_id = ( SELECT MAX(a.asdc_artifacts_id) configfileid  "
119                     + " FROM ASDC_ARTIFACTS a, ASDC_REFERENCE b " + " WHERE a.artifact_name = b.artifact_name "
120                     + " AND file_category =  $file-category AND action =  $request-action "
121                     + " AND vnf_type =  $vnf-type  " + " AND vnfc_type =   $vnfc-type ) ; ";
122             status = dblib.query(key, prefix, ctx);
123         }
124         return status;
125     }
126
127     public QueryStatus getTemplateByVnfTypeNAction(SvcLogicContext ctx, String prefix, String fileCategory)
128             throws SvcLogicException {
129         QueryStatus status = null;
130         if (dblib != null && ctx != null) {
131             ctx.setAttribute("file-category", fileCategory);
132             String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
133                     + " FROM ASDC_ARTIFACTS "
134                     + " WHERE asdc_artifacts_id = (SELECT MAX(a.asdc_artifacts_id) configfileid  "
135                     + " FROM ASDC_ARTIFACTS a, ASDC_REFERENCE b " + " WHERE a.artifact_name = b.artifact_name "
136                     + " AND file_category =  $file-category AND action =  $request-action "
137                     + " AND vnf_type =  $vnf-type ) ; ";
138             status = dblib.query(key, prefix, ctx);
139
140         }
141         return status;
142     }
143
144     public QueryStatus getTemplateByVnfType(SvcLogicContext ctx, String prefix, String fileCategory)
145             throws SvcLogicException {
146         QueryStatus status = null;
147         if (dblib != null && ctx != null) {
148             ctx.setAttribute("file-category", fileCategory);
149             String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
150                     + " FROM ASDC_ARTIFACTS "
151                     + " WHERE asdc_artifacts_id = (SELECT MAX(a.asdc_artifacts_id) configfileid  "
152                     + " FROM ASDC_ARTIFACTS a, ASDC_REFERENCE b " + " WHERE a.artifact_name = b.artifact_name "
153                     + " AND file_category =  $file-category AND vnf_type =  $vnf-type ) ; ";
154
155             status = dblib.query(key, prefix, ctx);
156         }
157         return status;
158     }
159
160     public QueryStatus getTemplateByTemplateName(SvcLogicContext ctx, String prefix, String templateName)
161             throws SvcLogicException {
162         QueryStatus status = null;
163         if (dblib != null && ctx != null) {
164             ctx.setAttribute("template-name", templateName);
165             String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
166                     + " FROM ASDC_ARTIFACTS "
167                     + " WHERE asdc_artifacts_id = (SELECT MAX(asdc_artifacts_id) configfileid  "
168                     + " FROM ASDC_ARTIFACTS  " + " WHERE artifact_name = $template-name ) ; ";
169
170             status = dblib.query(key, prefix, ctx);
171         }
172         return status;
173     }
174
175     public QueryStatus getConfigureActionDGByVnfTypeNAction(SvcLogicContext ctx, String prefix)
176             throws SvcLogicException {
177         QueryStatus status = null;
178         if (dblib != null && ctx != null) {
179             String key = "SELECT * " + " FROM CONFIGURE_ACTION_DG "
180                     + " where vnf_type = $vnf-type and action = $request-action ; ";
181
182             status = dblib.query(key, prefix, ctx);
183         }
184         return status;
185     }
186
187     public QueryStatus getConfigureActionDGByVnfType(SvcLogicContext ctx, String prefix) throws SvcLogicException {
188         QueryStatus status = null;
189         if (dblib != null && ctx != null) {
190             String key = "SELECT * " + " FROM CONFIGURE_ACTION_DG "
191                     + " where vnf_type = $vnf-type and action IS NULL ; ";
192
193             status = dblib.query(key, prefix, ctx);
194         }
195         return status;
196     }
197
198     public QueryStatus getMaxConfigFileId(SvcLogicContext ctx, String prefix, String fileCategory)
199             throws SvcLogicException {
200         QueryStatus status = null;
201         if (dblib != null && ctx != null) {
202             ctx.setAttribute("file-category", fileCategory);
203             String key = "SELECT MAX(config_file_id) configfileid " + " FROM CONFIGFILES " + " WHERE file_category = "
204                     + "$file-category AND vnf_id =  $vnf-id  AND vm_name = $vm-name ; ";
205
206             status = dblib.query(key, prefix, ctx);
207         }
208         return status;
209     }
210
211     public QueryStatus saveConfigFiles(SvcLogicContext ctx, String prefix) throws SvcLogicException {
212
213         QueryStatus status = null;
214
215         if (dblib != null && ctx != null) {
216             String key = "INSERT INTO CONFIGFILES " + " SET data_source        = $data-source , "
217                     + " service_instance_id =  $service-instance-id ," + " action              =   $request-action ,"
218                     + " vnf_type            =     $vnf-type ," + " vnfc_type           =     $vnfc-type ,"
219                     + " vnf_id              =   $vnf-id , " + " vnf_name            =   $vnf-name ,"
220                     + " vm_name            =   $vm-name ," + " file_category         =  $file-category ,"
221                     + " file_content        =  $file-content ; ";
222
223             status = dblib.save(key, ctx);
224
225         }
226         return status;
227
228     }
229
230     public QueryStatus savePrepareRelationship(SvcLogicContext ctx, String prefix, String fileId, String sdcInd)
231             throws SvcLogicException {
232
233         QueryStatus status = null;
234         String key = null;
235
236         if (dblib != null && ctx != null) {
237             ctx.setAttribute("file-id", fileId);
238             if ("Y".equals(sdcInd))
239
240                 key = "INSERT INTO PREPARE_FILE_RELATIONSHIP " + " SET service_instance_id =  $service-instance-id , "
241                         + "   request_id         = $request-id , " + "  asdc_artifacts_id        =  $file-id ;";
242             else
243                 key = "INSERT INTO PREPARE_FILE_RELATIONSHIP " + " SET service_instance_id =  $service-instance-id , "
244                         + "   request_id         = $request-id , " + "  config_file_id        =  $file-id ;";
245
246             status = dblib.save(key, ctx);
247
248             log.info("DGGeneralDBService.savePrepareRelationship()" + ctx.getAttributeKeySet());
249         }
250         return status;
251
252     }
253
254     public void cleanContextPropertyByPrefix(SvcLogicContext ctx, String prefix) {
255         if (ctx != null && ctx.getAttributeKeySet() != null && StringUtils.isNotBlank(prefix)) {
256
257             Set<String> keySet = ctx.getAttributeKeySet();
258             for (String key : keySet) {
259               //By default, context property prefixed with dot.
260                 if (StringUtils.isNotBlank(key) && key.startsWith(".")) {
261                     ctx.getAttributeKeySet().remove(key);
262                 }
263             }
264         }
265     }
266
267     public QueryStatus saveUploadConfig(SvcLogicContext ctx, String prefix) throws SvcLogicException {
268
269         QueryStatus status = null;
270
271         if (dblib != null && ctx != null) {
272             String key = "INSERT INTO UPLOAD_CONFIG " + " SET request_id = $request-id , "
273                     + " action = $request-action , " + " originator_id = $originator-id , " + " vnf_id =  $vnf-id , "
274                     + " vnf_name = $vnf-name ,  " + " vm_name =  $vm-name ,  "
275                     + " host_ip_address = $vnf-host-ip-address , " + " vnf_type            =     $vnf-type , "
276                     + " vnfc_type           =     $vnfc-type , " + " config_indicator         =  'Current' , "
277                     + " content        =  $tmp.escaped.devicerunningconfig ; ";
278
279             status = dblib.save(key, ctx);
280
281             log.info("DGGeneralDBService.saveUploadConfig()" + ctx.getAttributeKeySet());
282
283         }
284         return status;
285
286     }
287
288     /*public QueryStatus getMaxUploadConfigFileId(SvcLogicContext ctx, String prefix) throws SvcLogicException {
289         QueryStatus status = null;
290         if (serviceLogic != null && ctx != null) {
291             String key = "SELECT MAX(upload_config_id) uploadconfigid " + " FROM UPLOAD_CONFIG "
292                     + " WHERE vnf_id =  $vnf-id  AND vm_name = $vm-name ; ";
293
294             status = serviceLogic.query("SQL", false, null, key, prefix, null, ctx);
295             log.info("DGGeneralDBService.getMaxUploadConfigFileId()" + ctx.getAttributeKeySet());
296         }
297         return status;
298     }*/
299
300     public QueryStatus updateUploadConfig(SvcLogicContext ctx, String prefix, int maxId) throws SvcLogicException {
301         QueryStatus status = null;
302         if (dblib != null && ctx != null) {
303             String key = "UPDATE UPLOAD_CONFIG " + " SET  config_indicator         =  null "
304                     + " WHERE upload_config_id != " + maxId + " AND config_indicator         =  'Current' "
305                     + " AND vnf_id = $vnf-id " + " AND vnfc_type =  $vnfc-type ; ";
306
307             status = dblib.save(key, ctx);
308
309             log.info("DGGeneralDBService.updateUploadConfig()" + ctx.getAttributeKeySet());
310
311         }
312         return status;
313
314     }
315
316
317     public QueryStatus getTemplateByArtifactType(SvcLogicContext ctx, String prefix, String fileCategory, String artifactType)
318             throws SvcLogicException {
319         QueryStatus status = null;
320         if (dblib != null && ctx != null) {
321             ctx.setAttribute("file-category", fileCategory);
322             ctx.setAttribute("artifact-type", artifactType);
323             String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
324                     + " FROM ASDC_ARTIFACTS "
325                     + " WHERE asdc_artifacts_id = (SELECT MAX(a.asdc_artifacts_id) configfileid  "
326                     + " FROM ASDC_ARTIFACTS a, ASDC_REFERENCE b " + " WHERE a.artifact_name = b.artifact_name "
327                     + " AND file_category =  $file-category  AND action =  $request-action "
328                     + " AND artifactType =  $artifact-type  AND vnf_type =  $vnf-type ) ; ";
329
330             status = dblib.query(key, prefix, ctx);
331         }
332         return status;
333     }
334
335
336     public QueryStatus getConfigFilesByVnfVmNCategory(SvcLogicContext ctx, String prefix, String fileCategory, String vnfId, String vmName)
337             throws SvcLogicException {
338         QueryStatus status = null;
339         if (dblib != null && ctx != null) {
340             ctx.setAttribute("file-category", fileCategory);
341             ctx.setAttribute("vnf-id", vnfId);
342             ctx.setAttribute("vm-name", vmName);
343             String key = "SELECT  file_content ,  config_file_id "
344                     + " FROM CONFIGFILES "
345                     + " WHERE config_file_id = ( SELECT MAX(config_file_id) configfileid " + " FROM CONFIGFILES "
346                     + " WHERE file_category = $file-category"
347                     + " AND vnf_id =  $vnf-id"
348                     + " AND vm_name = $vm-name ) ; ";
349
350
351             status = dblib.query(key, prefix, ctx);
352         }
353         return status;
354     }
355
356
357     public QueryStatus getDownloadConfigTemplateByVnf(SvcLogicContext ctx, String prefix)
358             throws SvcLogicException {
359         QueryStatus status = null;
360         if (dblib != null && ctx != null) {
361             String key = "SELECT * FROM DOWNLOAD_CONFIG_TEMPLATE  WHERE vnf_type = $vnf-type ; ";
362             status = dblib.query(key, prefix, ctx);
363         }
364         return status;
365     }
366
367
368
369     public QueryStatus saveConfigTransactionLog(SvcLogicContext ctx, String prefix) throws SvcLogicException {
370
371         QueryStatus status = null;
372
373         if (dblib != null && ctx != null) {
374
375                 String key = "INSERT INTO CONFIG_TRANSACTION_LOG " + " SET request_id = $request-id , "
376                 + " message_type = $log-message-type , "
377                 + " message = $log-message ;";
378
379
380                 status = dblib.save(key, ctx);
381
382
383         }
384         return status;
385
386     }
387
388
389     public QueryStatus getVnfcReferenceByVnfcTypeNAction(SvcLogicContext ctx, String prefix)
390             throws SvcLogicException {
391         QueryStatus status = null;
392         if (dblib != null && ctx != null) {
393
394             String key = "SELECT  * "
395                     + " FROM VNFC_REFERENCE "
396                     + " WHERE vnf_type =  $vnf-type "
397                     + " AND vnfc_type = $vnfc-type "
398                     + " AND action =  $request-action "
399                     + " ORDER BY vm_instance, vnfc_instance ; ";
400
401
402             status = dblib.query(key, prefix, ctx);
403         }
404         return status;
405     }
406
407
408     public QueryStatus getVnfcReferenceByVnfTypeNAction(SvcLogicContext ctx, String prefix)
409             throws SvcLogicException {
410         QueryStatus status = null;
411         if (dblib != null && ctx != null) {
412
413             String key = "SELECT  * "
414                     + " FROM VNFC_REFERENCE "
415                     + " WHERE vnf_type =  $vnf-type "
416                     + " AND action =  $request-action   "
417                     + " ORDER BY vm_instance, vnfc_instance ; ";
418
419             status = dblib.query(key, prefix, ctx);
420         }
421         return status;
422     }
423
424
425     public QueryStatus getUploadConfigInfo(SvcLogicContext ctx, String prefix)
426             throws SvcLogicException {
427         QueryStatus status = null;
428         if (dblib != null && ctx != null) {
429
430             String key = "SELECT  * , UNIX_TIMESTAMP(UPLOAD_DATE) UPLOAD_TIMESTAMP "
431                     + " FROM UPLOAD_CONFIG "
432                     + " WHERE upload_config_id = " +
433                     "( SELECT MAX(upload_config_id) uploadconfigid " + " FROM UPLOAD_CONFIG "
434                     + " WHERE vnf_id =  $vnf-id  AND vm_name = $vm-name ) ; ";
435
436             status = dblib.query(key, prefix, ctx);
437         }
438         return status;
439     }
440      public String getCapability(SvcLogicContext ctx, String vnf_type) throws SvcLogicException {
441
442          //{"capabilities":{"vnfc":[],"vm":[],"vf-module":[],"vnf":["ConfigureTest","ConfigModify","HealthCheck"]}}
443          String fn = "getCapability ";
444          QueryStatus status = null;
445          SvcLogicContext localContext = new SvcLogicContext();
446          localContext.setAttribute("vnf-type", vnf_type);
447          if (dblib != null) {
448                  String queryString = "select max(internal_version) as maxInternalVersion, artifact_name as artifactName from ASDC_ARTIFACTS " +
449                                   " where artifact_name in (select artifact_name from ASDC_REFERENCE  where vnf_type= $vnf-type "  +
450                              " and file_category = 'capability' )" ;
451
452                  log.info(fn + "Query String : " + queryString);
453                  status = dblib.query(queryString, localContext);
454
455                  if(status.toString().equals("FAILURE"))
456                          throw new SvcLogicException("Error - while getting capabilitiesData ");
457
458                  String queryString1 = "select artifact_content from ASDC_ARTIFACTS  "  +
459                                  " where artifact_name = $artifactName  and internal_version = $maxInternalVersion ";
460
461                  log.debug(fn + "Query String : " + queryString1);
462                  status = dblib.query(queryString1, localContext);
463                  if (status.toString().equals("NOT_FOUND"))
464                      return null;
465                  if(status.toString().equals("FAILURE"))
466                          throw new SvcLogicException("Error - while getting capabilitiesData ");
467          }
468
469          return localContext.getAttribute("artifact-content");
470  }
471
472     public QueryStatus getTemplateWithTemplateModelId(SvcLogicContext ctx, String prefix, String fileCategory,
473             String templateModelId) throws SvcLogicException {
474         QueryStatus status = null;
475         String templatePattern = "%_"+ templateModelId +"%";
476         if (dblib != null && ctx != null) {
477             ctx.setAttribute("file-category", fileCategory);
478             ctx.setAttribute("template-pattern", templatePattern);
479             String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
480                     + " FROM ASDC_ARTIFACTS "
481                     + " WHERE asdc_artifacts_id = ( SELECT MAX(a.asdc_artifacts_id) configfileid  "
482                     + " FROM ASDC_ARTIFACTS a, ASDC_REFERENCE b " + " WHERE a.artifact_name = b.artifact_name "
483                     + " AND file_category =  $file-category AND action =  $request-action "
484                     + " AND vnf_type =  $vnf-type  " + " AND vnfc_type =   $vnfc-type ) and ASDC_ARTIFACTS.artifact_name like "
485                     + "$template-pattern ; ";
486             log.info("getTemplateWithTemplateModelId()::: with template:::"+ key);
487
488             status = dblib.query(key, prefix, ctx);
489         }
490         return status;
491     }
492
493     public QueryStatus getTemplateByVnfTypeNActionWithTemplateModelId(SvcLogicContext ctx, String prefix,
494             String fileCategory, String templateModelId) throws SvcLogicException {
495         QueryStatus status = null;
496         String templatePattern = "%_"+ templateModelId +"%";
497         if (dblib != null && ctx != null) {
498             ctx.setAttribute("file-category", fileCategory);
499             ctx.setAttribute("template-pattern", templatePattern);
500             String key = "SELECT artifact_content file_content , asdc_artifacts_id config_file_id "
501                     + " FROM ASDC_ARTIFACTS "
502                     + " WHERE asdc_artifacts_id = (SELECT MAX(a.asdc_artifacts_id) configfileid  "
503                     + " FROM ASDC_ARTIFACTS a, ASDC_REFERENCE b " + " WHERE a.artifact_name = b.artifact_name "
504                     + " AND file_category =  $file-category AND action =  $request-action "
505                     + " AND vnf_type =  $vnf-type )  and ASDC_ARTIFACTS.artifact_name like "
506                     + "$template-pattern ; ";
507             log.info("getTemplateByVnfTypeNActionWithTemplateModelId()::: with template:::"+ key);
508
509             status = dblib.query(key, prefix, ctx);
510         }
511         return status;
512
513     }
514
515     public QueryStatus getVnfcReferenceByVnfTypeNActionWithTemplateModelId(SvcLogicContext ctx, String prefix,
516             String templateModelId) throws SvcLogicException {
517         QueryStatus status = null;
518         if (dblib != null && ctx != null) {
519             ctx.setAttribute("template-model-id", templateModelId);
520             String key = "SELECT  * "
521                     + " FROM VNFC_REFERENCE "
522                     + " WHERE vnf_type =  $vnf-type "
523                     + " AND action =  $request-action   "
524                     + " AND template_id = "
525                     + "$template-model-id"
526                     + " ORDER BY vm_instance, vnfc_instance ; ";
527
528             log.info("getVnfcReferenceByVnfTypeNActionWithTemplateModelId()::: with template:::"+ key);
529             status = dblib.query(key, prefix, ctx);
530         }
531         return status;
532     }
533
534 }