logging changes
[ccsdk/sli/core.git] / sli / provider-base / src / main / java / org / onap / ccsdk / sli / core / sli / provider / base / AbstractSvcLogicNodeExecutor.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK
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.onap.ccsdk.sli.core.sli.provider.base;
23
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.Map;
27 import java.util.Set;
28
29 import org.onap.ccsdk.sli.core.sli.SvcLogicAdaptor;
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.onap.ccsdk.sli.core.sli.SvcLogicRecorder;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 public abstract class AbstractSvcLogicNodeExecutor {
41         protected SvcLogicResolver resolver;
42         public abstract SvcLogicNode execute(SvcLogicServiceBase svc, SvcLogicNode node, SvcLogicContext ctx) throws SvcLogicException;
43
44         private static final Logger LOG = LoggerFactory.getLogger(AbstractSvcLogicNodeExecutor.class);
45         protected static final String PARAMETER_DEBUG_PATTERN = "Parameter: {} resolves to: {} which came from the expression: {}";
46     protected static final String SETTING_DEBUG_PATTERN = "Setting context attribute: {} to: {} which came from the expression: {}";
47
48     protected String evaluateNodeTest(SvcLogicNode node, SvcLogicContext ctx)
49                         throws SvcLogicException {
50                 if (node == null) {
51                         return null;
52                 }
53
54                 return (SvcLogicExpressionResolver.evaluate(node.getAttribute("test"),
55                                 node, ctx));
56
57         }
58
59     public void setResolver(SvcLogicResolver resolver) {
60                 this.resolver = resolver;
61         }
62
63         protected SvcLogicAdaptor getAdaptor(String adaptorName) {
64         return resolver.getSvcLogicAdaptor(adaptorName);
65     }
66
67     protected SvcLogicResource getSvcLogicResource(String resourceName) {
68         return resolver.getSvcLogicResource(resourceName);
69     }
70
71     protected SvcLogicRecorder getSvcLogicRecorder(String recorderName) {
72         return resolver.getSvcLogicRecorder(recorderName);
73     }
74
75     protected SvcLogicJavaPlugin getSvcLogicJavaPlugin(String pluginName){
76         return resolver.getSvcLogicJavaPlugin(pluginName);
77     }
78
79     protected SvcLogicNode getNextNode(SvcLogicNode node, String outValue) {
80         SvcLogicNode nextNode = node.getOutcomeValue(outValue);
81         if (nextNode != null) {
82             if (LOG.isDebugEnabled()) {
83                 LOG.debug("about to execute " + outValue + " branch");
84             }
85             return (nextNode);
86         }
87
88         nextNode = node.getOutcomeValue("Other");
89         if (nextNode != null) {
90             if (LOG.isDebugEnabled()) {
91                 LOG.debug("about to execute Other branch");
92             }
93         } else {
94             if (LOG.isDebugEnabled()) {
95                 LOG.debug("no " + outValue + " or Other branch found");
96             }
97         }
98         return (nextNode);
99     }
100     
101     protected Map<String, String> getResolvedParameters(SvcLogicNode node, SvcLogicContext ctx) throws SvcLogicException{
102         Map<String, String> parmMap = new HashMap<>();
103
104         Set<Map.Entry<String, SvcLogicExpression>> parmSet = node
105                 .getParameterSet();
106
107         for (Iterator<Map.Entry<String, SvcLogicExpression>> iter = parmSet
108                 .iterator(); iter.hasNext();) {
109             Map.Entry<String, SvcLogicExpression> curEnt = iter.next();
110             String curName = curEnt.getKey();
111             SvcLogicExpression curExpr = curEnt.getValue();
112             String curExprValue = SvcLogicExpressionResolver.evaluate(curExpr, node, ctx);
113             LOG.trace(PARAMETER_DEBUG_PATTERN, curName, curExprValue, curExpr.toString());
114             parmMap.put(curName,curExprValue);
115         }
116         
117         return parmMap;
118     }
119     
120 }