2 * Copyright (c) 2013, 2016 Red Hat, Inc. and others. All rights reserved.
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
9 package org.opendaylight.ovsdb.openstack.netvirt;
11 import java.net.HttpURLConnection;
12 import java.util.List;
14 import org.opendaylight.ovsdb.openstack.netvirt.translator.NeutronPort;
15 import org.opendaylight.ovsdb.openstack.netvirt.translator.iaware.INeutronPortAware;
16 import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
17 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
18 import org.opendaylight.ovsdb.openstack.netvirt.api.EventDispatcher;
19 import org.opendaylight.ovsdb.openstack.netvirt.api.NodeCacheManager;
20 import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound;
21 import org.opendaylight.ovsdb.openstack.netvirt.impl.DistributedArpService;
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;
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 DistributedArpService distributedArpService;
41 private volatile Southbound southbound;
44 * Invoked when a port creation is requested
45 * to indicate if the specified port can be created.
47 * @param port An instance of proposed new Port object.
48 * @return A HTTP status code to the creation request.
51 public int canCreatePort(NeutronPort port) {
52 return HttpURLConnection.HTTP_OK;
56 * Invoked to take action after a port has been created.
58 * @param neutronPort An instance of new Neutron Port object.
61 public void neutronPortCreated(NeutronPort neutronPort) {
62 enqueueEvent(new NorthboundEvent(neutronPort, Action.ADD));
64 private void doNeutronPortCreated(NeutronPort neutronPort) {
65 LOG.debug(" Port-ADD successful for tenant-id - {}, network-id - {}, port-id - {}",
66 neutronPort.getTenantID(), neutronPort.getNetworkUUID(),
68 distributedArpService.handlePortEvent(neutronPort, Action.ADD);
69 neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.ADD);
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 return HttpURLConnection.HTTP_OK;
89 * Invoked to take action after a port has been updated.
91 * @param neutronPort An instance of modified Neutron Port object.
94 public void neutronPortUpdated(NeutronPort neutronPort) {
95 enqueueEvent(new NorthboundEvent(neutronPort, Action.UPDATE));
97 private void doNeutronPortUpdated(NeutronPort neutronPort) {
98 LOG.debug("Handling neutron update port {}", neutronPort);
99 distributedArpService.handlePortEvent(neutronPort, Action.UPDATE);
100 neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.UPDATE);
104 * Invoked when a port deletion is requested
105 * to indicate if the specified port can be deleted.
107 * @param port An instance of the Neutron Port object to be deleted.
108 * @return A HTTP status code to the deletion request.
111 public int canDeletePort(NeutronPort port) {
112 return HttpURLConnection.HTTP_OK;
116 * Invoked to take action after a port has been deleted.
118 * @param neutronPort An instance of deleted Neutron Port object.
121 public void neutronPortDeleted(NeutronPort neutronPort) {
122 enqueueEvent(new NorthboundEvent(neutronPort, Action.DELETE));
124 private void doNeutronPortDeleted(NeutronPort neutronPort) {
125 LOG.debug("Handling neutron delete port {}", neutronPort);
126 distributedArpService.handlePortEvent(neutronPort, Action.DELETE);
127 neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.DELETE);
128 //TODO: Need to implement getNodes
129 List<Node> nodes = nodeCacheManager.getNodes();
130 for (Node node : nodes) {
132 List<OvsdbTerminationPointAugmentation> ports = southbound.getTerminationPointsOfBridge(node);
133 for (OvsdbTerminationPointAugmentation port : ports) {
134 String neutronPortId =
135 southbound.getInterfaceExternalIdsValue(port, Constants.EXTERNAL_ID_INTERFACE_ID);
136 if (neutronPortId != null && neutronPortId.equalsIgnoreCase(neutronPort.getPortUUID())) {
137 LOG.trace("neutronPortDeleted: Delete interface {}", port.getName());
138 southbound.deleteTerminationPoint(node, port.getName());
142 } catch (Exception e) {
143 LOG.error("Exception during handlingNeutron port delete", e);
146 LOG.debug(" PORT delete successful for tenant-id - {}, network-id - {}, port-id - {}",
147 neutronPort.getTenantID(), neutronPort.getNetworkUUID(),
148 neutronPort.getID());
154 * @param abstractEvent the {@link org.opendaylight.ovsdb.openstack.netvirt.AbstractEvent} event to be handled.
155 * @see org.opendaylight.ovsdb.openstack.netvirt.api.EventDispatcher
158 public void processEvent(AbstractEvent abstractEvent) {
159 if (!(abstractEvent instanceof NorthboundEvent)) {
160 LOG.error("Unable to process abstract event {}", abstractEvent);
163 NorthboundEvent ev = (NorthboundEvent) abstractEvent;
164 switch (ev.getAction()) {
166 doNeutronPortCreated(ev.getPort());
169 doNeutronPortDeleted(ev.getPort());
172 doNeutronPortUpdated(ev.getPort());
175 LOG.warn("Unable to process event action {}", ev.getAction());
181 public void setDependencies(ServiceReference serviceReference) {
183 (NodeCacheManager) ServiceHelper.getGlobalInstance(NodeCacheManager.class, this);
185 (NeutronL3Adapter) ServiceHelper.getGlobalInstance(NeutronL3Adapter.class, this);
186 distributedArpService =
187 (DistributedArpService) ServiceHelper.getGlobalInstance(DistributedArpService.class, this);
189 (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
191 (EventDispatcher) ServiceHelper.getGlobalInstance(EventDispatcher.class, this);
192 eventDispatcher.eventHandlerAdded(serviceReference, this);
196 public void setDependencies(Object impl) {}