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