enahance Sonar coverage rate
[appc.git] / appc-config / appc-flow-controller / provider / src / main / java / org / onap / appc / flow / controller / dbervices / FlowControlDBService.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.onap.appc.flow.controller.dbervices;
22
23 import java.util.Map;
24
25 import org.onap.appc.flow.controller.data.Transaction;
26 import org.onap.appc.flow.controller.utils.EscapeUtils;
27 import org.onap.appc.flow.controller.utils.FlowControllerConstants;
28 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
29 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
30 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
31 import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
32 import org.onap.ccsdk.sli.adaptors.resource.sql.SqlResource;
33
34 import com.att.eelf.configuration.EELFLogger;
35 import com.att.eelf.configuration.EELFManager;
36
37 public class FlowControlDBService {
38
39     private static final EELFLogger log = EELFManager.getInstance().getLogger(FlowControlDBService.class);
40     private SvcLogicResource serviceLogic;
41     private static FlowControlDBService dgGeneralDBService = null;
42     public static FlowControlDBService initialise() {
43         if (dgGeneralDBService == null) {
44             dgGeneralDBService = new FlowControlDBService();
45         }
46         return dgGeneralDBService;
47     }
48     private FlowControlDBService() {
49         if (serviceLogic == null) {
50             serviceLogic = new SqlResource();
51         }
52     }
53
54     public void getFlowReferenceData(SvcLogicContext ctx, Map<String, String> inParams, SvcLogicContext localContext) throws SvcLogicException {
55         
56         String fn = "DBService.getflowModelInfo";
57     //    log.debug("Testing "  + ctx.getAttribute(FlowExecutorConstants.VNF_TYPE) + " and " + ctx.getAttribute(FlowExecutorConstants.ACTION_LEVEL));    
58         String whereClause = " where ACTION = $" +FlowControllerConstants.REQUEST_ACTION ;
59     
60         if(ctx.getAttribute(FlowControllerConstants.VNF_TYPE) !=null)        
61             whereClause = whereClause.concat(" and VNF_TYPE = $" + FlowControllerConstants.VNF_TYPE);
62         
63         if(ctx.getAttribute(FlowControllerConstants.ACTION_LEVEL) !=null)
64             whereClause = whereClause.concat(" and ACTION_LEVEL = $" + FlowControllerConstants.ACTION_LEVEL);
65         
66         QueryStatus status = null;
67         if (serviceLogic != null && localContext != null) {    
68             String key = "select SEQUENCE_TYPE, CATEGORY, GENERATION_NODE, EXECUTION_NODE from " + FlowControllerConstants.DB_MULTISTEP_FLOW_REFERENCE + 
69                         whereClause ;
70             log.debug(fn + "Query String : " + key);
71             status = serviceLogic.query("SQL", false, null, key, null, null, localContext);
72             if(status.toString().equals("FAILURE"))
73                 throw new SvcLogicException("Error - while getting FlowReferenceData ");
74         }        
75     }
76     public String getEndPointByAction(String action) {
77         // TODO Auto-generated method stub
78         return null;
79     }
80     public String getDesignTimeFlowModel(SvcLogicContext localContext) throws SvcLogicException {
81         String fn = "DBService.getDesignTimeFlowModel ";        
82         QueryStatus status = null;
83         if (serviceLogic != null && localContext != null) {    
84             String queryString = "select max(internal_version) as maxInternalVersion, artifact_name as artifactName from " + FlowControllerConstants.DB_SDC_ARTIFACTS
85                     + " where artifact_name in (select artifact_name from " + FlowControllerConstants.DB_SDC_REFERENCE +
86                     " where vnf_type= $" + FlowControllerConstants.VNF_TYPE + 
87                      " and  vnfc_type = $" + FlowControllerConstants.VNFC_TYPE + 
88                      " and  action = $" + FlowControllerConstants.REQUEST_ACTION + 
89                      " and file_category =  $" + FlowControllerConstants.CATEGORY + " )" ;
90                     
91                     
92             log.debug(fn + "Query String : " + queryString);
93             status = serviceLogic.query("SQL", false, null, queryString, null, null, localContext);        
94
95             if(status.toString().equals("FAILURE"))
96                 throw new SvcLogicException("Error - while getting FlowReferenceData ");
97                         
98             String queryString1 = "select artifact_content from " + FlowControllerConstants.DB_SDC_ARTIFACTS +
99                     " where artifact_name = $artifactName  and internal_version = $maxInternalVersion ";
100             
101             log.debug(fn + "Query String : " + queryString1);
102             status = serviceLogic.query("SQL", false, null, queryString1, null, null, localContext);            
103             if(status.toString().equals("FAILURE"))
104                 throw new SvcLogicException("Error - while getting FlowReferenceData ");
105         }
106         return localContext != null ? localContext.getAttribute("artifact-content") : null;
107     }
108     public QueryStatus loadSequenceIntoDB(SvcLogicContext localContext) throws SvcLogicException {
109              
110         QueryStatus status = null;
111       
112         if (localContext != null) {
113             String fn = "DBService.saveArtifacts";
114           
115             localContext.setAttribute(FlowControllerConstants.ARTIFACT_CONTENT_ESCAPED,
116                                       EscapeUtils.escapeSql(localContext.getAttribute(FlowControllerConstants.ARTIFACT_CONTENT)));
117             log.debug("ESCAPED sequence for DB : "  +  localContext.getAttribute(FlowControllerConstants.ARTIFACT_CONTENT_ESCAPED));
118         
119             for (Object key : localContext.getAttributeKeySet()) {
120                 String parmName = (String) key;
121                 String parmValue = localContext.getAttribute(parmName);
122                 log.debug(" loadSequenceIntoDB " + parmName +  "="  + parmValue);
123             
124             }
125           
126             String queryString = "INSERT INTO " + FlowControllerConstants.DB_REQUEST_ARTIFACTS + 
127                         " set request_id =  $" + FlowControllerConstants.REQUEST_ID + 
128                         " , action =  $" + FlowControllerConstants.REQUEST_ACTION + 
129                         " , action_level =  $" + FlowControllerConstants.ACTION_LEVEL + 
130                         " , vnf_type = $" + FlowControllerConstants.VNF_TYPE + 
131                         " , category = $" + FlowControllerConstants.CATEGORY + 
132                         " , artifact_content = $" + FlowControllerConstants.ARTIFACT_CONTENT_ESCAPED + 
133                         " , updated_date = sysdate() ";        
134           
135             log.debug(fn + "Query String : " + queryString);
136             status = serviceLogic.save("SQL", false, false, queryString, null, null, localContext);
137             if(status.toString().equals("FAILURE"))
138                 throw new SvcLogicException("Error While processing storing Artifact: " +localContext.getAttribute(FlowControllerConstants.ARTIFACT_NAME));
139         }
140         return status;
141
142     }
143     public void populateModuleAndRPC(Transaction transaction, String vnf_type) throws SvcLogicException {
144         String fn = "FlowControlDBService.populateModuleAndRPC ";
145         QueryStatus status = null;
146         SvcLogicContext context = new SvcLogicContext();        
147         
148         String key = "select execution_type, execution_module, execution_rpc from " + FlowControllerConstants.DB_PROCESS_FLOW_REFERENCE + 
149                     " where action = '" + transaction.getAction() + "'" + 
150                     " and action_level = '" + transaction.getActionLevel() + "'" + 
151                     " and protocol in ( select protocol from " + FlowControllerConstants.DB_PROTOCOL_REFERENCE  + 
152                     " where action = '" + transaction.getAction() + "'" ;
153         if(vnf_type !=null && !vnf_type.isEmpty())
154             key = key +     " and vnf_type ='" + vnf_type + "' )" ;
155         else
156             key = key + " ) " ;
157         log.debug(fn + "Query String : " + key);
158         status = serviceLogic.query("SQL", false, null, key, null, null, context);
159         if(status.toString().equals("FAILURE"))
160             throw new SvcLogicException("Error - while getting FlowReferenceData ");
161            
162         transaction.setExecutionModule(context.getAttribute(FlowControllerConstants.EXECUTTION_MODULE));
163         transaction.setExecutionRPC(context.getAttribute(FlowControllerConstants.EXECUTION_RPC));
164         transaction.setExecutionType(context.getAttribute(FlowControllerConstants.EXECUTION_TYPE));
165         
166     }
167     
168     public String getDependencyInfo(SvcLogicContext localContext) throws SvcLogicException {
169         String fn = "DBService.getDependencyInfo ";        
170         QueryStatus status = null;
171         if (serviceLogic != null && localContext != null) {    
172             String queryString = "select max(internal_version) as maxInternalVersion, artifact_name as artifactName from " + FlowControllerConstants.DB_SDC_ARTIFACTS
173                     + " where artifact_name in (select artifact_name from " + FlowControllerConstants.DB_SDC_REFERENCE +
174                     " where vnf_type= $" + FlowControllerConstants.VNF_TYPE  +
175                      " and file_category = '" + FlowControllerConstants.DEPENDENCYMODEL +"' )" ;
176                     
177             log.debug(fn + "Query String : " + queryString);
178             status = serviceLogic.query("SQL", false, null, queryString, null, null, localContext);        
179
180             if(status.toString().equals("FAILURE"))
181                 throw new SvcLogicException("Error - while getting dependencydata ");
182                         
183             String queryString1 = "select artifact_content from " + FlowControllerConstants.DB_SDC_ARTIFACTS +
184                     " where artifact_name = $artifactName  and internal_version = $maxInternalVersion ";
185             
186             log.debug(fn + "Query String : " + queryString1);
187             status = serviceLogic.query("SQL", false, null, queryString1, null, null, localContext);            
188             if(status.toString().equals("FAILURE"))
189                 throw new SvcLogicException("Error - while getting dependencyData ");
190         }
191
192         return localContext != null ? localContext.getAttribute("artifact-content") : null;
193
194     }
195     
196         public String getCapabilitiesData(SvcLogicContext localContext) throws SvcLogicException {
197             String fn = "DBService.getCapabilitiesData ";        
198             QueryStatus status = null;
199             if (serviceLogic != null && localContext != null) {    
200                 String queryString = "select max(internal_version) as maxInternalVersion, artifact_name as artifactName from " + FlowControllerConstants.DB_SDC_ARTIFACTS
201                         + " where artifact_name in (select artifact_name from " + FlowControllerConstants.DB_SDC_REFERENCE +
202                         " where vnf_type= $" + FlowControllerConstants.VNF_TYPE  +  
203                         " and file_category = '" + FlowControllerConstants.CAPABILITY +"' )" ;
204                                             
205                 log.info(fn + "Query String : " + queryString);
206                 status = serviceLogic.query("SQL", false, null, queryString, null, null, localContext);        
207
208                 if(status.toString().equals("FAILURE"))
209                     throw new SvcLogicException("Error - while getting capabilitiesData ");
210                             
211                 String queryString1 = "select artifact_content from " + FlowControllerConstants.DB_SDC_ARTIFACTS +
212                         " where artifact_name = $artifactName  and internal_version = $maxInternalVersion ";
213                 
214                 log.debug(fn + "Query String : " + queryString1);
215                 status = serviceLogic.query("SQL", false, null, queryString1, null, null, localContext);            
216                 if(status.toString().equals("FAILURE"))
217                     throw new SvcLogicException("Error - while getting capabilitiesData ");
218             }
219             return localContext != null ? localContext.getAttribute("artifact-content") : null;
220         }
221 }