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.neutron.spi.NeutronNetwork;
13 import org.opendaylight.ovsdb.lib.notation.Row;
14 import org.opendaylight.ovsdb.lib.notation.UUID;
15 import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
16 import org.opendaylight.ovsdb.openstack.netvirt.api.BridgeConfigurationManager;
17 import org.opendaylight.ovsdb.openstack.netvirt.api.ConfigurationService;
18 import org.opendaylight.ovsdb.openstack.netvirt.api.MdsalConsumer;
19 import org.opendaylight.ovsdb.openstack.netvirt.api.MdsalConsumerListener;
20 import org.opendaylight.ovsdb.openstack.netvirt.api.NetworkingProvider;
21 import org.opendaylight.ovsdb.openstack.netvirt.api.NetworkingProviderManager;
22 import org.opendaylight.ovsdb.openstack.netvirt.api.NodeCacheListener;
23 import org.opendaylight.ovsdb.openstack.netvirt.api.TenantNetworkManager;
24 import org.opendaylight.ovsdb.openstack.netvirt.impl.NeutronL3Adapter;
25 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
26 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
27 import org.opendaylight.ovsdb.schema.openvswitch.OpenVSwitch;
28 import org.opendaylight.ovsdb.schema.openvswitch.Port;
29 import org.opendaylight.ovsdb.utils.mdsal.node.StringConvertor;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 import java.net.InetAddress;
36 import java.util.List;
39 import java.util.concurrent.ConcurrentMap;
41 public class SouthboundHandler extends AbstractHandler
42 implements NodeCacheListener, /*OvsdbInventoryListener,*/ MdsalConsumerListener {
43 static final Logger logger = LoggerFactory.getLogger(SouthboundHandler.class);
44 //private Thread eventThread;
46 // The implementation for each of these services is resolved by the OSGi Service Manager
47 private volatile ConfigurationService configurationService;
48 private volatile BridgeConfigurationManager bridgeConfigurationManager;
49 private volatile TenantNetworkManager tenantNetworkManager;
50 private volatile NetworkingProviderManager networkingProviderManager;
51 //private volatile OvsdbConfigurationService ovsdbConfigurationService;
52 //private volatile OvsdbConnectionService connectionService;
53 private volatile MdsalConsumer mdsalConsumer; // TODO SB_MIGRATION
54 private volatile NeutronL3Adapter neutronL3Adapter;
57 logger.info(">>>>> init");
61 logger.info(">>>>> started");
62 this.triggerUpdates(); // TODO SB_MIGRATION
66 public void ovsdbNodeAdded(Node node) {
67 logger.info("nodeAdded: {}", node);
68 this.enqueueEvent(new SouthboundEvent(node, Action.ADD));
72 public void ovsdbNodeRemoved(Node node) {
73 this.enqueueEvent(new SouthboundEvent(node, Action.DELETE));
77 public void rowAdded(Node node, String tableName, String uuid, Row row) {
78 this.enqueueEvent(new SouthboundEvent(node, tableName, uuid, row, Action.ADD));
82 public void rowUpdated(Node node, String tableName, String uuid, Row oldRow, Row newRow) {
83 if (this.isUpdateOfInterest(node, oldRow, newRow)) {
84 this.enqueueEvent(new SouthboundEvent(node, tableName, uuid, newRow, Action.UPDATE));
89 * Ignore unnecessary updates to be even considered for processing.
90 * (Especially stats update are fast and furious).
93 private boolean isUpdateOfInterest(Node node, Row oldRow, Row newRow) {
95 if (oldRow == null) return true;
96 if (newRow.getTableSchema().getName().equals(ovsdbConfigurationService.getTableName(node, Interface.class))) {
97 // We are NOT interested in Stats only updates
98 Interface oldIntf = ovsdbConfigurationService.getTypedRow(node, Interface.class, oldRow);
99 if (oldIntf.getName() == null && oldIntf.getExternalIdsColumn() == null && oldIntf.getMacColumn() == null &&
100 oldIntf.getOpenFlowPortColumn() == null && oldIntf.getOptionsColumn() == null && oldIntf.getOtherConfigColumn() == null &&
101 oldIntf.getTypeColumn() == null) {
102 logger.trace("IGNORING Interface Update: node {}, row: {}", node, newRow);
105 } else if (newRow.getTableSchema().getName().equals(ovsdbConfigurationService.getTableName(node, Port.class))) {
106 // We are NOT interested in Stats only updates
107 Port oldPort = ovsdbConfigurationService.getTypedRow(node, Port.class, oldRow);
108 if (oldPort.getName() == null && oldPort.getExternalIdsColumn() == null && oldPort.getMacColumn() == null &&
109 oldPort.getInterfacesColumn() == null && oldPort.getTagColumn() == null && oldPort.getTrunksColumn() == null) {
110 logger.trace("IGNORING Port Update: node {}, row: {}", node, newRow);
113 } else if (newRow.getTableSchema().getName().equals(ovsdbConfigurationService.getTableName(node, OpenVSwitch.class))) {
114 OpenVSwitch oldOpenvSwitch = ovsdbConfigurationService.getTypedRow(node, OpenVSwitch.class, oldRow);
115 if (oldOpenvSwitch.getOtherConfigColumn()== null) {
116 *//* we are only interested in other_config field change *//*
124 public void rowRemoved(Node node, String tableName, String uuid, Row row, Object context) {
125 this.enqueueEvent(new SouthboundEvent(node, tableName, uuid, row, context, Action.DELETE));
128 public void processNodeUpdate(Node node, Action action) {
129 if (action == Action.DELETE) return;
130 logger.trace("Process Node added {}", node);
131 bridgeConfigurationManager.prepareNode(node);
134 private void processRowUpdate(Node node, String tableName, String uuid, Row row,
135 Object context, Action action) {
137 if (action == Action.DELETE) {
138 if (tableName.equalsIgnoreCase(ovsdbConfigurationService.getTableName(node, Interface.class))) {
139 logger.debug("Processing update of {}. Deleted node: {}, uuid: {}, row: {}", tableName, node, uuid, row);
140 Interface deletedIntf = ovsdbConfigurationService.getTypedRow(node, Interface.class, row);
141 NeutronNetwork network = null;
142 if (context == null) {
143 network = tenantNetworkManager.getTenantNetwork(deletedIntf);
145 network = (NeutronNetwork)context;
147 List<String> phyIfName = bridgeConfigurationManager.getAllPhysicalInterfaceNames(node);
148 logger.info("Delete interface " + deletedIntf.getName());
150 if (deletedIntf.getTypeColumn().getData().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VXLAN) ||
151 deletedIntf.getTypeColumn().getData().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_GRE) ||
152 phyIfName.contains(deletedIntf.getName())) {
153 *//* delete tunnel interfaces or physical interfaces *//*
154 this.handleInterfaceDelete(node, uuid, deletedIntf, false, null);
155 } else if (network != null && !network.getRouterExternal()) {
156 logger.debug("Processing update of {}:{} node {} intf {} network {}",
157 tableName, action, node, uuid, network.getNetworkUUID());
159 ConcurrentMap<String, Row> interfaces = this.ovsdbConfigurationService
160 .getRows(node, ovsdbConfigurationService.getTableName(node, Interface.class));
161 if (interfaces != null) {
162 boolean isLastInstanceOnNode = true;
163 for (String intfUUID : interfaces.keySet()) {
164 if (intfUUID.equals(uuid)) continue;
165 Interface intf = this.ovsdbConfigurationService.getTypedRow(node, Interface.class, interfaces.get(intfUUID));
166 NeutronNetwork neutronNetwork = tenantNetworkManager.getTenantNetwork(intf);
167 if (neutronNetwork != null && neutronNetwork.equals(network)) isLastInstanceOnNode = false;
169 this.handleInterfaceDelete(node, uuid, deletedIntf, isLastInstanceOnNode, network);
171 } catch (Exception e) {
172 logger.error("Error fetching Interface Rows for node " + node, e);
177 else if (tableName.equalsIgnoreCase(ovsdbConfigurationService.getTableName(node, Interface.class))) {
178 logger.debug("Processing update of {}:{} node: {}, interface uuid: {}, row: {}",
179 tableName, action, node, uuid, row);
180 Interface intf = this.ovsdbConfigurationService.getTypedRow(node, Interface.class, row);
181 NeutronNetwork network = tenantNetworkManager.getTenantNetwork(intf);
182 if (network != null && !network.getRouterExternal()) {
183 if (networkingProviderManager.getProvider(node).hasPerTenantTunneling()) {
184 int vlan = tenantNetworkManager.networkCreated(node, network.getID());
185 String portUUID = this.getPortIdForInterface(node, uuid, intf);
186 if (portUUID != null) {
187 logger.debug("Neutron Network {}:{} Created with Internal vlan {} port {}",
188 network.getNetworkUUID(), network.getNetworkName(), vlan, portUUID);
189 tenantNetworkManager.programInternalVlan(node, portUUID, network);
191 logger.trace("Neutron Network {}:{} Created with Internal vlan {} but have no portUUID",
192 network.getNetworkUUID(), network.getNetworkName(), vlan);
195 this.handleInterfaceUpdate(node, uuid, intf);
198 } else if (tableName.equalsIgnoreCase(ovsdbConfigurationService.getTableName(node, Port.class))) {
199 logger.debug("Processing update of {}:{} node: {}, port uuid: {}, row: {}", tableName, action, node, uuid, row);
200 Port port = this.ovsdbConfigurationService.getTypedRow(node, Port.class, row);
201 Set<UUID> interfaceUUIDs = port.getInterfacesColumn().getData();
202 for (UUID intfUUID : interfaceUUIDs) {
203 logger.trace("Scanning interface "+intfUUID);
205 Row intfRow = this.ovsdbConfigurationService
206 .getRow(node, ovsdbConfigurationService.getTableName(node, Interface.class),
207 intfUUID.toString());
208 Interface intf = this.ovsdbConfigurationService.getTypedRow(node, Interface.class, intfRow);
209 NeutronNetwork network = tenantNetworkManager.getTenantNetwork(intf);
210 if (network != null && !network.getRouterExternal()) {
211 logger.debug("Processing update of {}:{} node {} intf {} network {}",
212 tableName, action, node, intfUUID, network.getNetworkUUID());
213 tenantNetworkManager.programInternalVlan(node, uuid, network);
214 this.handleInterfaceUpdate(node, intfUUID.toString(), intf);
216 logger.trace("Ignoring update because there is not a neutron network {} for port {}, interface {}",
217 network, uuid, intfUUID);
219 } catch (Exception e) {
220 logger.error("Failed to process row update", e);
223 } else if (tableName.equalsIgnoreCase(ovsdbConfigurationService.getTableName(node, OpenVSwitch.class))) {
224 logger.debug("Processing update of {}:{} node: {}, ovs uuid: {}, row: {}", tableName, action, node, uuid, row);
226 ConcurrentMap<String, Row> interfaces = this.ovsdbConfigurationService
227 .getRows(node, ovsdbConfigurationService.getTableName(node, Interface.class));
228 if (interfaces != null) {
229 for (String intfUUID : interfaces.keySet()) {
230 Interface intf = ovsdbConfigurationService.getTypedRow(node, Interface.class, interfaces.get(intfUUID));
231 this.handleInterfaceUpdate(node, intfUUID, intf);
234 } catch (Exception e) {
235 logger.error("Error fetching Interface Rows for node " + node, e);
237 } else if (tableName.equalsIgnoreCase(ovsdbConfigurationService.getTableName(node, Bridge.class))) {
238 logger.debug("Processing update of {}:{} node: {}, bridge uuid: {}, row: {}", tableName, action, node, uuid, row);
239 Bridge bridge = ovsdbConfigurationService.getTypedRow(node, Bridge.class, row);
240 final Set<String> dpids = bridge.getDatapathIdColumn().getData();
242 (bridge.getName().equals(configurationService.getIntegrationBridgeName()) ||
243 bridge.getName().equals(configurationService.getExternalBridgeName()))) {
244 NetworkingProvider networkingProvider = networkingProviderManager.getProvider(node);
245 for (String dpid : dpids) {
246 networkingProvider.notifyFlowCapableNodeEvent(StringConvertor.dpidStringToLong(dpid), action);
252 private void handleInterfaceUpdate (Node node, String uuid, Interface intf) {
253 logger.trace("Interface update of node: {}, uuid: {}", node, uuid);
254 NeutronNetwork network = tenantNetworkManager.getTenantNetwork(intf);
255 if (network != null) {
256 neutronL3Adapter.handleInterfaceEvent(node, intf, network, Action.UPDATE);
257 if (bridgeConfigurationManager.createLocalNetwork(node, network))
258 networkingProviderManager.getProvider(node).handleInterfaceUpdate(network, node, intf);
260 logger.debug("No tenant network found on node: {}, uuid: {} for interface: {}", node, uuid, intf);
264 private void handleInterfaceDelete (Node node, String uuid, Interface intf, boolean isLastInstanceOnNode,
265 NeutronNetwork network) {
266 logger.debug("handleInterfaceDelete: node: {}, uuid: {}, isLastInstanceOnNode: {}, interface: {}",
267 node, uuid, isLastInstanceOnNode, intf);
269 neutronL3Adapter.handleInterfaceEvent(node, intf, network, Action.DELETE);
270 List<String> phyIfName = bridgeConfigurationManager.getAllPhysicalInterfaceNames(node);
271 if (intf.getTypeColumn().getData().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VXLAN) ||
272 intf.getTypeColumn().getData().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_GRE) ||
273 phyIfName.contains(intf.getName())) {
274 /* delete tunnel or physical interfaces */
275 networkingProviderManager.getProvider(node).handleInterfaceDelete(intf.getTypeColumn().getData(), null, node, intf, isLastInstanceOnNode);
276 } else if (network != null) {
277 if (!network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VLAN)) { /* vlan doesn't need a tunnel endpoint */
278 if (configurationService.getTunnelEndPoint(node) == null) {
279 logger.error("Tunnel end-point configuration missing. Please configure it in OpenVSwitch Table");
283 if (isLastInstanceOnNode & networkingProviderManager.getProvider(node).hasPerTenantTunneling()) {
284 tenantNetworkManager.reclaimInternalVlan(node, uuid, network);
286 networkingProviderManager.getProvider(node).handleInterfaceDelete(network.getProviderNetworkType(), network, node, intf, isLastInstanceOnNode);
290 private String getPortIdForInterface (Node node, String uuid, Interface intf) {
293 Map<String, Row> ports = this.ovsdbConfigurationService.getRows(node, ovsdbConfigurationService.getTableName(node, Port.class));
294 if (ports == null) return null;
295 for (String portUUID : ports.keySet()) {
296 Port port = ovsdbConfigurationService.getTypedRow(node, Port.class, ports.get(portUUID));
297 Set<UUID> interfaceUUIDs = port.getInterfacesColumn().getData();
298 logger.trace("Scanning Port {} to identify interface : {} ",port, uuid);
299 for (UUID intfUUID : interfaceUUIDs) {
300 if (intfUUID.toString().equalsIgnoreCase(uuid)) {
301 logger.trace("Found Interface {} -> {}", uuid, portUUID);
306 } catch (Exception e) {
307 logger.debug("Failed to get Port tag for for Intf " + intf, e);
312 private void triggerUpdates() {
314 List<Node> nodes = connectionService.getNodes();
315 if (nodes == null) return;
316 for (Node node : nodes) {
318 List<String> tableNames = ovsdbConfigurationService.getTables(node);
319 if (tableNames == null) continue;
320 for (String tableName : tableNames) {
321 Map<String, Row> rows = ovsdbConfigurationService.getRows(node, tableName);
322 if (rows == null) continue;
323 for (String uuid : rows.keySet()) {
324 Row row = rows.get(uuid);
325 this.rowAdded(node, tableName, uuid, row);
328 } catch (Exception e) {
329 logger.error("Exception during OVSDB Southbound update trigger", e);
337 * @param abstractEvent the {@link org.opendaylight.ovsdb.openstack.netvirt.AbstractEvent} event to be handled.
338 * @see EventDispatcher
341 public void processEvent(AbstractEvent abstractEvent) {
342 if (!(abstractEvent instanceof SouthboundEvent)) {
343 logger.error("Unable to process abstract event " + abstractEvent);
346 SouthboundEvent ev = (SouthboundEvent) abstractEvent;
347 //logger.info("processEvent: {}", ev);
348 switch (ev.getType()) {
351 processNodeUpdate(ev.getNode(), ev.getAction());
352 } catch (Exception e) {
353 logger.error("Exception caught in ProcessNodeUpdate for node " + ev.getNode(), e);
358 processRowUpdate(ev.getNode(), ev.getTableName(), ev.getUuid(), ev.getRow(),
359 ev.getContext(),ev.getAction());
360 } catch (Exception e) {
361 logger.error("Exception caught in ProcessRowUpdate for node " + ev.getNode(), e);
365 logger.warn("Unable to process type " + ev.getType() +
366 " action " + ev.getAction() + " for node " + ev.getNode());
372 * Notification about an OpenFlow Node
374 * @param openFlowNode the {@link org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node Node} of interest in the notification
375 * @param action the {@link Action}
376 * @see NodeCacheListener#notifyNode
379 public void notifyNode (Node openFlowNode, Action action) {
380 logger.info("notifyNode: Node {} update {} from Controller's inventory Service",
381 openFlowNode, action);
383 if (action.equals(Action.ADD)) {
384 networkingProviderManager.getProvider(openFlowNode).initializeOFFlowRules(openFlowNode);