76e791a6e700882a3b692f45757c7acb4fb36d7e
[appc.git] / appc-config / appc-flow-controller / provider / src / main / java / org / onap / appc / flow / controller / executorImpl / GraphExecutor.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 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  * ============LICENSE_END=========================================================
20  */
21 package org.onap.appc.flow.controller.executorImpl;
22
23 import org.json.JSONObject;
24 import com.att.eelf.configuration.EELFLogger;
25 import com.att.eelf.configuration.EELFManager;
26 import java.util.Enumeration;
27 import java.util.Map;
28 import java.util.Properties;
29 import org.onap.appc.flow.controller.data.Transaction;
30 import org.onap.appc.flow.controller.interfaces.FlowExecutorInterface;
31 import org.onap.appc.flow.controller.utils.FlowControllerConstants;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
34 import org.onap.ccsdk.sli.core.sli.provider.SvcLogicService;
35 import org.osgi.framework.BundleContext;
36 import org.osgi.framework.FrameworkUtil;
37 import org.osgi.framework.ServiceReference;
38
39
40 public class GraphExecutor implements FlowExecutorInterface {
41
42     private static final EELFLogger log = EELFManager.getInstance().getLogger(GraphExecutor.class);
43     private static final String SVC_LOGIC_STATUS_PARAM = "SvcLogic.status";
44
45     private SvcLogicService svcLogic = null;
46
47     public GraphExecutor() {
48         BundleContext bctx = FrameworkUtil.getBundle(SvcLogicService.class).getBundleContext();
49
50         ServiceReference sref = bctx.getServiceReference(SvcLogicService.NAME);
51         if (sref != null) {
52             svcLogic = (SvcLogicService) bctx.getService(sref);
53         } else {
54             log.warn("Cannot find service reference for " + SvcLogicService.NAME);
55         }
56         log.debug("Graph Executor Initialized successfully");
57     }
58
59     public boolean hasGraph(String module, String rpc, String version, String mode) throws SvcLogicException {
60         return svcLogic.hasGraph(module, rpc, version, mode);
61     }
62
63     public Properties executeGraph(String module, String rpc, String version, String mode, Properties parms)
64         throws SvcLogicException {
65         log.debug("Parameters passed to SLI");
66
67         Properties respProps = svcLogic.execute(module, rpc, version, mode, parms);
68         if (log.isDebugEnabled()) {
69             log.debug("Parameters returned by SLI");
70             for (Object key : respProps.keySet()) {
71                 String parmName = (String) key;
72                 String parmValue = respProps.getProperty(parmName);
73
74                 log.debug(parmName + " = " + parmValue);
75             }
76         }
77         return respProps;
78     }
79
80     @Override
81     public Map<String, String> execute(Transaction transaction, SvcLogicContext ctx) throws Exception {
82
83         String fn = "GraphExecutor.execute ";
84         log.debug(fn + "About to execute graph : " + transaction.getExecutionRPC());
85
86         String payload = transaction.getPayload();
87         log.debug("payload:" + payload);
88
89         JSONObject json = new JSONObject(payload);
90         for (Object key : json.keySet()) {
91             String keyStr = (String)key;
92             Object keyvalue = json.get(keyStr);
93             String value= keyvalue.toString();
94             ctx.setAttribute(keyStr, value);
95             log.debug("key: "+ keyStr + " value: " + keyvalue);
96         }
97         Properties parms = new Properties();
98         for (Object key : ctx.getAttributeKeySet()) {
99             String parmName = (String) key;
100             String parmValue = ctx.getAttribute(parmName);
101             parms.put(parmName, parmValue);
102             log.info(fn + "Setting Key= " + parmName + "and Value = " + parmValue);
103
104         }
105         Properties returnParams =
106             executeGraph(transaction.getExecutionModule(), transaction.getExecutionRPC(), null, "sync", parms);
107
108         log.debug("Returned Params from DG Module: " + transaction.getExecutionModule() + "and DG NAME: "
109             + transaction.getExecutionRPC() + returnParams.toString());
110
111         Enumeration e = returnParams.propertyNames();
112
113         while (e.hasMoreElements()) {
114             String key = (String) e.nextElement();
115             log.info("NEW KEY =  " + key + " -- " + returnParams.getProperty(key));
116
117             ctx.setAttribute(key, returnParams.getProperty(key));
118         }
119
120         if (FlowControllerConstants.FAILURE.equalsIgnoreCase(returnParams.getProperty(SVC_LOGIC_STATUS_PARAM))) {
121             transaction.setStatus(FlowControllerConstants.FAILURE);
122             ctx.setAttribute(
123                 ctx.getAttribute(FlowControllerConstants.RESPONSE_PREFIX)
124                     + FlowControllerConstants.OUTPUT_PARAM_STATUS,
125                 FlowControllerConstants.OUTPUT_STATUS_FAILURE);
126             ctx.setAttribute(ctx.getAttribute(FlowControllerConstants.RESPONSE_PREFIX)
127                 + FlowControllerConstants.OUTPUT_STATUS_MESSAGE, returnParams.getProperty("error-message"));
128             transaction.setStatusCode("401");
129             transaction.setState(ctx.getAttribute(transaction.getExecutionModule() + "." + transaction.getExecutionRPC()
130                 + "." + FlowControllerConstants.OUTPUT_STATUS_MESSAGE));
131             // Get error code from above instead setting here ...its for testing purpose
132
133         } else if (FlowControllerConstants.SUCCESS.equalsIgnoreCase(returnParams.getProperty(SVC_LOGIC_STATUS_PARAM))) {
134             transaction.setStatus(FlowControllerConstants.SUCCESS);
135             transaction.setStatusCode("400");
136             ctx.setAttribute(
137                 ctx.getAttribute(FlowControllerConstants.RESPONSE_PREFIX)
138                     + FlowControllerConstants.OUTPUT_PARAM_STATUS,
139                 FlowControllerConstants.OUTPUT_STATUS_SUCCESS);
140             transaction.setState(ctx.getAttribute(transaction.getExecutionModule() + "." + transaction.getExecutionRPC()
141                 + "." + FlowControllerConstants.OUTPUT_STATUS_MESSAGE));
142             // Get error code from above instead setting here ...its for testing purpose
143         } else {
144             transaction.setStatus(FlowControllerConstants.OTHERS);
145             ctx.setAttribute(
146                 ctx.getAttribute(FlowControllerConstants.RESPONSE_PREFIX)
147                     + FlowControllerConstants.OUTPUT_PARAM_STATUS,
148                 FlowControllerConstants.OUTPUT_STATUS_FAILURE);
149             transaction.setStatusCode("401");
150             ctx.setAttribute(ctx.getAttribute(FlowControllerConstants.RESPONSE_PREFIX)
151                 + FlowControllerConstants.OUTPUT_STATUS_MESSAGE, returnParams.getProperty("error-message"));
152         }
153
154         return null;
155         // Change null to required value if required in upper level
156     }
157 }