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