2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.openecomp.sdnc.sli.provider;
24 import java.util.HashMap;
25 import java.util.Properties;
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;
45 public class SvcLogicServiceImpl implements SvcLogicService {
47 private static final Logger LOG = LoggerFactory
48 .getLogger(SvcLogicServiceImpl.class);
50 private HashMap<String, SvcLogicNodeExecutor> nodeExecutors = null;
52 private BundleContext bctx = null;
54 private void registerExecutors() {
56 LOG.info("Entered register executors");
58 bctx = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
61 if (nodeExecutors == null) {
62 nodeExecutors = new HashMap<String, SvcLogicNodeExecutor>();
65 LOG.info("Opening service tracker");
66 ServiceTracker tracker = new ServiceTracker(bctx,
67 SvcLogicNodeExecutor.class.getName(), null);
71 ServiceListener listener = new ServiceListener() {
73 public void serviceChanged(ServiceEvent ev) {
74 ServiceReference sr = ev.getServiceReference();
75 switch (ev.getType()) {
76 case ServiceEvent.REGISTERED: {
80 case ServiceEvent.UNREGISTERING: {
81 unregisterExecutor(sr);
88 LOG.info("Adding service listener");
89 String filter = "(objectclass=" + SvcLogicNodeExecutor.class.getName()
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]));
99 } catch (InvalidSyntaxException e) {
102 LOG.info("Done registerExecutors");
105 public void registerExecutor(ServiceReference sr) {
107 String nodeName = (String) sr.getProperty("nodeType");
108 if (nodeName != null) {
110 SvcLogicNodeExecutor executor = null;
113 executor = (SvcLogicNodeExecutor) bctx.getService(sr);
114 } catch (Exception e) {
115 LOG.error("Cannot get service executor for " + nodeName);
119 registerExecutor(nodeName, executor);
124 public void registerExecutor(String nodeName, SvcLogicNodeExecutor executor)
126 if (nodeExecutors == null) {
127 nodeExecutors = new HashMap<String, SvcLogicNodeExecutor>();
129 LOG.info("SLI - registering executor for node type "+nodeName);
130 nodeExecutors.put(nodeName, executor);
133 public void unregisterExecutor(ServiceReference sr) {
134 String nodeName = (String) sr.getProperty("nodeType");
136 if (nodeName != null) {
138 unregisterExecutor(nodeName);
144 public void unregisterExecutor(String nodeName)
147 LOG.info("SLI - unregistering executor for node type "+nodeName);
148 nodeExecutors.remove(nodeName);
154 public SvcLogicContext execute(SvcLogicGraph graph, SvcLogicContext ctx)
155 throws SvcLogicException {
157 if (nodeExecutors == null) {
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());
164 SvcLogicNode curNode = graph.getRootNode();
165 LOG.info("About to execute graph " + graph.toString());
169 while (curNode != null) {
170 LOG.info("About to execute node # "+curNode.getNodeId()+" ("+curNode.getNodeType()+")");
172 SvcLogicNode nextNode = executeNode(curNode, ctx);
180 public SvcLogicNode executeNode(SvcLogicNode node, SvcLogicContext ctx)
181 throws SvcLogicException {
186 if (LOG.isDebugEnabled()) {
187 LOG.debug("Executing node " + node.getNodeId());
190 SvcLogicNodeExecutor executor = nodeExecutors.get(node.getNodeType());
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));
196 if (LOG.isDebugEnabled()) {
197 LOG.debug(node.getNodeType() + " node not implemented");
199 SvcLogicNode nextNode = node.getOutcomeValue("failure");
200 if (nextNode != null) {
201 if (LOG.isDebugEnabled()) {
202 LOG.debug("about to execute failure branch");
207 nextNode = node.getOutcomeValue("Other");
208 if (nextNode != null) {
209 if (LOG.isDebugEnabled()) {
210 LOG.debug("about to execute Other branch");
213 if (LOG.isDebugEnabled()) {
214 LOG.debug("no failure or Other branch found");
223 public boolean hasGraph(String module, String rpc, String version, String mode) throws SvcLogicException
225 SvcLogicStore store = SvcLogicActivator.getStore();
227 return (store.hasGraph(module, rpc, version, mode));
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));
238 public Properties execute(String module, String rpc, String version, String mode,
239 Properties props, DOMDataBroker domDataBroker) throws SvcLogicException {
242 // See if there is a service logic defined
244 SvcLogicStore store = SvcLogicActivator.getStore();
246 LOG.info("Fetching service logic from data store");
247 SvcLogicGraph graph = store.fetch(module, rpc, version, mode);
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+"]");
260 if (nodeExecutors == null) {
261 LOG.info("Start: registering node executors");
264 LOG.info("Done: registering node executors");
267 LOG.info("About to execute graph " + graph.toString());
269 LOG.info("Executing root node");
270 SvcLogicContext ctx = new SvcLogicContext(props);
271 ctx.setDomDataBroker(domDataBroker);
272 SvcLogicNode curNode = graph.getRootNode();
274 while (curNode != null) {
275 LOG.info("About to execute node # "+curNode.getNodeId()+" ("+curNode.getNodeType()+")");
276 SvcLogicNode nextNode = executeNode(curNode, ctx);
281 return(ctx.toProperties());