158c843c54ba9023e2abd06f61ae139e4f44e558
[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.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.openecomp.sdnc.sli.SvcLogicAdaptor;
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.openecomp.sdnc.sli.SvcLogicResource;
37 import org.osgi.framework.BundleContext;
38 import org.osgi.framework.FrameworkUtil;
39 import org.osgi.framework.ServiceReference;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 public class ExecuteNodeExecutor extends SvcLogicNodeExecutor {
44         private static final Logger LOG = LoggerFactory
45                         .getLogger(ExecuteNodeExecutor.class);
46
47         public SvcLogicNode execute(SvcLogicServiceImpl svc, SvcLogicNode node,
48                         SvcLogicContext ctx) throws SvcLogicException {
49
50                 String pluginName = SvcLogicExpressionResolver.evaluate(
51                                 node.getAttribute("plugin"), node, ctx);
52                 String outValue = "failure";
53
54                 if (LOG.isDebugEnabled()) {
55                         LOG.debug("execute node encountered - looking for plugin "
56                                         + pluginName);
57                 }
58                 
59                 BundleContext bctx = FrameworkUtil.getBundle(this.getClass())
60                                 .getBundleContext();
61
62                 ServiceReference sref = bctx.getServiceReference(pluginName);
63
64                 if (sref == null) {
65                         outValue = "not-found";
66                 } else {
67                         SvcLogicJavaPlugin plugin  = (SvcLogicJavaPlugin) bctx
68                                         .getService(sref);
69                         
70                         String methodName = SvcLogicExpressionResolver.evaluate(node.getAttribute("method"),  node, ctx);
71                         
72                         Class pluginClass = plugin.getClass();
73                         
74                         Method pluginMethod = null;
75                         
76                         try {
77                                 pluginMethod = pluginClass.getMethod(methodName, Map.class, SvcLogicContext.class);
78                         } catch (Exception e) {
79                                 LOG.error("Caught exception looking for method "+pluginName+"."+methodName+"(Map, SvcLogicContext)");
80                         }
81                         
82                         if (pluginMethod == null) {
83                                 outValue = "unsupported-method";
84                         } else {
85                                 try {
86                                         
87                                         Map<String, String> parmMap = new HashMap<String, String>();
88
89                                         Set<Map.Entry<String, SvcLogicExpression>> parmSet = node
90                                                         .getParameterSet();
91
92                                         for (Iterator<Map.Entry<String, SvcLogicExpression>> iter = parmSet
93                                                         .iterator(); iter.hasNext();) {
94                                                 Map.Entry<String, SvcLogicExpression> curEnt = iter.next();
95                                                 String curName = curEnt.getKey();
96                                                 SvcLogicExpression curExpr = curEnt.getValue();
97                                                 String curExprValue = SvcLogicExpressionResolver.evaluate(curExpr, node, ctx);
98                                                 
99                                                 LOG.debug("Parameter "+curName+" = "+curExpr.asParsedExpr()+" resolves to "+curExprValue);
100
101                                                 parmMap.put(curName,curExprValue);
102                                         }
103                                         
104                                         pluginMethod.invoke(plugin, parmMap, ctx);
105                                         
106                                         outValue = "success";
107                                 } catch (Exception e) {
108                                         LOG.error("Caught exception executing "+pluginName+"."+methodName, e);
109                                         
110                                         outValue = "failure";
111                                         ctx.setStatus("failure");
112                                 }
113                         }
114
115                 }
116
117                 SvcLogicNode nextNode = node.getOutcomeValue(outValue);
118                 if (nextNode != null) {
119                         if (LOG.isDebugEnabled()) {
120                                 LOG.debug("about to execute " + outValue + " branch");
121                         }
122                         return (nextNode);
123                 }
124
125                 nextNode = node.getOutcomeValue("Other");
126                 if (nextNode != null) {
127                         if (LOG.isDebugEnabled()) {
128                                 LOG.debug("about to execute Other branch");
129                         }
130                 } else {
131                         if (LOG.isDebugEnabled()) {
132                                 LOG.debug("no " + outValue + " or Other branch found");
133                         }
134                 }
135                 return (nextNode);
136         }
137
138 }