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.neutron;
12 import java.net.HttpURLConnection;
13 import java.util.ArrayList;
14 import java.util.List;
15 import java.util.concurrent.ConcurrentMap;
17 import org.opendaylight.controller.networkconfig.neutron.INeutronNetworkAware;
18 import org.opendaylight.controller.networkconfig.neutron.INeutronNetworkCRUD;
19 import org.opendaylight.controller.networkconfig.neutron.NeutronNetwork;
20 import org.opendaylight.controller.sal.core.Node;
21 import org.opendaylight.controller.sal.utils.ServiceHelper;
22 import org.opendaylight.ovsdb.lib.table.Interface;
23 import org.opendaylight.ovsdb.lib.table.internal.Table;
24 import org.opendaylight.ovsdb.plugin.IConnectionServiceInternal;
25 import org.opendaylight.ovsdb.plugin.OVSDBInventoryListener;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * Handle requests for Neutron Network.
32 public class NetworkHandler extends BaseHandler
33 implements INeutronNetworkAware {
35 public static final String NETWORK_TYPE_VXLAN = "vxlan";
36 public static final String NETWORK_TYPE_GRE = "gre";
37 public static final String NETWORK_TYPE_VLAN = "vlan";
42 static final Logger logger = LoggerFactory.getLogger(NetworkHandler.class);
44 // The implementation for each of these services is resolved by the OSGi Service Manager
45 private volatile ITenantNetworkManager tenantNetworkManager;
48 * Invoked when a network creation is requested
49 * to indicate if the specified network can be created.
51 * @param network An instance of proposed new Neutron Network object.
52 * @return A HTTP status code to the creation request.
55 public int canCreateNetwork(NeutronNetwork network) {
56 if (network.isShared()) {
57 logger.error(" Network shared attribute not supported ");
58 return HttpURLConnection.HTTP_NOT_ACCEPTABLE;
61 return HttpURLConnection.HTTP_CREATED;
65 * Invoked to take action after a network has been created.
67 * @param network An instance of new Neutron Network object.
70 public void neutronNetworkCreated(NeutronNetwork network) {
71 int result = HttpURLConnection.HTTP_BAD_REQUEST;
72 logger.trace("neutronNetworkCreated: network: {}", network);
73 result = canCreateNetwork(network);
74 if (result != HttpURLConnection.HTTP_CREATED) {
75 logger.debug("Network creation failed {} ", result);
82 * Invoked when a network update is requested
83 * to indicate if the specified network can be changed
84 * using the specified delta.
86 * @param delta Updates to the network object using patch semantics.
87 * @param original An instance of the Neutron Network object
89 * @return A HTTP status code to the update request.
92 public int canUpdateNetwork(NeutronNetwork delta,
93 NeutronNetwork original) {
94 logger.trace("canUpdateNetwork: network delta {} --- original {}", delta, original);
95 return HttpURLConnection.HTTP_OK;
99 * Invoked to take action after a network has been updated.
101 * @param network An instance of modified Neutron Network object.
104 public void neutronNetworkUpdated(NeutronNetwork network) {
105 logger.trace("neutronNetworkUpdated: network: {}", network);
110 * Invoked when a network deletion is requested
111 * to indicate if the specified network can be deleted.
113 * @param network An instance of the Neutron Network object to be deleted.
114 * @return A HTTP status code to the deletion request.
117 public int canDeleteNetwork(NeutronNetwork network) {
118 return HttpURLConnection.HTTP_OK;
122 * Invoked to take action after a network has been deleted.
124 * @param network An instance of deleted Neutron Network object.
127 public void neutronNetworkDeleted(NeutronNetwork network) {
129 int result = canDeleteNetwork(network);
130 logger.trace("canDeleteNetwork: network: {}", network);
131 if (result != HttpURLConnection.HTTP_OK) {
132 logger.error(" deleteNetwork validation failed for result - {} ",
136 /* Is this the last Neutron tenant network */
137 INeutronNetworkCRUD neutronNetworkService = (INeutronNetworkCRUD)ServiceHelper.getGlobalInstance(INeutronNetworkCRUD.class, this);
138 List <NeutronNetwork> networks = new ArrayList<NeutronNetwork>();
139 if (neutronNetworkService != null) {
140 networks = neutronNetworkService.getAllNetworks();
141 OVSDBInventoryListener inventoryListener = (OVSDBInventoryListener)ServiceHelper.getGlobalInstance(OVSDBInventoryListener.class, this);
142 if (networks.isEmpty()) {
143 logger.trace("neutronNetworkDeleted: last tenant network, delete tunnel ports...");
144 IConnectionServiceInternal connectionService = (IConnectionServiceInternal)
145 ServiceHelper.getGlobalInstance(IConnectionServiceInternal.class, this);
146 List<Node> nodes = connectionService.getNodes();
148 for (Node node : nodes) {
150 ConcurrentMap<String, Table<?>> interfaces = this.ovsdbConfigService.getRows(node, Interface.NAME.getName());
151 if (interfaces != null) {
152 for (String intfUUID : interfaces.keySet()) {
153 Interface intf = (Interface) interfaces.get(intfUUID);
154 String intfType = intf.getType();
155 if (intfType.equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VXLAN) || intfType.equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_GRE)) {
156 /* delete tunnel ports on this node */
157 logger.trace("Delete tunnel intf {}", intf);
158 inventoryListener.rowRemoved(node, Interface.NAME.getName(), intfUUID,
163 } catch (Exception e) {
164 logger.error("Exception during handlingNeutron network delete");
169 tenantNetworkManager.networkDeleted(network.getID());