aboutsummaryrefslogtreecommitdiffstats
path: root/appc-config/appc-flow-controller/provider/src/main/java/org/onap/appc/flow/controller/dbervices/FlowControlDBService.java
blob: 8c3345c0a7b3ca2ec474de5887d410e1dda89fc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*-
 * ============LICENSE_START=======================================================
 * ONAP : APPC
 * ================================================================================
 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Modifications Copyright (C) 2019 Ericsson
 * =============================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * ============LICENSE_END=========================================================
 */
package org.onap.appc.flow.controller.dbervices;

import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;

import java.util.ArrayList;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.onap.appc.flow.controller.data.Transaction;
import org.onap.appc.flow.controller.utils.EscapeUtils;
import org.onap.appc.flow.controller.utils.FlowControllerConstants;
import org.onap.ccsdk.sli.core.dblib.DbLibService;
import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
import org.onap.ccsdk.sli.core.sli.SvcLogicException;
import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;

public class FlowControlDBService {

    private static final EELFLogger log = EELFManager.getInstance().getLogger(FlowControlDBService.class);
    private static final String QUERY_STR = "Query String : ";
    private static final String FAILURE_PARAM = "FAILURE";
    protected static final String GET_FLOW_REF_DATA_ERROR = "Error - while getting FlowReferenceData ";
    private static final String ARTIFACT_CONTENT_PARAM = "artifact-content";
    protected static final String COUNT_PROTOCOL_PARAM = "count(protocol)";

    private DbLibServiceQueries dblib;
    private static FlowControlDBService dgGeneralDBService = null;

    private FlowControlDBService() {
        if (dblib== null) {
            dblib = new DbLibServiceQueries();
        }
    }

    protected FlowControlDBService(DbLibServiceQueries dbLibServiceQueries) {
            dblib = dbLibServiceQueries;
    }
    
    protected FlowControlDBService(DbLibService dbLibService) {
        dblib = new DbLibServiceQueries(dbLibService);
    }

    public static FlowControlDBService initialise() {
        if (dgGeneralDBService == null) {
            dgGeneralDBService = new FlowControlDBService();
        }
        return dgGeneralDBService;
    }

    public void getFlowReferenceData(SvcLogicContext ctx, Map<String, String> inParams, SvcLogicContext localContext)
        throws SvcLogicException {

        String fn = "DBService.getflowModelInfo";
        String whereClause = " where ACTION = $" + FlowControllerConstants.REQUEST_ACTION;

        if (StringUtils.isNotBlank(ctx.getAttribute(FlowControllerConstants.ACTION_LEVEL))) {
            whereClause = whereClause.concat(" and ACTION_LEVEL = $" + FlowControllerConstants.ACTION_LEVEL);
        }

        QueryStatus status;
        if (dblib != null && localContext != null) {
            String key = "select SEQUENCE_TYPE, CATEGORY, GENERATION_NODE, EXECUTION_NODE from "
                + FlowControllerConstants.DB_MULTISTEP_FLOW_REFERENCE + whereClause;
            log.debug(fn + QUERY_STR + key);
            status = dblib.query(key, localContext);
            if (status.toString().equals(FAILURE_PARAM)) {
                throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
            }
        }
    }

    public String getEndPointByAction(String action) {
        return null;
    }

    public String getDesignTimeFlowModel(SvcLogicContext localContext) throws SvcLogicException {
        String fn = "DBService.getDesignTimeFlowModel ";
        QueryStatus status;
        if (dblib != null && localContext != null) {
            String queryString =
                "select max(internal_version) as maxInternalVersion, artifact_name as artifactName from "
                    + FlowControllerConstants.DB_SDC_ARTIFACTS + " where artifact_name in (select artifact_name from "
                    + FlowControllerConstants.DB_SDC_REFERENCE + " where vnf_type= $"
                    + FlowControllerConstants.VNF_TYPE
                    + " and  vnfc_type = $" + FlowControllerConstants.VNFC_TYPE + " and  action = $"
                    + FlowControllerConstants.REQUEST_ACTION + " and file_category =  $"
                    + FlowControllerConstants.CATEGORY + " )";

            log.debug(fn + QUERY_STR + queryString);
            status = dblib.query(queryString, localContext);

            if (status.toString().equals(FAILURE_PARAM)) {
                throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
            }

            String queryString1 = "select artifact_content from " + FlowControllerConstants.DB_SDC_ARTIFACTS
                + " where artifact_name = $artifactName  and internal_version = $maxInternalVersion ";

            log.debug(fn + QUERY_STR + queryString1);
            status = dblib.query(queryString1, localContext);
            if (status.toString().equals(FAILURE_PARAM)) {
                throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
            }
        }
        return localContext != null ? localContext.getAttribute(ARTIFACT_CONTENT_PARAM) : null;
    }

