2 * Copyright (C) 2014 Red Hat, Inc.
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
8 * Authors : Dave Tucker, Madhu Venugopal
11 package org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13;
13 import com.google.common.collect.Lists;
14 import com.google.common.collect.Maps;
15 import java.util.List;
17 import java.util.concurrent.BlockingQueue;
18 import java.util.concurrent.ExecutorService;
19 import java.util.concurrent.Executors;
20 import java.util.concurrent.LinkedBlockingQueue;
21 import org.opendaylight.ovsdb.openstack.netvirt.MdsalUtils;
22 import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
23 import org.opendaylight.ovsdb.openstack.netvirt.api.NodeCacheListener;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
25 import org.osgi.framework.ServiceReference;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 public class PipelineOrchestratorImpl implements NodeCacheListener, PipelineOrchestrator {
31 private static final Logger logger = LoggerFactory.getLogger(PipelineOrchestratorImpl.class);
32 private List<Service> staticPipeline = Lists.newArrayList(
34 Service.ARP_RESPONDER,
37 Service.LOAD_BALANCER,
39 Service.L3_FORWARDING,
45 Map<Service, AbstractServiceInstance> serviceRegistry = Maps.newConcurrentMap();
46 private volatile BlockingQueue<Node> queue;
47 private ExecutorService eventHandler;
48 public PipelineOrchestratorImpl() {
51 public void registerService(final ServiceReference ref, AbstractServiceInstance serviceInstance){
52 Service service = (Service)ref.getProperty(AbstractServiceInstance.SERVICE_PROPERTY);
53 logger.info("registerService {} - {}", serviceInstance, service);
54 serviceRegistry.put(service, serviceInstance);
57 public void unregisterService(final ServiceReference ref) {
58 serviceRegistry.remove(ref.getProperty(AbstractServiceInstance.SERVICE_PROPERTY));
61 public Service getNextServiceInPipeline(Service service) {
62 int index = staticPipeline.indexOf(service);
63 if (index >= staticPipeline.size() - 1) return null;
64 return staticPipeline.get(index + 1);
68 public AbstractServiceInstance getServiceInstance(Service service) {
69 if (service == null) return null;
70 return serviceRegistry.get(service);
74 eventHandler = Executors.newSingleThreadExecutor();
75 this.queue = new LinkedBlockingQueue<Node>();
76 logger.info(">>>>> init PipelineOrchestratorImpl");
80 eventHandler.submit(new Runnable() {
85 Node node = queue.take();
87 * Since we are hooking on OpendaylightInventoryListener and as observed in
88 * Bug 1997 multiple Threads trying to write to a same table at the same time
89 * causes programming issues. Hence delaying the programming by a second to
90 * avoid the clash. This hack/workaround should be removed once Bug 1997 is resolved.
92 logger.info(">>>>> dequeue: {}", node);
94 for (Service service : staticPipeline) {
95 AbstractServiceInstance serviceInstance = getServiceInstance(service);
96 //logger.info("pipeline: {} - {}", service, serviceInstance);
97 if (serviceInstance != null) {
98 if (MdsalUtils.getBridge(node) != null) {
99 serviceInstance.programDefaultPipelineRule(node);
104 } catch (Exception e) {
105 logger.warn("Processing interrupted, terminating ", e);
108 while (!queue.isEmpty()) {
118 eventHandler.shutdownNow();
122 public void enqueue(Node node) {
123 logger.info(">>>>> enqueue: {}", node);
126 } catch (InterruptedException e) {
127 logger.warn("Failed to enqueue operation {}", node, e);
132 public void notifyNode(Node node, Action action) {
133 if (action == Action.ADD) {
136 logger.info("update ignored: {}", node);