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("processRowUpdate: {} 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()) {
203 ConcurrentMap<String, Row> interfaces = this.ovsdbConfigService.getRows(node, ovsdbConfigService.getTableName(node, Interface.class));
204 if (interfaces != null) {
205 boolean isLastInstanceOnNode = true;
206 for (String intfUUID : interfaces.keySet()) {
207 if (intfUUID.equals(uuid)) continue;
208 Interface intf = this.ovsdbConfigService.getTypedRow(node, Interface.class, interfaces.get(intfUUID));
209 NeutronNetwork neutronNetwork = tenantNetworkManager.getTenantNetwork(intf);
210 if (neutronNetwork != null && neutronNetwork.equals(network)) isLastInstanceOnNode = false;
212 this.handleInterfaceDelete(node, uuid, deletedIntf, isLastInstanceOnNode, network);
214 } catch (Exception e) {
215 logger.error("Error fetching Interface Rows for node " + node, e);
220 else if (tableName.equalsIgnoreCase(ovsdbConfigService.getTableName(node, Interface.class))) {
221 logger.debug("processRowUpdate: {} Added / Updated node: {}, uuid: {}, row: {}", tableName, node, uuid, row);
222 Interface intf = this.ovsdbConfigService.getTypedRow(node, Interface.class, row);
223 NeutronNetwork network = tenantNetworkManager.getTenantNetwork(intf);
224 if (network != null && !network.getRouterExternal()) {
225 if (networkingProviderManager.getProvider(node).hasPerTenantTunneling()) {
226 int vlan = tenantNetworkManager.networkCreated(node, network.getID());
227 logger.trace("Neutron Network {}:{} Created with Internal Vlan: {}", network.getNetworkUUID(), network.getNetworkName(), vlan);
229 String portUUID = this.getPortIdForInterface(node, uuid, intf);
230 if (portUUID != null) {
231 tenantNetworkManager.programInternalVlan(node, portUUID, network);
234 this.handleInterfaceUpdate(node, uuid, intf);
236 } else if (tableName.equalsIgnoreCase(ovsdbConfigService.getTableName(node, Port.class))) {
237 logger.debug("processRowUpdate: {} Added / Updated node: {}, uuid: {}, row: {}", tableName, node, uuid, row);
238 Port port = this.ovsdbConfigService.getTypedRow(node, Port.class, row);
239 Set<UUID> interfaceUUIDs = port.getInterfacesColumn().getData();
240 for (UUID intfUUID : interfaceUUIDs) {
241 logger.trace("Scanning interface "+intfUUID);
243 Row intfRow = this.ovsdbConfigService.getRow(node, ovsdbConfigService.getTableName(node, Interface.class), intfUUID.toString());
244 Interface intf = this.ovsdbConfigService.getTypedRow(node, Interface.class, intfRow);
245 NeutronNetwork network = tenantNetworkManager.getTenantNetwork(intf);
246 if (network != null && !network.getRouterExternal()) {
247 tenantNetworkManager.programInternalVlan(node, uuid, network);
248 this.handleInterfaceUpdate(node, intfUUID.toString(), intf);
250 logger.trace("ignore update because there is not a neutron network.");
252 } catch (Exception e) {
253 logger.error("Failed to process row update", e);
256 } else if (tableName.equalsIgnoreCase(ovsdbConfigService.getTableName(node, OpenVSwitch.class))) {
257 logger.debug("processRowUpdate: {} Added / Updated node: {}, uuid: {}, row: {}", tableName, node, uuid, row);
259 ConcurrentMap<String, Row> interfaces = this.ovsdbConfigService.getRows(node, ovsdbConfigService.getTableName(node, Interface.class));
260 if (interfaces != null) {
261 for (String intfUUID : interfaces.keySet()) {
262 Interface intf = ovsdbConfigService.getTypedRow(node, Interface.class, interfaces.get(intfUUID));
263 this.handleInterfaceUpdate(node, intfUUID, intf);
266 } catch (Exception e) {
267 logger.error("Error fetching Interface Rows for node " + node, e);
272 private void handleInterfaceUpdate (Node node, String uuid, Interface intf) {
273 logger.debug("handleInterfaceUpdate: node: {}, uuid: {}", node, uuid);
274 NeutronNetwork network = tenantNetworkManager.getTenantNetwork(intf);
275 if (network != null) {
276 if (bridgeConfigurationManager.createLocalNetwork(node, network)) {
277 networkingProviderManager.getProvider(node).handleInterfaceUpdate(network, node, intf);
282 private void handleInterfaceDelete (Node node, String uuid, Interface intf, boolean isLastInstanceOnNode,
283 NeutronNetwork network) {
284 logger.debug("handleInterfaceDelete: node: {}, uuid: {}, isLastInstanceOnNode: {}, interface: {}",
285 node, uuid, isLastInstanceOnNode, intf);
287 List<String> phyIfName = bridgeConfigurationManager.getAllPhysicalInterfaceNames(node);
288 if (intf.getTypeColumn().getData().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VXLAN) ||
289 intf.getTypeColumn().getData().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_GRE) ||
290 phyIfName.contains(intf.getName())) {
291 /* delete tunnel or physical interfaces */
292 networkingProviderManager.getProvider(node).handleInterfaceDelete(intf.getTypeColumn().getData(), null, node, intf, isLastInstanceOnNode);
293 } else if (network != null) {
294 if (!network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VLAN)) { /* vlan doesn't need a tunnel endpoint */
295 if (configurationService.getTunnelEndPoint(node) == null) {
296 logger.error("Tunnel end-point configuration missing. Please configure it in OpenVSwitch Table");
300 if (isLastInstanceOnNode & networkingProviderManager.getProvider(node).hasPerTenantTunneling()) {
301 tenantNetworkManager.reclaimInternalVlan(node, uuid, network);
303 networkingProviderManager.getProvider(node).handleInterfaceDelete(network.getProviderNetworkType(), network, node, intf, isLastInstanceOnNode);
307 private String getPortIdForInterface (Node node, String uuid, Interface intf) {
309 Map<String, Row> ports = this.ovsdbConfigService.getRows(node, ovsdbConfigService.getTableName(node, Port.class));
310 if (ports == null) return null;
311 for (String portUUID : ports.keySet()) {
312 Port port = ovsdbConfigService.getTypedRow(node, Port.class, ports.get(portUUID));
313 Set<UUID> interfaceUUIDs = port.getInterfacesColumn().getData();
314 logger.trace("Scanning Port {} to identify interface : {} ",port, uuid);
315 for (UUID intfUUID : interfaceUUIDs) {
316 if (intfUUID.toString().equalsIgnoreCase(uuid)) {
317 logger.trace("Found Interface {} -> {}", uuid, portUUID);
322 } catch (Exception e) {
323 logger.debug("Failed to add Port tag for for Intf {}",intf, e);
329 public void notifyNode(Node node, UpdateType type, Map<String, Property> propMap) {
330 logger.debug("notifyNode: Node {} update {} from Controller's inventory Service", node, type);
332 // Add the Node Type check back once the Consistency issue is resolved between MD-SAL and AD-SAL
333 if (!type.equals(UpdateType.REMOVED) && !nodeCache.contains(node)) {
335 networkingProviderManager.getProvider(node).initializeOFFlowRules(node);
336 } else if (type.equals(UpdateType.REMOVED)){
337 nodeCache.remove(node);
342 public void notifyNodeConnector(NodeConnector nodeConnector, UpdateType type, Map<String, Property> propMap) {
343 //We are not interested in the nodeConnectors at this moment
346 private void triggerUpdates() {
347 List<Node> nodes = connectionService.getNodes();
348 if (nodes == null) return;
349 for (Node node : nodes) {
351 List<String> tableNames = ovsdbConfigService.getTables(node);
352 if (tableNames == null) continue;
353 for (String tableName : tableNames) {
354 Map<String, Row> rows = ovsdbConfigService.getRows(node, tableName);
355 if (rows == null) continue;
356 for (String uuid : rows.keySet()) {
357 Row row = rows.get(uuid);
358 this.rowAdded(node, tableName, uuid, row);
361 } catch (Exception e) {
362 logger.error("Exception during OVSDB Southbound update trigger", e);