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 public static FlowControlDBService initialise() {
62 if (dgGeneralDBService == null) {
63 dgGeneralDBService = new FlowControlDBService();
65 return dgGeneralDBService;
68 public void getFlowReferenceData(SvcLogicContext ctx, Map<String, String> inParams, SvcLogicContext localContext)
69 throws SvcLogicException {
71 String fn = "DBService.getflowModelInfo";
72 String whereClause = " where ACTION = $" + FlowControllerConstants.REQUEST_ACTION;
74 if (StringUtils.isNotBlank(ctx.getAttribute(FlowControllerConstants.VNF_TYPE))) {
75 whereClause = whereClause.concat(" and VNF_TYPE = $" + FlowControllerConstants.VNF_TYPE);
77 if (StringUtils.isNotBlank(ctx.getAttribute(FlowControllerConstants.ACTION_LEVEL))) {
78 whereClause = whereClause.concat(" and ACTION_LEVEL = $" + FlowControllerConstants.ACTION_LEVEL);
82 if (serviceLogic != null && localContext != null) {
83 String key = "select SEQUENCE_TYPE, CATEGORY, GENERATION_NODE, EXECUTION_NODE from "
84 + FlowControllerConstants.DB_MULTISTEP_FLOW_REFERENCE + whereClause;
85 log.debug(fn + QUERY_STR + key);
86 status = serviceLogic.query("SQL", false, null, key, null, null, localContext);
87 if (status.toString().equals(FAILURE_PARAM)) {
88 throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
93 public String getEndPointByAction(String action) {
97 public String getDesignTimeFlowModel(SvcLogicContext localContext) throws SvcLogicException {
98 String fn = "DBService.getDesignTimeFlowModel ";
100 if (serviceLogic != null && localContext != null) {
103 + FlowControllerConstants.DB_SDC_ARTIFACTS + WHERE_ART_NAME_QUERY_STR
104 + FlowControllerConstants.DB_SDC_REFERENCE + WHERE_VNF_TYPE_QUERY_STR
105 + FlowControllerConstants.VNF_TYPE
106 + " and vnfc_type = $" + FlowControllerConstants.VNFC_TYPE + " and action = $"
107 + FlowControllerConstants.REQUEST_ACTION + " and file_category = $"
108 + FlowControllerConstants.CATEGORY + " )";
110 log.debug(fn + QUERY_STR + queryString);
111 status = serviceLogic.query("SQL", false, null, queryString, null, null, localContext);
113 if (status.toString().equals(FAILURE_PARAM)) {
114 throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
117 String queryString1 = SELECT_ART_CONTENT_QUERY_STR + FlowControllerConstants.DB_SDC_ARTIFACTS
118 + WHERE_ARTIFACT_NAME_QUERY_STR;
120 log.debug(fn + QUERY_STR + queryString1);
121 status = serviceLogic.query("SQL", false, null, queryString1, null, null, localContext);
122 if (status.toString().equals(FAILURE_PARAM)) {
123 throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
126 return localContext != null ? localContext.getAttribute(ARTIFACT_CONTENT_PARAM) : null;
129 public QueryStatus loadSequenceIntoDB(SvcLogicContext localContext) throws SvcLogicException {
131 QueryStatus status = null;
132 if (localContext != null) {
133 String fn = "DBService.saveArtifacts";
135 localContext.setAttribute(FlowControllerConstants.ARTIFACT_CONTENT_ESCAPED,
136 EscapeUtils.escapeSql(localContext.getAttribute(FlowControllerConstants.ARTIFACT_CONTENT)));
137 log.debug("ESCAPED sequence for DB : "
138 + localContext.getAttribute(FlowControllerConstants.ARTIFACT_CONTENT_ESCAPED));
140 for (Object key : localContext.getAttributeKeySet()) {
141 String parmName = (String) key;
142 String parmValue = localContext.getAttribute(parmName);
143 log.debug(" loadSequenceIntoDB " + parmName + "=" + parmValue);
146 String queryString = "INSERT INTO " + FlowControllerConstants.DB_REQUEST_ARTIFACTS + " set request_id = $"
147 + FlowControllerConstants.REQUEST_ID + " , action = $" + FlowControllerConstants.REQUEST_ACTION
148 + " , action_level = $" + FlowControllerConstants.ACTION_LEVEL + " , vnf_type = $"
149 + FlowControllerConstants.VNF_TYPE + " , category = $" + FlowControllerConstants.CATEGORY
150 + " , artifact_content = $" + FlowControllerConstants.ARTIFACT_CONTENT_ESCAPED
151 + " , updated_date = sysdate() ";
153 log.debug(fn + QUERY_STR + queryString);
154 status = serviceLogic.save("SQL", false, false, queryString, null, null, localContext);
155 if (status.toString().equals(FAILURE_PARAM)) {
156 throw new SvcLogicException("Error While processing storing Artifact: "
157 + localContext.getAttribute(FlowControllerConstants.ARTIFACT_NAME));
164 public void populateModuleAndRPC(Transaction transaction, String vnfType) throws SvcLogicException {
165 String fn = "FlowControlDBService.populateModuleAndRPC ";
167 SvcLogicContext context = new SvcLogicContext();
168 String protocolType = getProtocolType(transaction, vnfType, fn, context);
170 String key = "select execution_type, execution_module, execution_rpc from "
171 + FlowControllerConstants.DB_PROCESS_FLOW_REFERENCE + WHERE_ACTION_QUERY_STR + transaction.getAction()
172 + "'" + AND_ACTION_LEVEL_QUERY_STR + transaction.getActionLevel() + "'" + " and protocol = '"
173 + protocolType + "'";
175 log.debug(fn + QUERY_STR + key);
176 status = serviceLogic.query("SQL", false, null, key, null, null, context);
177 if (status.toString().equals(FAILURE_PARAM)) {
178 throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
181 transaction.setExecutionModule(context.getAttribute(FlowControllerConstants.EXECUTTION_MODULE));
182 transaction.setExecutionRPC(context.getAttribute(FlowControllerConstants.EXECUTION_RPC));
183 transaction.setExecutionType(context.getAttribute(FlowControllerConstants.EXECUTION_TYPE));
187 private String getProtocolType(Transaction transaction, String vnfType, String fn, SvcLogicContext context)
188 throws SvcLogicException {
190 String protocolQuery;
192 protocolQuery = "select count(protocol) from " + FlowControllerConstants.DB_PROTOCOL_REFERENCE
193 + WHERE_ACTION_QUERY_STR + transaction.getAction() + "'" + AND_ACTION_LEVEL_QUERY_STR
194 + transaction.getActionLevel() + "'";
196 log.debug(fn + QUERY_STR + protocolQuery);
197 status = serviceLogic.query("SQL", false, null, protocolQuery, null, null, context);
198 if (status.toString().equals(FAILURE_PARAM)) {
199 throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
202 log.debug(" Protocol Count " + context.getAttribute(COUNT_PROTOCOL_PARAM));
203 protocolCount = Integer.parseInt(context.getAttribute(COUNT_PROTOCOL_PARAM));
205 if (protocolCount == 1) {
206 protocolQuery = "select protocol from " + FlowControllerConstants.DB_PROTOCOL_REFERENCE
207 + WHERE_ACTION_QUERY_STR + transaction.getAction() + "'" + AND_ACTION_LEVEL_QUERY_STR
208 + transaction.getActionLevel() + "'";
210 log.debug(fn + QUERY_STR + protocolQuery);
211 status = serviceLogic.query("SQL", false, null, protocolQuery, null, null, context);
212 if (status.toString().equals(FAILURE_PARAM)) {
213 throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
215 return context.getAttribute("protocol");
217 if (hasSingleProtocol(transaction, vnfType, fn, context)) {
218 return context.getAttribute("protocol");
224 private boolean hasSingleProtocol(Transaction transaction, String vnfType, String fn, SvcLogicContext context)
225 throws SvcLogicException {
226 String protocolQuery;
229 protocolQuery = "select count(protocol) from " + FlowControllerConstants.DB_PROTOCOL_REFERENCE
230 + WHERE_ACTION_QUERY_STR + transaction.getAction() + "'" + AND_ACTION_LEVEL_QUERY_STR
231 + transaction.getActionLevel() + "'" + " and vnf_type = '" + vnfType + "'";
233 log.debug(fn + QUERY_STR + protocolQuery);
234 status = serviceLogic.query("SQL", false, null, protocolQuery, null, null, context);
235 if (status.toString().equals(FAILURE_PARAM)) {
236 throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
239 log.debug(" Protocol Count " + context.getAttribute(COUNT_PROTOCOL_PARAM));
240 protocolCount = Integer.parseInt(context.getAttribute(COUNT_PROTOCOL_PARAM));
241 if (protocolCount > 1) {
242 throw new SvcLogicException("Got more than 2 values..");
243 } else if (protocolCount == 1) {
244 protocolQuery = "select protocol from " + FlowControllerConstants.DB_PROTOCOL_REFERENCE
245 + WHERE_ACTION_QUERY_STR + transaction.getAction() + "'" + AND_ACTION_LEVEL_QUERY_STR
246 + transaction.getActionLevel() + "'" + " and vnf_type = '" + vnfType + "'";
247 log.debug(fn + QUERY_STR + protocolQuery);
248 status = serviceLogic.query("SQL", false, null, protocolQuery, null, null, context);
249 if (status.toString().equals(FAILURE_PARAM)) {
250 throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
257 public String getDependencyInfo(SvcLogicContext localContext) throws SvcLogicException {
258 String fn = "DBService.getDependencyInfo ";
260 if (serviceLogic != null && localContext != null) {
263 + FlowControllerConstants.DB_SDC_ARTIFACTS + WHERE_ART_NAME_QUERY_STR
264 + FlowControllerConstants.DB_SDC_REFERENCE + WHERE_VNF_TYPE_QUERY_STR
265 + FlowControllerConstants.VNF_TYPE
266 + " and file_category = '" + FlowControllerConstants.DEPENDENCYMODEL + "' )";
268 log.debug(fn + QUERY_STR + queryString);
269 status = serviceLogic.query("SQL", false, null, queryString, null, null, localContext);
271 if (status.toString().equals(FAILURE_PARAM)) {
272 throw new SvcLogicException("Error - while getting dependencydata ");
275 String queryString1 = SELECT_ART_CONTENT_QUERY_STR + FlowControllerConstants.DB_SDC_ARTIFACTS
276 + WHERE_ARTIFACT_NAME_QUERY_STR;
278 log.debug(fn + QUERY_STR + queryString1);
279 status = serviceLogic.query("SQL", false, null, queryString1, null, null, localContext);
280 if (status.toString().equals(FAILURE_PARAM)) {
281 throw new SvcLogicException("Error - while getting dependencyData ");
285 return localContext != null ? localContext.getAttribute(ARTIFACT_CONTENT_PARAM) : null;
289 public String getCapabilitiesData(SvcLogicContext localContext) throws SvcLogicException {
290 String fn = "DBService.getCapabilitiesData ";
292 if (serviceLogic != null && localContext != null) {
295 + FlowControllerConstants.DB_SDC_ARTIFACTS + WHERE_ART_NAME_QUERY_STR
296 + FlowControllerConstants.DB_SDC_REFERENCE + WHERE_VNF_TYPE_QUERY_STR
297 + FlowControllerConstants.VNF_TYPE
298 + " and file_category = '" + FlowControllerConstants.CAPABILITY + "' )";
300 log.info(fn + QUERY_STR + queryString);
301 status = serviceLogic.query("SQL", false, null, queryString, null, null, localContext);
303 if (status.toString().equals(FAILURE_PARAM)) {
304 throw new SvcLogicException("Error - while getting capabilitiesData ");
307 String queryString1 = SELECT_ART_CONTENT_QUERY_STR + FlowControllerConstants.DB_SDC_ARTIFACTS
308 + WHERE_ARTIFACT_NAME_QUERY_STR;
310 log.debug(fn + QUERY_STR + queryString1);
311 status = serviceLogic.query("SQL", false, null, queryString1, null, null, localContext);
312 if (status.toString().equals(FAILURE_PARAM)) {
313 throw new SvcLogicException("Error - while getting capabilitiesData ");
316 return localContext != null ? localContext.getAttribute(ARTIFACT_CONTENT_PARAM) : null;