cb9b9cfe9cb2f8d764099610c494c31887ed03c6
[netvirt.git] / openstack / net-virt / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / PortHandler.java
1 /*
2  * Copyright (C) 2013 Red Hat, Inc.
3  *
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
7  *
8  * Authors : Madhu Venugopal, Brent Salisbury
9  */
10 package org.opendaylight.ovsdb.openstack.netvirt;
11
12 import java.net.HttpURLConnection;
13 import java.util.List;
14
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;
30
31 /**
32  * Handle requests for Neutron Port.
33  */
34 public class PortHandler extends AbstractHandler implements INeutronPortAware, ConfigInterface {
35     private static final Logger LOG = LoggerFactory.getLogger(PortHandler.class);
36
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;
41
42     /**
43      * Invoked when a port creation is requested
44      * to indicate if the specified port can be created.
45      *
46      * @param port     An instance of proposed new Port object.
47      * @return A HTTP status code to the creation request.
48      */
49     @Override
50     public int canCreatePort(NeutronPort port) {
51         return HttpURLConnection.HTTP_OK;
52     }
53
54     /**
55      * Invoked to take action after a port has been created.
56      *
57      * @param neutronPort An instance of new Neutron Port object.
58      */
59     @Override
60     public void neutronPortCreated(NeutronPort neutronPort) {
61         enqueueEvent(new NorthboundEvent(neutronPort, Action.ADD));
62     }
63     private void doNeutronPortCreated(NeutronPort neutronPort) {
64         LOG.debug(" Port-ADD successful for tenant-id - {}, network-id - {}, port-id - {}",
65                      neutronPort.getTenantID(), neutronPort.getNetworkUUID(),
66                      neutronPort.getID());
67         neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.ADD);
68     }
69
70     /**
71      * Invoked when a port update is requested
72      * to indicate if the specified port can be changed
73      * using the specified delta.
74      *
75      * @param delta    Updates to the port object using patch semantics.
76      * @param original An instance of the Neutron Port object
77      *                  to be updated.
78      * @return A HTTP status code to the update request.
79      */
80     @Override
81     public int canUpdatePort(NeutronPort delta,
82                              NeutronPort original) {
83         return HttpURLConnection.HTTP_OK;
84     }
85
86     /**
87      * Invoked to take action after a port has been updated.
88      *
89      * @param neutronPort An instance of modified Neutron Port object.
90      */
91     @Override
92     public void neutronPortUpdated(NeutronPort neutronPort) {
93         enqueueEvent(new NorthboundEvent(neutronPort, Action.UPDATE));
94     }
95     private void doNeutronPortUpdated(NeutronPort neutronPort) {
96         LOG.debug("Handling neutron update port {}", neutronPort);
97         neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.UPDATE);
98     }
99
100     /**
101      * Invoked when a port deletion is requested
102      * to indicate if the specified port can be deleted.
103      *
104      * @param port     An instance of the Neutron Port object to be deleted.
105      * @return A HTTP status code to the deletion request.
106      */
107     @Override
108     public int canDeletePort(NeutronPort port) {
109         return HttpURLConnection.HTTP_OK;
110     }
111
112     /**
113      * Invoked to take action after a port has been deleted.
114      *
115      * @param neutronPort  An instance of deleted Neutron Port object.
116      */
117     @Override
118     public void neutronPortDeleted(NeutronPort neutronPort) {
119         enqueueEvent(new NorthboundEvent(neutronPort, Action.DELETE));
120     }
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) {
127             try {
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());
135                         break;
136                     }
137                 }
138             } catch (Exception e) {
139                 LOG.error("Exception during handlingNeutron port delete", e);
140             }
141         }
142         LOG.debug(" PORT delete successful for tenant-id - {}, network-id - {}, port-id - {}",
143                      neutronPort.getTenantID(), neutronPort.getNetworkUUID(),
144                      neutronPort.getID());
145     }
146
147     /**
148      * Process the event.
149      *
150      * @param abstractEvent the {@link org.opendaylight.ovsdb.openstack.netvirt.AbstractEvent} event to be handled.
151      * @see org.opendaylight.ovsdb.openstack.netvirt.api.EventDispatcher
152      */
153     @Override
154     public void processEvent(AbstractEvent abstractEvent) {
155         if (!(abstractEvent instanceof NorthboundEvent)) {
156             LOG.error("Unable to process abstract event {}", abstractEvent);
157             return;
158         }
159         NorthboundEvent ev = (NorthboundEvent) abstractEvent;
160         switch (ev.getAction()) {
161             case ADD:
162                 doNeutronPortCreated(ev.getPort());
163                 break;
164             case DELETE:
165                 doNeutronPortDeleted(ev.getPort());
166                 break;
167             case UPDATE:
168                 doNeutronPortUpdated(ev.getPort());
169                 break;
170             default:
171                 LOG.warn("Unable to process event action {}", ev.getAction());
172                 break;
173         }
174     }
175
176     @Override
177     public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
178         nodeCacheManager =
179                 (NodeCacheManager) ServiceHelper.getGlobalInstance(NodeCacheManager.class, this);
180         neutronL3Adapter =
181                 (NeutronL3Adapter) ServiceHelper.getGlobalInstance(NeutronL3Adapter.class, this);
182         southbound =
183                 (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
184         eventDispatcher =
185                 (EventDispatcher) ServiceHelper.getGlobalInstance(EventDispatcher.class, this);
186         eventDispatcher.eventHandlerAdded(
187                 bundleContext.getServiceReference(INeutronPortAware.class.getName()), this);
188     }
189
190     @Override
191     public void setDependencies(Object impl) {}
192 }