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.neutron.spi.INeutronPortAware;
13 import org.opendaylight.neutron.spi.NeutronPort;
14 import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
15 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
16 import org.opendaylight.ovsdb.openstack.netvirt.api.NodeCacheManager;
17 import org.opendaylight.ovsdb.openstack.netvirt.impl.NeutronL3Adapter;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
19 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 import java.net.HttpURLConnection;
24 import java.util.List;
27 * Handle requests for Neutron Port.
29 public class PortHandler extends AbstractHandler implements INeutronPortAware {
30 static final Logger logger = LoggerFactory.getLogger(PortHandler.class);
32 // The implementation for each of these services is resolved by the OSGi Service Manager
33 private volatile NodeCacheManager nodeCacheManager;
34 private volatile NeutronL3Adapter neutronL3Adapter;
37 * Invoked when a port creation is requested
38 * to indicate if the specified port can be created.
40 * @param port An instance of proposed new Port object.
41 * @return A HTTP status code to the creation request.
44 public int canCreatePort(NeutronPort port) {
45 return HttpURLConnection.HTTP_OK;
49 * Invoked to take action after a port has been created.
51 * @param neutronPort An instance of new Neutron Port object.
54 public void neutronPortCreated(NeutronPort neutronPort) {
55 enqueueEvent(new NorthboundEvent(neutronPort, Action.ADD));
57 private void doNeutronPortCreated(NeutronPort neutronPort) {
58 logger.debug(" Port-ADD successful for tenant-id - {}," +
59 " network-id - {}, port-id - {}",
60 neutronPort.getTenantID(), neutronPort.getNetworkUUID(),
62 neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.ADD);
66 * Invoked when a port update is requested
67 * to indicate if the specified port can be changed
68 * using the specified delta.
70 * @param delta Updates to the port object using patch semantics.
71 * @param original An instance of the Neutron Port object
73 * @return A HTTP status code to the update request.
76 public int canUpdatePort(NeutronPort delta,
77 NeutronPort original) {
78 return HttpURLConnection.HTTP_OK;
82 * Invoked to take action after a port has been updated.
84 * @param neutronPort An instance of modified Neutron Port object.
87 public void neutronPortUpdated(NeutronPort neutronPort) {
88 enqueueEvent(new NorthboundEvent(neutronPort, Action.UPDATE));
90 private void doNeutronPortUpdated(NeutronPort neutronPort) {
91 logger.debug("Handling neutron update port " + neutronPort);
92 neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.UPDATE);
96 * Invoked when a port deletion is requested
97 * to indicate if the specified port can be deleted.
99 * @param port An instance of the Neutron Port object to be deleted.
100 * @return A HTTP status code to the deletion request.
103 public int canDeletePort(NeutronPort port) {
104 return HttpURLConnection.HTTP_OK;
108 * Invoked to take action after a port has been deleted.
110 * @param neutronPort An instance of deleted Neutron Port object.
113 public void neutronPortDeleted(NeutronPort neutronPort) {
114 enqueueEvent(new NorthboundEvent(neutronPort, Action.DELETE));
116 private void doNeutronPortDeleted(NeutronPort neutronPort) {
117 logger.debug("Handling neutron delete port " + neutronPort);
118 neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.DELETE);
119 //TODO: Need to implement getNodes
120 List<Node> nodes = nodeCacheManager.getNodes();
121 for (Node node : nodes) {
123 List<OvsdbTerminationPointAugmentation> ports = MdsalUtils.getTerminationPointsOfBridge(node);
124 for (OvsdbTerminationPointAugmentation port : ports) {
125 String neutronPortId =
126 MdsalUtils.getInterfaceExternalIdsValue(port, Constants.EXTERNAL_ID_INTERFACE_ID);
127 if (neutronPortId != null && neutronPortId.equalsIgnoreCase(neutronPort.getPortUUID())) {
128 logger.trace("neutronPortDeleted: Delete interface {}", port.getName());
129 MdsalUtils.deleteTerminationPoint(node, port.getName());
133 } catch (Exception e) {
134 logger.error("Exception during handlingNeutron port delete", e);
137 logger.debug(" PORT delete successful for tenant-id - {}, " +
138 " network-id - {}, port-id - {}",
139 neutronPort.getTenantID(), neutronPort.getNetworkUUID(),
140 neutronPort.getID());
146 * @param abstractEvent the {@link org.opendaylight.ovsdb.openstack.netvirt.AbstractEvent} event to be handled.
147 * @see org.opendaylight.ovsdb.openstack.netvirt.api.EventDispatcher
150 public void processEvent(AbstractEvent abstractEvent) {
151 if (!(abstractEvent instanceof NorthboundEvent)) {
152 logger.error("Unable to process abstract event " + abstractEvent);
155 NorthboundEvent ev = (NorthboundEvent) abstractEvent;
156 switch (ev.getAction()) {
158 doNeutronPortCreated(ev.getPort());
161 doNeutronPortDeleted(ev.getPort());
164 doNeutronPortUpdated(ev.getPort());
167 logger.warn("Unable to process event action " + ev.getAction());