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