[SDNC-5] Rebase sdnc-core
[sdnc/core.git] / sli / provider / src / main / java / org / openecomp / sdnc / sli / provider / RecordNodeExecutor.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.util.HashMap;
25 import java.util.Iterator;
26 import java.util.Map;
27 import java.util.Set;
28
29 import org.openecomp.sdnc.sli.SvcLogicContext;
30 import org.openecomp.sdnc.sli.SvcLogicException;
31 import org.openecomp.sdnc.sli.SvcLogicExpression;
32 import org.openecomp.sdnc.sli.SvcLogicNode;
33 import org.openecomp.sdnc.sli.SvcLogicRecorder;
34 import org.osgi.framework.BundleContext;
35 import org.osgi.framework.FrameworkUtil;
36 import org.osgi.framework.ServiceReference;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 public class RecordNodeExecutor extends SvcLogicNodeExecutor {
41
42         private static final Logger LOG = LoggerFactory
43                         .getLogger(RecordNodeExecutor.class);
44         
45         @Override
46         public SvcLogicNode execute(SvcLogicServiceImpl svc, SvcLogicNode node,
47                         SvcLogicContext ctx) throws SvcLogicException {
48
49                 String plugin = SvcLogicExpressionResolver.evaluate(
50                                 node.getAttribute("plugin"), node, ctx);
51                 String outValue = "failure";
52
53                 if (LOG.isTraceEnabled()) {
54                         LOG.trace(node.getNodeType()
55                                         + " node encountered - looking for recorder class "
56                                         + plugin);
57                 }
58
59                 Map<String, String> parmMap = new HashMap<String, String>();
60
61                 Set<Map.Entry<String, SvcLogicExpression>> parmSet = node
62                                 .getParameterSet();
63                 boolean hasParms = false;
64
65                 for (Iterator<Map.Entry<String, SvcLogicExpression>> iter = parmSet
66                                 .iterator(); iter.hasNext();) {
67                         hasParms = true;
68                         Map.Entry<String, SvcLogicExpression> curEnt = iter.next();
69                         String curName = curEnt.getKey();
70                         SvcLogicExpression curExpr = curEnt.getValue();
71                         String curExprValue = SvcLogicExpressionResolver.evaluate(curExpr,
72                                         node, ctx);
73
74                         if (LOG.isTraceEnabled()) {
75                                 LOG.trace("executeRecordNode : parameter " + curName + " = "
76                                                 + curExpr + " => " + curExprValue);
77                         }
78                         parmMap.put(curName, curExprValue);
79                 }
80
81
82                         SvcLogicRecorder recorder = getSvcLogicResource(plugin);
83
84                         if (recorder != null) {
85
86                                 try {
87                                         recorder.record(parmMap);
88                                 } catch (SvcLogicException e) {
89                                         LOG.error("Caught exception from recorder plugin", e);
90                                         outValue = "failure";
91                                 }
92                         } else {
93                                 LOG.warn("Could not find SvcLogicRecorder object for plugin "
94                                                 + plugin);
95                         }
96
97                 SvcLogicNode nextNode = node.getOutcomeValue(outValue);
98                 if (nextNode != null) {
99                         if (LOG.isDebugEnabled()) {
100                                 LOG.debug("about to execute " + outValue + " branch");
101                         }
102                         return (nextNode);
103                 }
104
105                 nextNode = node.getOutcomeValue("Other");
106                 if (nextNode != null) {
107                         if (LOG.isDebugEnabled()) {
108                                 LOG.debug("about to execute Other branch");
109                         }
110                 } else {
111                         if (LOG.isTraceEnabled()) {
112                                 LOG.trace("no failure or Other branch found");
113                         }
114                 }
115                 return (nextNode);
116         }
117
118     protected SvcLogicRecorder getSvcLogicResource(String plugin) {
119         BundleContext bctx = FrameworkUtil.getBundle(this.getClass())
120                 .getBundleContext();
121
122         ServiceReference sref = bctx.getServiceReference(plugin);
123         if (sref != null) {
124             SvcLogicRecorder resourcePlugin = (SvcLogicRecorder) bctx
125                     .getService(sref);
126             return resourcePlugin;
127         }
128         else {
129             return null;
130         }
131     }
132
133
134
135 }