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