Remove unused service interfaces
[ovsdb.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 org.opendaylight.neutron.spi.INeutronPortAware;
13 import org.opendaylight.neutron.spi.NeutronPort;
14 import org.opendaylight.ovsdb.lib.notation.Row;
15 import org.opendaylight.ovsdb.lib.notation.UUID;
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.impl.NeutronL3Adapter;
19 import org.opendaylight.ovsdb.plugin.api.OvsdbConfigurationService;
20 import org.opendaylight.ovsdb.plugin.api.OvsdbConnectionService;
21 import org.opendaylight.ovsdb.plugin.api.OvsdbInventoryListener;
22 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
23 import org.opendaylight.ovsdb.schema.openvswitch.Port;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
25
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 import java.net.HttpURLConnection;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.concurrent.ConcurrentMap;
33
34 /**
35  * Handle requests for Neutron Port.
36  */
37 public class PortHandler extends AbstractHandler
38                          implements INeutronPortAware {
39
40     /**
41      * Logger instance.
42      */
43     static final Logger logger = LoggerFactory.getLogger(PortHandler.class);
44
45     // The implementation for each of these services is resolved by the OSGi Service Manager
46     private volatile OvsdbConfigurationService ovsdbConfigurationService;
47     private volatile OvsdbConnectionService connectionService;
48     private volatile NeutronL3Adapter neutronL3Adapter;
49
50     /**
51      * Invoked when a port creation is requested
52      * to indicate if the specified port can be created.
53      *
54      * @param port     An instance of proposed new Port object.
55      * @return A HTTP status code to the creation request.
56      */
57     @Override
58     public int canCreatePort(NeutronPort port) {
59         return HttpURLConnection.HTTP_CREATED;
60     }
61
62     /**
63      * Invoked to take action after a port has been created.
64      *
65      * @param port An instance of new Neutron Port object.
66      */
67     @Override
68     public void neutronPortCreated(NeutronPort neutronPort) {
69         int result = canCreatePort(neutronPort);
70         if (result != HttpURLConnection.HTTP_CREATED) {
71             logger.error(" Port create validation failed result - {} ", result);
72             return;
73         }
74
75         enqueueEvent(new NorthboundEvent(neutronPort, Action.ADD));
76     }
77     private void doNeutronPortCreated(NeutronPort neutronPort) {
78         logger.debug(" Port-ADD successful for tenant-id - {}," +
79                      " network-id - {}, port-id - {}",
80                      neutronPort.getTenantID(), neutronPort.getNetworkUUID(),
81                      neutronPort.getID());
82         neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.ADD);
83     }
84
85     /**
86      * Invoked when a port update is requested
87      * to indicate if the specified port can be changed
88      * using the specified delta.
89      *
90      * @param delta    Updates to the port object using patch semantics.
91      * @param original An instance of the Neutron Port object
92      *                  to be updated.
93      * @return A HTTP status code to the update request.
94      */
95     @Override
96     public int canUpdatePort(NeutronPort delta,
97                              NeutronPort original) {
98         int result = HttpURLConnection.HTTP_OK;
99         /**
100          * To basic validation of the request
101          */
102
103         if ((original == null) || (delta == null)) {
104             logger.error("port object not specified");
105             return HttpURLConnection.HTTP_BAD_REQUEST;
106         }
107         return result;
108     }
109
110     /**
111      * Invoked to take action after a port has been updated.
112      *
113      * @param port An instance of modified Neutron Port object.
114      */
115     @Override
116     public void neutronPortUpdated(NeutronPort neutronPort) {
117         enqueueEvent(new NorthboundEvent(neutronPort, Action.UPDATE));
118     }
119     private void doNeutronPortUpdated(NeutronPort neutronPort) {
120         logger.debug("Handling neutron update port " + neutronPort);
121         neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.UPDATE);
122     }
123
124     /**
125      * Invoked when a port deletion is requested
126      * to indicate if the specified port can be deleted.
127      *
128      * @param port     An instance of the Neutron Port object to be deleted.
129      * @return A HTTP status code to the deletion request.
130      */
131     @Override
132     public int canDeletePort(NeutronPort port) {
133         return HttpURLConnection.HTTP_OK;
134     }
135
136     /**
137      * Invoked to take action after a port has been deleted.
138      *
139      * @param neutronPort  An instance of deleted Neutron Port object.
140      */
141     @Override
142     public void neutronPortDeleted(NeutronPort neutronPort) {
143
144         int result = canDeletePort(neutronPort);
145         if  (result != HttpURLConnection.HTTP_OK) {
146             logger.error(" deletePort validation failed - result {} ", result);
147             return;
148         }
149
150         enqueueEvent(new NorthboundEvent(neutronPort, Action.DELETE));
151     }
152     private void doNeutronPortDeleted(NeutronPort neutronPort) {
153         logger.debug("Handling neutron delete port " + neutronPort);
154         neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.DELETE);
155
156         List<Node> nodes = connectionService.getNodes();
157         for (Node node : nodes) {
158             try {
159                 ConcurrentMap<String, Row> portRows =
160                         this.ovsdbConfigurationService.getRows(node,
161                                                         ovsdbConfigurationService.getTableName(node, Port.class));
162                 if (portRows != null) {
163                     for (Row portRow : portRows.values()) {
164                         Port port = ovsdbConfigurationService.getTypedRow(node, Port.class, portRow);
165                         for (UUID interfaceUuid : port.getInterfacesColumn().getData()) {
166                             Row ifaceRow = ovsdbConfigurationService
167                                     .getRow(node,
168                                             ovsdbConfigurationService.getTableName(node, Interface.class),
169                                             interfaceUuid.toString());
170                             Interface iface = ovsdbConfigurationService.getTypedRow(node, Interface.class, ifaceRow);
171                             Map<String, String> externalIds = iface.getExternalIdsColumn().getData();
172
173                             if (externalIds == null) {
174                                 logger.trace("No external_ids seen in {}", iface.getName());
175                                 continue;
176                             }
177
178                             /* Compare Neutron port uuid */
179                             String neutronPortId = externalIds.get(Constants.EXTERNAL_ID_INTERFACE_ID);
180                             if (neutronPortId == null) {
181                                 continue;
182                             }
183
184                             if (neutronPortId.equalsIgnoreCase(neutronPort.getPortUUID())) {
185                                 logger.trace("neutronPortDeleted: Delete interface {}", iface.getName());
186                                 ovsdbConfigurationService.deleteRow(node,
187                                                              ovsdbConfigurationService.getTableName(node, Port.class),
188                                                              port.getUuid().toString());
189                                 break;
190                             }
191                         }
192                     }
193                 }
194             } catch (Exception e) {
195                 logger.error("Exception during handlingNeutron port delete", e);
196             }
197         }
198         logger.debug(" PORT delete successful for tenant-id - {}, " +
199                      " network-id - {}, port-id - {}",
200                      neutronPort.getTenantID(), neutronPort.getNetworkUUID(),
201                      neutronPort.getID());
202
203     }
204
205     /**
206      * Process the event.
207      *
208      * @param abstractEvent the {@link org.opendaylight.ovsdb.openstack.netvirt.AbstractEvent} event to be handled.
209      * @see EventDispatcher
210      */
211     @Override
212     public void processEvent(AbstractEvent abstractEvent) {
213         if (!(abstractEvent instanceof NorthboundEvent)) {
214             logger.error("Unable to process abstract event " + abstractEvent);
215             return;
216         }
217         NorthboundEvent ev = (NorthboundEvent) abstractEvent;
218         switch (ev.getAction()) {
219             case ADD:
220                 doNeutronPortCreated(ev.getPort());
221                 break;
222             case DELETE:
223                 doNeutronPortDeleted(ev.getPort());
224                 break;
225             case UPDATE:
226                 doNeutronPortUpdated(ev.getPort());
227                 break;
228             default:
229                 logger.warn("Unable to process event action " + ev.getAction());
230                 break;
231         }
232     }
233 }