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