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;
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.core.sli.SvcLogicContext;
31 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
34 import org.onap.ccsdk.sli.adaptors.resource.sql.SqlResource;
36 import com.att.eelf.configuration.EELFLogger;
37 import com.att.eelf.configuration.EELFManager;
39 public class FlowControlDBService {
41 private static final EELFLogger log = EELFManager.getInstance().getLogger(FlowControlDBService.class);
42 private SvcLogicResource serviceLogic;
43 private static FlowControlDBService dgGeneralDBService = null;
45 public static FlowControlDBService initialise() {
46 if (dgGeneralDBService == null) {
47 dgGeneralDBService = new FlowControlDBService();
49 return dgGeneralDBService;
52 private FlowControlDBService() {
53 if (serviceLogic == null) {
54 serviceLogic = new SqlResource();
58 public void getFlowReferenceData(SvcLogicContext ctx, Map<String, String> inParams, SvcLogicContext localContext)
59 throws SvcLogicException {
61 String fn = "DBService.getflowModelInfo";
62 String whereClause = " where ACTION = $" + FlowControllerConstants.REQUEST_ACTION;
64 if (StringUtils.isNotBlank(ctx.getAttribute(FlowControllerConstants.VNF_TYPE)))
65 whereClause = whereClause.concat(" and VNF_TYPE = $" + FlowControllerConstants.VNF_TYPE);
66 if (StringUtils.isNotBlank(ctx.getAttribute(FlowControllerConstants.ACTION_LEVEL)))
67 whereClause = whereClause.concat(" and ACTION_LEVEL = $" + FlowControllerConstants.ACTION_LEVEL);
69 QueryStatus status = null;
70 if (serviceLogic != null& localContext != null) {
71 String key = "select SEQUENCE_TYPE, CATEGORY, GENERATION_NODE, EXECUTION_NODE from "
72 + FlowControllerConstants.DB_MULTISTEP_FLOW_REFERENCE + whereClause;
73 log.debug(fn + "Query String : " + key);
74 status = serviceLogic.query("SQL", false, null, key, null, null, localContext);
75 if (status.toString().equals("FAILURE"))
76 throw new SvcLogicException("Error - while getting FlowReferenceData ");
80 public String getEndPointByAction(String action) {
84 public String getDesignTimeFlowModel(SvcLogicContext localContext) throws SvcLogicException {
85 String fn = "DBService.getDesignTimeFlowModel ";
86 QueryStatus status = null;
87 if (serviceLogic != null& localContext != null) {
88 String queryString = "select max(internal_version) as maxInternalVersion, artifact_name as artifactName from "
89 + FlowControllerConstants.DB_SDC_ARTIFACTS + " where artifact_name in (select artifact_name from "
90 + FlowControllerConstants.DB_SDC_REFERENCE + " where vnf_type= $" + FlowControllerConstants.VNF_TYPE
91 + " and vnfc_type = $" + FlowControllerConstants.VNFC_TYPE + " and action = $"
92 + FlowControllerConstants.REQUEST_ACTION + " and file_category = $"
93 + FlowControllerConstants.CATEGORY + " )";
95 log.debug(fn + "Query String : " + queryString);
96 status = serviceLogic.query("SQL", false, null, queryString, null, null, localContext);
98 if (status.toString().equals("FAILURE"))
99 throw new SvcLogicException("Error - while getting FlowReferenceData ");
101 String queryString1 = "select artifact_content from " + FlowControllerConstants.DB_SDC_ARTIFACTS
102 + " where artifact_name = $artifactName and internal_version = $maxInternalVersion ";
104 log.debug(fn + "Query String : " + queryString1);
105 status = serviceLogic.query("SQL", false, null, queryString1, null, null, localContext);
106 if (status.toString().equals("FAILURE"))
107 throw new SvcLogicException("Error - while getting FlowReferenceData ");
109 return localContext != null ? localContext.getAttribute("artifact-content") : null;
112 public QueryStatus loadSequenceIntoDB(SvcLogicContext localContext) throws SvcLogicException {
114 QueryStatus status = null;
116 if (localContext != null) {
117 String fn = "DBService.saveArtifacts";
119 localContext.setAttribute(FlowControllerConstants.ARTIFACT_CONTENT_ESCAPED,
120 EscapeUtils.escapeSql(localContext.getAttribute(FlowControllerConstants.ARTIFACT_CONTENT)));
121 log.debug("ESCAPED sequence for DB : "
122 + localContext.getAttribute(FlowControllerConstants.ARTIFACT_CONTENT_ESCAPED));
124 for (Object key : localContext.getAttributeKeySet()) {
125 String parmName = (String) key;
126 String parmValue = localContext.getAttribute(parmName);
127 log.debug(" loadSequenceIntoDB " + parmName + "=" + parmValue);
130 String queryString = "INSERT INTO " + FlowControllerConstants.DB_REQUEST_ARTIFACTS + " set request_id = $"
131 + FlowControllerConstants.REQUEST_ID + " , action = $" + FlowControllerConstants.REQUEST_ACTION
132 + " , action_level = $" + FlowControllerConstants.ACTION_LEVEL + " , vnf_type = $"
133 + FlowControllerConstants.VNF_TYPE + " , category = $" + FlowControllerConstants.CATEGORY
134 + " , artifact_content = $" + FlowControllerConstants.ARTIFACT_CONTENT_ESCAPED
135 + " , updated_date = sysdate() ";
137 log.debug(fn + "Query String : " + queryString);
138 status = serviceLogic.save("SQL", false, false, queryString, null, null, localContext);
139 if (status.toString().equals("FAILURE"))
140 throw new SvcLogicException("Error While processing storing Artifact: "
141 + localContext.getAttribute(FlowControllerConstants.ARTIFACT_NAME));
147 public void populateModuleAndRPC(Transaction transaction, String vnf_type) throws Exception {
148 String fn = "FlowControlDBService.populateModuleAndRPC ";
149 QueryStatus status = null;
150 SvcLogicContext context = new SvcLogicContext();
151 String protocolType = null;
153 protocolType = getProtocolType(transaction, vnf_type, fn, context, protocolType);
155 String key = "select execution_type, execution_module, execution_rpc from "
156 + FlowControllerConstants.DB_PROCESS_FLOW_REFERENCE + " where action = '" + transaction.getAction()
157 + "'" + " and action_level = '" + transaction.getActionLevel() + "'" + " and protocol = '"
158 + protocolType + "'";
160 log.debug(fn + "Query String : " + key);
161 status = serviceLogic.query("SQL", false, null, key, null, null, context);
162 if (status.toString().equals("FAILURE"))
163 throw new SvcLogicException("Error - while getting FlowReferenceData ");
165 transaction.setExecutionModule(context.getAttribute(FlowControllerConstants.EXECUTTION_MODULE));
166 transaction.setExecutionRPC(context.getAttribute(FlowControllerConstants.EXECUTION_RPC));
167 transaction.setExecutionType(context.getAttribute(FlowControllerConstants.EXECUTION_TYPE));
171 private String getProtocolType(Transaction transaction, String vnf_type, String fn, SvcLogicContext context,
172 String protocolType) throws Exception {
174 String protocolQuery;
176 protocolQuery = "select count(protocol) from " + FlowControllerConstants.DB_PROTOCOL_REFERENCE
177 + " where action = '" + transaction.getAction() + "'" + " and action_level = '"
178 + transaction.getActionLevel() + "'";
180 log.debug(fn + "Query String : " + protocolQuery);
181 status = serviceLogic.query("SQL", false, null, protocolQuery, null, null, context);
182 if (status.toString().equals("FAILURE"))
183 throw new SvcLogicException("Error - while getting FlowReferenceData ");
185 log.debug(" Protocol Count " + context.getAttribute("count(protocol)"));
186 protocolCount = Integer.parseInt(context.getAttribute("count(protocol)"));
188 if (protocolCount == 1) {
189 protocolQuery = "select protocol from " + FlowControllerConstants.DB_PROTOCOL_REFERENCE
190 + " where action = '" + transaction.getAction() + "'" + " and action_level = '"
191 + transaction.getActionLevel() + "'";
193 log.debug(fn + "Query String : " + protocolQuery);
194 status = serviceLogic.query("SQL", false, null, protocolQuery, null, null, context);
195 if (status.toString().equals("FAILURE"))
196 throw new SvcLogicException("Error - while getting FlowReferenceData ");
197 protocolType = context.getAttribute("protocol");
200 protocolQuery = "select count(protocol) from " + FlowControllerConstants.DB_PROTOCOL_REFERENCE
201 + " where action = '" + transaction.getAction() + "'" + " and action_level = '"
202 + transaction.getActionLevel() + "'" + " and vnf_type = '" + vnf_type + "'";
204 log.debug(fn + "Query String : " + protocolQuery);
205 status = serviceLogic.query("SQL", false, null, protocolQuery, null, null, context);
206 if (status.toString().equals("FAILURE"))
207 throw new SvcLogicException("Error - while getting FlowReferenceData ");
209 log.debug(" Protocol Count " + context.getAttribute("count(protocol)"));
210 protocolCount = Integer.parseInt(context.getAttribute("count(protocol)"));
211 if(protocolCount > 1){
212 throw new Exception("Got more than 2 values..");
213 }else if(protocolCount == 1){
214 protocolQuery = "select protocol from " + FlowControllerConstants.DB_PROTOCOL_REFERENCE
215 + " where action = '" + transaction.getAction() + "'" + " and action_level = '"
216 + transaction.getActionLevel() + "'"+ " and vnf_type = '" + vnf_type + "'";
217 log.debug(fn + "Query String : " + protocolQuery);
218 status = serviceLogic.query("SQL", false, null, protocolQuery, null, null, context);
219 if (status.toString().equals("FAILURE"))
220 throw new SvcLogicException("Error - while getting FlowReferenceData ");
221 protocolType = context.getAttribute("protocol");
227 public String getDependencyInfo(SvcLogicContext localContext) throws SvcLogicException {
228 String fn = "DBService.getDependencyInfo ";
229 QueryStatus status = null;
230 if (serviceLogic != null& localContext != null) {
231 String queryString = "select max(internal_version) as maxInternalVersion, artifact_name as artifactName from "
232 + FlowControllerConstants.DB_SDC_ARTIFACTS + " where artifact_name in (select artifact_name from "
233 + FlowControllerConstants.DB_SDC_REFERENCE + " where vnf_type= $" + FlowControllerConstants.VNF_TYPE
234 + " and file_category = '" + FlowControllerConstants.DEPENDENCYMODEL + "' )";
236 log.debug(fn + "Query String : " + queryString);
237 status = serviceLogic.query("SQL", false, null, queryString, null, null, localContext);
239 if (status.toString().equals("FAILURE"))
240 throw new SvcLogicException("Error - while getting dependencydata ");
242 String queryString1 = "select artifact_content from " + FlowControllerConstants.DB_SDC_ARTIFACTS
243 + " where artifact_name = $artifactName and internal_version = $maxInternalVersion ";
245 log.debug(fn + "Query String : " + queryString1);
246 status = serviceLogic.query("SQL", false, null, queryString1, null, null, localContext);
247 if (status.toString().equals("FAILURE"))
248 throw new SvcLogicException("Error - while getting dependencyData ");
251 return localContext != null ? localContext.getAttribute("artifact-content") : null;
255 public String getCapabilitiesData(SvcLogicContext localContext) throws SvcLogicException {
256 String fn = "DBService.getCapabilitiesData ";
257 QueryStatus status = null;
258 if (serviceLogic != null& localContext != null) {
259 String queryString = "select max(internal_version) as maxInternalVersion, artifact_name as artifactName from "
260 + FlowControllerConstants.DB_SDC_ARTIFACTS + " where artifact_name in (select artifact_name from "
261 + FlowControllerConstants.DB_SDC_REFERENCE + " where vnf_type= $" + FlowControllerConstants.VNF_TYPE
262 + " and file_category = '" + FlowControllerConstants.CAPABILITY + "' )";
264 log.info(fn + "Query String : " + queryString);
265 status = serviceLogic.query("SQL", false, null, queryString, null, null, localContext);
267 if (status.toString().equals("FAILURE"))
268 throw new SvcLogicException("Error - while getting capabilitiesData ");
270 String queryString1 = "select artifact_content from " + FlowControllerConstants.DB_SDC_ARTIFACTS
271 + " where artifact_name = $artifactName and internal_version = $maxInternalVersion ";
273 log.debug(fn + "Query String : " + queryString1);
274 status = serviceLogic.query("SQL", false, null, queryString1, null, null, localContext);
275 if (status.toString().equals("FAILURE"))
276 throw new SvcLogicException("Error - while getting capabilitiesData ");
278 return localContext != null ? localContext.getAttribute("artifact-content") : null;