Change to CCSDK and ODL Carbon
[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.openecomp.appc.flow.controller.dbervices;
22
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import org.openecomp.appc.flow.controller.data.Transaction;
27 import org.openecomp.appc.flow.controller.utils.EscapeUtils;
28 import org.openecomp.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                     
108         return localContext.getAttribute("artifact-content");
109     }
110     public QueryStatus loadSequenceIntoDB(SvcLogicContext localContext) throws SvcLogicException {
111         String fn = "DBService.saveArtifacts";
112         localContext.setAttribute(FlowControllerConstants.ARTIFACT_CONTENT_ESCAPED, EscapeUtils.escapeSql(localContext.getAttribute(FlowControllerConstants.ARTIFACT_CONTENT)));
113         log.debug("ESCAPED sequence for DB : "  +  localContext.getAttribute(FlowControllerConstants.ARTIFACT_CONTENT_ESCAPED));
114         
115         for (Object key : localContext.getAttributeKeySet()) {
116             String parmName = (String) key;
117             String parmValue = localContext.getAttribute(parmName);
118             log.debug(" loadSequenceIntoDB " + parmName +  "="  + parmValue);
119             
120         }
121         
122         
123         QueryStatus status = null;
124         if (serviceLogic != null && localContext != null) {
125             String queryString = "INSERT INTO " + FlowControllerConstants.DB_REQUEST_ARTIFACTS + 
126                         " set request_id =  $" + FlowControllerConstants.REQUEST_ID + 
127                         " , action =  $" + FlowControllerConstants.REQUEST_ACTION + 
128                         " , action_level =  $" + FlowControllerConstants.ACTION_LEVEL + 
129                         " , vnf_type = $" + FlowControllerConstants.VNF_TYPE + 
130                         " , category = $" + FlowControllerConstants.CATEGORY + 
131                         " , artifact_content = $" + FlowControllerConstants.ARTIFACT_CONTENT_ESCAPED + 
132                         " , updated_date = sysdate() ";        
133
134             status = serviceLogic.save("SQL", false, false, queryString, null, null, localContext);
135             if(status.toString().equals("FAILURE"))
136                 throw new SvcLogicException("Error While processing storing Artifact: " +localContext.getAttribute(FlowControllerConstants.ARTIFACT_NAME));
137         }
138         return status;
139
140     }
141     public void populateModuleAndRPC(Transaction transaction, String vnf_type) throws SvcLogicException {
142         String fn = "FlowControlDBService.populateModuleAndRPC ";
143         QueryStatus status = null;
144         SvcLogicContext context = new SvcLogicContext();        
145         if (serviceLogic != null && context != null) {    
146             String key = "select execution_type, execution_module, execution_rpc from " + FlowControllerConstants.DB_PROCESS_FLOW_REFERENCE + 
147                         " where action = '" + transaction.getAction() + "'" + 
148                         " and action_level = '" + transaction.getActionLevel() + "'" + 
149                         " and protocol in ( select protocol from " + FlowControllerConstants.DB_PROTOCOL_REFERENCE  + 
150                         " where action = '" + transaction.getAction() + "'" ;
151             if(vnf_type !=null && !vnf_type.isEmpty())
152                 key = key +     " and vnf_type ='" + vnf_type + "' )" ;
153             else
154                 key = key + " ) " ;
155             log.debug(fn + "Query String : " + key);
156             status = serviceLogic.query("SQL", false, null, key, null, null, context);
157             if(status.toString().equals("FAILURE"))
158                 throw new SvcLogicException("Error - while getting FlowReferenceData ");
159             
160             transaction.setExecutionModule(context.getAttribute(FlowControllerConstants.EXECUTTION_MODULE));
161             transaction.setExecutionRPC(context.getAttribute(FlowControllerConstants.EXECUTION_RPC));
162             transaction.setExecutionType(context.getAttribute(FlowControllerConstants.EXECUTION_TYPE));
163         }
164      
165     }
166     
167     public String getDependencyInfo(SvcLogicContext localContext) throws SvcLogicException {
168         String fn = "DBService.getDependencyInfo ";        
169         QueryStatus status = null;
170         if (serviceLogic != null && localContext != null) {    
171             String queryString = "select max(internal_version) as maxInternalVersion, artifact_name as artifactName from " + FlowControllerConstants.DB_SDC_ARTIFACTS
172                     + " where artifact_name in (select artifact_name from " + FlowControllerConstants.DB_SDC_REFERENCE +
173                     " where vnf_type= $" + FlowControllerConstants.VNF_TYPE  +
174                      " and file_category = '" + FlowControllerConstants.DEPENDENCYMODEL +"' )" ;
175                     
176             log.debug(fn + "Query String : " + queryString);
177             status = serviceLogic.query("SQL", false, null, queryString, null, null, localContext);        
178
179             if(status.toString().equals("FAILURE"))
180                 throw new SvcLogicException("Error - while getting dependencydata ");
181                         
182             String queryString1 = "select artifact_content from " + FlowControllerConstants.DB_SDC_ARTIFACTS +
183                     " where artifact_name = $artifactName  and internal_version = $maxInternalVersion ";
184             
185             log.debug(fn + "Query String : " + queryString1);
186             status = serviceLogic.query("SQL", false, null, queryString1, null, null, localContext);            
187             if(status.toString().equals("FAILURE"))
188                 throw new SvcLogicException("Error - while getting dependencyData ");
189         }
190                     
191         return localContext.getAttribute("artifact-content");
192     }
193     
194         public String getCapabilitiesData(SvcLogicContext localContext) throws SvcLogicException {
195             String fn = "DBService.getCapabilitiesData ";        
196             QueryStatus status = null;
197             if (serviceLogic != null && localContext != null) {    
198                 String queryString = "select max(internal_version) as maxInternalVersion, artifact_name as artifactName from " + FlowControllerConstants.DB_SDC_ARTIFACTS
199                         + " where artifact_name in (select artifact_name from " + FlowControllerConstants.DB_SDC_REFERENCE +
200                         " where vnf_type= $" + FlowControllerConstants.VNF_TYPE  +  
201                         " and file_category = '" + FlowControllerConstants.CAPABILITY +"' )" ;
202                                             
203                 log.info(fn + "Query String : " + queryString);
204                 status = serviceLogic.query("SQL", false, null, queryString, null, null, localContext);        
205
206                 if(status.toString().equals("FAILURE"))
207                     throw new SvcLogicException("Error - while getting capabilitiesData ");
208                             
209                 String queryString1 = "select artifact_content from " + FlowControllerConstants.DB_SDC_ARTIFACTS +
210                         " where artifact_name = $artifactName  and internal_version = $maxInternalVersion ";
211                 
212                 log.debug(fn + "Query String : " + queryString1);
213                 status = serviceLogic.query("SQL", false, null, queryString1, null, null, localContext);            
214                 if(status.toString().equals("FAILURE"))
215                     throw new SvcLogicException("Error - while getting capabilitiesData ");
216             }
217                         
218             return localContext.getAttribute("artifact-content");
219         }
220 }