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