9da9bcde954bc7d6024c37c3cec754df0481cc09
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APP-C
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  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.appc.flow.controller.dbervices;
22
23 import java.util.Map;
24
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;
34
35 import com.att.eelf.configuration.EELFLogger;
36 import com.att.eelf.configuration.EELFManager;
37
38 public class FlowControlDBService {
39
40     private static final EELFLogger log = EELFManager.getInstance().getLogger(FlowControlDBService.class);
41     private SvcLogicResource serviceLogic;
42     private static FlowControlDBService dgGeneralDBService = null;
43
44     public static FlowControlDBService initialise() {
45         if (dgGeneralDBService == null) {
46             dgGeneralDBService = new FlowControlDBService();
47         }
48         return dgGeneralDBService;
49     }
50
51     private FlowControlDBService() {
52         if (serviceLogic == null) {
53             serviceLogic = new SqlResource();
54         }
55     }
56
57     public void getFlowReferenceData(SvcLogicContext ctx, Map<String, String> inParams, SvcLogicContext localContext)
58             throws SvcLogicException {
59
60         String fn = "DBService.getflowModelInfo";
61         String whereClause = " where ACTION = $" + FlowControllerConstants.REQUEST_ACTION;
62
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);
67
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 ");
76         }
77     }
78
79     public String getEndPointByAction(String action) {
80         return null;
81     }
82
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 + " )";
93
94             log.debug(fn + "Query String : " + queryString);
95             status = serviceLogic.query("SQL", false, null, queryString, null, null, localContext);
96
97             if (status.toString().equals("FAILURE"))
98                 throw new SvcLogicException("Error - while getting FlowReferenceData ");
99
100             String queryString1 = "select artifact_content from " + FlowControllerConstants.DB_SDC_ARTIFACTS
101                     + " where artifact_name = $artifactName  and internal_version = $maxInternalVersion ";
102
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 ");
107         }
108         return localContext != null ? localContext.getAttribute("artifact-content") : null;
109     }
110
111     public QueryStatus loadSequenceIntoDB(SvcLogicContext localContext) throws SvcLogicException {
112
113         QueryStatus status = null;
114
115         if (localContext != null) {
116             String fn = "DBService.saveArtifacts";
117
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));
122
123             for (Object key : localContext.getAttributeKeySet()) {
124                 String parmName = (String) key;
125                 String parmValue = localContext.getAttribute(parmName);
126                 log.debug(" loadSequenceIntoDB " + parmName + "=" + parmValue);
127             }
128
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() ";
135
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));
141         }
142         return status;
143
144     }
145
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;
151
152         protocolType = getProtocolType(transaction, vnf_type, fn, context, protocolType);
153
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 + "'";
158
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 ");
163
164         transaction.setExecutionModule(context.getAttribute(FlowControllerConstants.EXECUTTION_MODULE));
165         transaction.setExecutionRPC(context.getAttribute(FlowControllerConstants.EXECUTION_RPC));
166         transaction.setExecutionType(context.getAttribute(FlowControllerConstants.EXECUTION_TYPE));
167
168     }
169
170     private String getProtocolType(Transaction transaction, String vnf_type, String fn, SvcLogicContext context,
171             String protocolType) throws Exception {
172         QueryStatus status;
173         String protocolQuery;
174         int protocolCount;
175         protocolQuery = "select count(protocol) from " + FlowControllerConstants.DB_PROTOCOL_REFERENCE
176                 + " where action = '" + transaction.getAction() + "'" + " and action_level = '"
177                 + transaction.getActionLevel() + "'";
178
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 ");
183
184         log.debug(" Protocol Count " + context.getAttribute("count(protocol)"));
185         protocolCount = Integer.parseInt(context.getAttribute("count(protocol)"));
186
187         if (protocolCount == 1) {
188             protocolQuery = "select protocol from " + FlowControllerConstants.DB_PROTOCOL_REFERENCE
189                     + " where action = '" + transaction.getAction() + "'" + " and action_level = '"
190                     + transaction.getActionLevel() + "'";
191
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");
197         }
198             else  {
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 + "'";
202
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 ");
207
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");
221                 }
222             }
223         return protocolType;
224     }
225
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 + "' )";
234
235             log.debug(fn + "Query String : " + queryString);
236             status = serviceLogic.query("SQL", false, null, queryString, null, null, localContext);
237
238             if (status.toString().equals("FAILURE"))
239                 throw new SvcLogicException("Error - while getting dependencydata ");
240
241             String queryString1 = "select artifact_content from " + FlowControllerConstants.DB_SDC_ARTIFACTS
242                     + " where artifact_name = $artifactName  and internal_version = $maxInternalVersion ";
243
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 ");
248         }
249
250         return localContext != null ? localContext.getAttribute("artifact-content") : null;
251
252     }
253
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 + "' )";
262
263             log.info(fn + "Query String : " + queryString);
264             status = serviceLogic.query("SQL", false, null, queryString, null, null, localContext);
265
266             if (status.toString().equals("FAILURE"))
267                 throw new SvcLogicException("Error - while getting capabilitiesData ");
268
269             String queryString1 = "select artifact_content from " + FlowControllerConstants.DB_SDC_ARTIFACTS
270                     + " where artifact_name = $artifactName  and internal_version = $maxInternalVersion ";
271
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 ");
276         }
277         return localContext != null ? localContext.getAttribute("artifact-content") : null;
278     }
279 }