Changed to unmaintained
[appc.git] / appc-config / appc-flow-controller / provider / src / main / java / org / onap / appc / flow / controller / dbervices / FlowControlDBService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (C) 2019 Ericsson
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * ============LICENSE_END=========================================================
22  */
23 package org.onap.appc.flow.controller.dbervices;
24
25 import com.att.eelf.configuration.EELFLogger;
26 import com.att.eelf.configuration.EELFManager;
27
28 import java.util.ArrayList;
29 import java.util.Map;
30 import org.apache.commons.lang.StringUtils;
31 import org.onap.appc.flow.controller.data.Transaction;
32 import org.onap.appc.flow.controller.utils.EscapeUtils;
33 import org.onap.appc.flow.controller.utils.FlowControllerConstants;
34 import org.onap.ccsdk.sli.core.dblib.DbLibService;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
37 import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
38
39 public class FlowControlDBService {
40
41     private static final EELFLogger log = EELFManager.getInstance().getLogger(FlowControlDBService.class);
42     private static final String QUERY_STR = "Query String : ";
43     private static final String FAILURE_PARAM = "FAILURE";
44     protected static final String GET_FLOW_REF_DATA_ERROR = "Error - while getting FlowReferenceData ";
45     private static final String ARTIFACT_CONTENT_PARAM = "artifact-content";
46     protected static final String COUNT_PROTOCOL_PARAM = "count(protocol)";
47
48     private DbLibServiceQueries dblib;
49     private static FlowControlDBService dgGeneralDBService = null;
50
51     private FlowControlDBService() {
52         if (dblib== null) {
53             dblib = new DbLibServiceQueries();
54         }
55     }
56
57     protected FlowControlDBService(DbLibServiceQueries dbLibServiceQueries) {
58             dblib = dbLibServiceQueries;
59     }
60     
61     protected FlowControlDBService(DbLibService dbLibService) {
62         dblib = new DbLibServiceQueries(dbLibService);
63     }
64
65     public static FlowControlDBService initialise() {
66         if (dgGeneralDBService == null) {
67             dgGeneralDBService = new FlowControlDBService();
68         }
69         return dgGeneralDBService;
70     }
71
72     public void getFlowReferenceData(SvcLogicContext ctx, Map<String, String> inParams, SvcLogicContext localContext)
73         throws SvcLogicException {
74
75         String fn = "DBService.getflowModelInfo";
76         String whereClause = " where ACTION = $" + FlowControllerConstants.REQUEST_ACTION;
77
78         if (StringUtils.isNotBlank(ctx.getAttribute(FlowControllerConstants.ACTION_LEVEL))) {
79             whereClause = whereClause.concat(" and ACTION_LEVEL = $" + FlowControllerConstants.ACTION_LEVEL);
80         }
81
82         QueryStatus status;
83         if (dblib != 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 = dblib.query(key, localContext);
88             if (status.toString().equals(FAILURE_PARAM)) {
89                 throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
90             }
91         }
92     }
93
94     public String getEndPointByAction(String action) {
95         return null;
96     }
97
98     public String getDesignTimeFlowModel(SvcLogicContext localContext) throws SvcLogicException {
99         String fn = "DBService.getDesignTimeFlowModel ";
100         QueryStatus status;
101         if (dblib != null && localContext != null) {
102             String queryString =
103                 "select max(internal_version) as maxInternalVersion, artifact_name as artifactName from "
104                     + FlowControllerConstants.DB_SDC_ARTIFACTS + " where artifact_name in (select artifact_name from "
105                     + FlowControllerConstants.DB_SDC_REFERENCE + " where vnf_type= $"
106                     + FlowControllerConstants.VNF_TYPE
107                     + " and  vnfc_type = $" + FlowControllerConstants.VNFC_TYPE + " and  action = $"
108                     + FlowControllerConstants.REQUEST_ACTION + " and file_category =  $"
109                     + FlowControllerConstants.CATEGORY + " )";
110
111             log.debug(fn + QUERY_STR + queryString);
112             status = dblib.query(queryString, localContext);
113
114             if (status.toString().equals(FAILURE_PARAM)) {
115                 throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
116             }
117
118             String queryString1 = "select artifact_content from " + FlowControllerConstants.DB_SDC_ARTIFACTS
119                 + " where artifact_name = $artifactName  and internal_version = $maxInternalVersion ";
120
121             log.debug(fn + QUERY_STR + queryString1);
122             status = dblib.query(queryString1, localContext);
123             if (status.toString().equals(FAILURE_PARAM)) {
124                 throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
125             }
126         }
127         return localContext != null ? localContext.getAttribute(ARTIFACT_CONTENT_PARAM) : null;
128     }
129
130     public QueryStatus loadSequenceIntoDB(SvcLogicContext localContext) throws SvcLogicException {
131
132         QueryStatus status = null;
133         if (localContext != null) {
134             String fn = "DBService.saveArtifacts";
135
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));
140
141             for (Object key : localContext.getAttributeKeySet()) {
142                 String parmName = (String) key;
143                 String parmValue = localContext.getAttribute(parmName);
144                 log.debug(" loadSequenceIntoDB " + parmName + "=" + parmValue);
145             }
146
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() ";
153
154             log.debug(fn + QUERY_STR + queryString);
155             status = dblib.save(queryString, localContext);
156             if (status.toString().equals(FAILURE_PARAM)) {
157                 throw new SvcLogicException("Error While processing storing Artifact: "
158                     + localContext.getAttribute(FlowControllerConstants.ARTIFACT_NAME));
159             }
160         }
161         return status;
162
163     }
164
165     public void populateModuleAndRPC(Transaction transaction, String vnfType) throws SvcLogicException {
166         String fn = "FlowControlDBService.populateModuleAndRPC ";
167         QueryStatus status;
168         SvcLogicContext context = getSvcLogicContext();
169         String protocolType = getProtocolType(transaction, vnfType, fn, context);
170
171         String key = "select execution_type, execution_module, execution_rpc from "
172             + FlowControllerConstants.DB_PROCESS_FLOW_REFERENCE + " where action = ? "
173             + "and action_level = ? and protocol = ?";
174         ArrayList<String> arguments = new ArrayList<>();
175         arguments.add(transaction.getAction());
176         arguments.add(transaction.getActionLevel());
177         arguments.add(protocolType);
178
179         log.debug(fn + QUERY_STR + key);
180         status = dblib.query(key, context, arguments);
181         if (status.toString().equals(FAILURE_PARAM)) {
182             throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
183         }
184
185         transaction.setExecutionModule(context.getAttribute(FlowControllerConstants.EXECUTTION_MODULE));
186         transaction.setExecutionRPC(context.getAttribute(FlowControllerConstants.EXECUTION_RPC));
187         transaction.setExecutionType(context.getAttribute(FlowControllerConstants.EXECUTION_TYPE));
188
189     }
190
191     private String getProtocolType(Transaction transaction, String vnfType, String fn, SvcLogicContext context)
192         throws SvcLogicException {
193         QueryStatus status;
194         String protocolQuery;
195         int protocolCount;
196         protocolQuery = "select count(protocol) from " + FlowControllerConstants.DB_PROTOCOL_REFERENCE
197             + " where action = ? and action_level = ?";
198         ArrayList<String> arguments = new ArrayList<>();
199         arguments.add(transaction.getAction());
200         arguments.add(transaction.getActionLevel());
201
202         log.debug(fn + QUERY_STR + protocolQuery);
203         status = dblib.query(protocolQuery, context, arguments);
204         if (status.toString().equals(FAILURE_PARAM)) {
205             throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
206         }
207
208         String countProtocolParam = context.getAttribute(COUNT_PROTOCOL_PARAM);
209         log.debug(" Protocol Count " + countProtocolParam);
210         protocolCount = Integer.parseInt(countProtocolParam);
211
212         if (protocolCount == 1) {
213             protocolQuery = "select protocol from " + FlowControllerConstants.DB_PROTOCOL_REFERENCE
214                 + " where action = ? and action_level = ?";
215             ArrayList<String> arguments2 = new ArrayList<>();
216             arguments2.add(transaction.getAction());
217             arguments2.add(transaction.getActionLevel());
218
219             log.debug(fn + QUERY_STR + protocolQuery);
220             status = dblib.query(protocolQuery, context, arguments2);
221             if (status.toString().equals(FAILURE_PARAM)) {
222                 throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
223             }
224             return context.getAttribute("protocol");
225         } else {
226             if (hasSingleProtocol(transaction, vnfType, fn, context)) {
227                 return context.getAttribute("protocol");
228             }
229         }
230         return null;
231     }
232
233     private boolean hasSingleProtocol(Transaction transaction, String vnfType, String fn, SvcLogicContext context)
234         throws SvcLogicException {
235         String protocolQuery;
236         QueryStatus status;
237         int protocolCount;
238         protocolQuery = "select count(protocol) from " + FlowControllerConstants.DB_PROTOCOL_REFERENCE
239             + " where action = ? and action_level = ? and vnf_type = ?";
240         ArrayList<String> arguments = new ArrayList<>();
241         arguments.add(transaction.getAction());
242         arguments.add(transaction.getActionLevel());
243         arguments.add(vnfType);
244
245         log.debug(fn + QUERY_STR + protocolQuery);
246         status = dblib.query(protocolQuery, context, arguments);
247         if (status.toString().equals(FAILURE_PARAM)) {
248             throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
249         }
250
251         log.debug(" Protocol Count " + context.getAttribute(COUNT_PROTOCOL_PARAM));
252         protocolCount = Integer.parseInt(context.getAttribute(COUNT_PROTOCOL_PARAM));
253         if (protocolCount > 1) {
254             throw new SvcLogicException("Got more than 2 values..");
255         } else if (protocolCount == 1) {
256             protocolQuery = "select protocol from " + FlowControllerConstants.DB_PROTOCOL_REFERENCE
257                 + " where action = ? and action_level = ? and vnf_type = ?";
258             ArrayList<String> arguments2 = new ArrayList<>();
259             arguments2.add(transaction.getAction());
260             arguments2.add(transaction.getActionLevel());
261             arguments2.add(vnfType);
262             log.debug(fn + QUERY_STR + protocolQuery);
263             status = dblib.query(protocolQuery, context, arguments2);
264             if (status.toString().equals(FAILURE_PARAM)) {
265                 throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
266             }
267             return true;
268         }
269         return false;
270     }
271
272     public String getDependencyInfo(SvcLogicContext localContext) throws SvcLogicException {
273         String fn = "DBService.getDependencyInfo ";
274         QueryStatus status;
275         if (dblib != null && localContext != null) {
276             String queryString =
277                 "select max(internal_version) as maxInternalVersion, artifact_name as artifactName from "
278                     + FlowControllerConstants.DB_SDC_ARTIFACTS + " where artifact_name in (select artifact_name from "
279                     + FlowControllerConstants.DB_SDC_REFERENCE + " where vnf_type= $"
280                     + FlowControllerConstants.VNF_TYPE
281                     + " and file_category = '" + FlowControllerConstants.DEPENDENCYMODEL + "' )";
282
283             log.debug(fn + QUERY_STR + queryString);
284             status = dblib.query(queryString, localContext);
285
286             if (status.toString().equals(FAILURE_PARAM)) {
287                 throw new SvcLogicException("Error - while getting dependencydata ");
288             }
289
290             String queryString1 = "select artifact_content from " + FlowControllerConstants.DB_SDC_ARTIFACTS
291                 + " where artifact_name = $artifactName  and internal_version = $maxInternalVersion ";
292
293             log.debug(fn + QUERY_STR + queryString1);
294             status = dblib.query(queryString1, localContext);
295             if (status.toString().equals(FAILURE_PARAM)) {
296                 throw new SvcLogicException("Error - while getting dependencyData ");
297             }
298         }
299
300         return localContext != null ? localContext.getAttribute(ARTIFACT_CONTENT_PARAM) : null;
301
302     }
303
304     public String getCapabilitiesData(SvcLogicContext localContext) throws SvcLogicException {
305         String fn = "DBService.getCapabilitiesData ";
306         QueryStatus status;
307         if (dblib != null && localContext != null) {
308             String queryString =
309                 "select max(internal_version) as maxInternalVersion, artifact_name as artifactName from "
310                     + FlowControllerConstants.DB_SDC_ARTIFACTS + " where artifact_name in (select artifact_name from "
311                     + FlowControllerConstants.DB_SDC_REFERENCE + " where vnf_type= $"
312                     + FlowControllerConstants.VNF_TYPE
313                     + " and file_category = '" + FlowControllerConstants.CAPABILITY + "' )";
314
315             log.info(fn + QUERY_STR + queryString);
316             status = dblib.query(queryString, localContext);
317
318             if (status.toString().equals(FAILURE_PARAM)) {
319                 throw new SvcLogicException("Error - while getting capabilitiesData ");
320             }
321
322             String queryString1 = "select artifact_content from " + FlowControllerConstants.DB_SDC_ARTIFACTS
323                 + " where artifact_name = $artifactName  and internal_version = $maxInternalVersion ";
324
325             log.debug(fn + QUERY_STR + queryString1);
326             status = dblib.query(queryString1, localContext);
327             if (status.toString().equals(FAILURE_PARAM)) {
328                 throw new SvcLogicException("Error - while getting capabilitiesData ");
329             }
330         }
331         return localContext != null ? localContext.getAttribute(ARTIFACT_CONTENT_PARAM) : null;
332     }
333
334     protected SvcLogicContext getSvcLogicContext() {
335         return new SvcLogicContext();
336     }
337 }