[SDNC-5] Rebase sdnc-core
[sdnc/core.git] / sli / provider / src / main / java / org / openecomp / sdnc / sli / provider / ExecuteNodeExecutor.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.sdnc.sli.provider;
23
24 import java.lang.reflect.InvocationTargetException;
25 import java.lang.reflect.Method;
26 import java.util.HashMap;
27 import java.util.Iterator;
28 import java.util.Map;
29 import java.util.Set;
30
31 import org.openecomp.sdnc.sli.SvcLogicAdaptor;
32 import org.openecomp.sdnc.sli.SvcLogicContext;
33 import org.openecomp.sdnc.sli.SvcLogicException;
34 import org.openecomp.sdnc.sli.SvcLogicExpression;
35 import org.openecomp.sdnc.sli.SvcLogicJavaPlugin;
36 import org.openecomp.sdnc.sli.SvcLogicNode;
37 import org.openecomp.sdnc.sli.SvcLogicResource;
38 import org.osgi.framework.BundleContext;
39 import org.osgi.framework.FrameworkUtil;
40 import org.osgi.framework.ServiceReference;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 public class ExecuteNodeExecutor extends SvcLogicNodeExecutor {
45         private static final Logger LOG = LoggerFactory
46                         .getLogger(ExecuteNodeExecutor.class);
47
48         private static final String pluginErrorMessage = "Could not execute plugin. SvcLogic status will be set to failure.";
49         public SvcLogicNode execute(SvcLogicServiceImpl svc, SvcLogicNode node,
50                         SvcLogicContext ctx) throws SvcLogicException {
51
52                 String pluginName = SvcLogicExpressionResolver.evaluate(
53                                 node.getAttribute("plugin"), node, ctx);
54                 String outValue = "failure";
55
56                 if (LOG.isDebugEnabled()) {
57                         LOG.debug("execute node encountered - looking for plugin "
58                                         + pluginName);
59                 }
60
61         SvcLogicJavaPlugin plugin  = getSvcLogicJavaPlugin(pluginName);
62
63                 if (plugin == null) {
64                         outValue = "not-found";
65                 } else {
66
67                         String methodName = evaluate(node.getAttribute("method"),  node, ctx);
68
69                         Class pluginClass = plugin.getClass();
70
71                         Method pluginMethod = null;
72
73                         try {
74                                 pluginMethod = pluginClass.getMethod(methodName, Map.class, SvcLogicContext.class);
75                         } catch (NoSuchMethodException e) {
76                                 LOG.error(pluginErrorMessage, e);
77                         }
78
79                         if (pluginMethod == null) {
80                                 outValue = "unsupported-method";
81                         } else {
82                                 try {
83
84                                         Map<String, String> parmMap = new HashMap<String, String>();
85
86                                         Set<Map.Entry<String, SvcLogicExpression>> parmSet = node
87                                                         .getParameterSet();
88
89                                         for (Iterator<Map.Entry<String, SvcLogicExpression>> iter = parmSet
90                                                         .iterator(); iter.hasNext();) {
91                                                 Map.Entry<String, SvcLogicExpression> curEnt = iter.next();
92                                                 String curName = curEnt.getKey();
93                                                 SvcLogicExpression curExpr = curEnt.getValue();
94                                                 String curExprValue = SvcLogicExpressionResolver.evaluate(curExpr, node, ctx);
95
96                                                 LOG.debug("Parameter "+curName+" = "+curExpr.asParsedExpr()+" resolves to "+curExprValue);
97
98                                                 parmMap.put(curName,curExprValue);
99                                         }
100
101                                         Object o = pluginMethod.invoke(plugin, parmMap, ctx);
102                                 String emitsOutcome = SvcLogicExpressionResolver.evaluate(node.getAttribute("emitsOutcome"),  node, ctx);
103
104                                         outValue = mapOutcome(o, emitsOutcome);
105
106                                 } catch (InvocationTargetException e) {
107                                     if(e.getCause() != null){
108                             LOG.error(pluginErrorMessage, e.getCause());
109                                     }else{
110                                         LOG.error(pluginErrorMessage, e);
111                                     }
112                                         outValue = "failure";
113                                         ctx.setStatus("failure");
114                                 } catch (IllegalAccessException e) {
115                     LOG.error(pluginErrorMessage, e);
116                     outValue = "failure";
117                     ctx.setStatus("failure");
118                 } catch (IllegalArgumentException e) {
119                     LOG.error(pluginErrorMessage, e);
120                     outValue = "failure";
121                     ctx.setStatus("failure");
122                 }
123                         }
124
125                 }
126
127                 SvcLogicNode nextNode = node.getOutcomeValue(outValue);
128                 if (nextNode != null) {
129                         if (LOG.isDebugEnabled()) {
130                                 LOG.debug("about to execute " + outValue + " branch");
131                         }
132                         return (nextNode);
133                 }
134
135                 nextNode = node.getOutcomeValue("Other");
136                 if (nextNode != null) {
137                         if (LOG.isDebugEnabled()) {
138                                 LOG.debug("about to execute Other branch");
139                         }
140                 } else {
141                         if (LOG.isDebugEnabled()) {
142                                 LOG.debug("no " + outValue + " or Other branch found");
143                         }
144                 }
145                 return (nextNode);
146         }
147
148         protected SvcLogicJavaPlugin getSvcLogicJavaPlugin(String pluginName){
149                BundleContext bctx = FrameworkUtil.getBundle(this.getClass())
150                         .getBundleContext();
151
152                 ServiceReference sref = bctx.getServiceReference(pluginName);
153
154                 if (sref == null) {
155                     LOG.warn("Could not find service reference object for plugin " + pluginName);
156                     return null;
157                 } else {
158                     SvcLogicJavaPlugin plugin  = (SvcLogicJavaPlugin) bctx
159                             .getService(sref);
160                     return plugin;
161                 }
162         }
163         protected String evaluate(SvcLogicExpression expr, SvcLogicNode node, SvcLogicContext ctx) throws SvcLogicException {
164         return SvcLogicExpressionResolver.evaluate(node.getAttribute("method"), node, ctx);
165     }
166
167     public String mapOutcome(Object o, String emitsOutcome) {
168         if (emitsOutcome != null) {
169             Boolean nodeEmitsOutcome = Boolean.valueOf(emitsOutcome);
170             if (nodeEmitsOutcome) {
171                 return (String) o;
172             } else {
173                 return "success";
174             }
175
176         } else {
177             return "success";
178         }
179     }
180
181 }