Merge "Drop the #1997-related sleep"
[netvirt.git] / openstack / net-virt-providers / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / providers / openflow13 / PipelineOrchestratorImpl.java
1 /*
2  * Copyright (c) 2014, 2015 Red Hat, Inc. and others. All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13;
10
11 import java.util.List;
12 import java.util.Map;
13 import java.util.concurrent.BlockingQueue;
14 import java.util.concurrent.ExecutorService;
15 import java.util.concurrent.Executors;
16 import java.util.concurrent.LinkedBlockingQueue;
17
18 import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
19 import org.opendaylight.ovsdb.openstack.netvirt.api.NodeCacheListener;
20 import org.opendaylight.ovsdb.openstack.netvirt.api.NodeCacheManager;
21 import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound;
22 import org.opendaylight.ovsdb.openstack.netvirt.providers.ConfigInterface;
23 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
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.BundleContext;
26 import org.osgi.framework.ServiceReference;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import com.google.common.collect.Lists;
31 import com.google.common.collect.Maps;
32
33 public class PipelineOrchestratorImpl implements ConfigInterface, NodeCacheListener, PipelineOrchestrator {
34     private static final Logger LOG = LoggerFactory.getLogger(PipelineOrchestratorImpl.class);
35     private List<Service> staticPipeline = Lists.newArrayList(
36             Service.CLASSIFIER,
37             Service.ARP_RESPONDER,
38             Service.INBOUND_NAT,
39             Service.EGRESS_ACL,
40             Service.LOAD_BALANCER,
41             Service.ROUTING,
42             Service.L3_FORWARDING,
43             Service.L2_REWRITE,
44             Service.INGRESS_ACL,
45             Service.OUTBOUND_NAT,
46             Service.L2_FORWARDING
47     );
48     Map<Service, AbstractServiceInstance> serviceRegistry = Maps.newConcurrentMap();
49     private volatile BlockingQueue<Node> queue;
50     private ExecutorService eventHandler;
51     private Southbound southbound;
52
53     public PipelineOrchestratorImpl() {
54         eventHandler = Executors.newSingleThreadExecutor();
55         this.queue = new LinkedBlockingQueue<>();
56         LOG.info("PipelineOrchestratorImpl constructor");
57         start();
58     }
59
60     public void registerService(final ServiceReference ref, AbstractServiceInstance serviceInstance){
61         Service service = (Service)ref.getProperty(AbstractServiceInstance.SERVICE_PROPERTY);
62         LOG.info("registerService {} - {}", serviceInstance, service);
63         serviceRegistry.put(service, serviceInstance);
64     }
65
66     public void unregisterService(final ServiceReference ref) {
67         serviceRegistry.remove(ref.getProperty(AbstractServiceInstance.SERVICE_PROPERTY));
68     }
69     @Override
70     public Service getNextServiceInPipeline(Service service) {
71         int index = staticPipeline.indexOf(service);
72         if (index >= staticPipeline.size() - 1) {
73             return null;
74         }
75         return staticPipeline.get(index + 1);
76     }
77
78     @Override
79     public AbstractServiceInstance getServiceInstance(Service service) {
80         if (service == null) {
81             return null;
82         }
83         return serviceRegistry.get(service);
84     }
85
86     public void start() {
87         eventHandler.submit(new Runnable()  {
88             @Override
89             public void run() {
90                 try {
91                     while (true) {
92                         Node node = queue.take();
93                         LOG.info(">>>>> dequeue: {}", node);
94                         for (Service service : staticPipeline) {
95                             AbstractServiceInstance serviceInstance = getServiceInstance(service);
96                             //LOG.info("pipeline: {} - {}", service, serviceInstance);
97                             if (serviceInstance != null) {
98                                 if (southbound.getBridge(node) != null) {
99                                     serviceInstance.programDefaultPipelineRule(node);
100                                 }
101                             }
102                         }
103                     }
104                 } catch (Exception e) {
105                     LOG.warn("Processing interrupted, terminating ", e);
106                 }
107
108                 while (!queue.isEmpty()) {
109                     queue.poll();
110                 }
111                 queue = null;
112             }
113         });
114     }
115
116     public void stop() {
117         queue.clear();
118         eventHandler.shutdownNow();
119     }
120
121     @Override
122     public void enqueue(Node node) {
123         LOG.info(">>>>> enqueue: {}", node);
124         try {
125             queue.put(node);
126         } catch (InterruptedException e) {
127             LOG.warn("Failed to enqueue operation {}", node, e);
128         }
129     }
130
131     @Override
132     public void notifyNode(Node node, Action action) {
133         if (action == Action.ADD) {
134             enqueue(node);
135         } else {
136             LOG.info("update ignored: {}", node);
137         }
138     }
139
140     @Override
141     public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
142         NodeCacheManager nodeCacheManager =
143                 (NodeCacheManager) ServiceHelper.getGlobalInstance(NodeCacheManager.class, this);
144         nodeCacheManager.cacheListenerAdded(
145                 bundleContext.getServiceReference(PipelineOrchestrator.class.getName()), this);
146         southbound =
147                 (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
148     }
149
150     @Override
151     public void setDependencies(Object impl) {}
152 }