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