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