2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.ccsdk.sli.core.sli.provider;
23 import java.util.HashMap;
24 import java.util.Properties;
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;
44 public class SvcLogicServiceImpl implements SvcLogicService {
46 private static final Logger LOG = LoggerFactory
47 .getLogger(SvcLogicServiceImpl.class);
49 private HashMap<String, SvcLogicNodeExecutor> nodeExecutors = null;
51 private BundleContext bctx = null;
53 private void registerExecutors() {
55 LOG.info("Entered register executors");
57 bctx = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
60 if (nodeExecutors == null) {
61 nodeExecutors = new HashMap<String, SvcLogicNodeExecutor>();
64 LOG.info("Opening service tracker");
65 ServiceTracker tracker = new ServiceTracker(bctx,
66 SvcLogicNodeExecutor.class.getName(), null);
70 ServiceListener listener = new ServiceListener() {
72 public void serviceChanged(ServiceEvent ev) {
73 ServiceReference sr = ev.getServiceReference();
74 switch (ev.getType()) {
75 case ServiceEvent.REGISTERED: {
79 case ServiceEvent.UNREGISTERING: {
80 unregisterExecutor(sr);
87 LOG.info("Adding service listener");
88 String filter = "(objectclass=" + SvcLogicNodeExecutor.class.getName()
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]));
98 } catch (InvalidSyntaxException e) {
101 LOG.info("Done registerExecutors");
104 public void registerExecutor(ServiceReference sr) {
106 String nodeName = (String) sr.getProperty("nodeType");
107 if (nodeName != null) {
109 SvcLogicNodeExecutor executor = null;
112 executor = (SvcLogicNodeExecutor) bctx.getService(sr);
113 } catch (Exception e) {
114 LOG.error("Cannot get service executor for " + nodeName);
118 registerExecutor(nodeName, executor);
123 public void registerExecutor(String nodeName, SvcLogicNodeExecutor executor)
125 if (nodeExecutors == null) {
126 nodeExecutors = new HashMap<String, SvcLogicNodeExecutor>();
128 LOG.info("SLI - registering executor for node type "+nodeName);
129 nodeExecutors.put(nodeName, executor);
132 public void unregisterExecutor(ServiceReference sr) {
133 String nodeName = (String) sr.getProperty("nodeType");
135 if (nodeName != null) {
137 unregisterExecutor(nodeName);
143 public void unregisterExecutor(String nodeName)
146 LOG.info("SLI - unregistering executor for node type "+nodeName);
147 nodeExecutors.remove(nodeName);
153 public SvcLogicContext execute(SvcLogicGraph graph, SvcLogicContext ctx)
154 throws SvcLogicException {
156 if (nodeExecutors == null) {
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());
163 SvcLogicNode curNode = graph.getRootNode();
164 LOG.info("About to execute graph " + graph.toString());
168 while (curNode != null) {
169 LOG.info("About to execute node # "+curNode.getNodeId()+" ("+curNode.getNodeType()+")");
171 SvcLogicNode nextNode = executeNode(curNode, ctx);
179 public SvcLogicNode executeNode(SvcLogicNode node, SvcLogicContext ctx)
180 throws SvcLogicException {
185 if (LOG.isDebugEnabled()) {
186 LOG.debug("Executing node " + node.getNodeId());
189 SvcLogicNodeExecutor executor = nodeExecutors.get(node.getNodeType());
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));
195 if (LOG.isDebugEnabled()) {
196 LOG.debug(node.getNodeType() + " node not implemented");
198 SvcLogicNode nextNode = node.getOutcomeValue("failure");
199 if (nextNode != null) {
200 if (LOG.isDebugEnabled()) {
201 LOG.debug("about to execute failure branch");
206 nextNode = node.getOutcomeValue("Other");
207 if (nextNode != null) {
208 if (LOG.isDebugEnabled()) {
209 LOG.debug("about to execute Other branch");
212 if (LOG.isDebugEnabled()) {
213 LOG.debug("no failure or Other branch found");
222 public boolean hasGraph(String module, String rpc, String version, String mode) throws SvcLogicException
224 SvcLogicStore store = SvcLogicActivator.getStore();
226 return (store.hasGraph(module, rpc, version, mode));
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));
237 public Properties execute(String module, String rpc, String version, String mode,
238 Properties props, DOMDataBroker domDataBroker) throws SvcLogicException {
241 // See if there is a service logic defined
243 SvcLogicStore store = SvcLogicActivator.getStore();
245 LOG.info("Fetching service logic from data store");
246 SvcLogicGraph graph = store.fetch(module, rpc, version, mode);
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+"]");
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);
266 return(ctx.toProperties());