Update groupId to org.onap.ccsdk.sli
[ccsdk/sli/core.git] / sli / provider / src / main / java / org / onap / ccsdk / sli / core / sli / provider / ExecuteNodeExecutor.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK
4  * ================================================================================
5  * Copyright (C) 2017 ONAP
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.ccsdk.sli.core.sli.provider;
22
23 import java.lang.reflect.InvocationTargetException;
24 import java.lang.reflect.Method;
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.Map;
28 import java.util.Set;
29
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.SvcLogicExpression;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicNode;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 public class ExecuteNodeExecutor extends SvcLogicNodeExecutor {
39         private static final Logger LOG = LoggerFactory
40                         .getLogger(ExecuteNodeExecutor.class);
41
42         private static final String pluginErrorMessage = "Could not execute plugin. SvcLogic status will be set to failure.";
43         public SvcLogicNode execute(SvcLogicServiceImpl svc, SvcLogicNode node,
44                         SvcLogicContext ctx) throws SvcLogicException {
45
46                 String pluginName = SvcLogicExpressionResolver.evaluate(
47                                 node.getAttribute("plugin"), node, ctx);
48                 String outValue = "failure";
49
50                 if (LOG.isDebugEnabled()) {
51                         LOG.debug("execute node encountered - looking for plugin "
52                                         + pluginName);
53                 }
54
55         SvcLogicJavaPlugin plugin  = getSvcLogicJavaPlugin(pluginName);
56
57                 if (plugin == null) {
58                         outValue = "not-found";
59                 } else {
60
61                         String methodName = evaluate(node.getAttribute("method"),  node, ctx);
62
63                         Class pluginClass = plugin.getClass();
64
65                         Method pluginMethod = null;
66
67                         try {
68                                 pluginMethod = pluginClass.getMethod(methodName, Map.class, SvcLogicContext.class);
69                         } catch (NoSuchMethodException e) {
70                                 LOG.error(pluginErrorMessage, e);
71                         }
72
73                         if (pluginMethod == null) {
74                                 outValue = "unsupported-method";
75                         } else {
76                                 try {
77
78                                         Map<String, String> parmMap = new HashMap<String, String>();
79
80                                         Set<Map.Entry<String, SvcLogicExpression>> parmSet = node
81                                                         .getParameterSet();
82
83                                         for (Iterator<Map.Entry<String, SvcLogicExpression>> iter = parmSet
84                                                         .iterator(); iter.hasNext();) {
85                                                 Map.Entry<String, SvcLogicExpression> curEnt = iter.next();
86                                                 String curName = curEnt.getKey();
87                                                 SvcLogicExpression curExpr = curEnt.getValue();
88                                                 String curExprValue = SvcLogicExpressionResolver.evaluate(curExpr, node, ctx);
89
90                                                 LOG.debug("Parameter "+curName+" = "+curExpr.asParsedExpr()+" resolves to "+curExprValue);
91
92                                                 parmMap.put(curName,curExprValue);
93                                         }
94
95                                         Object o = pluginMethod.invoke(plugin, parmMap, ctx);
96                                 String emitsOutcome = SvcLogicExpressionResolver.evaluate(node.getAttribute("emitsOutcome"),  node, ctx);
97
98                                         outValue = mapOutcome(o, emitsOutcome);
99
100                                 } catch (InvocationTargetException e) {
101                                     if(e.getCause() != null){
102                             LOG.error(pluginErrorMessage, e.getCause());
103                                     }else{
104                                         LOG.error(pluginErrorMessage, e);
105                                     }
106                                         outValue = "failure";
107                                         ctx.setStatus("failure");
108                                 } catch (IllegalAccessException e) {
109                     LOG.error(pluginErrorMessage, e);
110                     outValue = "failure";
111                     ctx.setStatus("failure");
112                 } catch (IllegalArgumentException e) {
113                     LOG.error(pluginErrorMessage, e);
114                     outValue = "failure";
115                     ctx.setStatus("failure");
116                 }
117                         }
118
119                 }
120
121                 SvcLogicNode nextNode = node.getOutcomeValue(outValue);
122                 if (nextNode != null) {
123                         if (LOG.isDebugEnabled()) {
124                                 LOG.debug("about to execute " + outValue + " branch");
125                         }
126                         return (nextNode);
127                 }
128
129                 nextNode = node.getOutcomeValue("Other");
130                 if (nextNode != null) {
131                         if (LOG.isDebugEnabled()) {
132                                 LOG.debug("about to execute Other branch");
133                         }
134                 } else {
135                         if (LOG.isDebugEnabled()) {
136                                 LOG.debug("no " + outValue + " or Other branch found");
137                         }
138                 }
139                 return (nextNode);
140         }
141
142         protected String evaluate(SvcLogicExpression expr, SvcLogicNode node, SvcLogicContext ctx) throws SvcLogicException {
143         return SvcLogicExpressionResolver.evaluate(node.getAttribute("method"), node, ctx);
144     }
145
146     public String mapOutcome(Object o, String emitsOutcome) {
147         if (emitsOutcome != null) {
148             Boolean nodeEmitsOutcome = Boolean.valueOf(emitsOutcome);
149             if (nodeEmitsOutcome) {
150                 return (String) o;
151             } else {
152                 return "success";
153             }
154
155         } else {
156             return "success";
157         }
158     }
159
160 }