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.openstack.netvirt;
12 import org.opendaylight.controller.networkconfig.neutron.INeutronPortAware;
13 import org.opendaylight.controller.networkconfig.neutron.NeutronPort;
14 import org.opendaylight.controller.sal.core.Node;
15 import org.opendaylight.ovsdb.lib.notation.Row;
16 import org.opendaylight.ovsdb.lib.notation.UUID;
17 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
18 import org.opendaylight.ovsdb.plugin.IConnectionServiceInternal;
19 import org.opendaylight.ovsdb.plugin.OvsdbConfigService;
20 import org.opendaylight.ovsdb.plugin.OvsdbInventoryListener;
21 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
22 import org.opendaylight.ovsdb.schema.openvswitch.Port;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 import java.net.HttpURLConnection;
28 import java.util.List;
30 import java.util.concurrent.ConcurrentMap;
33 * Handle requests for Neutron Port.
35 public class PortHandler extends AbstractHandler
36 implements INeutronPortAware {
41 static final Logger logger = LoggerFactory.getLogger(PortHandler.class);
43 private volatile OvsdbConfigService ovsdbConfigService;
44 private volatile IConnectionServiceInternal connectionService;
45 private volatile OvsdbInventoryListener ovsdbInventoryListener;
48 * Invoked when a port creation is requested
49 * to indicate if the specified port can be created.
51 * @param port An instance of proposed new Port object.
52 * @return A HTTP status code to the creation request.
55 public int canCreatePort(NeutronPort port) {
56 return HttpURLConnection.HTTP_CREATED;
60 * Invoked to take action after a port has been created.
62 * @param port An instance of new Neutron Port object.
65 public void neutronPortCreated(NeutronPort port) {
66 int result = canCreatePort(port);
67 if (result != HttpURLConnection.HTTP_CREATED) {
68 logger.error(" Port create validation failed result - {} ", result);
72 logger.debug(" Port-ADD successful for tenant-id - {}," +
73 " network-id - {}, port-id - {}, result - {} ",
74 port.getTenantID(), port.getNetworkUUID(),
75 port.getID(), result);
79 * Invoked when a port update is requested
80 * to indicate if the specified port can be changed
81 * using the specified delta.
83 * @param delta Updates to the port object using patch semantics.
84 * @param original An instance of the Neutron Port object
86 * @return A HTTP status code to the update request.
89 public int canUpdatePort(NeutronPort delta,
90 NeutronPort original) {
91 int result = HttpURLConnection.HTTP_OK;
93 * To basic validation of the request
96 if ((original == null) || (delta == null)) {
97 logger.error("port object not specified");
98 return HttpURLConnection.HTTP_BAD_REQUEST;
104 * Invoked to take action after a port has been updated.
106 * @param port An instance of modified Neutron Port object.
109 public void neutronPortUpdated(NeutronPort port) {
113 * Invoked when a port deletion is requested
114 * to indicate if the specified port can be deleted.
116 * @param port An instance of the Neutron Port object to be deleted.
117 * @return A HTTP status code to the deletion request.
120 public int canDeletePort(NeutronPort port) {
121 return HttpURLConnection.HTTP_OK;
125 * Invoked to take action after a port has been deleted.
127 * @param neutronPort An instance of deleted Neutron Port object.
130 public void neutronPortDeleted(NeutronPort neutronPort) {
132 int result = canDeletePort(neutronPort);
133 if (result != HttpURLConnection.HTTP_OK) {
134 logger.error(" deletePort validation failed - result {} ", result);
138 List<Node> nodes = connectionService.getNodes();
139 for (Node node : nodes) {
141 ConcurrentMap<String, Row> portRows =
142 this.ovsdbConfigService.getRows(node,
143 ovsdbConfigService.getTableName(node, Port.class));
144 if (portRows != null) {
145 for (Row portRow : portRows.values()) {
146 Port port = ovsdbConfigService.getTypedRow(node, Port.class, portRow);
147 for (UUID interfaceUuid : port.getInterfacesColumn().getData()) {
148 Interface interfaceRow = (Interface) ovsdbConfigService
150 ovsdbConfigService.getTableName(node, Interface.class),
151 interfaceUuid.toString());
153 Map<String, String> externalIds = interfaceRow.getExternalIdsColumn().getData();
155 if (externalIds == null) {
156 logger.trace("No external_ids seen in {}", interfaceRow);
160 /* Compare Neutron port uuid */
161 String neutronPortId = externalIds.get(Constants.EXTERNAL_ID_INTERFACE_ID);
162 if (neutronPortId == null) {
166 if (neutronPortId.equalsIgnoreCase(neutronPort.getPortUUID())) {
167 logger.trace("neutronPortDeleted: Delete interface {}", interfaceRow.getName());
168 ovsdbConfigService.deleteRow(node,
169 ovsdbConfigService.getTableName(node, Port.class),
170 port.getUuid().toString());
176 } catch (Exception e) {
177 logger.error("Exception during handlingNeutron network delete");
180 logger.debug(" PORT delete successful for tenant-id - {}, " +
181 " network-id - {}, port-id - {}",
182 neutronPort.getTenantID(), neutronPort.getNetworkUUID(),
183 neutronPort.getID());