ba929bae72689d4e9206d89e59d9c26d5fafd9ff
[ccsdk/sli/core.git] / sli / provider / src / main / java / org / openecomp / sdnc / sli / provider / SvcLogicServiceImpl.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.Properties;
26
27 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
28 import org.openecomp.sdnc.sli.MetricLogger;
29 import org.openecomp.sdnc.sli.SvcLogicContext;
30 import org.openecomp.sdnc.sli.SvcLogicException;
31 import org.openecomp.sdnc.sli.SvcLogicGraph;
32 import org.openecomp.sdnc.sli.SvcLogicNode;
33 import org.openecomp.sdnc.sli.SvcLogicStore;
34 import org.osgi.framework.BundleContext;
35 import org.osgi.framework.FrameworkUtil;
36 import org.osgi.framework.InvalidSyntaxException;
37 import org.osgi.framework.ServiceEvent;
38 import org.osgi.framework.ServiceListener;
39 import org.osgi.framework.ServiceReference;
40 import org.osgi.util.tracker.ServiceTracker;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.slf4j.MDC;
44
45 public class SvcLogicServiceImpl implements SvcLogicService {
46
47         private static final Logger LOG = LoggerFactory
48                         .getLogger(SvcLogicServiceImpl.class);
49
50         private HashMap<String, SvcLogicNodeExecutor> nodeExecutors = null;
51
52         private BundleContext bctx = null;
53
54         private void registerExecutors() {
55
56                 LOG.info("Entered register executors");
57                 if (bctx == null) {
58                         bctx = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
59                 }
60
61                 if (nodeExecutors == null) {
62                         nodeExecutors = new HashMap<String, SvcLogicNodeExecutor>();
63                 }
64
65                 LOG.info("Opening service tracker");
66                 ServiceTracker tracker = new ServiceTracker(bctx,
67                                 SvcLogicNodeExecutor.class.getName(), null);
68                 
69                 tracker.open();
70
71                 ServiceListener listener = new ServiceListener() {
72
73                         public void serviceChanged(ServiceEvent ev) {
74                                 ServiceReference sr = ev.getServiceReference();
75                                 switch (ev.getType()) {
76                                 case ServiceEvent.REGISTERED: {
77                                         registerExecutor(sr);
78                                 }
79                                         break;
80                                 case ServiceEvent.UNREGISTERING: {
81                                         unregisterExecutor(sr);
82                                 }
83                                         break;
84                                 }
85                         }
86                 };
87
88                 LOG.info("Adding service listener");
89                 String filter = "(objectclass=" + SvcLogicNodeExecutor.class.getName()
90                                 + ")";
91                 try {
92                         bctx.addServiceListener(listener, filter);
93                         ServiceReference[] srl = bctx.getServiceReferences(
94                                         SvcLogicNodeExecutor.class.getName(), null);
95                         for (int i = 0; srl != null && i < srl.length; i++) {
96                                 listener.serviceChanged(new ServiceEvent(
97                                                 ServiceEvent.REGISTERED, srl[i]));
98                         }
99                 } catch (InvalidSyntaxException e) {
100                         e.printStackTrace();
101                 }
102                 LOG.info("Done registerExecutors");
103         }
104
105         public void registerExecutor(ServiceReference sr) {
106
107                 String nodeName = (String) sr.getProperty("nodeType");
108                 if (nodeName != null) {
109
110                         SvcLogicNodeExecutor executor = null;
111
112                         try {
113                                 executor = (SvcLogicNodeExecutor) bctx.getService(sr);
114                         } catch (Exception e) {
115                                 LOG.error("Cannot get service executor for " + nodeName);
116                                 return;
117                         }
118
119             registerExecutor(nodeName, executor);
120
121                 }
122         }
123         
124         public void registerExecutor(String nodeName, SvcLogicNodeExecutor executor)
125         {
126                 if (nodeExecutors == null) {
127                         nodeExecutors = new HashMap<String, SvcLogicNodeExecutor>();
128                 }
129                 LOG.info("SLI - registering executor for node type "+nodeName);
130                 nodeExecutors.put(nodeName, executor);
131         }
132
133         public void unregisterExecutor(ServiceReference sr) {
134                 String nodeName = (String) sr.getProperty("nodeType");
135
136                 if (nodeName != null) {
137                         
138              unregisterExecutor(nodeName);
139
140                 }
141
142         }
143         
144         public void unregisterExecutor(String nodeName)
145         {
146
147                 LOG.info("SLI - unregistering executor for node type "+nodeName);
148                 nodeExecutors.remove(nodeName);
149         }
150         
151
152
153         
154         public SvcLogicContext execute(SvcLogicGraph graph, SvcLogicContext ctx)
155                         throws SvcLogicException {
156
157                 if (nodeExecutors == null) {
158                         registerExecutors();
159                 }
160                 
161                 // Set service name in MDC to reference current working directed graph
162                 MDC.put(MetricLogger.SERVICE_NAME, graph.getModule()+":"+graph.getRpc()+"/v"+graph.getVersion());
163
164                 SvcLogicNode curNode = graph.getRootNode();
165                 LOG.info("About to execute graph " + graph.toString());
166                 
167                 
168
169                 while (curNode != null) {
170                         LOG.info("About to execute node # "+curNode.getNodeId()+" ("+curNode.getNodeType()+")");
171                         
172                         SvcLogicNode nextNode = executeNode(curNode, ctx);
173                         curNode = nextNode;
174                 }
175
176                 return (ctx);
177         }
178
179
180         public SvcLogicNode executeNode(SvcLogicNode node, SvcLogicContext ctx)
181                         throws SvcLogicException {
182                 if (node == null) {
183                         return (null);
184                 }
185
186                 if (LOG.isDebugEnabled()) {
187                         LOG.debug("Executing node " + node.getNodeId());
188                 }
189
190                 SvcLogicNodeExecutor executor = nodeExecutors.get(node.getNodeType());
191
192                 if (executor != null) {
193                         LOG.debug("Executing node executor for node type "+node.getNodeType()+" - "+executor.getClass().getName());
194                         return (executor.execute(this, node, ctx));
195                 } else {
196                         if (LOG.isDebugEnabled()) {
197                                 LOG.debug(node.getNodeType() + " node not implemented");
198                         }
199                         SvcLogicNode nextNode = node.getOutcomeValue("failure");
200                         if (nextNode != null) {
201                                 if (LOG.isDebugEnabled()) {
202                                         LOG.debug("about to execute failure branch");
203                                 }
204                                 return (nextNode);
205                         }
206
207                         nextNode = node.getOutcomeValue("Other");
208                         if (nextNode != null) {
209                                 if (LOG.isDebugEnabled()) {
210                                         LOG.debug("about to execute Other branch");
211                                 }
212                         } else {
213                                 if (LOG.isDebugEnabled()) {
214                                         LOG.debug("no failure or Other branch found");
215                                 }
216                         }
217                         return (nextNode);
218                 }
219
220         }
221
222         @Override
223         public boolean hasGraph(String module, String rpc, String version, String mode) throws SvcLogicException
224         {
225                 SvcLogicStore store = SvcLogicActivator.getStore();
226
227                 return (store.hasGraph(module, rpc, version, mode));
228         }
229         
230         @Override
231         public Properties execute(String module, String rpc, String version, String mode, Properties props)
232                         throws SvcLogicException {
233                 return(execute(module, rpc, version, mode, props, null));
234         }
235         
236         
237         @Override
238         public Properties execute(String module, String rpc, String version, String mode,
239                         Properties props, DOMDataBroker domDataBroker) throws SvcLogicException {
240                 
241
242                 // See if there is a service logic defined
243                 //
244                 SvcLogicStore store = SvcLogicActivator.getStore();
245
246                 LOG.info("Fetching service logic from data store");
247                 SvcLogicGraph graph = store.fetch(module, rpc, version, mode);
248                 
249                 
250                 
251                 if (graph == null)
252                 {
253                         Properties retProps = new Properties();
254                         retProps.setProperty("error-code", "401");
255                         retProps.setProperty("error-message", "No service logic found for ["+module+","+rpc+","+version+","+mode+"]");
256                         return(retProps);
257
258                 }
259                 
260                 SvcLogicContext ctx = new SvcLogicContext(props);
261         ctx.setAttribute("currentGraph", graph.toString());
262         ctx.setAttribute("X-ECOMP-RequestID", MDC.get("X-ECOMP-RequestID"));
263                 ctx.setDomDataBroker(domDataBroker);
264                 
265                 execute(graph, ctx);
266
267                 return(ctx.toProperties());
268         }
269         
270
271
272         
273 }