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, Hsin-Yi Shen
10 package org.opendaylight.ovsdb.openstack.netvirt;
12 import org.opendaylight.controller.networkconfig.neutron.INeutronNetworkAware;
13 import org.opendaylight.controller.networkconfig.neutron.INeutronNetworkCRUD;
14 import org.opendaylight.controller.networkconfig.neutron.NeutronNetwork;
15 import org.opendaylight.controller.sal.core.Node;
16 import org.opendaylight.ovsdb.lib.notation.Row;
17 import org.opendaylight.ovsdb.lib.notation.UUID;
18 import org.opendaylight.ovsdb.openstack.netvirt.api.BridgeConfigurationManager;
19 import org.opendaylight.ovsdb.openstack.netvirt.api.TenantNetworkManager;
20 import org.opendaylight.ovsdb.plugin.api.OvsdbConfigurationService;
21 import org.opendaylight.ovsdb.plugin.api.OvsdbConnectionService;
22 import org.opendaylight.ovsdb.plugin.api.OvsdbInventoryListener;
23 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
24 import org.opendaylight.ovsdb.schema.openvswitch.Port;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 import java.net.HttpURLConnection;
30 import java.util.List;
31 import java.util.concurrent.ConcurrentMap;
34 * Handle requests for Neutron Network.
36 public class NetworkHandler extends AbstractHandler
37 implements INeutronNetworkAware {
39 public static final String NETWORK_TYPE_VXLAN = "vxlan";
40 public static final String NETWORK_TYPE_GRE = "gre";
41 public static final String NETWORK_TYPE_VLAN = "vlan";
46 static final Logger logger = LoggerFactory.getLogger(NetworkHandler.class);
48 // The implementation for each of these services is resolved by the OSGi Service Manager
49 private volatile TenantNetworkManager tenantNetworkManager;
50 private volatile BridgeConfigurationManager bridgeConfigurationManager;
51 private volatile org.opendaylight.ovsdb.openstack.netvirt.api.ConfigurationService configurationService;
52 private volatile OvsdbConfigurationService ovsdbConfigurationService;
53 private volatile OvsdbConnectionService connectionService;
54 private volatile INeutronNetworkCRUD neutronNetworkCache;
55 private volatile OvsdbInventoryListener ovsdbInventoryListener;
58 * Invoked when a network creation is requested
59 * to indicate if the specified network can be created.
61 * @param network An instance of proposed new Neutron Network object.
62 * @return A HTTP status code to the creation request.
65 public int canCreateNetwork(NeutronNetwork network) {
66 if (network.isShared()) {
67 logger.error(" Network shared attribute not supported ");
68 return HttpURLConnection.HTTP_NOT_ACCEPTABLE;
71 return HttpURLConnection.HTTP_CREATED;
75 * Invoked to take action after a network has been created.
77 * @param network An instance of new Neutron Network object.
80 public void neutronNetworkCreated(NeutronNetwork network) {
81 int result = HttpURLConnection.HTTP_BAD_REQUEST;
82 logger.trace("neutronNetworkCreated: network: {}", network);
83 result = canCreateNetwork(network);
84 if (result != HttpURLConnection.HTTP_CREATED) {
85 logger.debug("Network creation failed {} ", result);
92 * Invoked when a network update is requested
93 * to indicate if the specified network can be changed
94 * using the specified delta.
96 * @param delta Updates to the network object using patch semantics.
97 * @param original An instance of the Neutron Network object
99 * @return A HTTP status code to the update request.
102 public int canUpdateNetwork(NeutronNetwork delta,
103 NeutronNetwork original) {
104 logger.trace("canUpdateNetwork: network delta {} --- original {}", delta, original);
105 return HttpURLConnection.HTTP_OK;
109 * Invoked to take action after a network has been updated.
111 * @param network An instance of modified Neutron Network object.
114 public void neutronNetworkUpdated(NeutronNetwork network) {
115 logger.trace("neutronNetworkUpdated: network: {}", network);
120 * Invoked when a network deletion is requested
121 * to indicate if the specified network can be deleted.
123 * @param network An instance of the Neutron Network object to be deleted.
124 * @return A HTTP status code to the deletion request.
127 public int canDeleteNetwork(NeutronNetwork network) {
128 return HttpURLConnection.HTTP_OK;
132 * Invoked to take action after a network has been deleted.
134 * @param network An instance of deleted Neutron Network object.
137 public void neutronNetworkDeleted(NeutronNetwork network) {
139 int result = canDeleteNetwork(network);
140 logger.trace("canDeleteNetwork: network: {}", network);
141 if (result != HttpURLConnection.HTTP_OK) {
142 logger.error(" deleteNetwork validation failed for result - {} ",
146 /* Is this the last Neutron tenant network */
147 List <NeutronNetwork> networks;
148 if (neutronNetworkCache != null) {
149 networks = neutronNetworkCache.getAllNetworks();
150 if (networks.isEmpty()) {
151 logger.trace("neutronNetworkDeleted: last tenant network, delete tunnel ports...");
152 List<Node> nodes = connectionService.getNodes();
154 for (Node node : nodes) {
155 List<String> phyIfName = bridgeConfigurationManager.getAllPhysicalInterfaceNames(node);
157 ConcurrentMap<String, Row> ports =
158 this.ovsdbConfigurationService.getRows(node,
159 ovsdbConfigurationService.getTableName(node, Port.class));
161 for (Row portRow : ports.values()) {
162 Port port = ovsdbConfigurationService.getTypedRow(node, Port.class, portRow);
163 for (UUID interfaceUuid : port.getInterfacesColumn().getData()) {
164 Interface interfaceRow = (Interface) ovsdbConfigurationService
166 ovsdbConfigurationService.getTableName(node, Interface.class),
167 interfaceUuid.toString());
169 String interfaceType = interfaceRow.getTypeColumn().getData();
170 if (interfaceType.equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VXLAN)
171 || interfaceType.equalsIgnoreCase(
172 NetworkHandler.NETWORK_TYPE_GRE)) {
173 /* delete tunnel ports on this node */
174 logger.trace("Delete tunnel interface {}", interfaceRow);
175 ovsdbConfigurationService.deleteRow(node,
176 ovsdbConfigurationService.getTableName(node, Port.class),
177 port.getUuid().toString());
179 } else if (!phyIfName.isEmpty() && phyIfName.contains(interfaceRow.getName())) {
180 logger.trace("Delete physical interface {}", interfaceRow);
181 ovsdbConfigurationService.deleteRow(node,
182 ovsdbConfigurationService.getTableName(node, Port.class),
183 port.getUuid().toString());
189 } catch (Exception e) {
190 logger.error("Exception during handlingNeutron network delete");
195 tenantNetworkManager.networkDeleted(network.getID());