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