    public QueryStatus loadSequenceIntoDB(SvcLogicContext localContext) throws SvcLogicException {

        QueryStatus status = null;
        if (localContext != null) {
            String fn = "DBService.saveArtifacts";

            localContext.setAttribute(FlowControllerConstants.ARTIFACT_CONTENT_ESCAPED,
                EscapeUtils.escapeSql(localContext.getAttribute(FlowControllerConstants.ARTIFACT_CONTENT)));
            log.debug("ESCAPED sequence for DB : "
                + localContext.getAttribute(FlowControllerConstants.ARTIFACT_CONTENT_ESCAPED));

            for (Object key : localContext.getAttributeKeySet()) {
                String parmName = (String) key;
                String parmValue = localContext.getAttribute(parmName);
                log.debug(" loadSequenceIntoDB " + parmName + "=" + parmValue);
            }

            String queryString = "INSERT INTO " + FlowControllerConstants.DB_REQUEST_ARTIFACTS + " set request_id =  $"
                + FlowControllerConstants.REQUEST_ID + " , action =  $" + FlowControllerConstants.REQUEST_ACTION
                + " , action_level =  $" + FlowControllerConstants.ACTION_LEVEL + " , vnf_type = $"
                + FlowControllerConstants.VNF_TYPE + " , category = $" + FlowControllerConstants.CATEGORY
                + " , artifact_content = $" + FlowControllerConstants.ARTIFACT_CONTENT_ESCAPED
                + " , updated_date = sysdate() ";

            log.debug(fn + QUERY_STR + queryString);
            status = dblib.save(queryString, localContext);
            if (status.toString().equals(FAILURE_PARAM)) {
                throw new SvcLogicException("Error While processing storing Artifact: "
                    + localContext.getAttribute(FlowControllerConstants.ARTIFACT_NAME));
            }
        }
        return status;

    }

    public void populateModuleAndRPC(Transaction transaction, String vnfType) throws SvcLogicException {
        String fn = "FlowControlDBService.populateModuleAndRPC ";
        QueryStatus status;
        SvcLogicContext context = getSvcLogicContext();
        String protocolType = getProtocolType(transaction, vnfType, fn, context);

        String key = "select execution_type, execution_module, execution_rpc from "
            + FlowControllerConstants.DB_PROCESS_FLOW_REFERENCE + " where action = ? "
            + "and action_level = ? and protocol = ?";
        ArrayList<String> arguments = new ArrayList<>();
        arguments.add(transaction.getAction());
        arguments.add(transaction.getActionLevel());
        arguments.add(protocolType);

        log.debug(fn + QUERY_STR + key);
        status = dblib.query(key, context, arguments);
        if (status.toString().equals(FAILURE_PARAM)) {
            throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
        }

        transaction.setExecutionModule(context.getAttribute(FlowControllerConstants.EXECUTTION_MODULE));
        transaction.setExecutionRPC(context.getAttribute(FlowControllerConstants.EXECUTION_RPC));
        transaction.setExecutionType(context.getAttribute(FlowControllerConstants.EXECUTION_TYPE));

    }

    private String getProtocolType(Transaction transaction, String vnfType, String fn, SvcLogicContext context)
        throws SvcLogicException {
        QueryStatus status;
        String protocolQuery;
        int protocolCount;
        protocolQuery = "select count(protocol) from " + FlowControllerConstants.DB_PROTOCOL_REFERENCE
            + " where action = ? and action_level = ?";
        ArrayList<String> arguments = new ArrayList<>();
        arguments.add(transaction.getAction());
        arguments.add(transaction.getActionLevel());

        log.debug(fn + QUERY_STR + protocolQuery);
        status = dblib.query(protocolQuery, context, arguments);
        if (status.toString().equals(FAILURE_PARAM)) {
            throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
        }

        String countProtocolParam = context.getAttribute(COUNT_PROTOCOL_PARAM);
        log.debug(" Protocol Count " + countProtocolParam);
        protocolCount = Integer.parseInt(countProtocolParam);

        if (protocolCount == 1) {
            protocolQuery = "select protocol from " + FlowControllerConstants.DB_PROTOCOL_REFERENCE
                + " where action = ? and action_level = ?";
            ArrayList<String> arguments2 = new ArrayList<>();
            arguments2.add(transaction.getAction());
            arguments2.add(transaction.getActionLevel());

            log.debug(fn + QUERY_STR + protocolQuery);
            status = dblib.query(protocolQuery, context, arguments2);
            if (status.toString().equals(FAILURE_PARAM)) {
                throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
            }
            return context.getAttribute("protocol");
        } else {
            if (hasSingleProtocol(transaction, vnfType, fn, context)) {
                return context.getAttribute("protocol");
            }
        }
        return null;
    }

