2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ============LICENSE_END=========================================================
23 package org.onap.appc.flow.controller.dbervices;
25 import com.att.eelf.configuration.EELFLogger;
26 import com.att.eelf.configuration.EELFManager;
28 import java.util.ArrayList;
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.core.dblib.DbLibService;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
37 import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
39 public class FlowControlDBService {
41 private static final EELFLogger log = EELFManager.getInstance().getLogger(FlowControlDBService.class);
42 private static final String QUERY_STR = "Query String : ";
43 private static final String FAILURE_PARAM = "FAILURE";
44 protected static final String GET_FLOW_REF_DATA_ERROR = "Error - while getting FlowReferenceData ";
45 private static final String ARTIFACT_CONTENT_PARAM = "artifact-content";
46 protected static final String COUNT_PROTOCOL_PARAM = "count(protocol)";
48 private DbLibServiceQueries dblib;
49 private static FlowControlDBService dgGeneralDBService = null;
51 private FlowControlDBService() {
53 dblib = new DbLibServiceQueries();
57 protected FlowControlDBService(DbLibServiceQueries dbLibServiceQueries) {
58 dblib = dbLibServiceQueries;
61 protected FlowControlDBService(DbLibService dbLibService) {
62 dblib = new DbLibServiceQueries(dbLibService);
65 public static FlowControlDBService initialise() {
66 if (dgGeneralDBService == null) {
67 dgGeneralDBService = new FlowControlDBService();
69 return dgGeneralDBService;
72 public void getFlowReferenceData(SvcLogicContext ctx, Map<String, String> inParams, SvcLogicContext localContext)
73 throws SvcLogicException {
75 String fn = "DBService.getflowModelInfo";
76 String whereClause = " where ACTION = $" + FlowControllerConstants.REQUEST_ACTION;
78 if (StringUtils.isNotBlank(ctx.getAttribute(FlowControllerConstants.ACTION_LEVEL))) {
79 whereClause = whereClause.concat(" and ACTION_LEVEL = $" + FlowControllerConstants.ACTION_LEVEL);
83 if (dblib != 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 = dblib.query(key, localContext);
88 if (status.toString().equals(FAILURE_PARAM)) {
89 throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
94 public String getEndPointByAction(String action) {
98 public String getDesignTimeFlowModel(SvcLogicContext localContext) throws SvcLogicException {
99 String fn = "DBService.getDesignTimeFlowModel ";
101 if (dblib != null && localContext != null) {
103 "select max(internal_version) as maxInternalVersion, artifact_name as artifactName from "
104 + FlowControllerConstants.DB_SDC_ARTIFACTS + " where artifact_name in (select artifact_name from "
105 + FlowControllerConstants.DB_SDC_REFERENCE + " where vnf_type= $"
106 + FlowControllerConstants.VNF_TYPE
107 + " and vnfc_type = $" + FlowControllerConstants.VNFC_TYPE + " and action = $"
108 + FlowControllerConstants.REQUEST_ACTION + " and file_category = $"
109 + FlowControllerConstants.CATEGORY + " )";
111 log.debug(fn + QUERY_STR + queryString);
112 status = dblib.query(queryString, localContext);
114 if (status.toString().equals(FAILURE_PARAM)) {
115 throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
118 String queryString1 = "select artifact_content from " + FlowControllerConstants.DB_SDC_ARTIFACTS
119 + " where artifact_name = $artifactName and internal_version = $maxInternalVersion ";
121 log.debug(fn + QUERY_STR + queryString1);
122 status = dblib.query(queryString1, localContext);
123 if (status.toString().equals(FAILURE_PARAM)) {
124 throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
127 return localContext != null ? localContext.getAttribute(ARTIFACT_CONTENT_PARAM) : null;
130 public QueryStatus loadSequenceIntoDB(SvcLogicContext localContext) throws SvcLogicException {
132 QueryStatus status = null;
133 if (localContext != null) {
134 String fn = "DBService.saveArtifacts";
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));
141 for (Object key : localContext.getAttributeKeySet()) {
142 String parmName = (String) key;
143 String parmValue = localContext.getAttribute(parmName);
144 log.debug(" loadSequenceIntoDB " + parmName + "=" + parmValue);
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() ";
154 log.debug(fn + QUERY_STR + queryString);
155 status = dblib.save(queryString, localContext);
156 if (status.toString().equals(FAILURE_PARAM)) {
157 throw new SvcLogicException("Error While processing storing Artifact: "
158 + localContext.getAttribute(FlowControllerConstants.ARTIFACT_NAME));
165 public void populateModuleAndRPC(Transaction transaction, String vnfType) throws SvcLogicException {
166 String fn = "FlowControlDBService.populateModuleAndRPC ";
168 SvcLogicContext context = getSvcLogicContext();
169 String protocolType = getProtocolType(transaction, vnfType, fn, context);
171 String key = "select execution_type, execution_module, execution_rpc from "
172 + FlowControllerConstants.DB_PROCESS_FLOW_REFERENCE + " where action = ? "
173 + "and action_level = ? and protocol = ?";
174 ArrayList<String> arguments = new ArrayList<>();
175 arguments.add(transaction.getAction());
176 arguments.add(transaction.getActionLevel());
177 arguments.add(protocolType);
179 log.debug(fn + QUERY_STR + key);
180 status = dblib.query(key, context, arguments);
181 if (status.toString().equals(FAILURE_PARAM)) {
182 throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
185 transaction.setExecutionModule(context.getAttribute(FlowControllerConstants.EXECUTTION_MODULE));
186 transaction.setExecutionRPC(context.getAttribute(FlowControllerConstants.EXECUTION_RPC));
187 transaction.setExecutionType(context.getAttribute(FlowControllerConstants.EXECUTION_TYPE));
191 private String getProtocolType(Transaction transaction, String vnfType, String fn, SvcLogicContext context)
192 throws SvcLogicException {
194 String protocolQuery;
196 protocolQuery = "select count(protocol) from " + FlowControllerConstants.DB_PROTOCOL_REFERENCE
197 + " where action = ? and action_level = ?";
198 ArrayList<String> arguments = new ArrayList<>();
199 arguments.add(transaction.getAction());
200 arguments.add(transaction.getActionLevel());
202 log.debug(fn + QUERY_STR + protocolQuery);
203 status = dblib.query(protocolQuery, context, arguments);
204 if (status.toString().equals(FAILURE_PARAM)) {
205 throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
208 String countProtocolParam = context.getAttribute(COUNT_PROTOCOL_PARAM);
209 log.debug(" Protocol Count " + countProtocolParam);
210 protocolCount = Integer.parseInt(countProtocolParam);
212 if (protocolCount == 1) {
213 protocolQuery = "select protocol from " + FlowControllerConstants.DB_PROTOCOL_REFERENCE
214 + " where action = ? and action_level = ?";
215 ArrayList<String> arguments2 = new ArrayList<>();
216 arguments2.add(transaction.getAction());
217 arguments2.add(transaction.getActionLevel());
219 log.debug(fn + QUERY_STR + protocolQuery);
220 status = dblib.query(protocolQuery, context, arguments2);
221 if (status.toString().equals(FAILURE_PARAM)) {
222 throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
224 return context.getAttribute("protocol");
226 if (hasSingleProtocol(transaction, vnfType, fn, context)) {
227 return context.getAttribute("protocol");
233 private boolean hasSingleProtocol(Transaction transaction, String vnfType, String fn, SvcLogicContext context)
234 throws SvcLogicException {
235 String protocolQuery;
238 protocolQuery = "select count(protocol) from " + FlowControllerConstants.DB_PROTOCOL_REFERENCE
239 + " where action = ? and action_level = ? and vnf_type = ?";
240 ArrayList<String> arguments = new ArrayList<>();
241 arguments.add(transaction.getAction());
242 arguments.add(transaction.getActionLevel());
243 arguments.add(vnfType);
245 log.debug(fn + QUERY_STR + protocolQuery);
246 status = dblib.query(protocolQuery, context, arguments);
247 if (status.toString().equals(FAILURE_PARAM)) {
248 throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
251 log.debug(" Protocol Count " + context.getAttribute(COUNT_PROTOCOL_PARAM));
252 protocolCount = Integer.parseInt(context.getAttribute(COUNT_PROTOCOL_PARAM));
253 if (protocolCount > 1) {
254 throw new SvcLogicException("Got more than 2 values..");
255 } else if (protocolCount == 1) {
256 protocolQuery = "select protocol from " + FlowControllerConstants.DB_PROTOCOL_REFERENCE
257 + " where action = ? and action_level = ? and vnf_type = ?";
258 ArrayList<String> arguments2 = new ArrayList<>();
259 arguments2.add(transaction.getAction());
260 arguments2.add(transaction.getActionLevel());
261 arguments2.add(vnfType);
262 log.debug(fn + QUERY_STR + protocolQuery);
263 status = dblib.query(protocolQuery, context, arguments2);
264 if (status.toString().equals(FAILURE_PARAM)) {
265 throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
272 public String getDependencyInfo(SvcLogicContext localContext) throws SvcLogicException {
273 String fn = "DBService.getDependencyInfo ";
275 if (dblib != null && localContext != null) {
277 "select max(internal_version) as maxInternalVersion, artifact_name as artifactName from "
278 + FlowControllerConstants.DB_SDC_ARTIFACTS + " where artifact_name in (select artifact_name from "
279 + FlowControllerConstants.DB_SDC_REFERENCE + " where vnf_type= $"
280 + FlowControllerConstants.VNF_TYPE
281 + " and file_category = '" + FlowControllerConstants.DEPENDENCYMODEL + "' )";
283 log.debug(fn + QUERY_STR + queryString);
284 status = dblib.query(queryString, localContext);
286 if (status.toString().equals(FAILURE_PARAM)) {
287 throw new SvcLogicException("Error - while getting dependencydata ");
290 String queryString1 = "select artifact_content from " + FlowControllerConstants.DB_SDC_ARTIFACTS
291 + " where artifact_name = $artifactName and internal_version = $maxInternalVersion ";
293 log.debug(fn + QUERY_STR + queryString1);
294 status = dblib.query(queryString1, localContext);
295 if (status.toString().equals(FAILURE_PARAM)) {
296 throw new SvcLogicException("Error - while getting dependencyData ");
300 return localContext != null ? localContext.getAttribute(ARTIFACT_CONTENT_PARAM) : null;
304 public String getCapabilitiesData(SvcLogicContext localContext) throws SvcLogicException {
305 String fn = "DBService.getCapabilitiesData ";
307 if (dblib != null && localContext != null) {
309 "select max(internal_version) as maxInternalVersion, artifact_name as artifactName from "
310 + FlowControllerConstants.DB_SDC_ARTIFACTS + " where artifact_name in (select artifact_name from "
311 + FlowControllerConstants.DB_SDC_REFERENCE + " where vnf_type= $"
312 + FlowControllerConstants.VNF_TYPE
313 + " and file_category = '" + FlowControllerConstants.CAPABILITY + "' )";
315 log.info(fn + QUERY_STR + queryString);
316 status = dblib.query(queryString, localContext);
318 if (status.toString().equals(FAILURE_PARAM)) {
319 throw new SvcLogicException("Error - while getting capabilitiesData ");
322 String queryString1 = "select artifact_content from " + FlowControllerConstants.DB_SDC_ARTIFACTS
323 + " where artifact_name = $artifactName and internal_version = $maxInternalVersion ";
325 log.debug(fn + QUERY_STR + queryString1);
326 status = dblib.query(queryString1, localContext);
327 if (status.toString().equals(FAILURE_PARAM)) {
328 throw new SvcLogicException("Error - while getting capabilitiesData ");
331 return localContext != null ? localContext.getAttribute(ARTIFACT_CONTENT_PARAM) : null;
334 protected SvcLogicContext getSvcLogicContext() {
335 return new SvcLogicContext();