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 java.net.HttpURLConnection;
13 import java.util.List;
15 import org.opendaylight.neutron.spi.INeutronNetworkAware;
16 import org.opendaylight.neutron.spi.INeutronNetworkCRUD;
17 import org.opendaylight.neutron.spi.NeutronNetwork;
18 import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
19 import org.opendaylight.ovsdb.openstack.netvirt.api.BridgeConfigurationManager;
20 import org.opendaylight.ovsdb.openstack.netvirt.api.EventDispatcher;
21 import org.opendaylight.ovsdb.openstack.netvirt.api.NodeCacheManager;
22 import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound;
23 import org.opendaylight.ovsdb.openstack.netvirt.api.TenantNetworkManager;
24 import org.opendaylight.ovsdb.openstack.netvirt.impl.NeutronL3Adapter;
25 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
28 import org.osgi.framework.BundleContext;
29 import org.osgi.framework.ServiceReference;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * Handle requests for Neutron Network.
36 public class NetworkHandler extends AbstractHandler implements INeutronNetworkAware, ConfigInterface {
37 private static final Logger LOG = LoggerFactory.getLogger(NetworkHandler.class);
38 public static final String NETWORK_TYPE_VXLAN = "vxlan";
39 public static final String NETWORK_TYPE_GRE = "gre";
40 public static final String NETWORK_TYPE_VLAN = "vlan";
42 // The implementation for each of these services is resolved by the OSGi Service Manager
43 private volatile TenantNetworkManager tenantNetworkManager;
44 private volatile BridgeConfigurationManager bridgeConfigurationManager;
45 private volatile NodeCacheManager nodeCacheManager;
46 private volatile INeutronNetworkCRUD neutronNetworkCache;
47 private volatile NeutronL3Adapter neutronL3Adapter;
48 private volatile Southbound southbound;
51 * Invoked when a network creation is requested
52 * to indicate if the specified network can be created.
54 * @param network An instance of proposed new Neutron Network object.
55 * @return A HTTP status code to the creation request.
58 public int canCreateNetwork(NeutronNetwork network) {
59 if (network.isShared()) {
60 LOG.error(" Network shared attribute not supported ");
61 return HttpURLConnection.HTTP_NOT_ACCEPTABLE;
64 return HttpURLConnection.HTTP_OK;
68 * Invoked to take action after a network has been created.
70 * @param network An instance of new Neutron Network object.
73 public void neutronNetworkCreated(NeutronNetwork network) {
74 enqueueEvent(new NorthboundEvent(network, Action.ADD));
76 private void doNeutronNetworkCreated(NeutronNetwork network) {
77 neutronL3Adapter.handleNeutronNetworkEvent(network, Action.ADD);
81 * Invoked when a network update is requested
82 * to indicate if the specified network can be changed
83 * using the specified delta.
85 * @param delta Updates to the network object using patch semantics.
86 * @param original An instance of the Neutron Network object
88 * @return A HTTP status code to the update request.
91 public int canUpdateNetwork(NeutronNetwork delta,
92 NeutronNetwork original) {
93 if (delta.isShared()) {
94 LOG.error(" Network shared attribute not supported ");
95 return HttpURLConnection.HTTP_NOT_ACCEPTABLE;
98 return HttpURLConnection.HTTP_OK;
102 * Invoked to take action after a network has been updated.
104 * @param network An instance of modified Neutron Network object.
107 public void neutronNetworkUpdated(NeutronNetwork network) {
108 enqueueEvent(new NorthboundEvent(network, Action.UPDATE));
110 private void doNeutronNetworkUpdated(NeutronNetwork network) {
111 neutronL3Adapter.handleNeutronNetworkEvent(network, Action.UPDATE);
115 * Invoked when a network deletion is requested
116 * to indicate if the specified network can be deleted.
118 * @param network An instance of the Neutron Network object to be deleted.
119 * @return A HTTP status code to the deletion request.
122 public int canDeleteNetwork(NeutronNetwork network) {
123 return HttpURLConnection.HTTP_OK;
127 * Invoked to take action after a network has been deleted.
129 * @param network An instance of deleted Neutron Network object.
132 public void neutronNetworkDeleted(NeutronNetwork network) {
133 enqueueEvent(new NorthboundEvent(network, Action.DELETE));
135 private void doNeutronNetworkDeleted(NeutronNetwork network) {
136 neutronL3Adapter.handleNeutronNetworkEvent(network, Action.DELETE);
138 /* Is this the last Neutron tenant network */
139 List <NeutronNetwork> networks;
140 if (neutronNetworkCache != null) {
141 networks = neutronNetworkCache.getAllNetworks();
142 if (networks.isEmpty()) {
143 LOG.trace("neutronNetworkDeleted: last tenant network, delete tunnel ports...");
144 List<Node> nodes = nodeCacheManager.getNodes();
146 for (Node node : nodes) {
147 List<String> phyIfName = bridgeConfigurationManager.getAllPhysicalInterfaceNames(node);
149 List<OvsdbTerminationPointAugmentation> ports = southbound.getTerminationPointsOfBridge(node);
150 for (OvsdbTerminationPointAugmentation port : ports) {
151 if (southbound.isTunnel(port)) {
152 LOG.trace("Delete tunnel interface {}", port.getName());
153 southbound.deleteTerminationPoint(node, port.getName());
154 } else if (!phyIfName.isEmpty() && phyIfName.contains(port.getName())) {
155 LOG.trace("Delete physical interface {}", port.getName());
156 southbound.deleteTerminationPoint(node, port.getName());
159 } catch (Exception e) {
160 LOG.error("Exception during handlingNeutron network delete", e);
165 tenantNetworkManager.networkDeleted(network.getID());
171 * @param abstractEvent the {@link org.opendaylight.ovsdb.openstack.netvirt.AbstractEvent} event to be handled.
172 * @see org.opendaylight.ovsdb.openstack.netvirt.api.EventDispatcher
175 public void processEvent(AbstractEvent abstractEvent) {
176 if (!(abstractEvent instanceof NorthboundEvent)) {
177 LOG.error("Unable to process abstract event {}", abstractEvent);
180 NorthboundEvent ev = (NorthboundEvent) abstractEvent;
181 switch (ev.getAction()) {
183 doNeutronNetworkCreated(ev.getNeutronNetwork());
186 doNeutronNetworkUpdated(ev.getNeutronNetwork());
189 doNeutronNetworkDeleted(ev.getNeutronNetwork());
192 LOG.warn("Unable to process event action {}", ev.getAction());
198 public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
199 tenantNetworkManager =
200 (TenantNetworkManager) ServiceHelper.getGlobalInstance(TenantNetworkManager.class, this);
201 bridgeConfigurationManager =
202 (BridgeConfigurationManager) ServiceHelper.getGlobalInstance(BridgeConfigurationManager.class, this);
204 (NodeCacheManager) ServiceHelper.getGlobalInstance(NodeCacheManager.class, this);
206 (NeutronL3Adapter) ServiceHelper.getGlobalInstance(NeutronL3Adapter.class, this);
208 (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
210 (EventDispatcher) ServiceHelper.getGlobalInstance(EventDispatcher.class, this);
211 eventDispatcher.eventHandlerAdded(
212 bundleContext.getServiceReference(INeutronNetworkAware.class.getName()), this);
216 public void setDependencies(Object impl) {
217 if (impl instanceof INeutronNetworkCRUD) {
218 neutronNetworkCache = (INeutronNetworkCRUD)impl;