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.neutron.spi.INeutronNetworkAware;
13 import org.opendaylight.neutron.spi.INeutronNetworkCRUD;
14 import org.opendaylight.neutron.spi.NeutronNetwork;
15 import org.opendaylight.ovsdb.lib.notation.Row;
16 import org.opendaylight.ovsdb.lib.notation.UUID;
17 import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
18 import org.opendaylight.ovsdb.openstack.netvirt.api.BridgeConfigurationManager;
19 //import org.opendaylight.ovsdb.openstack.netvirt.api.ConfigurationService;
20 import org.opendaylight.ovsdb.openstack.netvirt.api.MdsalConsumer;
21 import org.opendaylight.ovsdb.openstack.netvirt.api.TenantNetworkManager;
22 import org.opendaylight.ovsdb.openstack.netvirt.impl.NeutronL3Adapter;
23 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
24 import org.opendaylight.ovsdb.schema.openvswitch.Port;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 import java.net.HttpURLConnection;
31 import java.util.List;
32 import java.util.concurrent.ConcurrentMap;
35 * Handle requests for Neutron Network.
37 public class NetworkHandler extends AbstractHandler
38 implements INeutronNetworkAware {
40 public static final String NETWORK_TYPE_VXLAN = "vxlan";
41 public static final String NETWORK_TYPE_GRE = "gre";
42 public static final String NETWORK_TYPE_VLAN = "vlan";
47 static final Logger logger = LoggerFactory.getLogger(NetworkHandler.class);
49 // The implementation for each of these services is resolved by the OSGi Service Manager
50 private volatile TenantNetworkManager tenantNetworkManager;
51 private volatile BridgeConfigurationManager bridgeConfigurationManager;
52 /* TODO SB_MIGRATION */
53 //private volatile OvsdbConfigurationService ovsdbConfigurationService;
54 //private volatile OvsdbConnectionService connectionService;
55 private volatile MdsalConsumer mdsalConsumer; // TODO SB_MIGRATION
56 private volatile INeutronNetworkCRUD neutronNetworkCache;
57 private volatile NeutronL3Adapter neutronL3Adapter;
60 * Invoked when a network creation is requested
61 * to indicate if the specified network can be created.
63 * @param network An instance of proposed new Neutron Network object.
64 * @return A HTTP status code to the creation request.
67 public int canCreateNetwork(NeutronNetwork network) {
68 if (network.isShared()) {
69 logger.error(" Network shared attribute not supported ");
70 return HttpURLConnection.HTTP_NOT_ACCEPTABLE;
73 return HttpURLConnection.HTTP_CREATED;
77 * Invoked to take action after a network has been created.
79 * @param network An instance of new Neutron Network object.
82 public void neutronNetworkCreated(NeutronNetwork network) {
83 int result = HttpURLConnection.HTTP_BAD_REQUEST;
84 logger.trace("neutronNetworkCreated: network: {}", network);
85 result = canCreateNetwork(network);
86 if (result != HttpURLConnection.HTTP_CREATED) {
87 logger.debug("Network creation failed {} ", result);
91 enqueueEvent(new NorthboundEvent(network, Action.ADD));
93 private void doNeutronNetworkCreated(NeutronNetwork network) {
94 neutronL3Adapter.handleNeutronNetworkEvent(network, Action.ADD);
98 * Invoked when a network update is requested
99 * to indicate if the specified network can be changed
100 * using the specified delta.
102 * @param delta Updates to the network object using patch semantics.
103 * @param original An instance of the Neutron Network object
105 * @return A HTTP status code to the update request.
108 public int canUpdateNetwork(NeutronNetwork delta,
109 NeutronNetwork original) {
110 logger.trace("canUpdateNetwork: network delta {} --- original {}", delta, original);
111 return HttpURLConnection.HTTP_OK;
115 * Invoked to take action after a network has been updated.
117 * @param network An instance of modified Neutron Network object.
120 public void neutronNetworkUpdated(NeutronNetwork network) {
121 enqueueEvent(new NorthboundEvent(network, Action.UPDATE));
123 private void doNeutronNetworkUpdated(NeutronNetwork network) {
124 neutronL3Adapter.handleNeutronNetworkEvent(network, Action.UPDATE);
128 * Invoked when a network deletion is requested
129 * to indicate if the specified network can be deleted.
131 * @param network An instance of the Neutron Network object to be deleted.
132 * @return A HTTP status code to the deletion request.
135 public int canDeleteNetwork(NeutronNetwork network) {
136 return HttpURLConnection.HTTP_OK;
140 * Invoked to take action after a network has been deleted.
142 * @param network An instance of deleted Neutron Network object.
145 public void neutronNetworkDeleted(NeutronNetwork network) {
146 enqueueEvent(new NorthboundEvent(network, Action.DELETE));
148 private void doNeutronNetworkDeleted(NeutronNetwork network) {
150 neutronL3Adapter.handleNeutronNetworkEvent(network, Action.DELETE);
152 int result = canDeleteNetwork(network);
153 logger.trace("canDeleteNetwork: network: {}", network);
154 if (result != HttpURLConnection.HTTP_OK) {
155 logger.error(" deleteNetwork validation failed for result - {} ",
159 *//* Is this the last Neutron tenant network *//*
160 List <NeutronNetwork> networks;
161 if (neutronNetworkCache != null) {
162 networks = neutronNetworkCache.getAllNetworks();
163 if (networks.isEmpty()) {
164 logger.trace("neutronNetworkDeleted: last tenant network, delete tunnel ports...");
165 List<Node> nodes = connectionService.getNodes();
167 for (Node node : nodes) {
168 List<String> phyIfName = bridgeConfigurationManager.getAllPhysicalInterfaceNames(node);
170 ConcurrentMap<String, Row> ports =
171 this.ovsdbConfigurationService.getRows(node,
172 ovsdbConfigurationService.getTableName(node, Port.class));
174 for (Row portRow : ports.values()) {
175 Port port = ovsdbConfigurationService.getTypedRow(node, Port.class, portRow);
176 for (UUID interfaceUuid : port.getInterfacesColumn().getData()) {
177 Row ifaceRow = ovsdbConfigurationService
179 ovsdbConfigurationService.getTableName(node, Interface.class),
180 interfaceUuid.toString());
181 Interface iface = ovsdbConfigurationService.getTypedRow(node, Interface.class, ifaceRow);
182 String interfaceType = iface.getTypeColumn().getData();
183 if (interfaceType.equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VXLAN)
184 || interfaceType.equalsIgnoreCase(
185 NetworkHandler.NETWORK_TYPE_GRE)) {
186 *//* delete tunnel ports on this node *//*
187 logger.trace("Delete tunnel interface {}", iface.getName());
188 ovsdbConfigurationService.deleteRow(node,
189 ovsdbConfigurationService.getTableName(node, Port.class),
190 port.getUuid().toString());
192 } else if (!phyIfName.isEmpty() && phyIfName.contains(iface.getName())) {
193 logger.trace("Delete physical interface {}", iface.getName());
194 ovsdbConfigurationService.deleteRow(node,
195 ovsdbConfigurationService.getTableName(node, Port.class),
196 port.getUuid().toString());
202 } catch (Exception e) {
203 logger.error("Exception during handlingNeutron network delete", e);
208 tenantNetworkManager.networkDeleted(network.getID());*/
214 * @param abstractEvent the {@link org.opendaylight.ovsdb.openstack.netvirt.AbstractEvent} event to be handled.
215 * @see org.opendaylight.ovsdb.openstack.netvirt.api.EventDispatcher
218 public void processEvent(AbstractEvent abstractEvent) {
219 if (!(abstractEvent instanceof NorthboundEvent)) {
220 logger.error("Unable to process abstract event " + abstractEvent);
223 NorthboundEvent ev = (NorthboundEvent) abstractEvent;
224 switch (ev.getAction()) {
226 doNeutronNetworkCreated(ev.getNeutronNetwork());
229 doNeutronNetworkUpdated(ev.getNeutronNetwork());
232 doNeutronNetworkDeleted(ev.getNeutronNetwork());
235 logger.warn("Unable to process event action " + ev.getAction());