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