2 * Copyright (C) 2013 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 : Madhu Venugopal, Brent Salisbury, Sam Hague, Dave Tucker
10 package org.opendaylight.ovsdb.openstack.netvirt;
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;
31 import com.google.common.collect.Lists;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 import java.util.List;
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;
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;
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;
60 eventHandler = Executors.newSingleThreadExecutor();
61 this.events = new LinkedBlockingQueue<>();
62 nodeCache = Lists.newArrayList();
66 eventHandler.submit(new Runnable() {
73 } catch (InterruptedException e) {
74 logger.info("The event handler thread was interrupted, shutting down", e);
77 switch (ev.getType()) {
80 processNodeUpdate(ev.getNode(), ev.getAction());
81 } catch (Exception e) {
82 logger.error("Exception caught in ProcessNodeUpdate for node " + ev.getNode(), e);
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);
94 logger.warn("Unable to process action " + ev.getAction() + " for node " + ev.getNode());
99 this.triggerUpdates();
103 eventHandler.shutdownNow();
107 public void nodeAdded(Node node) {
108 this.enqueueEvent(new SouthboundEvent(node, SouthboundEvent.Action.ADD));
112 public void nodeRemoved(Node node) {
113 this.enqueueEvent(new SouthboundEvent(node, SouthboundEvent.Action.DELETE));
117 public void rowAdded(Node node, String tableName, String uuid, Row row) {
118 this.enqueueEvent(new SouthboundEvent(node, tableName, uuid, row, SouthboundEvent.Action.ADD));
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));
129 * Ignore unneccesary updates to be even considered for processing.
130 * (Especially stats update are fast and furious).
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);
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);
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 */
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));
167 private void enqueueEvent (SouthboundEvent event) {
170 } catch (InterruptedException e) {
171 logger.error("Thread was interrupted while trying to enqueue event ", e);
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);
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);
191 network = (NeutronNetwork)context;
193 List<String> phyIfName = bridgeConfigurationManager.getAllPhysicalInterfaceNames(node);
194 logger.info("Delete interface " + deletedIntf.getName());
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());
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;
214 this.handleInterfaceDelete(node, uuid, deletedIntf, isLastInstanceOnNode, network);
216 } catch (Exception e) {
217 logger.error("Error fetching Interface Rows for node " + node, e);
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);
236 logger.trace("Neutron Network {}:{} Created with Internal vlan {} but have no portUUID",
237 network.getNetworkUUID(), network.getNetworkName(), vlan);
240 this.handleInterfaceUpdate(node, uuid, intf);
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);
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);
258 logger.trace("Ignoring update because there is not a neutron network {} for port {}, interface {}",
259 network, uuid, intfUUID);
261 } catch (Exception e) {
262 logger.error("Failed to process row update", e);
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);
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);
275 } catch (Exception e) {
276 logger.error("Error fetching Interface Rows for node " + node, e);
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);
288 logger.debug("No tenant network found on node: {}, uuid: {} for interface: {}", node, uuid, intf);
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);
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");
310 if (isLastInstanceOnNode & networkingProviderManager.getProvider(node).hasPerTenantTunneling()) {
311 tenantNetworkManager.reclaimInternalVlan(node, uuid, network);
313 networkingProviderManager.getProvider(node).handleInterfaceDelete(network.getProviderNetworkType(), network, node, intf, isLastInstanceOnNode);
317 private String getPortIdForInterface (Node node, String uuid, Interface intf) {
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);
332 } catch (Exception e) {
333 logger.debug("Failed to get Port tag for for Intf {}:{}", intf, e);
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);
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)) {
345 networkingProviderManager.getProvider(node).initializeOFFlowRules(node);
346 } else if (type.equals(UpdateType.REMOVED)){
347 nodeCache.remove(node);
352 public void notifyNodeConnector(NodeConnector nodeConnector, UpdateType type, Map<String, Property> propMap) {
353 //We are not interested in the nodeConnectors at this moment
356 private void triggerUpdates() {
357 List<Node> nodes = connectionService.getNodes();
358 if (nodes == null) return;
359 for (Node node : nodes) {
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);
371 } catch (Exception e) {
372 logger.error("Exception during OVSDB Southbound update trigger", e);