2 * Copyright (c) 2014, 2015 Red Hat, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13;
11 import java.util.Collections;
12 import java.util.List;
14 import java.util.concurrent.BlockingQueue;
15 import java.util.concurrent.ExecutorService;
16 import java.util.concurrent.Executors;
17 import java.util.concurrent.LinkedBlockingQueue;
19 import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
20 import org.opendaylight.ovsdb.openstack.netvirt.api.NodeCacheListener;
21 import org.opendaylight.ovsdb.openstack.netvirt.api.NodeCacheManager;
22 import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound;
23 import org.opendaylight.ovsdb.openstack.netvirt.providers.ConfigInterface;
24 import org.opendaylight.ovsdb.openstack.netvirt.providers.NetvirtProvidersProvider;
25 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
27 import org.osgi.framework.BundleContext;
28 import org.osgi.framework.ServiceReference;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import com.google.common.collect.Lists;
33 import com.google.common.collect.Maps;
35 public class PipelineOrchestratorImpl implements ConfigInterface, NodeCacheListener, PipelineOrchestrator {
36 private static final Logger LOG = LoggerFactory.getLogger(PipelineOrchestratorImpl.class);
39 * Return the current table offset
40 * @return The table offset
43 public short getTableOffset() {
44 return NetvirtProvidersProvider.getTableOffset();
48 * Return the offset adjusted table for the given {@link Service}
49 * @param service Identifies the openflow {@link Service}
50 * @return The table id
53 public short getTable(Service service) {
54 return (short)(getTableOffset() + service.getTable());
57 public List<Service> getStaticPipeline() {
58 return staticPipeline;
61 private List<Service> staticPipeline = Lists.newArrayList(
63 Service.ARP_RESPONDER,
66 Service.LOAD_BALANCER,
68 Service.L3_FORWARDING,
75 public Map<Service, AbstractServiceInstance> getServiceRegistry() {
76 return serviceRegistry;
79 Map<Service, AbstractServiceInstance> serviceRegistry = Maps.newConcurrentMap();
80 private volatile BlockingQueue<Node> queue;
81 private ExecutorService eventHandler;
82 private Southbound southbound;
84 public PipelineOrchestratorImpl() {
85 eventHandler = Executors.newSingleThreadExecutor();
86 this.queue = new LinkedBlockingQueue<>();
87 LOG.info("PipelineOrchestratorImpl constructor");
91 public void registerService(final ServiceReference ref, AbstractServiceInstance serviceInstance){
92 Service service = (Service)ref.getProperty(AbstractServiceInstance.SERVICE_PROPERTY);
93 LOG.info("registerService {} - {}", serviceInstance, service);
94 serviceRegistry.put(service, serviceInstance);
95 // insert the service if not already there. The list is ordered based of table ID.
96 if (!staticPipeline.contains(service) && !isTableInPipeline(service.getTable())) {
97 staticPipeline.add(service);
98 Collections.sort(staticPipeline, Service.insertComparator);
100 LOG.info("registerService: {}", staticPipeline);
103 private boolean isTableInPipeline (short tableId) {
104 boolean found = false;
105 for (Service service : staticPipeline) {
106 if (service.getTable() == tableId) {
114 public void unregisterService(final ServiceReference ref) {
115 serviceRegistry.remove(ref.getProperty(AbstractServiceInstance.SERVICE_PROPERTY));
118 public Service getNextServiceInPipeline(Service service) {
119 int index = staticPipeline.indexOf(service);
120 if (index >= staticPipeline.size() - 1) {
123 return staticPipeline.get(index + 1);
127 public AbstractServiceInstance getServiceInstance(Service service) {
128 if (service == null) {
131 return serviceRegistry.get(service);
134 public final void start() {
135 eventHandler.submit(new Runnable() {
140 Node node = queue.take();
141 LOG.info(">>>>> dequeue: {}", node);
142 if (southbound.getBridge(node) != null) {
143 for (Service service : staticPipeline) {
144 AbstractServiceInstance serviceInstance = getServiceInstance(service);
145 if (serviceInstance != null) {
146 serviceInstance.programDefaultPipelineRule(node);
149 // TODO: might need a flow to go from table 0 to the pipeline
152 } catch (Exception e) {
153 LOG.warn("Processing interrupted, terminating ", e);
156 while (!queue.isEmpty()) {
166 eventHandler.shutdownNow();
170 public void enqueue(Node node) {
171 LOG.info(">>>>> enqueue: {}", node);
174 } catch (InterruptedException e) {
175 LOG.warn("Failed to enqueue operation {}", node, e);
180 public void notifyNode(Node node, Action action) {
181 if (action == Action.ADD) {
184 LOG.info("update ignored: {}", node);
189 public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
190 NodeCacheManager nodeCacheManager =
191 (NodeCacheManager) ServiceHelper.getGlobalInstance(NodeCacheManager.class, this);
192 nodeCacheManager.cacheListenerAdded(
193 bundleContext.getServiceReference(PipelineOrchestrator.class.getName()), this);
195 (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
199 public void setDependencies(Object impl) {}