Merge "Init bundle files for ovs-sfc."
[ovsdb.git] / openstack / net-virt / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / SouthboundHandler.java
1 /*
2  * Copyright (C) 2013 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 : Madhu Venugopal, Brent Salisbury, Sam Hague, Dave Tucker
9  */
10 package org.opendaylight.ovsdb.openstack.netvirt;
11
12 import org.opendaylight.controller.networkconfig.neutron.NeutronNetwork;
13 import org.opendaylight.controller.sal.core.Node;
14 import org.opendaylight.controller.sal.core.NodeConnector;
15 import org.opendaylight.controller.sal.core.Property;
16 import org.opendaylight.controller.sal.core.UpdateType;
17 import org.opendaylight.controller.switchmanager.IInventoryListener;
18 import org.opendaylight.ovsdb.lib.notation.Row;
19 import org.opendaylight.ovsdb.lib.notation.UUID;
20 import org.opendaylight.ovsdb.openstack.netvirt.api.BridgeConfigurationManager;
21 import org.opendaylight.ovsdb.openstack.netvirt.api.ConfigurationService;
22 import org.opendaylight.ovsdb.openstack.netvirt.api.NetworkingProviderManager;
23 import org.opendaylight.ovsdb.openstack.netvirt.api.TenantNetworkManager;
24 import org.opendaylight.ovsdb.plugin.IConnectionServiceInternal;
25 import org.opendaylight.ovsdb.plugin.OvsdbConfigService;
26 import org.opendaylight.ovsdb.plugin.OvsdbInventoryListener;
27 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
28 import org.opendaylight.ovsdb.schema.openvswitch.OpenVSwitch;
29 import org.opendaylight.ovsdb.schema.openvswitch.Port;
30
31 import com.google.common.collect.Lists;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Set;
38 import java.util.concurrent.BlockingQueue;
39 import java.util.concurrent.ConcurrentMap;
40 import java.util.concurrent.ExecutorService;
41 import java.util.concurrent.Executors;
42 import java.util.concurrent.LinkedBlockingQueue;
43
44 public class SouthboundHandler extends AbstractHandler implements OvsdbInventoryListener, IInventoryListener {
45     static final Logger logger = LoggerFactory.getLogger(SouthboundHandler.class);
46     //private Thread eventThread;
47     private ExecutorService eventHandler;
48     private BlockingQueue<SouthboundEvent> events;
49     List<Node> nodeCache;
50
51     // The implementation for each of these services is resolved by the OSGi Service Manager
52     private volatile ConfigurationService configurationService;
53     private volatile BridgeConfigurationManager bridgeConfigurationManager;
54     private volatile TenantNetworkManager tenantNetworkManager;
55     private volatile NetworkingProviderManager networkingProviderManager;
56     private volatile OvsdbConfigService ovsdbConfigService;
57     private volatile IConnectionServiceInternal connectionService;
58
59     void init() {
60         eventHandler = Executors.newSingleThreadExecutor();
61         this.events = new LinkedBlockingQueue<>();
62         nodeCache = Lists.newArrayList();
63     }
64
65     void start() {
66         eventHandler.submit(new Runnable()  {
67             @Override
68             public void run() {
69                 while (true) {
70                     SouthboundEvent ev;
71                     try {
72                         ev = events.take();
73                     } catch (InterruptedException e) {
74                         logger.info("The event handler thread was interrupted, shutting down", e);
75                         return;
76                     }
77                     switch (ev.getType()) {
78                     case NODE:
79                         try {
80                             processNodeUpdate(ev.getNode(), ev.getAction());
81                         } catch (Exception e) {
82                             logger.error("Exception caught in ProcessNodeUpdate for node " + ev.getNode(), e);
83                         }
84                         break;
85                     case ROW:
86                         try {
87                             processRowUpdate(ev.getNode(), ev.getTableName(), ev.getUuid(), ev.getRow(),
88                                              ev.getContext(),ev.getAction());
89                         } catch (Exception e) {
90                             logger.error("Exception caught in ProcessRowUpdate for node " + ev.getNode(), e);
91                         }
92                         break;
93                     default:
94                         logger.warn("Unable to process action " + ev.getAction() + " for node " + ev.getNode());
95                     }
96                 }
97             }
98         });
99         this.triggerUpdates();
100     }
101
102     void stop() {
103         eventHandler.shutdownNow();
104     }
105
106     @Override
107     public void nodeAdded(Node node) {
108         this.enqueueEvent(new SouthboundEvent(node, SouthboundEvent.Action.ADD));
109     }
110
111     @Override
112     public void nodeRemoved(Node node) {
113         this.enqueueEvent(new SouthboundEvent(node, SouthboundEvent.Action.DELETE));
114     }
115
116     @Override
117     public void rowAdded(Node node, String tableName, String uuid, Row row) {
118         this.enqueueEvent(new SouthboundEvent(node, tableName, uuid, row, SouthboundEvent.Action.ADD));
119     }
120
121     @Override
122     public void rowUpdated(Node node, String tableName, String uuid, Row oldRow, Row newRow) {
123         if (this.isUpdateOfInterest(node, oldRow, newRow)) {
124             this.enqueueEvent(new SouthboundEvent(node, tableName, uuid, newRow, SouthboundEvent.Action.UPDATE));
125         }
126     }
127
128     /*
129      * Ignore unneccesary updates to be even considered for processing.
130      * (Especially stats update are fast and furious).
131      */
132
133     private boolean isUpdateOfInterest(Node node, Row oldRow, Row newRow) {
134         if (oldRow == null) return true;
135         if (newRow.getTableSchema().getName().equals(ovsdbConfigService.getTableName(node, Interface.class))) {
136             // We are NOT interested in Stats only updates
137             Interface oldIntf = ovsdbConfigService.getTypedRow(node, Interface.class, oldRow);
138             if (oldIntf.getName() == null && oldIntf.getExternalIdsColumn() == null && oldIntf.getMacColumn() == null &&
139                 oldIntf.getOpenFlowPortColumn() == null && oldIntf.getOptionsColumn() == null && oldIntf.getOtherConfigColumn() == null &&
140                 oldIntf.getTypeColumn() == null) {
141                 logger.trace("IGNORING Interface Update: node {}, row: {}", node, newRow);
142                 return false;
143             }
144         } else if (newRow.getTableSchema().getName().equals(ovsdbConfigService.getTableName(node, Port.class))) {
145             // We are NOT interested in Stats only updates
146             Port oldPort = ovsdbConfigService.getTypedRow(node, Port.class, oldRow);
147             if (oldPort.getName() == null && oldPort.getExternalIdsColumn() == null && oldPort.getMacColumn() == null &&
148                 oldPort.getInterfacesColumn() == null && oldPort.getTagColumn() == null && oldPort.getTrunksColumn() == null) {
149                 logger.trace("IGNORING Port Update: node {}, row: {}", node, newRow);
150                 return false;
151             }
152         } else if (newRow.getTableSchema().getName().equals(ovsdbConfigService.getTableName(node, OpenVSwitch.class))) {
153             OpenVSwitch oldOpenvSwitch = ovsdbConfigService.getTypedRow(node, OpenVSwitch.class, oldRow);
154             if (oldOpenvSwitch.getOtherConfigColumn()== null) {
155                 /* we are only interested in other_config field change */
156                 return false;
157             }
158         }
159         return true;
160     }
161
162     @Override
163     public void rowRemoved(Node node, String tableName, String uuid, Row row, Object context) {
164         this.enqueueEvent(new SouthboundEvent(node, tableName, uuid, row, context, SouthboundEvent.Action.DELETE));
165     }
166
167     private void enqueueEvent (SouthboundEvent event) {
168         try {
169             events.put(event);
170         } catch (InterruptedException e) {
171             logger.error("Thread was interrupted while trying to enqueue event ", e);
172         }
173     }
174
175     public void processNodeUpdate(Node node, SouthboundEvent.Action action) {
176         if (action == SouthboundEvent.Action.DELETE) return;
177         logger.trace("Process Node added {}", node);
178         bridgeConfigurationManager.prepareNode(node);
179     }
180
181     private void processRowUpdate(Node node, String tableName, String uuid, Row row,
182                                   Object context, SouthboundEvent.Action action) {
183         if (action == SouthboundEvent.Action.DELETE) {
184             if (tableName.equalsIgnoreCase(ovsdbConfigService.getTableName(node, Interface.class))) {
185                 logger.debug("Processing update of {}. Deleted node: {}, uuid: {}, row: {}", tableName, node, uuid, row);
186                 Interface deletedIntf = ovsdbConfigService.getTypedRow(node, Interface.class, row);
187                 NeutronNetwork network = null;
188                 if (context == null) {
189                     network = tenantNetworkManager.getTenantNetwork(deletedIntf);
190                 } else {
191                     network = (NeutronNetwork)context;
192                 }
193                 List<String> phyIfName = bridgeConfigurationManager.getAllPhysicalInterfaceNames(node);
194                 logger.info("Delete interface " + deletedIntf.getName());
195
196                 if (deletedIntf.getTypeColumn().getData().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VXLAN) ||
197                     deletedIntf.getTypeColumn().getData().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_GRE) ||
198                     phyIfName.contains(deletedIntf.getName())) {
199                     /* delete tunnel interfaces or physical interfaces */
200                     this.handleInterfaceDelete(node, uuid, deletedIntf, false, null);
201                 } else if (network != null && !network.getRouterExternal()) {
202                     logger.debug("Processing update of {}:{} node {} intf {} network {}",
203                             tableName, action, node, uuid, network.getNetworkUUID());
204                     try {
205                         ConcurrentMap<String, Row> interfaces = this.ovsdbConfigService.getRows(node, ovsdbConfigService.getTableName(node, Interface.class));
206                         if (interfaces != null) {
207                             boolean isLastInstanceOnNode = true;
208                             for (String intfUUID : interfaces.keySet()) {
209                                 if (intfUUID.equals(uuid)) continue;
210                                 Interface intf = this.ovsdbConfigService.getTypedRow(node, Interface.class, interfaces.get(intfUUID));
211                                 NeutronNetwork neutronNetwork = tenantNetworkManager.getTenantNetwork(intf);
212                                 if (neutronNetwork != null && neutronNetwork.equals(network)) isLastInstanceOnNode = false;
213                             }
214                             this.handleInterfaceDelete(node, uuid, deletedIntf, isLastInstanceOnNode, network);
215                         }
216                     } catch (Exception e) {
217                         logger.error("Error fetching Interface Rows for node " + node, e);
218                     }
219                 }
220             }
221         }
222         else if (tableName.equalsIgnoreCase(ovsdbConfigService.getTableName(node, Interface.class))) {
223             logger.debug("Processing update of {}:{} node: {}, interface uuid: {}, row: {}",
224                     tableName, action, node, uuid, row);
225             Interface intf = this.ovsdbConfigService.getTypedRow(node, Interface.class, row);
226             NeutronNetwork network = tenantNetworkManager.getTenantNetwork(intf);
227             if (network != null && !network.getRouterExternal()) {
228                 if (networkingProviderManager.getProvider(node).hasPerTenantTunneling()) {
229                     int vlan = tenantNetworkManager.networkCreated(node, network.getID());
230                     String portUUID = this.getPortIdForInterface(node, uuid, intf);
231                     if (portUUID != null) {
232                         logger.debug("Neutron Network {}:{} Created with Internal vlan {} port {}",
233                                  network.getNetworkUUID(), network.getNetworkName(), vlan, portUUID);
234                         tenantNetworkManager.programInternalVlan(node, portUUID, network);
235                     } else {
236                         logger.trace("Neutron Network {}:{} Created with Internal vlan {} but have no portUUID",
237                                  network.getNetworkUUID(), network.getNetworkName(), vlan);
238                     }
239                 }
240                 this.handleInterfaceUpdate(node, uuid, intf);
241             }
242         } else if (tableName.equalsIgnoreCase(ovsdbConfigService.getTableName(node, Port.class))) {
243             logger.debug("Processing update of {}:{} node: {}, port uuid: {}, row: {}", tableName, action, node, uuid, row);
244             Port port = this.ovsdbConfigService.getTypedRow(node, Port.class, row);
245             Set<UUID> interfaceUUIDs = port.getInterfacesColumn().getData();
246             for (UUID intfUUID : interfaceUUIDs) {
247                 logger.trace("Scanning interface "+intfUUID);
248                 try {
249                     Row intfRow = this.ovsdbConfigService.getRow(node, ovsdbConfigService.getTableName(node, Interface.class), intfUUID.toString());
250                     Interface intf = this.ovsdbConfigService.getTypedRow(node, Interface.class, intfRow);
251                     NeutronNetwork network = tenantNetworkManager.getTenantNetwork(intf);
252                     if (network != null && !network.getRouterExternal()) {
253                          logger.debug("Processing update of {}:{} node {} intf {} network {}",
254                                  tableName, action, node, intfUUID, network.getNetworkUUID());
255                         tenantNetworkManager.programInternalVlan(node, uuid, network);
256                         this.handleInterfaceUpdate(node, intfUUID.toString(), intf);
257                     } else {
258                         logger.trace("Ignoring update because there is not a neutron network {} for port {}, interface {}",
259                                 network, uuid, intfUUID);
260                     }
261                 } catch (Exception e) {
262                     logger.error("Failed to process row update", e);
263                 }
264             }
265         } else if (tableName.equalsIgnoreCase(ovsdbConfigService.getTableName(node, OpenVSwitch.class))) {
266             logger.debug("Processing update of {}:{} node: {}, ovs uuid: {}, row: {}", tableName, action, node, uuid, row);
267             try {
268                 ConcurrentMap<String, Row> interfaces = this.ovsdbConfigService.getRows(node, ovsdbConfigService.getTableName(node, Interface.class));
269                 if (interfaces != null) {
270                     for (String intfUUID : interfaces.keySet()) {
271                         Interface intf = ovsdbConfigService.getTypedRow(node, Interface.class, interfaces.get(intfUUID));
272                         this.handleInterfaceUpdate(node, intfUUID, intf);
273                     }
274                 }
275             } catch (Exception e) {
276                 logger.error("Error fetching Interface Rows for node " + node, e);
277             }
278         }
279     }
280
281     private void handleInterfaceUpdate (Node node, String uuid, Interface intf) {
282         logger.trace("Interface update of node: {}, uuid: {}", node, uuid);
283         NeutronNetwork network = tenantNetworkManager.getTenantNetwork(intf);
284         if (network != null) {
285             if (bridgeConfigurationManager.createLocalNetwork(node, network))
286                 networkingProviderManager.getProvider(node).handleInterfaceUpdate(network, node, intf);
287         } else {
288             logger.debug("No tenant network found on node: {}, uuid: {} for interface: {}", node, uuid, intf);
289         }
290     }
291
292     private void handleInterfaceDelete (Node node, String uuid, Interface intf, boolean isLastInstanceOnNode,
293                                         NeutronNetwork network) {
294         logger.debug("handleInterfaceDelete: node: {}, uuid: {}, isLastInstanceOnNode: {}, interface: {}",
295                 node, uuid, isLastInstanceOnNode, intf);
296
297         List<String> phyIfName = bridgeConfigurationManager.getAllPhysicalInterfaceNames(node);
298         if (intf.getTypeColumn().getData().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VXLAN) ||
299             intf.getTypeColumn().getData().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_GRE) ||
300             phyIfName.contains(intf.getName())) {
301             /* delete tunnel or physical interfaces */
302             networkingProviderManager.getProvider(node).handleInterfaceDelete(intf.getTypeColumn().getData(), null, node, intf, isLastInstanceOnNode);
303         } else if (network != null) {
304             if (!network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VLAN)) { /* vlan doesn't need a tunnel endpoint */
305                 if (configurationService.getTunnelEndPoint(node) == null) {
306                     logger.error("Tunnel end-point configuration missing. Please configure it in OpenVSwitch Table");
307                     return;
308                 }
309             }
310             if (isLastInstanceOnNode & networkingProviderManager.getProvider(node).hasPerTenantTunneling()) {
311                 tenantNetworkManager.reclaimInternalVlan(node, uuid, network);
312             }
313             networkingProviderManager.getProvider(node).handleInterfaceDelete(network.getProviderNetworkType(), network, node, intf, isLastInstanceOnNode);
314         }
315     }
316
317     private String getPortIdForInterface (Node node, String uuid, Interface intf) {
318         try {
319             Map<String, Row> ports = this.ovsdbConfigService.getRows(node, ovsdbConfigService.getTableName(node, Port.class));
320             if (ports == null) return null;
321             for (String portUUID : ports.keySet()) {
322                 Port port = ovsdbConfigService.getTypedRow(node, Port.class, ports.get(portUUID));
323                 Set<UUID> interfaceUUIDs = port.getInterfacesColumn().getData();
324                 logger.trace("Scanning Port {} to identify interface : {} ",port, uuid);
325                 for (UUID intfUUID : interfaceUUIDs) {
326                     if (intfUUID.toString().equalsIgnoreCase(uuid)) {
327                         logger.trace("Found Interface {} -> {}", uuid, portUUID);
328                         return portUUID;
329                     }
330                 }
331             }
332         } catch (Exception e) {
333             logger.debug("Failed to get Port tag for for Intf {}:{}", intf, e);
334         }
335         return null;
336     }
337
338     @Override
339     public void notifyNode(Node node, UpdateType type, Map<String, Property> propMap) {
340         logger.debug("notifyNode: Node {} update {} from Controller's inventory Service", node, type);
341
342         // Add the Node Type check back once the Consistency issue is resolved between MD-SAL and AD-SAL
343         if (!type.equals(UpdateType.REMOVED) && !nodeCache.contains(node)) {
344             nodeCache.add(node);
345             networkingProviderManager.getProvider(node).initializeOFFlowRules(node);
346         } else if (type.equals(UpdateType.REMOVED)){
347             nodeCache.remove(node);
348         }
349     }
350
351     @Override
352     public void notifyNodeConnector(NodeConnector nodeConnector, UpdateType type, Map<String, Property> propMap) {
353         //We are not interested in the nodeConnectors at this moment
354     }
355
356     private void triggerUpdates() {
357         List<Node> nodes = connectionService.getNodes();
358         if (nodes == null) return;
359         for (Node node : nodes) {
360             try {
361                 List<String> tableNames = ovsdbConfigService.getTables(node);
362                 if (tableNames == null) continue;
363                 for (String tableName : tableNames) {
364                     Map<String, Row> rows = ovsdbConfigService.getRows(node, tableName);
365                     if (rows == null) continue;
366                     for (String uuid : rows.keySet()) {
367                         Row row = rows.get(uuid);
368                         this.rowAdded(node, tableName, uuid, row);
369                     }
370                 }
371             } catch (Exception e) {
372                 logger.error("Exception during OVSDB Southbound update trigger", e);
373             }
374         }
375     }
376 }