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.NetworkingProviderManager;
22 import org.opendaylight.ovsdb.openstack.netvirt.api.TenantNetworkManager;
23 import org.opendaylight.ovsdb.openstack.netvirt.impl.NeutronL3Adapter;
24 import org.opendaylight.ovsdb.plugin.api.OvsdbConfigurationService;
25 import org.opendaylight.ovsdb.plugin.api.OvsdbConnectionService;
26 import org.opendaylight.ovsdb.plugin.api.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.net.InetAddress;
36 import java.util.List;
39 import java.util.concurrent.ConcurrentMap;
41 public class SouthboundHandler extends AbstractHandler implements OvsdbInventoryListener,
43 static final Logger logger = LoggerFactory.getLogger(SouthboundHandler.class);
44 //private Thread eventThread;
47 // The implementation for each of these services is resolved by the OSGi Service Manager
48 private volatile org.opendaylight.ovsdb.openstack.netvirt.api.ConfigurationService configurationService;
49 private volatile BridgeConfigurationManager bridgeConfigurationManager;
50 private volatile TenantNetworkManager tenantNetworkManager;
51 private volatile NetworkingProviderManager networkingProviderManager;
52 private volatile OvsdbConfigurationService ovsdbConfigurationService;
53 private volatile OvsdbConnectionService connectionService;
54 private volatile NeutronL3Adapter neutronL3Adapter;
57 nodeCache = Lists.newArrayList();
61 this.triggerUpdates();
65 public void nodeAdded(Node node, InetAddress address, int port) {
66 this.enqueueEvent(new SouthboundEvent(node, SouthboundEvent.Action.ADD));
70 public void nodeRemoved(Node node) {
71 this.enqueueEvent(new SouthboundEvent(node, SouthboundEvent.Action.DELETE));
75 public void rowAdded(Node node, String tableName, String uuid, Row row) {
76 this.enqueueEvent(new SouthboundEvent(node, tableName, uuid, row, SouthboundEvent.Action.ADD));
80 public void rowUpdated(Node node, String tableName, String uuid, Row oldRow, Row newRow) {
81 if (this.isUpdateOfInterest(node, oldRow, newRow)) {
82 this.enqueueEvent(new SouthboundEvent(node, tableName, uuid, newRow, SouthboundEvent.Action.UPDATE));
87 * Ignore unnecessary updates to be even considered for processing.
88 * (Especially stats update are fast and furious).
91 private boolean isUpdateOfInterest(Node node, Row oldRow, Row newRow) {
92 if (oldRow == null) return true;
93 if (newRow.getTableSchema().getName().equals(ovsdbConfigurationService.getTableName(node, Interface.class))) {
94 // We are NOT interested in Stats only updates
95 Interface oldIntf = ovsdbConfigurationService.getTypedRow(node, Interface.class, oldRow);
96 if (oldIntf.getName() == null && oldIntf.getExternalIdsColumn() == null && oldIntf.getMacColumn() == null &&
97 oldIntf.getOpenFlowPortColumn() == null && oldIntf.getOptionsColumn() == null && oldIntf.getOtherConfigColumn() == null &&
98 oldIntf.getTypeColumn() == null) {
99 logger.trace("IGNORING Interface Update: node {}, row: {}", node, newRow);
102 } else if (newRow.getTableSchema().getName().equals(ovsdbConfigurationService.getTableName(node, Port.class))) {
103 // We are NOT interested in Stats only updates
104 Port oldPort = ovsdbConfigurationService.getTypedRow(node, Port.class, oldRow);
105 if (oldPort.getName() == null && oldPort.getExternalIdsColumn() == null && oldPort.getMacColumn() == null &&
106 oldPort.getInterfacesColumn() == null && oldPort.getTagColumn() == null && oldPort.getTrunksColumn() == null) {
107 logger.trace("IGNORING Port Update: node {}, row: {}", node, newRow);
110 } else if (newRow.getTableSchema().getName().equals(ovsdbConfigurationService.getTableName(node, OpenVSwitch.class))) {
111 OpenVSwitch oldOpenvSwitch = ovsdbConfigurationService.getTypedRow(node, OpenVSwitch.class, oldRow);
112 if (oldOpenvSwitch.getOtherConfigColumn()== null) {
113 /* we are only interested in other_config field change */
121 public void rowRemoved(Node node, String tableName, String uuid, Row row, Object context) {
122 this.enqueueEvent(new SouthboundEvent(node, tableName, uuid, row, context, SouthboundEvent.Action.DELETE));
125 public void processNodeUpdate(Node node, SouthboundEvent.Action action) {
126 if (action == SouthboundEvent.Action.DELETE) return;
127 logger.trace("Process Node added {}", node);
128 bridgeConfigurationManager.prepareNode(node);
131 private void processRowUpdate(Node node, String tableName, String uuid, Row row,
132 Object context, SouthboundEvent.Action action) {
133 if (action == SouthboundEvent.Action.DELETE) {
134 if (tableName.equalsIgnoreCase(ovsdbConfigurationService.getTableName(node, Interface.class))) {
135 logger.debug("Processing update of {}. Deleted node: {}, uuid: {}, row: {}", tableName, node, uuid, row);
136 Interface deletedIntf = ovsdbConfigurationService.getTypedRow(node, Interface.class, row);
137 NeutronNetwork network = null;
138 if (context == null) {
139 network = tenantNetworkManager.getTenantNetwork(deletedIntf);
141 network = (NeutronNetwork)context;
143 List<String> phyIfName = bridgeConfigurationManager.getAllPhysicalInterfaceNames(node);
144 logger.info("Delete interface " + deletedIntf.getName());
146 if (deletedIntf.getTypeColumn().getData().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VXLAN) ||
147 deletedIntf.getTypeColumn().getData().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_GRE) ||
148 phyIfName.contains(deletedIntf.getName())) {
149 /* delete tunnel interfaces or physical interfaces */
150 this.handleInterfaceDelete(node, uuid, deletedIntf, false, null);
151 } else if (network != null && !network.getRouterExternal()) {
152 logger.debug("Processing update of {}:{} node {} intf {} network {}",
153 tableName, action, node, uuid, network.getNetworkUUID());
155 ConcurrentMap<String, Row> interfaces = this.ovsdbConfigurationService
156 .getRows(node, ovsdbConfigurationService.getTableName(node, Interface.class));
157 if (interfaces != null) {
158 boolean isLastInstanceOnNode = true;
159 for (String intfUUID : interfaces.keySet()) {
160 if (intfUUID.equals(uuid)) continue;
161 Interface intf = this.ovsdbConfigurationService.getTypedRow(node, Interface.class, interfaces.get(intfUUID));
162 NeutronNetwork neutronNetwork = tenantNetworkManager.getTenantNetwork(intf);
163 if (neutronNetwork != null && neutronNetwork.equals(network)) isLastInstanceOnNode = false;
165 this.handleInterfaceDelete(node, uuid, deletedIntf, isLastInstanceOnNode, network);
167 } catch (Exception e) {
168 logger.error("Error fetching Interface Rows for node " + node, e);
173 else if (tableName.equalsIgnoreCase(ovsdbConfigurationService.getTableName(node, Interface.class))) {
174 logger.debug("Processing update of {}:{} node: {}, interface uuid: {}, row: {}",
175 tableName, action, node, uuid, row);
176 Interface intf = this.ovsdbConfigurationService.getTypedRow(node, Interface.class, row);
177 NeutronNetwork network = tenantNetworkManager.getTenantNetwork(intf);
178 if (network != null && !network.getRouterExternal()) {
179 if (networkingProviderManager.getProvider(node).hasPerTenantTunneling()) {
180 int vlan = tenantNetworkManager.networkCreated(node, network.getID());
181 String portUUID = this.getPortIdForInterface(node, uuid, intf);
182 if (portUUID != null) {
183 logger.debug("Neutron Network {}:{} Created with Internal vlan {} port {}",
184 network.getNetworkUUID(), network.getNetworkName(), vlan, portUUID);
185 tenantNetworkManager.programInternalVlan(node, portUUID, network);
187 logger.trace("Neutron Network {}:{} Created with Internal vlan {} but have no portUUID",
188 network.getNetworkUUID(), network.getNetworkName(), vlan);
191 this.handleInterfaceUpdate(node, uuid, intf);
194 } else if (tableName.equalsIgnoreCase(ovsdbConfigurationService.getTableName(node, Port.class))) {
195 logger.debug("Processing update of {}:{} node: {}, port uuid: {}, row: {}", tableName, action, node, uuid, row);
196 Port port = this.ovsdbConfigurationService.getTypedRow(node, Port.class, row);
197 Set<UUID> interfaceUUIDs = port.getInterfacesColumn().getData();
198 for (UUID intfUUID : interfaceUUIDs) {
199 logger.trace("Scanning interface "+intfUUID);
201 Row intfRow = this.ovsdbConfigurationService
202 .getRow(node, ovsdbConfigurationService.getTableName(node, Interface.class),
203 intfUUID.toString());
204 Interface intf = this.ovsdbConfigurationService.getTypedRow(node, Interface.class, intfRow);
205 NeutronNetwork network = tenantNetworkManager.getTenantNetwork(intf);
206 if (network != null && !network.getRouterExternal()) {
207 logger.debug("Processing update of {}:{} node {} intf {} network {}",
208 tableName, action, node, intfUUID, network.getNetworkUUID());
209 tenantNetworkManager.programInternalVlan(node, uuid, network);
210 this.handleInterfaceUpdate(node, intfUUID.toString(), intf);
212 logger.trace("Ignoring update because there is not a neutron network {} for port {}, interface {}",
213 network, uuid, intfUUID);
215 } catch (Exception e) {
216 logger.error("Failed to process row update", e);
219 } else if (tableName.equalsIgnoreCase(ovsdbConfigurationService.getTableName(node, OpenVSwitch.class))) {
220 logger.debug("Processing update of {}:{} node: {}, ovs uuid: {}, row: {}", tableName, action, node, uuid, row);
222 ConcurrentMap<String, Row> interfaces = this.ovsdbConfigurationService
223 .getRows(node, ovsdbConfigurationService.getTableName(node, Interface.class));
224 if (interfaces != null) {
225 for (String intfUUID : interfaces.keySet()) {
226 Interface intf = ovsdbConfigurationService.getTypedRow(node, Interface.class, interfaces.get(intfUUID));
227 this.handleInterfaceUpdate(node, intfUUID, intf);
230 } catch (Exception e) {
231 logger.error("Error fetching Interface Rows for node " + node, e);
236 private void handleInterfaceUpdate (Node node, String uuid, Interface intf) {
237 logger.trace("Interface update of node: {}, uuid: {}", node, uuid);
238 NeutronNetwork network = tenantNetworkManager.getTenantNetwork(intf);
239 if (network != null) {
240 neutronL3Adapter.handleInterfaceEvent(node, intf, network, AbstractEvent.Action.UPDATE);
241 if (bridgeConfigurationManager.createLocalNetwork(node, network))
242 networkingProviderManager.getProvider(node).handleInterfaceUpdate(network, node, intf);
244 logger.debug("No tenant network found on node: {}, uuid: {} for interface: {}", node, uuid, intf);
248 private void handleInterfaceDelete (Node node, String uuid, Interface intf, boolean isLastInstanceOnNode,
249 NeutronNetwork network) {
250 logger.debug("handleInterfaceDelete: node: {}, uuid: {}, isLastInstanceOnNode: {}, interface: {}",
251 node, uuid, isLastInstanceOnNode, intf);
253 neutronL3Adapter.handleInterfaceEvent(node, intf, network, AbstractEvent.Action.DELETE);
254 List<String> phyIfName = bridgeConfigurationManager.getAllPhysicalInterfaceNames(node);
255 if (intf.getTypeColumn().getData().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VXLAN) ||
256 intf.getTypeColumn().getData().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_GRE) ||
257 phyIfName.contains(intf.getName())) {
258 /* delete tunnel or physical interfaces */
259 networkingProviderManager.getProvider(node).handleInterfaceDelete(intf.getTypeColumn().getData(), null, node, intf, isLastInstanceOnNode);
260 } else if (network != null) {
261 if (!network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VLAN)) { /* vlan doesn't need a tunnel endpoint */
262 if (configurationService.getTunnelEndPoint(node) == null) {
263 logger.error("Tunnel end-point configuration missing. Please configure it in OpenVSwitch Table");
267 if (isLastInstanceOnNode & networkingProviderManager.getProvider(node).hasPerTenantTunneling()) {
268 tenantNetworkManager.reclaimInternalVlan(node, uuid, network);
270 networkingProviderManager.getProvider(node).handleInterfaceDelete(network.getProviderNetworkType(), network, node, intf, isLastInstanceOnNode);
274 private String getPortIdForInterface (Node node, String uuid, Interface intf) {
276 Map<String, Row> ports = this.ovsdbConfigurationService.getRows(node, ovsdbConfigurationService.getTableName(node, Port.class));
277 if (ports == null) return null;
278 for (String portUUID : ports.keySet()) {
279 Port port = ovsdbConfigurationService.getTypedRow(node, Port.class, ports.get(portUUID));
280 Set<UUID> interfaceUUIDs = port.getInterfacesColumn().getData();
281 logger.trace("Scanning Port {} to identify interface : {} ",port, uuid);
282 for (UUID intfUUID : interfaceUUIDs) {
283 if (intfUUID.toString().equalsIgnoreCase(uuid)) {
284 logger.trace("Found Interface {} -> {}", uuid, portUUID);
289 } catch (Exception e) {
290 logger.debug("Failed to get Port tag for for Intf {}:{}", intf, e);
296 public void notifyNode(Node node, UpdateType type, Map<String, Property> propMap) {
297 logger.debug("notifyNode: Node {} update {} from Controller's inventory Service", node, type);
299 // Add the Node Type check back once the Consistency issue is resolved between MD-SAL and AD-SAL
300 if (!type.equals(UpdateType.REMOVED) && !nodeCache.contains(node)) {
302 networkingProviderManager.getProvider(node).initializeOFFlowRules(node);
303 } else if (type.equals(UpdateType.REMOVED)){
304 nodeCache.remove(node);
309 public void notifyNodeConnector(NodeConnector nodeConnector, UpdateType type, Map<String, Property> propMap) {
310 //We are not interested in the nodeConnectors at this moment
313 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 switch (ev.getType()) {
350 processNodeUpdate(ev.getNode(), ev.getAction());
351 } catch (Exception e) {
352 logger.error("Exception caught in ProcessNodeUpdate for node " + ev.getNode(), e);
357 processRowUpdate(ev.getNode(), ev.getTableName(), ev.getUuid(), ev.getRow(),
358 ev.getContext(),ev.getAction());
359 } catch (Exception e) {
360 logger.error("Exception caught in ProcessRowUpdate for node " + ev.getNode(), e);
364 logger.warn("Unable to process type " + ev.getType() +
365 " action " + ev.getAction() + " for node " + ev.getNode());