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.openstack.netvirt.api.Action;
16 import org.opendaylight.ovsdb.openstack.netvirt.api.BridgeConfigurationManager;
17 import org.opendaylight.ovsdb.openstack.netvirt.api.OvsdbConnectionService;
18 import org.opendaylight.ovsdb.openstack.netvirt.api.TenantNetworkManager;
19 import org.opendaylight.ovsdb.openstack.netvirt.impl.MdsalUtils;
20 import org.opendaylight.ovsdb.openstack.netvirt.impl.NeutronL3Adapter;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
22 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 import java.net.HttpURLConnection;
28 import java.util.List;
31 * Handle requests for Neutron Network.
33 public class NetworkHandler extends AbstractHandler
34 implements INeutronNetworkAware {
36 public static final String NETWORK_TYPE_VXLAN = "vxlan";
37 public static final String NETWORK_TYPE_GRE = "gre";
38 public static final String NETWORK_TYPE_VLAN = "vlan";
43 static final Logger logger = LoggerFactory.getLogger(NetworkHandler.class);
45 // The implementation for each of these services is resolved by the OSGi Service Manager
46 private volatile TenantNetworkManager tenantNetworkManager;
47 private volatile BridgeConfigurationManager bridgeConfigurationManager;
48 /* TODO SB_MIGRATION */
49 private volatile OvsdbConnectionService connectionService;
50 private volatile INeutronNetworkCRUD neutronNetworkCache;
51 private volatile NeutronL3Adapter neutronL3Adapter;
54 * Invoked when a network creation is requested
55 * to indicate if the specified network can be created.
57 * @param network An instance of proposed new Neutron Network object.
58 * @return A HTTP status code to the creation request.
61 public int canCreateNetwork(NeutronNetwork network) {
62 if (network.isShared()) {
63 logger.error(" Network shared attribute not supported ");
64 return HttpURLConnection.HTTP_NOT_ACCEPTABLE;
67 return HttpURLConnection.HTTP_OK;
71 * Invoked to take action after a network has been created.
73 * @param network An instance of new Neutron Network object.
76 public void neutronNetworkCreated(NeutronNetwork network) {
77 enqueueEvent(new NorthboundEvent(network, Action.ADD));
79 private void doNeutronNetworkCreated(NeutronNetwork network) {
80 neutronL3Adapter.handleNeutronNetworkEvent(network, Action.ADD);
84 * Invoked when a network update is requested
85 * to indicate if the specified network can be changed
86 * using the specified delta.
88 * @param delta Updates to the network object using patch semantics.
89 * @param original An instance of the Neutron Network object
91 * @return A HTTP status code to the update request.
94 public int canUpdateNetwork(NeutronNetwork delta,
95 NeutronNetwork original) {
96 if (delta.isShared()) {
97 logger.error(" Network shared attribute not supported ");
98 return HttpURLConnection.HTTP_NOT_ACCEPTABLE;
101 return HttpURLConnection.HTTP_OK;
105 * Invoked to take action after a network has been updated.
107 * @param network An instance of modified Neutron Network object.
110 public void neutronNetworkUpdated(NeutronNetwork network) {
111 enqueueEvent(new NorthboundEvent(network, Action.UPDATE));
113 private void doNeutronNetworkUpdated(NeutronNetwork network) {
114 neutronL3Adapter.handleNeutronNetworkEvent(network, Action.UPDATE);
118 * Invoked when a network deletion is requested
119 * to indicate if the specified network can be deleted.
121 * @param network An instance of the Neutron Network object to be deleted.
122 * @return A HTTP status code to the deletion request.
125 public int canDeleteNetwork(NeutronNetwork network) {
126 return HttpURLConnection.HTTP_OK;
130 * Invoked to take action after a network has been deleted.
132 * @param network An instance of deleted Neutron Network object.
135 public void neutronNetworkDeleted(NeutronNetwork network) {
136 enqueueEvent(new NorthboundEvent(network, Action.DELETE));
138 private void doNeutronNetworkDeleted(NeutronNetwork network) {
139 neutronL3Adapter.handleNeutronNetworkEvent(network, Action.DELETE);
141 /* Is this the last Neutron tenant network */
142 List <NeutronNetwork> networks;
143 if (neutronNetworkCache != null) {
144 networks = neutronNetworkCache.getAllNetworks();
145 if (networks.isEmpty()) {
146 logger.trace("neutronNetworkDeleted: last tenant network, delete tunnel ports...");
147 List<Node> nodes = connectionService.getNodes();
149 for (Node node : nodes) {
150 List<String> phyIfName = bridgeConfigurationManager.getAllPhysicalInterfaceNames(node);
152 List<OvsdbTerminationPointAugmentation> ports = MdsalUtils.getPorts(node);
153 for (OvsdbTerminationPointAugmentation port : ports) {
154 if (MdsalUtils.isTunnel(port)) {
155 logger.trace("Delete tunnel interface {}", port.getName());
156 MdsalUtils.deletePort(node, port.getName());
157 } else if (!phyIfName.isEmpty() && phyIfName.contains(port.getName())) {
158 logger.trace("Delete physical interface {}", port.getName());
159 MdsalUtils.deletePort(node, port.getName());
162 } catch (Exception e) {
163 logger.error("Exception during handlingNeutron network delete", e);
168 tenantNetworkManager.networkDeleted(network.getID());
174 * @param abstractEvent the {@link org.opendaylight.ovsdb.openstack.netvirt.AbstractEvent} event to be handled.
175 * @see org.opendaylight.ovsdb.openstack.netvirt.api.EventDispatcher
178 public void processEvent(AbstractEvent abstractEvent) {
179 if (!(abstractEvent instanceof NorthboundEvent)) {
180 logger.error("Unable to process abstract event " + abstractEvent);
183 NorthboundEvent ev = (NorthboundEvent) abstractEvent;
184 switch (ev.getAction()) {
186 doNeutronNetworkCreated(ev.getNeutronNetwork());
189 doNeutronNetworkUpdated(ev.getNeutronNetwork());
192 doNeutronNetworkDeleted(ev.getNeutronNetwork());
195 logger.warn("Unable to process event action " + ev.getAction());