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