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 private static final Logger LOG = 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 LOG.debug(" Port-ADD successful for tenant-id - {}, network-id - {}, port-id - {}",
65 neutronPort.getTenantID(), neutronPort.getNetworkUUID(),
67 neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.ADD);
71 * Invoked when a port update is requested
72 * to indicate if the specified port can be changed
73 * using the specified delta.
75 * @param delta Updates to the port object using patch semantics.
76 * @param original An instance of the Neutron Port object
78 * @return A HTTP status code to the update request.
81 public int canUpdatePort(NeutronPort delta,
82 NeutronPort original) {
83 return HttpURLConnection.HTTP_OK;
87 * Invoked to take action after a port has been updated.
89 * @param neutronPort An instance of modified Neutron Port object.
92 public void neutronPortUpdated(NeutronPort neutronPort) {
93 enqueueEvent(new NorthboundEvent(neutronPort, Action.UPDATE));
95 private void doNeutronPortUpdated(NeutronPort neutronPort) {
96 LOG.debug("Handling neutron update port {}", neutronPort);
97 neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.UPDATE);
101 * Invoked when a port deletion is requested
102 * to indicate if the specified port can be deleted.
104 * @param port An instance of the Neutron Port object to be deleted.
105 * @return A HTTP status code to the deletion request.
108 public int canDeletePort(NeutronPort port) {
109 return HttpURLConnection.HTTP_OK;
113 * Invoked to take action after a port has been deleted.
115 * @param neutronPort An instance of deleted Neutron Port object.
118 public void neutronPortDeleted(NeutronPort neutronPort) {
119 enqueueEvent(new NorthboundEvent(neutronPort, Action.DELETE));
121 private void doNeutronPortDeleted(NeutronPort neutronPort) {
122 LOG.debug("Handling neutron delete port {}", neutronPort);
123 neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.DELETE);
124 //TODO: Need to implement getNodes
125 List<Node> nodes = nodeCacheManager.getNodes();
126 for (Node node : nodes) {
128 List<OvsdbTerminationPointAugmentation> ports = southbound.getTerminationPointsOfBridge(node);
129 for (OvsdbTerminationPointAugmentation port : ports) {
130 String neutronPortId =
131 southbound.getInterfaceExternalIdsValue(port, Constants.EXTERNAL_ID_INTERFACE_ID);
132 if (neutronPortId != null && neutronPortId.equalsIgnoreCase(neutronPort.getPortUUID())) {
133 LOG.trace("neutronPortDeleted: Delete interface {}", port.getName());
134 southbound.deleteTerminationPoint(node, port.getName());
138 } catch (Exception e) {
139 LOG.error("Exception during handlingNeutron port delete", e);
142 LOG.debug(" PORT delete successful for tenant-id - {}, network-id - {}, port-id - {}",
143 neutronPort.getTenantID(), neutronPort.getNetworkUUID(),
144 neutronPort.getID());
150 * @param abstractEvent the {@link org.opendaylight.ovsdb.openstack.netvirt.AbstractEvent} event to be handled.
151 * @see org.opendaylight.ovsdb.openstack.netvirt.api.EventDispatcher
154 public void processEvent(AbstractEvent abstractEvent) {
155 if (!(abstractEvent instanceof NorthboundEvent)) {
156 LOG.error("Unable to process abstract event {}", abstractEvent);
159 NorthboundEvent ev = (NorthboundEvent) abstractEvent;
160 switch (ev.getAction()) {
162 doNeutronPortCreated(ev.getPort());
165 doNeutronPortDeleted(ev.getPort());
168 doNeutronPortUpdated(ev.getPort());
171 LOG.warn("Unable to process event action {}", ev.getAction());
177 public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
179 (NodeCacheManager) ServiceHelper.getGlobalInstance(NodeCacheManager.class, this);
181 (NeutronL3Adapter) ServiceHelper.getGlobalInstance(NeutronL3Adapter.class, this);
183 (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
185 (EventDispatcher) ServiceHelper.getGlobalInstance(EventDispatcher.class, this);
186 eventDispatcher.eventHandlerAdded(
187 bundleContext.getServiceReference(INeutronPortAware.class.getName()), this);
191 public void setDependencies(Object impl) {}