Update java files with correct license text
[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 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.executorImpl;
23
24 import java.util.Enumeration;
25 import java.util.HashMap;
26 import java.util.Properties;
27 import org.onap.appc.flow.controller.data.Transaction;
28 import org.onap.appc.flow.controller.interfaces.FlowExecutorInterface;
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.provider.SvcLogicService;
33 import org.osgi.framework.BundleContext;
34 import org.osgi.framework.FrameworkUtil;
35 import org.osgi.framework.ServiceReference;
36 import com.att.eelf.configuration.EELFLogger;
37 import com.att.eelf.configuration.EELFManager;
38
39
40 public class GraphExecutor implements FlowExecutorInterface {
41
42     private static final EELFLogger log = EELFManager.getInstance().getLogger(GraphExecutor.class);
43
44     private SvcLogicService svcLogic = null;
45
46     public GraphExecutor() {
47         BundleContext bctx = FrameworkUtil.getBundle(SvcLogicService.class).getBundleContext();
48
49         ServiceReference sref = bctx.getServiceReference(SvcLogicService.NAME);
50         if (sref != null) {
51             svcLogic = (SvcLogicService) bctx.getService(sref);
52
53
54         } else {
55             log.warn("Cannot find service reference for " + SvcLogicService.NAME);
56         }
57         log.debug("Graph Executor Initialized successfully");
58     }
59
60     public boolean hasGraph(String module, String rpc, String version, String mode) throws SvcLogicException {
61         return (svcLogic.hasGraph(module, rpc, version, mode));
62     }
63
64     public Properties executeGraph(String module, String rpc, String version, String mode, Properties parms)
65             throws SvcLogicException {
66         log.debug("Parameters passed to SLI");
67
68         // for (Object key : parms.keySet()) {
69         // String parmName = (String) key;
70         // String parmValue = parms.getProperty(parmName);
71         //
72         // log.debug(parmName + " = " + parmValue);
73         // }
74
75         Properties respProps = svcLogic.execute(module, rpc, version, mode, parms);
76         if (log.isDebugEnabled()) {
77             log.debug("Parameters returned by SLI");
78             for (Object key : respProps.keySet()) {
79                 String parmName = (String) key;
80                 String parmValue = respProps.getProperty(parmName);
81
82                 log.debug(parmName + " = " + parmValue);
83             }
84         }
85         if ("failure".equalsIgnoreCase(respProps.getProperty("SvcLogic.status"))) {
86             return (respProps);
87         }
88         return (respProps);
89     }
90
91     @Override
92     public HashMap<String, String> execute(Transaction transaction, SvcLogicContext ctx) throws Exception {
93
94         String fn = "GraphExecutor.execute ";
95         log.debug(fn + "About to execute graph : " + transaction.getExecutionRPC());
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("Return Params executing DG :" + returnParams.toString());
109
110         log.debug("Returned Params from DG Module: " + transaction.getExecutionModule() + "and DG NAME: "
111                 + transaction.getExecutionRPC() + returnParams.toString());
112
113         Enumeration e = returnParams.propertyNames();
114
115         while (e.hasMoreElements()) {
116             String key = (String) e.nextElement();
117             log.info("NEW KEY =  " + key + " -- " + returnParams.getProperty(key));
118
119             ctx.setAttribute(key, returnParams.getProperty(key));
120         }
121
122
123         // Get the correct code from the SVC Logic and set it in transaction
124         // transaction.setStatusCode(returnParams.getProperty("SvcLogic.code"));
125
126         if (FlowControllerConstants.FAILURE.equalsIgnoreCase(returnParams.getProperty("SvcLogic.status"))) {
127             transaction.setStatus(FlowControllerConstants.FAILURE);
128             ctx.setAttribute(
129                     ctx.getAttribute(FlowControllerConstants.RESPONSE_PREFIX)
130                             + FlowControllerConstants.OUTPUT_PARAM_STATUS,
131                     FlowControllerConstants.OUTPUT_STATUS_FAILURE);
132             ctx.setAttribute(ctx.getAttribute(FlowControllerConstants.RESPONSE_PREFIX)
133                     + FlowControllerConstants.OUTPUT_STATUS_MESSAGE, returnParams.getProperty("error-message"));
134             transaction.setStatusCode("401");
135             transaction.setState((ctx.getAttribute(transaction.getExecutionModule() + "."
136                     + transaction.getExecutionRPC() + "." + FlowControllerConstants.OUTPUT_STATUS_MESSAGE)) != null
137                             ? ctx.getAttribute(transaction.getExecutionModule() + "." + transaction.getExecutionRPC()
138                                     + "." + FlowControllerConstants.OUTPUT_STATUS_MESSAGE)
139                             : null);
140             // Get error code from above instead setting here ...its for testing purpose
141
142
143         } else if (FlowControllerConstants.SUCCESS.equalsIgnoreCase(returnParams.getProperty("SvcLogic.status"))) {
144             transaction.setStatus(FlowControllerConstants.SUCCESS);
145             transaction.setStatusCode("400");
146             ctx.setAttribute(
147                     ctx.getAttribute(FlowControllerConstants.RESPONSE_PREFIX)
148                             + FlowControllerConstants.OUTPUT_PARAM_STATUS,
149                     FlowControllerConstants.OUTPUT_STATUS_SUCCESS);
150             transaction.setState((ctx.getAttribute(transaction.getExecutionModule() + "."
151                     + transaction.getExecutionRPC() + "." + FlowControllerConstants.OUTPUT_STATUS_MESSAGE)) != null
152                             ? ctx.getAttribute(transaction.getExecutionModule() + "." + transaction.getExecutionRPC()
153                                     + "." + FlowControllerConstants.OUTPUT_STATUS_MESSAGE)
154                             : null);
155             // Get error code from above instead setting here ...its for testing purpose
156         } else {
157             transaction.setStatus(FlowControllerConstants.OTHERS);
158             ctx.setAttribute(
159                     ctx.getAttribute(FlowControllerConstants.RESPONSE_PREFIX)
160                             + FlowControllerConstants.OUTPUT_PARAM_STATUS,
161                     FlowControllerConstants.OUTPUT_STATUS_FAILURE);
162             transaction.setStatusCode("401");
163             ctx.setAttribute(ctx.getAttribute(FlowControllerConstants.RESPONSE_PREFIX)
164                     + FlowControllerConstants.OUTPUT_STATUS_MESSAGE, returnParams.getProperty("error-message"));
165         }
166
167         return null;
168         // Change null to required value if required in upper level
169     }
170 }