2 * ============LICENSE_START=======================================================
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
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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
20 * ============LICENSE_END=========================================================
22 package org.onap.appc.flow.controller.dbervices;
24 import com.att.eelf.configuration.EELFLogger;
25 import com.att.eelf.configuration.EELFManager;
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;
37 public class FlowControlDBService {
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 = '";
53 private SvcLogicResource serviceLogic;
54 private static FlowControlDBService dgGeneralDBService = null;
56 private FlowControlDBService() {
57 if (serviceLogic == null) {
58 serviceLogic = new SqlResource();
62 public static FlowControlDBService initialise() {
63 if (dgGeneralDBService == null) {
64 dgGeneralDBService = new FlowControlDBService();
66 return dgGeneralDBService;
69 public void getFlowReferenceData(SvcLogicContext ctx, Map<String, String> inParams, SvcLogicContext localContext)
70 throws SvcLogicException {
72 String fn = "DBService.getflowModelInfo";
73 String whereClause = " where ACTION = $" + FlowControllerConstants.REQUEST_ACTION;
75 if (StringUtils.isNotBlank(ctx.getAttribute(FlowControllerConstants.VNF_TYPE))) {
76 whereClause = whereClause.concat(" and VNF_TYPE = $" + FlowControllerConstants.VNF_TYPE);
78 if (StringUtils.isNotBlank(ctx.getAttribute(FlowControllerConstants.ACTION_LEVEL))) {
79 whereClause = whereClause.concat(" and ACTION_LEVEL = $" + FlowControllerConstants.ACTION_LEVEL);
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);
94 public String getEndPointByAction(String action) {
98 public String getDesignTimeFlowModel(SvcLogicContext localContext) throws SvcLogicException {
99 String fn = "DBService.getDesignTimeFlowModel ";
101 if (serviceLogic != null && localContext != null) {
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 + " )";
111 log.debug(fn + QUERY_STR + queryString);
112 status = serviceLogic.query("SQL", false, null, queryString, null, null, localContext);
114 if (status.toString().equals(FAILURE_PARAM)) {
115 throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
118 String queryString1 = SELECT_ART_CONTENT_QUERY_STR + FlowControllerConstants.DB_SDC_ARTIFACTS
119 + WHERE_ARTIFACT_NAME_QUERY_STR;
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);
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 = 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));
165 public void populateModuleAndRPC(Transaction transaction, String vnfType) throws SvcLogicException {
166 String fn = "FlowControlDBService.populateModuleAndRPC ";
168 SvcLogicContext context = new SvcLogicContext();
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_QUERY_STR + transaction.getAction()
173 + "'" + AND_ACTION_LEVEL_QUERY_STR + transaction.getActionLevel() + "'" + " and protocol = '"
174 + protocolType + "'";
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);
182 transaction.setExecutionModule(context.getAttribute(FlowControllerConstants.EXECUTTION_MODULE));
183 transaction.setExecutionRPC(context.getAttribute(FlowControllerConstants.EXECUTION_RPC));
184 transaction.setExecutionType(context.getAttribute(FlowControllerConstants.EXECUTION_TYPE));
188 private String getProtocolType(Transaction transaction, String vnfType, String fn, SvcLogicContext context)
189 throws SvcLogicException {
191 String protocolQuery;
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() + "'";
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);
203 log.debug(" Protocol Count " + context.getAttribute(COUNT_PROTOCOL_PARAM));
204 protocolCount = Integer.parseInt(context.getAttribute(COUNT_PROTOCOL_PARAM));
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() + "'";
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);
216 return context.getAttribute("protocol");
218 if (hasSingleProtocol(transaction, vnfType, fn, context)) {
219 return context.getAttribute("protocol");
225 private boolean hasSingleProtocol(Transaction transaction, String vnfType, String fn, SvcLogicContext context)
226 throws SvcLogicException {
227 String protocolQuery;
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 + "'";
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);
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);
258 public String getDependencyInfo(SvcLogicContext localContext) throws SvcLogicException {
259 String fn = "DBService.getDependencyInfo ";
261 if (serviceLogic != null && localContext != null) {
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 + "' )";
269 log.debug(fn + QUERY_STR + queryString);
270 status = serviceLogic.query("SQL", false, null, queryString, null, null, localContext);
272 if (status.toString().equals(FAILURE_PARAM)) {
273 throw new SvcLogicException("Error - while getting dependencydata ");
276 String queryString1 = SELECT_ART_CONTENT_QUERY_STR + FlowControllerConstants.DB_SDC_ARTIFACTS
277 + WHERE_ARTIFACT_NAME_QUERY_STR;
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 ");
286 return localContext != null ? localContext.getAttribute(ARTIFACT_CONTENT_PARAM) : null;
290 public String getCapabilitiesData(SvcLogicContext localContext) throws SvcLogicException {
291 String fn = "DBService.getCapabilitiesData ";
293 if (serviceLogic != null && localContext != null) {
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 + "' )";
301 log.info(fn + QUERY_STR + queryString);
302 status = serviceLogic.query("SQL", false, null, queryString, null, null, localContext);
304 if (status.toString().equals(FAILURE_PARAM)) {
305 throw new SvcLogicException("Error - while getting capabilitiesData ");
308 String queryString1 = SELECT_ART_CONTENT_QUERY_STR + FlowControllerConstants.DB_SDC_ARTIFACTS
309 + WHERE_ARTIFACT_NAME_QUERY_STR;
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 ");
317 return localContext != null ? localContext.getAttribute(ARTIFACT_CONTENT_PARAM) : null;