Merge "Clean up Maven dependencies (part 1)"
[netvirt.git] / openstack / net-virt-providers / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / providers / openflow13 / PipelineOrchestratorImpl.java
1 /*
2  * Copyright (C) 2014 Red Hat, Inc.
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  * Authors : Dave Tucker, Madhu Venugopal
9  */
10
11 package org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13;
12
13 import com.google.common.collect.Lists;
14 import com.google.common.collect.Maps;
15 import java.util.List;
16 import java.util.Map;
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.Southbound;
22 import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
23 import org.opendaylight.ovsdb.openstack.netvirt.api.NodeCacheListener;
24 import org.opendaylight.ovsdb.openstack.netvirt.api.NodeCacheManager;
25 import org.opendaylight.ovsdb.openstack.netvirt.providers.ConfigInterface;
26 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
28 import org.osgi.framework.BundleContext;
29 import org.osgi.framework.ServiceReference;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
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                         /*
94                          * Since we are hooking on OpendaylightInventoryListener and as observed in
95                          * Bug 1997 multiple Threads trying to write to a same table at the same time
96                          * causes programming issues. Hence delaying the programming by a second to
97                          * avoid the clash. This hack/workaround should be removed once Bug 1997 is resolved.
98                          */
99                         LOG.info(">>>>> dequeue: {}", node);
100                         Thread.sleep(1000);
101                         for (Service service : staticPipeline) {
102                             AbstractServiceInstance serviceInstance = getServiceInstance(service);
103                             //LOG.info("pipeline: {} - {}", service, serviceInstance);
104                             if (serviceInstance != null) {
105                                 if (southbound.getBridge(node) != null) {
106                                     serviceInstance.programDefaultPipelineRule(node);
107                                 }
108                             }
109                         }
110                     }
111                 } catch (Exception e) {
112                     LOG.warn("Processing interrupted, terminating ", e);
113                 }
114
115                 while (!queue.isEmpty()) {
116                     queue.poll();
117                 }
118                 queue = null;
119             }
120         });
121     }
122
123     public void stop() {
124         queue.clear();
125         eventHandler.shutdownNow();
126     }
127
128     @Override
129     public void enqueue(Node node) {
130         LOG.info(">>>>> enqueue: {}", node);
131         try {
132             queue.put(node);
133         } catch (InterruptedException e) {
134             LOG.warn("Failed to enqueue operation {}", node, e);
135         }
136     }
137
138     @Override
139     public void notifyNode(Node node, Action action) {
140         if (action == Action.ADD) {
141             enqueue(node);
142         } else {
143             LOG.info("update ignored: {}", node);
144         }
145     }
146
147     @Override
148     public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
149         NodeCacheManager nodeCacheManager =
150                 (NodeCacheManager) ServiceHelper.getGlobalInstance(NodeCacheManager.class, this);
151         nodeCacheManager.cacheListenerAdded(
152                 bundleContext.getServiceReference(PipelineOrchestrator.class.getName()), this);
153         southbound =
154                 (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
155     }
156
157     @Override
158     public void setDependencies(Object impl) {}
159 }