2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.openecomp.sdnc.sli.provider;
24 import java.lang.reflect.InvocationTargetException;
25 import java.lang.reflect.Method;
26 import java.util.HashMap;
27 import java.util.Iterator;
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;
44 public class ExecuteNodeExecutor extends SvcLogicNodeExecutor {
45 private static final Logger LOG = LoggerFactory
46 .getLogger(ExecuteNodeExecutor.class);
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 {
52 String pluginName = SvcLogicExpressionResolver.evaluate(
53 node.getAttribute("plugin"), node, ctx);
54 String outValue = "failure";
56 if (LOG.isDebugEnabled()) {
57 LOG.debug("execute node encountered - looking for plugin "
61 SvcLogicJavaPlugin plugin = getSvcLogicJavaPlugin(pluginName);
64 outValue = "not-found";
67 String methodName = evaluate(node.getAttribute("method"), node, ctx);
69 Class pluginClass = plugin.getClass();
71 Method pluginMethod = null;
74 pluginMethod = pluginClass.getMethod(methodName, Map.class, SvcLogicContext.class);
75 } catch (NoSuchMethodException e) {
76 LOG.error(pluginErrorMessage, e);
79 if (pluginMethod == null) {
80 outValue = "unsupported-method";
84 Map<String, String> parmMap = new HashMap<String, String>();
86 Set<Map.Entry<String, SvcLogicExpression>> parmSet = node
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);
96 LOG.debug("Parameter "+curName+" = "+curExpr.asParsedExpr()+" resolves to "+curExprValue);
98 parmMap.put(curName,curExprValue);
101 Object o = pluginMethod.invoke(plugin, parmMap, ctx);
102 String emitsOutcome = SvcLogicExpressionResolver.evaluate(node.getAttribute("emitsOutcome"), node, ctx);
104 outValue = mapOutcome(o, emitsOutcome);
106 } catch (InvocationTargetException e) {
107 if(e.getCause() != null){
108 LOG.error(pluginErrorMessage, e.getCause());
110 LOG.error(pluginErrorMessage, e);
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");
127 SvcLogicNode nextNode = node.getOutcomeValue(outValue);
128 if (nextNode != null) {
129 if (LOG.isDebugEnabled()) {
130 LOG.debug("about to execute " + outValue + " branch");
135 nextNode = node.getOutcomeValue("Other");
136 if (nextNode != null) {
137 if (LOG.isDebugEnabled()) {
138 LOG.debug("about to execute Other branch");
141 if (LOG.isDebugEnabled()) {
142 LOG.debug("no " + outValue + " or Other branch found");
148 protected SvcLogicJavaPlugin getSvcLogicJavaPlugin(String pluginName){
149 BundleContext bctx = FrameworkUtil.getBundle(this.getClass())
152 ServiceReference sref = bctx.getServiceReference(pluginName);
155 LOG.warn("Could not find service reference object for plugin " + pluginName);
158 SvcLogicJavaPlugin plugin = (SvcLogicJavaPlugin) bctx
163 protected String evaluate(SvcLogicExpression expr, SvcLogicNode node, SvcLogicContext ctx) throws SvcLogicException {
164 return SvcLogicExpressionResolver.evaluate(node.getAttribute("method"), node, ctx);
167 public String mapOutcome(Object o, String emitsOutcome) {
168 if (emitsOutcome != null) {
169 Boolean nodeEmitsOutcome = Boolean.valueOf(emitsOutcome);
170 if (nodeEmitsOutcome) {