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
10 package org.opendaylight.ovsdb.neutron;
12 import java.net.HttpURLConnection;
13 import java.util.List;
15 import java.util.concurrent.ConcurrentMap;
17 import org.opendaylight.controller.networkconfig.neutron.INeutronNetworkCRUD;
18 import org.opendaylight.controller.networkconfig.neutron.INeutronPortAware;
19 import org.opendaylight.controller.networkconfig.neutron.NeutronNetwork;
20 import org.opendaylight.controller.networkconfig.neutron.NeutronPort;
21 import org.opendaylight.controller.sal.core.Node;
22 import org.opendaylight.controller.sal.utils.ServiceHelper;
23 import org.opendaylight.ovsdb.lib.table.Interface;
24 import org.opendaylight.ovsdb.lib.table.internal.Table;
25 import org.opendaylight.ovsdb.plugin.IConnectionServiceInternal;
26 import org.opendaylight.ovsdb.plugin.OVSDBInventoryListener;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * Handle requests for Neutron Port.
33 public class PortHandler extends BaseHandler
34 implements INeutronPortAware {
39 static final Logger logger = LoggerFactory.getLogger(PortHandler.class);
42 * Invoked when a port creation is requested
43 * to indicate if the specified port can be created.
45 * @param port An instance of proposed new Port object.
46 * @return A HTTP status code to the creation request.
49 public int canCreatePort(NeutronPort port) {
50 return HttpURLConnection.HTTP_CREATED;
54 * Invoked to take action after a port has been created.
56 * @param port An instance of new Neutron Port object.
59 public void neutronPortCreated(NeutronPort port) {
60 int result = canCreatePort(port);
61 if (result != HttpURLConnection.HTTP_CREATED) {
62 logger.error(" Port create validation failed result - {} ", result);
66 logger.debug(" Port-ADD successful for tenant-id - {}," +
67 " network-id - {}, port-id - {}, result - {} ",
68 port.getTenantID(), port.getNetworkUUID(),
69 port.getID(), result);
73 * Invoked when a port update is requested
74 * to indicate if the specified port can be changed
75 * using the specified delta.
77 * @param delta Updates to the port object using patch semantics.
78 * @param original An instance of the Neutron Port object
80 * @return A HTTP status code to the update request.
83 public int canUpdatePort(NeutronPort delta,
84 NeutronPort original) {
85 int result = HttpURLConnection.HTTP_OK;
87 * To basic validation of the request
90 if ((original == null) || (delta == null)) {
91 logger.error("port object not specified");
92 return HttpURLConnection.HTTP_BAD_REQUEST;
98 * Invoked to take action after a port has been updated.
100 * @param port An instance of modified Neutron Port object.
103 public void neutronPortUpdated(NeutronPort port) {
107 * Invoked when a port deletion is requested
108 * to indicate if the specified port can be deleted.
110 * @param port An instance of the Neutron Port object to be deleted.
111 * @return A HTTP status code to the deletion request.
114 public int canDeletePort(NeutronPort port) {
115 return HttpURLConnection.HTTP_OK;
119 * Invoked to take action after a port has been deleted.
121 * @param port An instance of deleted Neutron Port object.
124 public void neutronPortDeleted(NeutronPort port) {
126 int result = canDeletePort(port);
127 if (result != HttpURLConnection.HTTP_OK) {
128 logger.error(" deletePort validation failed - result {} ", result);
132 IConnectionServiceInternal connectionService = (IConnectionServiceInternal)ServiceHelper.getGlobalInstance(IConnectionServiceInternal.class, this);
133 INeutronNetworkCRUD neutronNetworkService = (INeutronNetworkCRUD)ServiceHelper.getGlobalInstance(INeutronNetworkCRUD.class, this);
134 NeutronNetwork neutronNetwork = neutronNetworkService.getNetwork(port.getNetworkUUID());
135 OVSDBInventoryListener inventoryListener = (OVSDBInventoryListener)ServiceHelper.getGlobalInstance(OVSDBInventoryListener.class, this);
136 List<Node> nodes = connectionService.getNodes();
137 for (Node node : nodes) {
139 ConcurrentMap<String, Table<?>> interfaces = this.ovsdbConfigService.getRows(node, Interface.NAME.getName());
140 if (interfaces != null) {
141 for (String intfUUID : interfaces.keySet()) {
142 Interface intf = (Interface) interfaces.get(intfUUID);
143 Map<String, String> externalIds = intf.getExternal_ids();
144 if (externalIds == null) {
145 logger.trace("No external_ids seen in {}", intf);
148 /* Compare Neutron port uuid */
149 String neutronPortId = externalIds.get(TenantNetworkManager.EXTERNAL_ID_INTERFACE_ID);
150 if (neutronPortId == null) {
153 if (neutronPortId.equalsIgnoreCase(port.getPortUUID())) {
154 logger.trace("neutronPortDeleted: Delete interface {}", intf.getName());
155 inventoryListener.rowRemoved(node, Interface.NAME.getName(), intfUUID,
156 intf, neutronNetwork);
161 } catch (Exception e) {
162 logger.error("Exception during handlingNeutron network delete");
165 logger.debug(" PORT delete successful for tenant-id - {}, " +
166 " network-id - {}, port-id - {}",
167 port.getTenantID(), port.getNetworkUUID(),