logging changes
[ccsdk/sli/core.git] / sli / provider-base / src / main / java / org / onap / ccsdk / sli / core / sli / provider / base / ExecuteNodeExecutor.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Modifications Copyright (C) 2018 IBM.
9  * ================================================================================
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  * 
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  * 
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.ccsdk.sli.core.sli.provider.base;
25
26 import java.lang.reflect.InvocationTargetException;
27 import java.lang.reflect.Method;
28 import java.util.HashMap;
29 import java.util.Iterator;
30 import java.util.Map;
31 import java.util.Set;
32
33 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicExpression;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
37 import org.onap.ccsdk.sli.core.sli.SvcLogicNode;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 public class ExecuteNodeExecutor extends AbstractSvcLogicNodeExecutor {
42         private static final Logger LOG = LoggerFactory
43                         .getLogger(ExecuteNodeExecutor.class);
44         private static final String FAILURE="failure";
45
46         private static final String pluginErrorMessage = "Could not execute plugin. SvcLogic status will be set to failure.";
47         public SvcLogicNode execute(SvcLogicServiceBase 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         SvcLogicJavaPlugin plugin  = getSvcLogicJavaPlugin(pluginName);
60
61                 if (plugin == null) {
62                         outValue = "not-found";
63                 } else {
64
65                         String methodName = evaluate(node.getAttribute("method"),  node, ctx);
66
67                         Class pluginClass = plugin.getClass();
68
69                         Method pluginMethod = null;
70
71                         try {
72                                 pluginMethod = pluginClass.getMethod(methodName, Map.class, SvcLogicContext.class);
73                         } catch (NoSuchMethodException e) {
74                                 LOG.error(pluginErrorMessage, e);
75                         }
76
77                         if (pluginMethod == null) {
78                                 outValue = "unsupported-method";
79                         } else {
80                                 try {
81
82
83                                     Map<String, String> parmMap = getResolvedParameters(node,ctx);
84                                         Object o = pluginMethod.invoke(plugin, parmMap, ctx);
85                                 String emitsOutcome = SvcLogicExpressionResolver.evaluate(node.getAttribute("emitsOutcome"),  node, ctx);
86
87                                         outValue = mapOutcome(o, emitsOutcome);
88
89                                 } catch (InvocationTargetException e) {
90                                     if(e.getCause() != null){
91                             LOG.error(pluginErrorMessage, e.getCause());
92                                     }else{
93                                         LOG.error(pluginErrorMessage, e);
94                                     }
95                                         outValue = FAILURE;
96                                         ctx.setStatus(FAILURE);
97                                 } catch (IllegalAccessException e) {
98                     LOG.error(pluginErrorMessage, e);
99                     outValue = FAILURE;
100                     ctx.setStatus(FAILURE);
101                 } catch (IllegalArgumentException e) {
102                     LOG.error(pluginErrorMessage, e);
103                     outValue = FAILURE;
104                     ctx.setStatus(FAILURE);
105                 }
106                         }
107
108                 }
109         return (getNextNode(node, outValue));
110         }
111
112         protected String evaluate(SvcLogicExpression expr, SvcLogicNode node, SvcLogicContext ctx) throws SvcLogicException {
113         return SvcLogicExpressionResolver.evaluate(node.getAttribute("method"), node, ctx);
114     }
115
116     public String mapOutcome(Object o, String emitsOutcome) {
117         if (emitsOutcome != null) {
118             Boolean nodeEmitsOutcome = Boolean.valueOf(emitsOutcome);
119             if (nodeEmitsOutcome) {
120                 return (String) o;
121             } else {
122                 return "success";
123             }
124         } else {
125             return "success";
126         }
127     }
128
129 }