2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Nokia. 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.
18 * ============LICENSE_END=========================================================
20 package org.onap.appc.flow.controller.node;
22 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.DESINGTIME;
23 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.EXTERNAL;
24 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.FLOW_SEQUENCE;
25 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.GENERATION_NODE;
26 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.OUTPUT_PARAM_ERROR_CODE;
27 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.OUTPUT_PARAM_ERROR_MESSAGE;
28 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.RUNTIME;
29 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.SEQUENCE_TYPE;
30 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.VNFC_TYPE;
31 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.VNF_TYPE;
33 import com.att.eelf.configuration.EELFLogger;
34 import com.att.eelf.configuration.EELFManager;
35 import com.fasterxml.jackson.databind.ObjectMapper;
37 import org.json.JSONObject;
38 import org.onap.appc.flow.controller.data.Transaction;
39 import org.onap.appc.flow.controller.data.Transactions;
40 import org.onap.appc.flow.controller.dbervices.FlowControlDBService;
41 import org.onap.appc.flow.controller.executorImpl.GraphExecutor;
42 import org.onap.appc.flow.controller.executorImpl.RestExecutor;
43 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
46 * Helper class for FlowControlNode
48 class FlowSequenceGenerator {
50 private static final EELFLogger log = EELFManager.getInstance().getLogger(FlowSequenceGenerator.class);
52 static final String MODULE = "APPC_COMMOM";
54 private final FlowControlDBService dbService;
55 private final FlowGenerator flowGenerator;
56 private final GraphExecutor transactionExecutor;
57 private final RestExecutor restExecutor;
58 private final InputParamsCollector inputParamsCollector;
60 FlowSequenceGenerator() {
61 this.dbService = FlowControlDBService.initialise();
62 this.flowGenerator = new FlowGenerator();
63 this.transactionExecutor = new GraphExecutor();
64 this.restExecutor = new RestExecutor();
65 this.inputParamsCollector = new InputParamsCollector();
69 * Constructor for tests, prefer to use default one
71 FlowSequenceGenerator(FlowControlDBService dbService, FlowGenerator flowGenerator,
72 GraphExecutor graphExecutor, RestExecutor restExecutor, EnvVariables envVariables) {
73 this.dbService = dbService;
74 this.flowGenerator = flowGenerator;
75 this.transactionExecutor = graphExecutor;
76 this.restExecutor = restExecutor;
77 this.inputParamsCollector = new InputParamsCollector(envVariables, dbService);
80 String getFlowSequence(Map<String, String> inParams, SvcLogicContext ctx, SvcLogicContext localContext)
84 if (localContext.getAttribute(SEQUENCE_TYPE) == null) {
85 Transactions trans = flowGenerator.createSingleStepModel(inParams, ctx);
86 ObjectMapper mapper = new ObjectMapper();
87 flowSequence = mapper.writeValueAsString(trans);
88 log.debug("Single step Flow Sequence : " + flowSequence);
93 if (localContext.getAttribute(GENERATION_NODE) != null) {
94 flowSequence = generationNode(localContext.getAttribute(GENERATION_NODE));
96 flowSequence = getFlowSequenceFromType(ctx, localContext, localContext.getAttribute(SEQUENCE_TYPE));
101 private String generationNode(String generationNode) throws Exception {
102 if (!transactionExecutor.hasGraph(MODULE, generationNode, null, "sync")) {
103 throw new Exception("Can not find Custom defined Flow Generator for " + generationNode);
105 return transactionExecutor
106 .executeGraph(MODULE, generationNode, null, "sync", null)
107 .getProperty(FLOW_SEQUENCE);
110 private String getFlowSequenceFromType(SvcLogicContext ctx, SvcLogicContext localContext,
111 String sequenceType) throws Exception {
113 String vnfType = ctx.getAttribute(VNF_TYPE);
114 if (sequenceType.equalsIgnoreCase(DESINGTIME)) {
116 localContext.setAttribute(VNFC_TYPE, ctx.getAttribute(VNFC_TYPE));
117 flowSequence = dbService.getDesignTimeFlowModel(localContext);
119 if (flowSequence == null) {
120 throw new Exception("Flow Sequence is not found User Designed VNF " + vnfType);
122 } else if (sequenceType.equalsIgnoreCase(RUNTIME)) {
124 Transaction transaction = inputParamsCollector.collectInputParams(ctx);
125 log.info("CollectInputParamsData-Input: " + transaction.getPayload());
127 Map<String, String> flowSeq = restExecutor.execute(transaction, localContext);
129 JSONObject output = new JSONObject(flowSeq.get("restResponse")).optJSONObject("output");
130 if (output == null) {
131 throw new Exception("Failed to get the Flow Sequence runtime for VNF type " + vnfType);
133 flowSequence = output.toString();
134 log.info("MultistepSequenceGenerator-Output: " + flowSequence);
136 // check for transactions data
137 if (!flowSequence.contains("transaction-id")) {
138 // check for status data
139 JSONObject statusJson = new JSONObject(output.toString()).optJSONObject("status");
140 if (statusJson != null) {
141 log.info("statusJson=" + statusJson);
142 if (statusJson.has("code")) {
143 // extract code and set into ctx
144 log.info("Setting " + OUTPUT_PARAM_ERROR_CODE + "=" + statusJson.get("code").toString() + " in context ctx");
145 ctx.setAttribute(OUTPUT_PARAM_ERROR_CODE, statusJson.get("code").toString());
146 log.info("Setting " + OUTPUT_PARAM_ERROR_MESSAGE + "=" + statusJson.get("message").toString() + " in context ctx");
147 ctx.setAttribute(OUTPUT_PARAM_ERROR_MESSAGE, statusJson.get("message").toString());
150 throw new Exception("Failed to generate the sequence for this request");
153 } else if (sequenceType.equalsIgnoreCase(EXTERNAL)) {
154 //String input = collectInputParams(localContext);
155 // flowSequnce = ""; //get it from the External interface calling the Rest End point - TBD
156 //if(flowSequnce == null)
158 throw new Exception("Flow Sequence not found for " + vnfType);
161 //No other type of model supported...
162 //in Future can get flowModel from other generators which will be included here
163 throw new Exception("No information found for sequence Owner Design-Time Vs Run-Time");