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 java.net.HttpURLConnection;
13 import java.util.List;
15 import org.opendaylight.neutron.spi.INeutronPortAware;
16 import org.opendaylight.neutron.spi.NeutronPort;
17 import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
18 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
19 import org.opendaylight.ovsdb.openstack.netvirt.api.EventDispatcher;
20 import org.opendaylight.ovsdb.openstack.netvirt.api.NodeCacheManager;
21 import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound;
22 import org.opendaylight.ovsdb.openstack.netvirt.impl.NeutronL3Adapter;
23 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
26 import org.osgi.framework.BundleContext;
27 import org.osgi.framework.ServiceReference;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * Handle requests for Neutron Port.
34 public class PortHandler extends AbstractHandler implements INeutronPortAware, ConfigInterface {
35 static final Logger logger = LoggerFactory.getLogger(PortHandler.class);
37 // The implementation for each of these services is resolved by the OSGi Service Manager
38 private volatile NodeCacheManager nodeCacheManager;
39 private volatile NeutronL3Adapter neutronL3Adapter;
40 private volatile Southbound southbound;
43 * Invoked when a port creation is requested
44 * to indicate if the specified port can be created.
46 * @param port An instance of proposed new Port object.
47 * @return A HTTP status code to the creation request.
50 public int canCreatePort(NeutronPort port) {
51 return HttpURLConnection.HTTP_OK;
55 * Invoked to take action after a port has been created.
57 * @param neutronPort An instance of new Neutron Port object.
60 public void neutronPortCreated(NeutronPort neutronPort) {
61 enqueueEvent(new NorthboundEvent(neutronPort, Action.ADD));
63 private void doNeutronPortCreated(NeutronPort neutronPort) {
64 logger.debug(" Port-ADD successful for tenant-id - {}," +
65 " network-id - {}, port-id - {}",
66 neutronPort.getTenantID(), neutronPort.getNetworkUUID(),
68 neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.ADD);
72 * Invoked when a port update is requested
73 * to indicate if the specified port can be changed
74 * using the specified delta.
76 * @param delta Updates to the port object using patch semantics.
77 * @param original An instance of the Neutron Port object
79 * @return A HTTP status code to the update request.
82 public int canUpdatePort(NeutronPort delta,
83 NeutronPort original) {
84 return HttpURLConnection.HTTP_OK;
88 * Invoked to take action after a port has been updated.
90 * @param neutronPort An instance of modified Neutron Port object.
93 public void neutronPortUpdated(NeutronPort neutronPort) {
94 enqueueEvent(new NorthboundEvent(neutronPort, Action.UPDATE));
96 private void doNeutronPortUpdated(NeutronPort neutronPort) {
97 logger.debug("Handling neutron update port " + neutronPort);
98 neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.UPDATE);
102 * Invoked when a port deletion is requested
103 * to indicate if the specified port can be deleted.
105 * @param port An instance of the Neutron Port object to be deleted.
106 * @return A HTTP status code to the deletion request.
109 public int canDeletePort(NeutronPort port) {
110 return HttpURLConnection.HTTP_OK;
114 * Invoked to take action after a port has been deleted.
116 * @param neutronPort An instance of deleted Neutron Port object.
119 public void neutronPortDeleted(NeutronPort neutronPort) {
120 enqueueEvent(new NorthboundEvent(neutronPort, Action.DELETE));
122 private void doNeutronPortDeleted(NeutronPort neutronPort) {
123 logger.debug("Handling neutron delete port " + neutronPort);
124 neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.DELETE);
125 //TODO: Need to implement getNodes
126 List<Node> nodes = nodeCacheManager.getNodes();
127 for (Node node : nodes) {
129 List<OvsdbTerminationPointAugmentation> ports = southbound.getTerminationPointsOfBridge(node);
130 for (OvsdbTerminationPointAugmentation port : ports) {
131 String neutronPortId =
132 southbound.getInterfaceExternalIdsValue(port, Constants.EXTERNAL_ID_INTERFACE_ID);
133 if (neutronPortId != null && neutronPortId.equalsIgnoreCase(neutronPort.getPortUUID())) {
134 logger.trace("neutronPortDeleted: Delete interface {}", port.getName());
135 southbound.deleteTerminationPoint(node, port.getName());
139 } catch (Exception e) {
140 logger.error("Exception during handlingNeutron port delete", e);
143 logger.debug(" PORT delete successful for tenant-id - {}, " +
144 " network-id - {}, port-id - {}",
145 neutronPort.getTenantID(), neutronPort.getNetworkUUID(),
146 neutronPort.getID());
152 * @param abstractEvent the {@link org.opendaylight.ovsdb.openstack.netvirt.AbstractEvent} event to be handled.
153 * @see org.opendaylight.ovsdb.openstack.netvirt.api.EventDispatcher
156 public void processEvent(AbstractEvent abstractEvent) {
157 if (!(abstractEvent instanceof NorthboundEvent)) {
158 logger.error("Unable to process abstract event " + abstractEvent);
161 NorthboundEvent ev = (NorthboundEvent) abstractEvent;
162 switch (ev.getAction()) {
164 doNeutronPortCreated(ev.getPort());
167 doNeutronPortDeleted(ev.getPort());
170 doNeutronPortUpdated(ev.getPort());
173 logger.warn("Unable to process event action " + ev.getAction());
179 public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
181 (NodeCacheManager) ServiceHelper.getGlobalInstance(NodeCacheManager.class, this);
183 (NeutronL3Adapter) ServiceHelper.getGlobalInstance(NeutronL3Adapter.class, this);
185 (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
187 (EventDispatcher) ServiceHelper.getGlobalInstance(EventDispatcher.class, this);
188 eventDispatcher.eventHandlerAdded(
189 bundleContext.getServiceReference(INeutronPortAware.class.getName()), this);
193 public void setDependencies(Object impl) {}