    private boolean hasSingleProtocol(Transaction transaction, String vnfType, String fn, SvcLogicContext context)
        throws SvcLogicException {
        String protocolQuery;
        QueryStatus status;
        int protocolCount;
        protocolQuery = "select count(protocol) from " + FlowControllerConstants.DB_PROTOCOL_REFERENCE
            + " where action = ? and action_level = ? and vnf_type = ?";
        ArrayList<String> arguments = new ArrayList<>();
        arguments.add(transaction.getAction());
        arguments.add(transaction.getActionLevel());
        arguments.add(vnfType);

        log.debug(fn + QUERY_STR + protocolQuery);
        status = dblib.query(protocolQuery, context, arguments);
        if (status.toString().equals(FAILURE_PARAM)) {
            throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
        }

        log.debug(" Protocol Count " + context.getAttribute(COUNT_PROTOCOL_PARAM));
        protocolCount = Integer.parseInt(context.getAttribute(COUNT_PROTOCOL_PARAM));
        if (protocolCount > 1) {
            throw new SvcLogicException("Got more than 2 values..");
        } else if (protocolCount == 1) {
            protocolQuery = "select protocol from " + FlowControllerConstants.DB_PROTOCOL_REFERENCE
                + " where action = ? and action_level = ? and vnf_type = ?";
            ArrayList<String> arguments2 = new ArrayList<>();
            arguments2.add(transaction.getAction());
            arguments2.add(transaction.getActionLevel());
            arguments2.add(vnfType);
            log.debug(fn + QUERY_STR + protocolQuery);
            status = dblib.query(protocolQuery, context, arguments2);
            if (status.toString().equals(FAILURE_PARAM)) {
                throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
            }
            return true;
        }
        return false;
    }

    public String getDependencyInfo(SvcLogicContext localContext) throws SvcLogicException {
        String fn = "DBService.getDependencyInfo ";
        QueryStatus status;
        if (dblib != null && localContext != null) {
            String queryString =
                "select max(internal_version) as maxInternalVersion, artifact_name as artifactName from "
                    + FlowControllerConstants.DB_SDC_ARTIFACTS + " where artifact_name in (select artifact_name from "
                    + FlowControllerConstants.DB_SDC_REFERENCE + " where vnf_type= $"
                    + FlowControllerConstants.VNF_TYPE
                    + " and file_category = '" + FlowControllerConstants.DEPENDENCYMODEL + "' )";

            log.debug(fn + QUERY_STR + queryString);
            status = dblib.query(queryString, localContext);

            if (status.toString().equals(FAILURE_PARAM)) {
                throw new SvcLogicException("Error - while getting dependencydata ");
            }

            String queryString1 = "select artifact_content from " + FlowControllerConstants.DB_SDC_ARTIFACTS
                + " where artifact_name = $artifactName  and internal_version = $maxInternalVersion ";

            log.debug(fn + QUERY_STR + queryString1);
            status = dblib.query(queryString1, localContext);
            if (status.toString().equals(FAILURE_PARAM)) {
                throw new SvcLogicException("Error - while getting dependencyData ");
            }
        }

        return localContext != null ? localContext.getAttribute(ARTIFACT_CONTENT_PARAM) : null;

    }

    public String getCapabilitiesData(SvcLogicContext localContext) throws SvcLogicException {
        String fn = "DBService.getCapabilitiesData ";
        QueryStatus status;
        if (dblib != null && localContext != null) {
            String queryString =
                "select max(internal_version) as maxInternalVersion, artifact_name as artifactName from "
                    + FlowControllerConstants.DB_SDC_ARTIFACTS + " where artifact_name in (select artifact_name from "
                    + FlowControllerConstants.DB_SDC_REFERENCE + " where vnf_type= $"
                    + FlowControllerConstants.VNF_TYPE
                    + " and file_category = '" + FlowControllerConstants.CAPABILITY + "' )";

            log.info(fn + QUERY_STR + queryString);
            status = dblib.query(queryString, localContext);

            if (status.toString().equals(FAILURE_PARAM)) {
                throw new SvcLogicException("Error - while getting capabilitiesData ");
            }

            String queryString1 = "select artifact_content from " + FlowControllerConstants.DB_SDC_ARTIFACTS
                + " where artifact_name = $artifactName  and internal_version = $maxInternalVersion ";

            log.debug(fn + QUERY_STR + queryString1);
            status = dblib.query(queryString1, localContext);
            if (status.toString().equals(FAILURE_PARAM)) {
                throw new SvcLogicException("Error - while getting capabilitiesData ");
            }
        }
        return localContext != null ? localContext.getAttribute(ARTIFACT_CONTENT_PARAM) : null;
    }

    protected SvcLogicContext getSvcLogicContext() {
        return new SvcLogicContext();
    }
}