f3260947d665630953e21c36ee20c6f948f499ca
[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 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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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  *
19  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
20  * ============LICENSE_END=========================================================
21  */
22 package org.onap.appc.flow.controller.dbervices;
23
24 import java.util.Map;
25
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;
35
36 import com.att.eelf.configuration.EELFLogger;
37 import com.att.eelf.configuration.EELFManager;
38
39 public class FlowControlDBService {
40
41     private static final EELFLogger log = EELFManager.getInstance().getLogger(FlowControlDBService.class);
42     private SvcLogicResource serviceLogic;
43     private static FlowControlDBService dgGeneralDBService = null;
44
45     public static FlowControlDBService initialise() {
46         if (dgGeneralDBService == null) {
47             dgGeneralDBService = new FlowControlDBService();
48         }
49         return dgGeneralDBService;
50     }
51
52     private FlowControlDBService() {
53         if (serviceLogic == null) {
54             serviceLogic = new SqlResource();
55         }
56     }
57
58     public void getFlowReferenceData(SvcLogicContext ctx, Map<String, String> inParams, SvcLogicContext localContext)
59             throws SvcLogicException {
60
61         String fn = "DBService.getflowModelInfo";
62         String whereClause = " where ACTION = $" + FlowControllerConstants.REQUEST_ACTION;
63
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);
68
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 ");
77         }
78     }
79
80     public String getEndPointByAction(String action) {
81         return null;
82     }
83
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 + " )";
94
95             log.debug(fn + "Query String : " + queryString);
96             status = serviceLogic.query("SQL", false, null, queryString, null, null, localContext);
97
98             if (status.toString().equals("FAILURE"))
99                 throw new SvcLogicException("Error - while getting FlowReferenceData ");
100
101             String queryString1 = "select artifact_content from " + FlowControllerConstants.DB_SDC_ARTIFACTS
102                     + " where artifact_name = $artifactName  and internal_version = $maxInternalVersion ";
103
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 ");
108         }
109         return localContext != null ? localContext.getAttribute("artifact-content") : null;
110     }
111
112     public QueryStatus loadSequenceIntoDB(SvcLogicContext localContext) throws SvcLogicException {
113
114         QueryStatus status = null;
115
116         if (localContext != null) {
117             String fn = "DBService.saveArtifacts";
118
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));
123
124             for (Object key : localContext.getAttributeKeySet()) {
125                 String parmName = (String) key;
126                 String parmValue = localContext.getAttribute(parmName);
127                 log.debug(" loadSequenceIntoDB " + parmName + "=" + parmValue);
128             }
129
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() ";
136
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));
142         }
143         return status;
144
145     }
146
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;
152
153         protocolType = getProtocolType(transaction, vnf_type, fn, context, protocolType);
154
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 + "'";
159
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 ");
164
165         transaction.setExecutionModule(context.getAttribute(FlowControllerConstants.EXECUTTION_MODULE));
166         transaction.setExecutionRPC(context.getAttribute(FlowControllerConstants.EXECUTION_RPC));
167         transaction.setExecutionType(context.getAttribute(FlowControllerConstants.EXECUTION_TYPE));
168
169     }
170
171     private String getProtocolType(Transaction transaction, String vnf_type, String fn, SvcLogicContext context,
172             String protocolType) throws Exception {
173         QueryStatus status;
174         String protocolQuery;
175         int protocolCount;
176         protocolQuery = "select count(protocol) from " + FlowControllerConstants.DB_PROTOCOL_REFERENCE
177                 + " where action = '" + transaction.getAction() + "'" + " and action_level = '"
178                 + transaction.getActionLevel() + "'";
179
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 ");
184
185         log.debug(" Protocol Count " + context.getAttribute("count(protocol)"));
186         protocolCount = Integer.parseInt(context.getAttribute("count(protocol)"));
187
188         if (protocolCount == 1) {
189             protocolQuery = "select protocol from " + FlowControllerConstants.DB_PROTOCOL_REFERENCE
190                     + " where action = '" + transaction.getAction() + "'" + " and action_level = '"
191                     + transaction.getActionLevel() + "'";
192
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");
198         }
199             else  {
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 + "'";
203
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 ");
208
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");
222                 }
223             }
224         return protocolType;
225     }
226
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 + "' )";
235
236             log.debug(fn + "Query String : " + queryString);
237             status = serviceLogic.query("SQL", false, null, queryString, null, null, localContext);
238
239             if (status.toString().equals("FAILURE"))
240                 throw new SvcLogicException("Error - while getting dependencydata ");
241
242             String queryString1 = "select artifact_content from " + FlowControllerConstants.DB_SDC_ARTIFACTS
243                     + " where artifact_name = $artifactName  and internal_version = $maxInternalVersion ";
244
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 ");
249         }
250
251         return localContext != null ? localContext.getAttribute("artifact-content") : null;
252
253     }
254
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 + "' )";
263
264             log.info(fn + "Query String : " + queryString);
265             status = serviceLogic.query("SQL", false, null, queryString, null, null, localContext);
266
267             if (status.toString().equals("FAILURE"))
268                 throw new SvcLogicException("Error - while getting capabilitiesData ");
269
270             String queryString1 = "select artifact_content from " + FlowControllerConstants.DB_SDC_ARTIFACTS
271                     + " where artifact_name = $artifactName  and internal_version = $maxInternalVersion ";
272
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 ");
277         }
278         return localContext != null ? localContext.getAttribute("artifact-content") : null;
279     }
280 }