47228eae1118744280da77b3e9914bf267f4d22f
[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
28 import java.util.ArrayList;
29 import java.util.Map;
30 import org.apache.commons.lang.StringUtils;
31 import org.onap.appc.flow.controller.data.Transaction;
32 import org.onap.appc.flow.controller.utils.EscapeUtils;
33 import org.onap.appc.flow.controller.utils.FlowControllerConstants;
34 import org.onap.ccsdk.sli.adaptors.resource.sql.SqlResource;
35 import org.onap.ccsdk.sli.core.dblib.DbLibService;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
37 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
38 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
39 import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
40
41 public class FlowControlDBService {
42
43     private static final EELFLogger log = EELFManager.getInstance().getLogger(FlowControlDBService.class);
44     private static final String QUERY_STR = "Query String : ";
45     private static final String FAILURE_PARAM = "FAILURE";
46     protected static final String GET_FLOW_REF_DATA_ERROR = "Error - while getting FlowReferenceData ";
47     private static final String ARTIFACT_CONTENT_PARAM = "artifact-content";
48     protected static final String COUNT_PROTOCOL_PARAM = "count(protocol)";
49
50     private DbLibServiceQueries dblib;
51     private static FlowControlDBService dgGeneralDBService = null;
52
53     private FlowControlDBService() {
54         if (dblib== null) {
55             dblib = new DbLibServiceQueries();
56         }
57     }
58
59     protected FlowControlDBService(DbLibServiceQueries dbLibServiceQueries) {
60             dblib = dbLibServiceQueries;
61     }
62     
63     protected FlowControlDBService(DbLibService dbLibService) {
64         dblib = new DbLibServiceQueries(dbLibService);
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 (dblib != 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 = dblib.query(key, 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 (dblib != null && localContext != null) {
104             String queryString =
105                 "select max(internal_version) as maxInternalVersion, artifact_name as artifactName from "
106                     + FlowControllerConstants.DB_SDC_ARTIFACTS + " where artifact_name in (select artifact_name from "
107                     + FlowControllerConstants.DB_SDC_REFERENCE + " where vnf_type= $"
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 = dblib.query(queryString, localContext);
115
116             if (status.toString().equals(FAILURE_PARAM)) {
117                 throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
118             }
119
120             String queryString1 = "select artifact_content from " + FlowControllerConstants.DB_SDC_ARTIFACTS
121                 + " where artifact_name = $artifactName  and internal_version = $maxInternalVersion ";
122
123             log.debug(fn + QUERY_STR + queryString1);
124             status = dblib.query(queryString1, 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 = dblib.save(queryString, 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 = ? "
175             + "and action_level = ? and protocol = ?";
176         ArrayList<String> arguments = new ArrayList<>();
177         arguments.add(transaction.getAction());
178         arguments.add(transaction.getActionLevel());
179         arguments.add(protocolType);
180
181         log.debug(fn + QUERY_STR + key);
182         status = dblib.query(key, context, arguments);
183         if (status.toString().equals(FAILURE_PARAM)) {
184             throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
185         }
186
187         transaction.setExecutionModule(context.getAttribute(FlowControllerConstants.EXECUTTION_MODULE));
188         transaction.setExecutionRPC(context.getAttribute(FlowControllerConstants.EXECUTION_RPC));
189         transaction.setExecutionType(context.getAttribute(FlowControllerConstants.EXECUTION_TYPE));
190
191     }
192
193     private String getProtocolType(Transaction transaction, String vnfType, String fn, SvcLogicContext context)
194         throws SvcLogicException {
195         QueryStatus status;
196         String protocolQuery;
197         int protocolCount;
198         protocolQuery = "select count(protocol) from " + FlowControllerConstants.DB_PROTOCOL_REFERENCE
199             + " where action = ? and action_level = ?";
200         ArrayList<String> arguments = new ArrayList<>();
201         arguments.add(transaction.getAction());
202         arguments.add(transaction.getActionLevel());
203
204         log.debug(fn + QUERY_STR + protocolQuery);
205         status = dblib.query(protocolQuery, context, arguments);
206         if (status.toString().equals(FAILURE_PARAM)) {
207             throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
208         }
209
210         String countProtocolParam = context.getAttribute(COUNT_PROTOCOL_PARAM);
211         log.debug(" Protocol Count " + countProtocolParam);
212         protocolCount = Integer.parseInt(countProtocolParam);
213
214         if (protocolCount == 1) {
215             protocolQuery = "select protocol from " + FlowControllerConstants.DB_PROTOCOL_REFERENCE
216                 + " where action = ? and action_level = ?";
217             ArrayList<String> arguments2 = new ArrayList<>();
218             arguments2.add(transaction.getAction());
219             arguments2.add(transaction.getActionLevel());
220
221             log.debug(fn + QUERY_STR + protocolQuery);
222             status = dblib.query(protocolQuery, context, arguments2);
223             if (status.toString().equals(FAILURE_PARAM)) {
224                 throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
225             }
226             return context.getAttribute("protocol");
227         } else {
228             if (hasSingleProtocol(transaction, vnfType, fn, context)) {
229                 return context.getAttribute("protocol");
230             }
231         }
232         return null;
233     }
234
235     private boolean hasSingleProtocol(Transaction transaction, String vnfType, String fn, SvcLogicContext context)
236         throws SvcLogicException {
237         String protocolQuery;
238         QueryStatus status;
239         int protocolCount;
240         protocolQuery = "select count(protocol) from " + FlowControllerConstants.DB_PROTOCOL_REFERENCE
241             + " where action = ? and action_level = ? and vnf_type = ?";
242         ArrayList<String> arguments = new ArrayList<>();
243         arguments.add(transaction.getAction());
244         arguments.add(transaction.getActionLevel());
245         arguments.add(vnfType);
246
247         log.debug(fn + QUERY_STR + protocolQuery);
248         status = dblib.query(protocolQuery, context, arguments);
249         if (status.toString().equals(FAILURE_PARAM)) {
250             throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
251         }
252
253         log.debug(" Protocol Count " + context.getAttribute(COUNT_PROTOCOL_PARAM));
254         protocolCount = Integer.parseInt(context.getAttribute(COUNT_PROTOCOL_PARAM));
255         if (protocolCount > 1) {
256             throw new SvcLogicException("Got more than 2 values..");
257         } else if (protocolCount == 1) {
258             protocolQuery = "select protocol from " + FlowControllerConstants.DB_PROTOCOL_REFERENCE
259                 + " where action = ? and action_level = ? and vnf_type = ?";
260             ArrayList<String> arguments2 = new ArrayList<>();
261             arguments2.add(transaction.getAction());
262             arguments2.add(transaction.getActionLevel());
263             arguments2.add(vnfType);
264             log.debug(fn + QUERY_STR + protocolQuery);
265             status = dblib.query(protocolQuery, context, arguments2);
266             if (status.toString().equals(FAILURE_PARAM)) {
267                 throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
268             }
269             return true;
270         }
271         return false;
272     }
273
274     public String getDependencyInfo(SvcLogicContext localContext) throws SvcLogicException {
275         String fn = "DBService.getDependencyInfo ";
276         QueryStatus status;
277         if (dblib != null && localContext != null) {
278             String queryString =
279                 "select max(internal_version) as maxInternalVersion, artifact_name as artifactName from "
280                     + FlowControllerConstants.DB_SDC_ARTIFACTS + " where artifact_name in (select artifact_name from "
281                     + FlowControllerConstants.DB_SDC_REFERENCE + " where vnf_type= $"
282                     + FlowControllerConstants.VNF_TYPE
283                     + " and file_category = '" + FlowControllerConstants.DEPENDENCYMODEL + "' )";
284
285             log.debug(fn + QUERY_STR + queryString);
286             status = dblib.query(queryString, localContext);
287
288             if (status.toString().equals(FAILURE_PARAM)) {
289                 throw new SvcLogicException("Error - while getting dependencydata ");
290             }
291
292             String queryString1 = "select artifact_content from " + FlowControllerConstants.DB_SDC_ARTIFACTS
293                 + " where artifact_name = $artifactName  and internal_version = $maxInternalVersion ";
294
295             log.debug(fn + QUERY_STR + queryString1);
296             status = dblib.query(queryString1, localContext);
297             if (status.toString().equals(FAILURE_PARAM)) {
298                 throw new SvcLogicException("Error - while getting dependencyData ");
299             }
300         }
301
302         return localContext != null ? localContext.getAttribute(ARTIFACT_CONTENT_PARAM) : null;
303
304     }
305
306     public String getCapabilitiesData(SvcLogicContext localContext) throws SvcLogicException {
307         String fn = "DBService.getCapabilitiesData ";
308         QueryStatus status;
309         if (dblib != null && localContext != null) {
310             String queryString =
311                 "select max(internal_version) as maxInternalVersion, artifact_name as artifactName from "
312                     + FlowControllerConstants.DB_SDC_ARTIFACTS + " where artifact_name in (select artifact_name from "
313                     + FlowControllerConstants.DB_SDC_REFERENCE + " where vnf_type= $"
314                     + FlowControllerConstants.VNF_TYPE
315                     + " and file_category = '" + FlowControllerConstants.CAPABILITY + "' )";
316
317             log.info(fn + QUERY_STR + queryString);
318             status = dblib.query(queryString, localContext);
319
320             if (status.toString().equals(FAILURE_PARAM)) {
321                 throw new SvcLogicException("Error - while getting capabilitiesData ");
322             }
323
324             String queryString1 = "select artifact_content from " + FlowControllerConstants.DB_SDC_ARTIFACTS
325                 + " where artifact_name = $artifactName  and internal_version = $maxInternalVersion ";
326
327             log.debug(fn + QUERY_STR + queryString1);
328             status = dblib.query(queryString1, localContext);
329             if (status.toString().equals(FAILURE_PARAM)) {
330                 throw new SvcLogicException("Error - while getting capabilitiesData ");
331             }
332         }
333         return localContext != null ? localContext.getAttribute(ARTIFACT_CONTENT_PARAM) : null;
334     }
335
336     protected SvcLogicContext getSvcLogicContext() {
337         return new SvcLogicContext();
338     }
339 }