2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * ============LICENSE_END=========================================================
21 package org.onap.appc.flow.controller.dbervices;
23 import com.att.eelf.configuration.EELFLogger;
24 import com.att.eelf.configuration.EELFManager;
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;
36 public class FlowControlDBService {
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 = '";
52 private SvcLogicResource serviceLogic;
53 private static FlowControlDBService dgGeneralDBService = null;
55 private FlowControlDBService() {
56 if (serviceLogic == null) {
57 serviceLogic = new SqlResource();
61 protected FlowControlDBService(SqlResource svcLogic) {
62 serviceLogic = svcLogic;
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.VNF_TYPE))) {
79 whereClause = whereClause.concat(" and VNF_TYPE = $" + FlowControllerConstants.VNF_TYPE);
81 if (StringUtils.isNotBlank(ctx.getAttribute(FlowControllerConstants.ACTION_LEVEL))) {
82 whereClause = whereClause.concat(" and ACTION_LEVEL = $" + FlowControllerConstants.ACTION_LEVEL);
86 if (serviceLogic != null && localContext != null) {
87 String key = "select SEQUENCE_TYPE, CATEGORY, GENERATION_NODE, EXECUTION_NODE from "
88 + FlowControllerConstants.DB_MULTISTEP_FLOW_REFERENCE + whereClause;
89 log.debug(fn + QUERY_STR + key);
90 status = serviceLogic.query("SQL", false, null, key, null, null, localContext);
91 if (status.toString().equals(FAILURE_PARAM)) {
92 throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
97 public String getEndPointByAction(String action) {
101 public String getDesignTimeFlowModel(SvcLogicContext localContext) throws SvcLogicException {
102 String fn = "DBService.getDesignTimeFlowModel ";
104 if (serviceLogic != null && localContext != null) {
107 + FlowControllerConstants.DB_SDC_ARTIFACTS + WHERE_ART_NAME_QUERY_STR
108 + FlowControllerConstants.DB_SDC_REFERENCE + WHERE_VNF_TYPE_QUERY_STR
109 + FlowControllerConstants.VNF_TYPE
110 + " and vnfc_type = $" + FlowControllerConstants.VNFC_TYPE + " and action = $"
111 + FlowControllerConstants.REQUEST_ACTION + " and file_category = $"
112 + FlowControllerConstants.CATEGORY + " )";
114 log.debug(fn + QUERY_STR + queryString);
115 status = serviceLogic.query("SQL", false, null, queryString, null, null, localContext);
117 if (status.toString().equals(FAILURE_PARAM)) {
118 throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
121 String queryString1 = SELECT_ART_CONTENT_QUERY_STR + FlowControllerConstants.DB_SDC_ARTIFACTS
122 + WHERE_ARTIFACT_NAME_QUERY_STR;
124 log.debug(fn + QUERY_STR + queryString1);
125 status = serviceLogic.query("SQL", false, null, queryString1, null, null, localContext);
126 if (status.toString().equals(FAILURE_PARAM)) {
127 throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
130 return localContext != null ? localContext.getAttribute(ARTIFACT_CONTENT_PARAM) : null;
133 public QueryStatus loadSequenceIntoDB(SvcLogicContext localContext) throws SvcLogicException {
135 QueryStatus status = null;
136 if (localContext != null) {
137 String fn = "DBService.saveArtifacts";
139 localContext.setAttribute(FlowControllerConstants.ARTIFACT_CONTENT_ESCAPED,
140 EscapeUtils.escapeSql(localContext.getAttribute(FlowControllerConstants.ARTIFACT_CONTENT)));
141 log.debug("ESCAPED sequence for DB : "
142 + localContext.getAttribute(FlowControllerConstants.ARTIFACT_CONTENT_ESCAPED));
144 for (Object key : localContext.getAttributeKeySet()) {
145 String parmName = (String) key;
146 String parmValue = localContext.getAttribute(parmName);
147 log.debug(" loadSequenceIntoDB " + parmName + "=" + parmValue);
150 String queryString = "INSERT INTO " + FlowControllerConstants.DB_REQUEST_ARTIFACTS + " set request_id = $"
151 + FlowControllerConstants.REQUEST_ID + " , action = $" + FlowControllerConstants.REQUEST_ACTION
152 + " , action_level = $" + FlowControllerConstants.ACTION_LEVEL + " , vnf_type = $"
153 + FlowControllerConstants.VNF_TYPE + " , category = $" + FlowControllerConstants.CATEGORY
154 + " , artifact_content = $" + FlowControllerConstants.ARTIFACT_CONTENT_ESCAPED
155 + " , updated_date = sysdate() ";
157 log.debug(fn + QUERY_STR + queryString);
158 status = serviceLogic.save("SQL", false, false, queryString, null, null, localContext);
159 if (status.toString().equals(FAILURE_PARAM)) {
160 throw new SvcLogicException("Error While processing storing Artifact: "
161 + localContext.getAttribute(FlowControllerConstants.ARTIFACT_NAME));
168 public void populateModuleAndRPC(Transaction transaction, String vnfType) throws SvcLogicException {
169 String fn = "FlowControlDBService.populateModuleAndRPC ";
171 SvcLogicContext context = new SvcLogicContext();
172 String protocolType = getProtocolType(transaction, vnfType, fn, context);
174 String key = "select execution_type, execution_module, execution_rpc from "
175 + FlowControllerConstants.DB_PROCESS_FLOW_REFERENCE + WHERE_ACTION_QUERY_STR + transaction.getAction()
176 + "'" + AND_ACTION_LEVEL_QUERY_STR + transaction.getActionLevel() + "'" + " and protocol = '"
177 + protocolType + "'";
179 log.debug(fn + QUERY_STR + key);
180 status = serviceLogic.query("SQL", false, null, key, null, null, context);
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_QUERY_STR + transaction.getAction() + "'" + AND_ACTION_LEVEL_QUERY_STR
198 + transaction.getActionLevel() + "'";
200 log.debug(fn + QUERY_STR + protocolQuery);
201 status = serviceLogic.query("SQL", false, null, protocolQuery, null, null, context);
202 if (status.toString().equals(FAILURE_PARAM)) {
203 throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
206 log.debug(" Protocol Count " + context.getAttribute(COUNT_PROTOCOL_PARAM));
207 protocolCount = Integer.parseInt(context.getAttribute(COUNT_PROTOCOL_PARAM));
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() + "'";
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);
219 return context.getAttribute("protocol");
221 if (hasSingleProtocol(transaction, vnfType, fn, context)) {
222 return context.getAttribute("protocol");
228 private boolean hasSingleProtocol(Transaction transaction, String vnfType, String fn, SvcLogicContext context)
229 throws SvcLogicException {
230 String protocolQuery;
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 + "'";
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);
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);
261 public String getDependencyInfo(SvcLogicContext localContext) throws SvcLogicException {
262 String fn = "DBService.getDependencyInfo ";
264 if (serviceLogic != null && localContext != null) {
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 + "' )";
272 log.debug(fn + QUERY_STR + queryString);
273 status = serviceLogic.query("SQL", false, null, queryString, null, null, localContext);
275 if (status.toString().equals(FAILURE_PARAM)) {
276 throw new SvcLogicException("Error - while getting dependencydata ");
279 String queryString1 = SELECT_ART_CONTENT_QUERY_STR + FlowControllerConstants.DB_SDC_ARTIFACTS
280 + WHERE_ARTIFACT_NAME_QUERY_STR;
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 ");
289 return localContext != null ? localContext.getAttribute(ARTIFACT_CONTENT_PARAM) : null;
293 public String getCapabilitiesData(SvcLogicContext localContext) throws SvcLogicException {
294 String fn = "DBService.getCapabilitiesData ";
296 if (serviceLogic != null && localContext != null) {
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 + "' )";
304 log.info(fn + QUERY_STR + queryString);
305 status = serviceLogic.query("SQL", false, null, queryString, null, null, localContext);
307 if (status.toString().equals(FAILURE_PARAM)) {
308 throw new SvcLogicException("Error - while getting capabilitiesData ");
311 String queryString1 = SELECT_ART_CONTENT_QUERY_STR + FlowControllerConstants.DB_SDC_ARTIFACTS
312 + WHERE_ARTIFACT_NAME_QUERY_STR;
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 ");
320 return localContext != null ? localContext.getAttribute(ARTIFACT_CONTENT_PARAM) : null;