Update license header in appc-flow-controller file
[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 : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 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  *
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.appc.flow.controller.dbervices;
22
23 import com.att.eelf.configuration.EELFLogger;
24 import com.att.eelf.configuration.EELFManager;
25 import java.util.Map;
26 import org.apache.commons.lang.StringUtils;
27 import org.onap.appc.flow.controller.data.Transaction;
28 import org.onap.appc.flow.controller.utils.EscapeUtils;
29 import org.onap.appc.flow.controller.utils.FlowControllerConstants;
30 import org.onap.ccsdk.sli.adaptors.resource.sql.SqlResource;
31 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
35
36 public class FlowControlDBService {
37
38     private static final EELFLogger log = EELFManager.getInstance().getLogger(FlowControlDBService.class);
39     private static final String QUERY_STR = "Query String : ";
40     private static final String FAILURE_PARAM = "FAILURE";
41     private static final String GET_FLOW_REF_DATA_ERROR = "Error - while getting FlowReferenceData ";
42     private static final String SELECT_AS_QUERY_STR = "select max(internal_version) as maxInternalVersion, artifact_name as artifactName from ";
43     private static final String WHERE_ART_NAME_QUERY_STR = " where artifact_name in (select artifact_name from ";
44     private static final String WHERE_VNF_TYPE_QUERY_STR = " where vnf_type= $";
45     private static final String SELECT_ART_CONTENT_QUERY_STR = "select artifact_content from ";
46     private static final String WHERE_ARTIFACT_NAME_QUERY_STR = " where artifact_name = $artifactName  and internal_version = $maxInternalVersion ";
47     private static final String ARTIFACT_CONTENT_PARAM = "artifact-content";
48     private static final String COUNT_PROTOCOL_PARAM = "count(protocol)";
49     private static final String WHERE_ACTION_QUERY_STR = " where action = '";
50     private static final String AND_ACTION_LEVEL_QUERY_STR = " and action_level = '";
51
52     private SvcLogicResource serviceLogic;
53     private static FlowControlDBService dgGeneralDBService = null;
54
55     private FlowControlDBService() {
56         if (serviceLogic == null) {
57             serviceLogic = new SqlResource();
58         }
59     }
60     
61     public static FlowControlDBService initialise() {
62         if (dgGeneralDBService == null) {
63             dgGeneralDBService = new FlowControlDBService();
64         }
65         return dgGeneralDBService;
66     }
67
68     public void getFlowReferenceData(SvcLogicContext ctx, Map<String, String> inParams, SvcLogicContext localContext)
69         throws SvcLogicException {
70
71         String fn = "DBService.getflowModelInfo";
72         String whereClause = " where ACTION = $" + FlowControllerConstants.REQUEST_ACTION;
73
74         if (StringUtils.isNotBlank(ctx.getAttribute(FlowControllerConstants.VNF_TYPE))) {
75             whereClause = whereClause.concat(" and VNF_TYPE = $" + FlowControllerConstants.VNF_TYPE);
76         }
77         if (StringUtils.isNotBlank(ctx.getAttribute(FlowControllerConstants.ACTION_LEVEL))) {
78             whereClause = whereClause.concat(" and ACTION_LEVEL = $" + FlowControllerConstants.ACTION_LEVEL);
79         }
80
81         QueryStatus status;
82         if (serviceLogic != null && localContext != null) {
83             String key = "select SEQUENCE_TYPE, CATEGORY, GENERATION_NODE, EXECUTION_NODE from "
84                 + FlowControllerConstants.DB_MULTISTEP_FLOW_REFERENCE + whereClause;
85             log.debug(fn + QUERY_STR + key);
86             status = serviceLogic.query("SQL", false, null, key, null, null, localContext);
87             if (status.toString().equals(FAILURE_PARAM)) {
88                 throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
89             }
90         }
91     }
92
93     public String getEndPointByAction(String action) {
94         return null;
95     }
96
97     public String getDesignTimeFlowModel(SvcLogicContext localContext) throws SvcLogicException {
98         String fn = "DBService.getDesignTimeFlowModel ";
99         QueryStatus status;
100         if (serviceLogic != null && localContext != null) {
101             String queryString =
102                 SELECT_AS_QUERY_STR
103                     + FlowControllerConstants.DB_SDC_ARTIFACTS + WHERE_ART_NAME_QUERY_STR
104                     + FlowControllerConstants.DB_SDC_REFERENCE + WHERE_VNF_TYPE_QUERY_STR
105                     + FlowControllerConstants.VNF_TYPE
106                     + " and  vnfc_type = $" + FlowControllerConstants.VNFC_TYPE + " and  action = $"
107                     + FlowControllerConstants.REQUEST_ACTION + " and file_category =  $"
108                     + FlowControllerConstants.CATEGORY + " )";
109
110             log.debug(fn + QUERY_STR + queryString);
111             status = serviceLogic.query("SQL", false, null, queryString, null, null, localContext);
112
113             if (status.toString().equals(FAILURE_PARAM)) {
114                 throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
115             }
116
117             String queryString1 = SELECT_ART_CONTENT_QUERY_STR + FlowControllerConstants.DB_SDC_ARTIFACTS
118                 + WHERE_ARTIFACT_NAME_QUERY_STR;
119
120             log.debug(fn + QUERY_STR + queryString1);
121             status = serviceLogic.query("SQL", false, null, queryString1, null, null, localContext);
122             if (status.toString().equals(FAILURE_PARAM)) {
123                 throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
124             }
125         }
126         return localContext != null ? localContext.getAttribute(ARTIFACT_CONTENT_PARAM) : null;
127     }
128
129     public QueryStatus loadSequenceIntoDB(SvcLogicContext localContext) throws SvcLogicException {
130
131         QueryStatus status = null;
132         if (localContext != null) {
133             String fn = "DBService.saveArtifacts";
134
135             localContext.setAttribute(FlowControllerConstants.ARTIFACT_CONTENT_ESCAPED,
136                 EscapeUtils.escapeSql(localContext.getAttribute(FlowControllerConstants.ARTIFACT_CONTENT)));
137             log.debug("ESCAPED sequence for DB : "
138                 + localContext.getAttribute(FlowControllerConstants.ARTIFACT_CONTENT_ESCAPED));
139
140             for (Object key : localContext.getAttributeKeySet()) {
141                 String parmName = (String) key;
142                 String parmValue = localContext.getAttribute(parmName);
143                 log.debug(" loadSequenceIntoDB " + parmName + "=" + parmValue);
144             }
145
146             String queryString = "INSERT INTO " + FlowControllerConstants.DB_REQUEST_ARTIFACTS + " set request_id =  $"
147                 + FlowControllerConstants.REQUEST_ID + " , action =  $" + FlowControllerConstants.REQUEST_ACTION
148                 + " , action_level =  $" + FlowControllerConstants.ACTION_LEVEL + " , vnf_type = $"
149                 + FlowControllerConstants.VNF_TYPE + " , category = $" + FlowControllerConstants.CATEGORY
150                 + " , artifact_content = $" + FlowControllerConstants.ARTIFACT_CONTENT_ESCAPED
151                 + " , updated_date = sysdate() ";
152
153             log.debug(fn + QUERY_STR + queryString);
154             status = serviceLogic.save("SQL", false, false, queryString, null, null, localContext);
155             if (status.toString().equals(FAILURE_PARAM)) {
156                 throw new SvcLogicException("Error While processing storing Artifact: "
157                     + localContext.getAttribute(FlowControllerConstants.ARTIFACT_NAME));
158             }
159         }
160         return status;
161
162     }
163
164     public void populateModuleAndRPC(Transaction transaction, String vnfType) throws SvcLogicException {
165         String fn = "FlowControlDBService.populateModuleAndRPC ";
166         QueryStatus status;
167         SvcLogicContext context = new SvcLogicContext();
168         String protocolType = getProtocolType(transaction, vnfType, fn, context);
169
170         String key = "select execution_type, execution_module, execution_rpc from "
171             + FlowControllerConstants.DB_PROCESS_FLOW_REFERENCE + WHERE_ACTION_QUERY_STR + transaction.getAction()
172             + "'" + AND_ACTION_LEVEL_QUERY_STR + transaction.getActionLevel() + "'" + " and protocol = '"
173             + protocolType + "'";
174
175         log.debug(fn + QUERY_STR + key);
176         status = serviceLogic.query("SQL", false, null, key, null, null, context);
177         if (status.toString().equals(FAILURE_PARAM)) {
178             throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
179         }
180
181         transaction.setExecutionModule(context.getAttribute(FlowControllerConstants.EXECUTTION_MODULE));
182         transaction.setExecutionRPC(context.getAttribute(FlowControllerConstants.EXECUTION_RPC));
183         transaction.setExecutionType(context.getAttribute(FlowControllerConstants.EXECUTION_TYPE));
184
185     }
186
187     private String getProtocolType(Transaction transaction, String vnfType, String fn, SvcLogicContext context)
188         throws SvcLogicException {
189         QueryStatus status;
190         String protocolQuery;
191         int protocolCount;
192         protocolQuery = "select count(protocol) from " + FlowControllerConstants.DB_PROTOCOL_REFERENCE
193             + WHERE_ACTION_QUERY_STR + transaction.getAction() + "'" + AND_ACTION_LEVEL_QUERY_STR
194             + transaction.getActionLevel() + "'";
195
196         log.debug(fn + QUERY_STR + protocolQuery);
197         status = serviceLogic.query("SQL", false, null, protocolQuery, null, null, context);
198         if (status.toString().equals(FAILURE_PARAM)) {
199             throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
200         }
201
202         log.debug(" Protocol Count " + context.getAttribute(COUNT_PROTOCOL_PARAM));
203         protocolCount = Integer.parseInt(context.getAttribute(COUNT_PROTOCOL_PARAM));
204
205         if (protocolCount == 1) {
206             protocolQuery = "select protocol from " + FlowControllerConstants.DB_PROTOCOL_REFERENCE
207                 + WHERE_ACTION_QUERY_STR + transaction.getAction() + "'" + AND_ACTION_LEVEL_QUERY_STR
208                 + transaction.getActionLevel() + "'";
209
210             log.debug(fn + QUERY_STR + protocolQuery);
211             status = serviceLogic.query("SQL", false, null, protocolQuery, null, null, context);
212             if (status.toString().equals(FAILURE_PARAM)) {
213                 throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
214             }
215             return context.getAttribute("protocol");
216         } else {
217             if (hasSingleProtocol(transaction, vnfType, fn, context)) {
218                 return context.getAttribute("protocol");
219             }
220         }
221         return null;
222     }
223
224     private boolean hasSingleProtocol(Transaction transaction, String vnfType, String fn, SvcLogicContext context)
225         throws SvcLogicException {
226         String protocolQuery;
227         QueryStatus status;
228         int protocolCount;
229         protocolQuery = "select count(protocol) from " + FlowControllerConstants.DB_PROTOCOL_REFERENCE
230             + WHERE_ACTION_QUERY_STR + transaction.getAction() + "'" + AND_ACTION_LEVEL_QUERY_STR
231             + transaction.getActionLevel() + "'" + " and vnf_type = '" + vnfType + "'";
232
233         log.debug(fn + QUERY_STR + protocolQuery);
234         status = serviceLogic.query("SQL", false, null, protocolQuery, null, null, context);
235         if (status.toString().equals(FAILURE_PARAM)) {
236             throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
237         }
238
239         log.debug(" Protocol Count " + context.getAttribute(COUNT_PROTOCOL_PARAM));
240         protocolCount = Integer.parseInt(context.getAttribute(COUNT_PROTOCOL_PARAM));
241         if (protocolCount > 1) {
242             throw new SvcLogicException("Got more than 2 values..");
243         } else if (protocolCount == 1) {
244             protocolQuery = "select protocol from " + FlowControllerConstants.DB_PROTOCOL_REFERENCE
245                 + WHERE_ACTION_QUERY_STR + transaction.getAction() + "'" + AND_ACTION_LEVEL_QUERY_STR
246                 + transaction.getActionLevel() + "'" + " and vnf_type = '" + vnfType + "'";
247             log.debug(fn + QUERY_STR + protocolQuery);
248             status = serviceLogic.query("SQL", false, null, protocolQuery, null, null, context);
249             if (status.toString().equals(FAILURE_PARAM)) {
250                 throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
251             }
252             return true;
253         }
254         return false;
255     }
256
257     public String getDependencyInfo(SvcLogicContext localContext) throws SvcLogicException {
258         String fn = "DBService.getDependencyInfo ";
259         QueryStatus status;
260         if (serviceLogic != null && localContext != null) {
261             String queryString =
262                 SELECT_AS_QUERY_STR
263                     + FlowControllerConstants.DB_SDC_ARTIFACTS + WHERE_ART_NAME_QUERY_STR
264                     + FlowControllerConstants.DB_SDC_REFERENCE + WHERE_VNF_TYPE_QUERY_STR
265                     + FlowControllerConstants.VNF_TYPE
266                     + " and file_category = '" + FlowControllerConstants.DEPENDENCYMODEL + "' )";
267
268             log.debug(fn + QUERY_STR + queryString);
269             status = serviceLogic.query("SQL", false, null, queryString, null, null, localContext);
270
271             if (status.toString().equals(FAILURE_PARAM)) {
272                 throw new SvcLogicException("Error - while getting dependencydata ");
273             }
274
275             String queryString1 = SELECT_ART_CONTENT_QUERY_STR + FlowControllerConstants.DB_SDC_ARTIFACTS
276                 + WHERE_ARTIFACT_NAME_QUERY_STR;
277
278             log.debug(fn + QUERY_STR + queryString1);
279             status = serviceLogic.query("SQL", false, null, queryString1, null, null, localContext);
280             if (status.toString().equals(FAILURE_PARAM)) {
281                 throw new SvcLogicException("Error - while getting dependencyData ");
282             }
283         }
284
285         return localContext != null ? localContext.getAttribute(ARTIFACT_CONTENT_PARAM) : null;
286
287     }
288
289     public String getCapabilitiesData(SvcLogicContext localContext) throws SvcLogicException {
290         String fn = "DBService.getCapabilitiesData ";
291         QueryStatus status;
292         if (serviceLogic != null && localContext != null) {
293             String queryString =
294                 SELECT_AS_QUERY_STR
295                     + FlowControllerConstants.DB_SDC_ARTIFACTS + WHERE_ART_NAME_QUERY_STR
296                     + FlowControllerConstants.DB_SDC_REFERENCE + WHERE_VNF_TYPE_QUERY_STR
297                     + FlowControllerConstants.VNF_TYPE
298                     + " and file_category = '" + FlowControllerConstants.CAPABILITY + "' )";
299
300             log.info(fn + QUERY_STR + queryString);
301             status = serviceLogic.query("SQL", false, null, queryString, null, null, localContext);
302
303             if (status.toString().equals(FAILURE_PARAM)) {
304                 throw new SvcLogicException("Error - while getting capabilitiesData ");
305             }
306
307             String queryString1 = SELECT_ART_CONTENT_QUERY_STR + FlowControllerConstants.DB_SDC_ARTIFACTS
308                 + WHERE_ARTIFACT_NAME_QUERY_STR;
309
310             log.debug(fn + QUERY_STR + queryString1);
311             status = serviceLogic.query("SQL", false, null, queryString1, null, null, localContext);
312             if (status.toString().equals(FAILURE_PARAM)) {
313                 throw new SvcLogicException("Error - while getting capabilitiesData ");
314             }
315         }
316         return localContext != null ? localContext.getAttribute(ARTIFACT_CONTENT_PARAM) : null;
317     }
318 }