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