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.
18 * ============LICENSE_END=========================================================
21 package org.onap.appc.flow.controller.dbervices;
25 import org.apache.commons.lang.StringUtils;
26 import org.onap.appc.flow.controller.data.Transaction;
27 import org.onap.appc.flow.controller.utils.EscapeUtils;
28 import org.onap.appc.flow.controller.utils.FlowControllerConstants;
29 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
30 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
31 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
33 import org.onap.ccsdk.sli.adaptors.resource.sql.SqlResource;
35 import com.att.eelf.configuration.EELFLogger;
36 import com.att.eelf.configuration.EELFManager;
38 public class FlowControlDBService {
40 private static final EELFLogger log = EELFManager.getInstance().getLogger(FlowControlDBService.class);
41 private SvcLogicResource serviceLogic;
42 private static FlowControlDBService dgGeneralDBService = null;
44 public static FlowControlDBService initialise() {
45 if (dgGeneralDBService == null) {
46 dgGeneralDBService = new FlowControlDBService();
48 return dgGeneralDBService;
51 private FlowControlDBService() {
52 if (serviceLogic == null) {
53 serviceLogic = new SqlResource();
57 public void getFlowReferenceData(SvcLogicContext ctx, Map<String, String> inParams, SvcLogicContext localContext)
58 throws SvcLogicException {
60 String fn = "DBService.getflowModelInfo";
61 String whereClause = " where ACTION = $" + FlowControllerConstants.REQUEST_ACTION;
63 if (StringUtils.isNotBlank(ctx.getAttribute(FlowControllerConstants.VNF_TYPE)))
64 whereClause = whereClause.concat(" and VNF_TYPE = $" + FlowControllerConstants.VNF_TYPE);
65 if (StringUtils.isNotBlank(ctx.getAttribute(FlowControllerConstants.ACTION_LEVEL)))
66 whereClause = whereClause.concat(" and ACTION_LEVEL = $" + FlowControllerConstants.ACTION_LEVEL);
68 QueryStatus status = null;
69 if (serviceLogic != null& localContext != null) {
70 String key = "select SEQUENCE_TYPE, CATEGORY, GENERATION_NODE, EXECUTION_NODE from "
71 + FlowControllerConstants.DB_MULTISTEP_FLOW_REFERENCE + whereClause;
72 log.debug(fn + "Query String : " + key);
73 status = serviceLogic.query("SQL", false, null, key, null, null, localContext);
74 if (status.toString().equals("FAILURE"))
75 throw new SvcLogicException("Error - while getting FlowReferenceData ");
79 public String getEndPointByAction(String action) {
83 public String getDesignTimeFlowModel(SvcLogicContext localContext) throws SvcLogicException {
84 String fn = "DBService.getDesignTimeFlowModel ";
85 QueryStatus status = null;
86 if (serviceLogic != null& localContext != null) {
87 String queryString = "select max(internal_version) as maxInternalVersion, artifact_name as artifactName from "
88 + FlowControllerConstants.DB_SDC_ARTIFACTS + " where artifact_name in (select artifact_name from "
89 + FlowControllerConstants.DB_SDC_REFERENCE + " where vnf_type= $" + FlowControllerConstants.VNF_TYPE
90 + " and vnfc_type = $" + FlowControllerConstants.VNFC_TYPE + " and action = $"
91 + FlowControllerConstants.REQUEST_ACTION + " and file_category = $"
92 + FlowControllerConstants.CATEGORY + " )";
94 log.debug(fn + "Query String : " + queryString);
95 status = serviceLogic.query("SQL", false, null, queryString, null, null, localContext);
97 if (status.toString().equals("FAILURE"))
98 throw new SvcLogicException("Error - while getting FlowReferenceData ");
100 String queryString1 = "select artifact_content from " + FlowControllerConstants.DB_SDC_ARTIFACTS
101 + " where artifact_name = $artifactName and internal_version = $maxInternalVersion ";
103 log.debug(fn + "Query String : " + queryString1);
104 status = serviceLogic.query("SQL", false, null, queryString1, null, null, localContext);
105 if (status.toString().equals("FAILURE"))
106 throw new SvcLogicException("Error - while getting FlowReferenceData ");
108 return localContext != null ? localContext.getAttribute("artifact-content") : null;
111 public QueryStatus loadSequenceIntoDB(SvcLogicContext localContext) throws SvcLogicException {
113 QueryStatus status = null;
115 if (localContext != null) {
116 String fn = "DBService.saveArtifacts";
118 localContext.setAttribute(FlowControllerConstants.ARTIFACT_CONTENT_ESCAPED,
119 EscapeUtils.escapeSql(localContext.getAttribute(FlowControllerConstants.ARTIFACT_CONTENT)));
120 log.debug("ESCAPED sequence for DB : "
121 + localContext.getAttribute(FlowControllerConstants.ARTIFACT_CONTENT_ESCAPED));
123 for (Object key : localContext.getAttributeKeySet()) {
124 String parmName = (String) key;
125 String parmValue = localContext.getAttribute(parmName);
126 log.debug(" loadSequenceIntoDB " + parmName + "=" + parmValue);
129 String queryString = "INSERT INTO " + FlowControllerConstants.DB_REQUEST_ARTIFACTS + " set request_id = $"
130 + FlowControllerConstants.REQUEST_ID + " , action = $" + FlowControllerConstants.REQUEST_ACTION
131 + " , action_level = $" + FlowControllerConstants.ACTION_LEVEL + " , vnf_type = $"
132 + FlowControllerConstants.VNF_TYPE + " , category = $" + FlowControllerConstants.CATEGORY
133 + " , artifact_content = $" + FlowControllerConstants.ARTIFACT_CONTENT_ESCAPED
134 + " , updated_date = sysdate() ";
136 log.debug(fn + "Query String : " + queryString);
137 status = serviceLogic.save("SQL", false, false, queryString, null, null, localContext);
138 if (status.toString().equals("FAILURE"))
139 throw new SvcLogicException("Error While processing storing Artifact: "
140 + localContext.getAttribute(FlowControllerConstants.ARTIFACT_NAME));
146 public void populateModuleAndRPC(Transaction transaction, String vnf_type) throws Exception {
147 String fn = "FlowControlDBService.populateModuleAndRPC ";
148 QueryStatus status = null;
149 SvcLogicContext context = new SvcLogicContext();
150 String protocolType = null;
152 protocolType = getProtocolType(transaction, vnf_type, fn, context, protocolType);
154 String key = "select execution_type, execution_module, execution_rpc from "
155 + FlowControllerConstants.DB_PROCESS_FLOW_REFERENCE + " where action = '" + transaction.getAction()
156 + "'" + " and action_level = '" + transaction.getActionLevel() + "'" + " and protocol = '"
157 + protocolType + "'";
159 log.debug(fn + "Query String : " + key);
160 status = serviceLogic.query("SQL", false, null, key, null, null, context);
161 if (status.toString().equals("FAILURE"))
162 throw new SvcLogicException("Error - while getting FlowReferenceData ");
164 transaction.setExecutionModule(context.getAttribute(FlowControllerConstants.EXECUTTION_MODULE));
165 transaction.setExecutionRPC(context.getAttribute(FlowControllerConstants.EXECUTION_RPC));
166 transaction.setExecutionType(context.getAttribute(FlowControllerConstants.EXECUTION_TYPE));
170 private String getProtocolType(Transaction transaction, String vnf_type, String fn, SvcLogicContext context,
171 String protocolType) throws Exception {
173 String protocolQuery;
175 protocolQuery = "select count(protocol) from " + FlowControllerConstants.DB_PROTOCOL_REFERENCE
176 + " where action = '" + transaction.getAction() + "'" + " and action_level = '"
177 + transaction.getActionLevel() + "'";
179 log.debug(fn + "Query String : " + protocolQuery);
180 status = serviceLogic.query("SQL", false, null, protocolQuery, null, null, context);
181 if (status.toString().equals("FAILURE"))
182 throw new SvcLogicException("Error - while getting FlowReferenceData ");
184 log.debug(" Protocol Count " + context.getAttribute("count(protocol)"));
185 protocolCount = Integer.parseInt(context.getAttribute("count(protocol)"));
187 if (protocolCount == 1) {
188 protocolQuery = "select protocol from " + FlowControllerConstants.DB_PROTOCOL_REFERENCE
189 + " where action = '" + transaction.getAction() + "'" + " and action_level = '"
190 + transaction.getActionLevel() + "'";
192 log.debug(fn + "Query String : " + protocolQuery);
193 status = serviceLogic.query("SQL", false, null, protocolQuery, null, null, context);
194 if (status.toString().equals("FAILURE"))
195 throw new SvcLogicException("Error - while getting FlowReferenceData ");
196 protocolType = context.getAttribute("protocol");
199 protocolQuery = "select count(protocol) from " + FlowControllerConstants.DB_PROTOCOL_REFERENCE
200 + " where action = '" + transaction.getAction() + "'" + " and action_level = '"
201 + transaction.getActionLevel() + "'" + " and vnf_type = '" + vnf_type + "'";
203 log.debug(fn + "Query String : " + protocolQuery);
204 status = serviceLogic.query("SQL", false, null, protocolQuery, null, null, context);
205 if (status.toString().equals("FAILURE"))
206 throw new SvcLogicException("Error - while getting FlowReferenceData ");
208 log.debug(" Protocol Count " + context.getAttribute("count(protocol)"));
209 protocolCount = Integer.parseInt(context.getAttribute("count(protocol)"));
210 if(protocolCount > 1){
211 throw new Exception("Got more than 2 values..");
212 }else if(protocolCount == 1){
213 protocolQuery = "select protocol from " + FlowControllerConstants.DB_PROTOCOL_REFERENCE
214 + " where action = '" + transaction.getAction() + "'" + " and action_level = '"
215 + transaction.getActionLevel() + "'"+ " and vnf_type = '" + vnf_type + "'";
216 log.debug(fn + "Query String : " + protocolQuery);
217 status = serviceLogic.query("SQL", false, null, protocolQuery, null, null, context);
218 if (status.toString().equals("FAILURE"))
219 throw new SvcLogicException("Error - while getting FlowReferenceData ");
220 protocolType = context.getAttribute("protocol");
226 public String getDependencyInfo(SvcLogicContext localContext) throws SvcLogicException {
227 String fn = "DBService.getDependencyInfo ";
228 QueryStatus status = null;
229 if (serviceLogic != null& localContext != null) {
230 String queryString = "select max(internal_version) as maxInternalVersion, artifact_name as artifactName from "
231 + FlowControllerConstants.DB_SDC_ARTIFACTS + " where artifact_name in (select artifact_name from "
232 + FlowControllerConstants.DB_SDC_REFERENCE + " where vnf_type= $" + FlowControllerConstants.VNF_TYPE
233 + " and file_category = '" + FlowControllerConstants.DEPENDENCYMODEL + "' )";
235 log.debug(fn + "Query String : " + queryString);
236 status = serviceLogic.query("SQL", false, null, queryString, null, null, localContext);
238 if (status.toString().equals("FAILURE"))
239 throw new SvcLogicException("Error - while getting dependencydata ");
241 String queryString1 = "select artifact_content from " + FlowControllerConstants.DB_SDC_ARTIFACTS
242 + " where artifact_name = $artifactName and internal_version = $maxInternalVersion ";
244 log.debug(fn + "Query String : " + queryString1);
245 status = serviceLogic.query("SQL", false, null, queryString1, null, null, localContext);
246 if (status.toString().equals("FAILURE"))
247 throw new SvcLogicException("Error - while getting dependencyData ");
250 return localContext != null ? localContext.getAttribute("artifact-content") : null;
254 public String getCapabilitiesData(SvcLogicContext localContext) throws SvcLogicException {
255 String fn = "DBService.getCapabilitiesData ";
256 QueryStatus status = null;
257 if (serviceLogic != null& localContext != null) {
258 String queryString = "select max(internal_version) as maxInternalVersion, artifact_name as artifactName from "
259 + FlowControllerConstants.DB_SDC_ARTIFACTS + " where artifact_name in (select artifact_name from "
260 + FlowControllerConstants.DB_SDC_REFERENCE + " where vnf_type= $" + FlowControllerConstants.VNF_TYPE
261 + " and file_category = '" + FlowControllerConstants.CAPABILITY + "' )";
263 log.info(fn + "Query String : " + queryString);
264 status = serviceLogic.query("SQL", false, null, queryString, null, null, localContext);
266 if (status.toString().equals("FAILURE"))
267 throw new SvcLogicException("Error - while getting capabilitiesData ");
269 String queryString1 = "select artifact_content from " + FlowControllerConstants.DB_SDC_ARTIFACTS
270 + " where artifact_name = $artifactName and internal_version = $maxInternalVersion ";
272 log.debug(fn + "Query String : " + queryString1);
273 status = serviceLogic.query("SQL", false, null, queryString1, null, null, localContext);
274 if (status.toString().equals("FAILURE"))
275 throw new SvcLogicException("Error - while getting capabilitiesData ");
277 return localContext != null ? localContext.getAttribute("artifact-content") : null;