Use the memorized service reference
[ovsdb.git] / openstack / net-virt / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / PortHandler.java
1 /*
2  * Copyright (c) 2013, 2015 Red Hat, Inc. and others. All rights reserved.
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
9 package org.opendaylight.ovsdb.openstack.netvirt;
10
11 import java.net.HttpURLConnection;
12 import java.util.List;
13
14 import org.opendaylight.neutron.spi.INeutronPortAware;
15 import org.opendaylight.neutron.spi.NeutronPort;
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.NeutronL3Adapter;
22 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
25
26 import org.osgi.framework.ServiceReference;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Handle requests for Neutron Port.
32  */
33 public class PortHandler extends AbstractHandler implements INeutronPortAware, ConfigInterface {
34     private static final Logger LOG = LoggerFactory.getLogger(PortHandler.class);
35
36     // The implementation for each of these services is resolved by the OSGi Service Manager
37     private volatile NodeCacheManager nodeCacheManager;
38     private volatile NeutronL3Adapter neutronL3Adapter;
39     private volatile Southbound southbound;
40
41     /**
42      * Invoked when a port creation is requested
43      * to indicate if the specified port can be created.
44      *
45      * @param port     An instance of proposed new Port object.
46      * @return A HTTP status code to the creation request.
47      */
48     @Override
49     public int canCreatePort(NeutronPort port) {
50         return HttpURLConnection.HTTP_OK;
51     }
52
53     /**
54      * Invoked to take action after a port has been created.
55      *
56      * @param neutronPort An instance of new Neutron Port object.
57      */
58     @Override
59     public void neutronPortCreated(NeutronPort neutronPort) {
60         enqueueEvent(new NorthboundEvent(neutronPort, Action.ADD));
61     }
62     private void doNeutronPortCreated(NeutronPort neutronPort) {
63         LOG.debug(" Port-ADD successful for tenant-id - {}, network-id - {}, port-id - {}",
64                      neutronPort.getTenantID(), neutronPort.getNetworkUUID(),
65                      neutronPort.getID());
66         neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.ADD);
67     }
68
69     /**
70      * Invoked when a port update is requested
71      * to indicate if the specified port can be changed
72      * using the specified delta.
73      *
74      * @param delta    Updates to the port object using patch semantics.
75      * @param original An instance of the Neutron Port object
76      *                  to be updated.
77      * @return A HTTP status code to the update request.
78      */
79     @Override
80     public int canUpdatePort(NeutronPort delta,
81                              NeutronPort original) {
82         return HttpURLConnection.HTTP_OK;
83     }
84
85     /**
86      * Invoked to take action after a port has been updated.
87      *
88      * @param neutronPort An instance of modified Neutron Port object.
89      */
90     @Override
91     public void neutronPortUpdated(NeutronPort neutronPort) {
92         enqueueEvent(new NorthboundEvent(neutronPort, Action.UPDATE));
93     }
94     private void doNeutronPortUpdated(NeutronPort neutronPort) {
95         LOG.debug("Handling neutron update port {}", neutronPort);
96         neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.UPDATE);
97     }
98
99     /**
100      * Invoked when a port deletion is requested
101      * to indicate if the specified port can be deleted.
102      *
103      * @param port     An instance of the Neutron Port object to be deleted.
104      * @return A HTTP status code to the deletion request.
105      */
106     @Override
107     public int canDeletePort(NeutronPort port) {
108         return HttpURLConnection.HTTP_OK;
109     }
110
111     /**
112      * Invoked to take action after a port has been deleted.
113      *
114      * @param neutronPort  An instance of deleted Neutron Port object.
115      */
116     @Override
117     public void neutronPortDeleted(NeutronPort neutronPort) {
118         enqueueEvent(new NorthboundEvent(neutronPort, Action.DELETE));
119     }
120     private void doNeutronPortDeleted(NeutronPort neutronPort) {
121         LOG.debug("Handling neutron delete port {}", neutronPort);
122         neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.DELETE);
123         //TODO: Need to implement getNodes
124         List<Node> nodes = nodeCacheManager.getNodes();
125         for (Node node : nodes) {
126             try {
127                 List<OvsdbTerminationPointAugmentation> ports = southbound.getTerminationPointsOfBridge(node);
128                 for (OvsdbTerminationPointAugmentation port : ports) {
129                     String neutronPortId =
130                             southbound.getInterfaceExternalIdsValue(port, Constants.EXTERNAL_ID_INTERFACE_ID);
131                     if (neutronPortId != null && neutronPortId.equalsIgnoreCase(neutronPort.getPortUUID())) {
132                         LOG.trace("neutronPortDeleted: Delete interface {}", port.getName());
133                         southbound.deleteTerminationPoint(node, port.getName());
134                         break;
135                     }
136                 }
137             } catch (Exception e) {
138                 LOG.error("Exception during handlingNeutron port delete", e);
139             }
140         }
141         LOG.debug(" PORT delete successful for tenant-id - {}, network-id - {}, port-id - {}",
142                      neutronPort.getTenantID(), neutronPort.getNetworkUUID(),
143                      neutronPort.getID());
144     }
145
146     /**
147      * Process the event.
148      *
149      * @param abstractEvent the {@link org.opendaylight.ovsdb.openstack.netvirt.AbstractEvent} event to be handled.
150      * @see org.opendaylight.ovsdb.openstack.netvirt.api.EventDispatcher
151      */
152     @Override
153     public void processEvent(AbstractEvent abstractEvent) {
154         if (!(abstractEvent instanceof NorthboundEvent)) {
155             LOG.error("Unable to process abstract event {}", abstractEvent);
156             return;
157         }
158         NorthboundEvent ev = (NorthboundEvent) abstractEvent;
159         switch (ev.getAction()) {
160             case ADD:
161                 doNeutronPortCreated(ev.getPort());
162                 break;
163             case DELETE:
164                 doNeutronPortDeleted(ev.getPort());
165                 break;
166             case UPDATE:
167                 doNeutronPortUpdated(ev.getPort());
168                 break;
169             default:
170                 LOG.warn("Unable to process event action {}", ev.getAction());
171                 break;
172         }
173     }
174
175     @Override
176     public void setDependencies(ServiceReference serviceReference) {
177         nodeCacheManager =
178                 (NodeCacheManager) ServiceHelper.getGlobalInstance(NodeCacheManager.class, this);
179         neutronL3Adapter =
180                 (NeutronL3Adapter) ServiceHelper.getGlobalInstance(NeutronL3Adapter.class, this);
181         southbound =
182                 (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
183         eventDispatcher =
184                 (EventDispatcher) ServiceHelper.getGlobalInstance(EventDispatcher.class, this);
185         eventDispatcher.eventHandlerAdded(serviceReference, this);
186     }
187
188     @Override
189     public void setDependencies(Object impl) {}
190 }