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.openstack.netvirt.api.BridgeConfigurationManager;
18 import org.opendaylight.ovsdb.openstack.netvirt.api.ConfigurationService;
19 import org.opendaylight.ovsdb.openstack.netvirt.api.TenantNetworkManager;
20 import org.opendaylight.ovsdb.plugin.IConnectionServiceInternal;
21 import org.opendaylight.ovsdb.plugin.OvsdbConfigService;
22 import org.opendaylight.ovsdb.plugin.OvsdbInventoryListener;
23 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 import java.net.HttpURLConnection;
29 import java.util.List;
30 import java.util.concurrent.ConcurrentMap;
33 * Handle requests for Neutron Network.
35 public class NetworkHandler extends AbstractHandler
36 implements INeutronNetworkAware {
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";
45 static final Logger logger = LoggerFactory.getLogger(NetworkHandler.class);
47 // The implementation for each of these services is resolved by the OSGi Service Manager
48 private volatile TenantNetworkManager tenantNetworkManager;
49 private volatile BridgeConfigurationManager bridgeConfigurationManager;
50 private volatile ConfigurationService configurationService;
51 private volatile OvsdbConfigService ovsdbConfigService;
52 private volatile IConnectionServiceInternal connectionService;
53 private volatile INeutronNetworkCRUD neutronNetworkCache;
54 private volatile OvsdbInventoryListener inventoryListener;
57 * Invoked when a network creation is requested
58 * to indicate if the specified network can be created.
60 * @param network An instance of proposed new Neutron Network object.
61 * @return A HTTP status code to the creation request.
64 public int canCreateNetwork(NeutronNetwork network) {
65 if (network.isShared()) {
66 logger.error(" Network shared attribute not supported ");
67 return HttpURLConnection.HTTP_NOT_ACCEPTABLE;
70 return HttpURLConnection.HTTP_CREATED;
74 * Invoked to take action after a network has been created.
76 * @param network An instance of new Neutron Network object.
79 public void neutronNetworkCreated(NeutronNetwork network) {
80 int result = HttpURLConnection.HTTP_BAD_REQUEST;
81 logger.trace("neutronNetworkCreated: network: {}", network);
82 result = canCreateNetwork(network);
83 if (result != HttpURLConnection.HTTP_CREATED) {
84 logger.debug("Network creation failed {} ", result);
91 * Invoked when a network update is requested
92 * to indicate if the specified network can be changed
93 * using the specified delta.
95 * @param delta Updates to the network object using patch semantics.
96 * @param original An instance of the Neutron Network object
98 * @return A HTTP status code to the update request.
101 public int canUpdateNetwork(NeutronNetwork delta,
102 NeutronNetwork original) {
103 logger.trace("canUpdateNetwork: network delta {} --- original {}", delta, original);
104 return HttpURLConnection.HTTP_OK;
108 * Invoked to take action after a network has been updated.
110 * @param network An instance of modified Neutron Network object.
113 public void neutronNetworkUpdated(NeutronNetwork network) {
114 logger.trace("neutronNetworkUpdated: network: {}", network);
119 * Invoked when a network deletion is requested
120 * to indicate if the specified network can be deleted.
122 * @param network An instance of the Neutron Network object to be deleted.
123 * @return A HTTP status code to the deletion request.
126 public int canDeleteNetwork(NeutronNetwork network) {
127 return HttpURLConnection.HTTP_OK;
131 * Invoked to take action after a network has been deleted.
133 * @param network An instance of deleted Neutron Network object.
136 public void neutronNetworkDeleted(NeutronNetwork network) {
138 int result = canDeleteNetwork(network);
139 logger.trace("canDeleteNetwork: network: {}", network);
140 if (result != HttpURLConnection.HTTP_OK) {
141 logger.error(" deleteNetwork validation failed for result - {} ",
145 /* Is this the last Neutron tenant network */
146 List <NeutronNetwork> networks;
147 if (neutronNetworkCache != null) {
148 networks = neutronNetworkCache.getAllNetworks();
149 if (networks.isEmpty()) {
150 logger.trace("neutronNetworkDeleted: last tenant network, delete tunnel ports...");
151 List<Node> nodes = connectionService.getNodes();
153 for (Node node : nodes) {
154 List<String> phyIfName = bridgeConfigurationManager.getAllPhysicalInterfaceNames(node);
156 ConcurrentMap<String, Row> interfaces = this.ovsdbConfigService.getRows(node, ovsdbConfigService.getTableName(node, Interface.class));
157 if (interfaces != null) {
158 for (String intfUUID : interfaces.keySet()) {
159 Interface intf = ovsdbConfigService.getTypedRow(node, Interface.class, interfaces.get(intfUUID));
160 String intfType = intf.getTypeColumn().getData();
161 if (intfType.equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VXLAN) || intfType.equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_GRE)) {
162 /* delete tunnel ports on this node */
163 logger.trace("Delete tunnel intf {}", intf);
164 inventoryListener.rowRemoved(node, intf.getSchema().getName(), intfUUID,
165 intf.getRow(), null);
166 } else if (!phyIfName.isEmpty() && phyIfName.contains(intf.getName())) {
167 logger.trace("Delete physical intf {}", intf);
168 inventoryListener.rowRemoved(node, intf.getSchema().getName(), intfUUID,
169 intf.getRow(), null);
173 } catch (Exception e) {
174 logger.error("Exception during handlingNeutron network delete");
179 tenantNetworkManager.networkDeleted(network.getID());