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.api.Action;
22 import org.opendaylight.ovsdb.openstack.netvirt.api.NodeCacheListener;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
24 import org.osgi.framework.ServiceReference;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 public class PipelineOrchestratorImpl implements NodeCacheListener, PipelineOrchestrator {
30 private static final Logger logger = LoggerFactory.getLogger(PipelineOrchestratorImpl.class);
31 private List<Service> staticPipeline = Lists.newArrayList(
33 Service.ARP_RESPONDER,
36 Service.LOAD_BALANCER,
38 Service.L3_FORWARDING,
44 Map<Service, AbstractServiceInstance> serviceRegistry = Maps.newConcurrentMap();
45 private volatile BlockingQueue<Node> queue;
46 private ExecutorService eventHandler;
47 public PipelineOrchestratorImpl() {
50 public void registerService(final ServiceReference ref, AbstractServiceInstance serviceInstance){
51 Service service = (Service)ref.getProperty(AbstractServiceInstance.SERVICE_PROPERTY);
52 logger.info("registerService {} - {}", serviceInstance, service);
53 serviceRegistry.put(service, serviceInstance);
56 public void unregisterService(final ServiceReference ref) {
57 serviceRegistry.remove(ref.getProperty(AbstractServiceInstance.SERVICE_PROPERTY));
60 public Service getNextServiceInPipeline(Service service) {
61 int index = staticPipeline.indexOf(service);
62 if (index >= staticPipeline.size() - 1) return null;
63 return staticPipeline.get(index + 1);
67 public AbstractServiceInstance getServiceInstance(Service service) {
68 if (service == null) return null;
69 return serviceRegistry.get(service);
73 eventHandler = Executors.newSingleThreadExecutor();
74 this.queue = new LinkedBlockingQueue<Node>();
75 logger.info(">>>>> init PipelineOrchestratorImpl");
79 eventHandler.submit(new Runnable() {
84 Node node = queue.take();
86 * Since we are hooking on OpendaylightInventoryListener and as observed in
87 * Bug 1997 multiple Threads trying to write to a same table at the same time
88 * causes programming issues. Hence delaying the programming by a second to
89 * avoid the clash. This hack/workaround should be removed once Bug 1997 is resolved.
91 logger.info(">>>>> dequeue: {}", node);
93 for (Service service : staticPipeline) {
94 AbstractServiceInstance serviceInstance = getServiceInstance(service);
95 logger.info("pipeline: {} - {}", service, serviceInstance);
96 if (serviceInstance != null) {
97 serviceInstance.programDefaultPipelineRule(node);
101 } catch (Exception e) {
102 logger.warn("Processing interrupted, terminating ", e);
105 while (!queue.isEmpty()) {
115 eventHandler.shutdownNow();
119 public void enqueue(Node node) {
120 logger.info(">>>>> enqueue: {}", node);
123 } catch (InterruptedException e) {
124 logger.warn("Failed to enqueue operation {}", node, e);
129 public void notifyNode(Node node, Action action) {