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