Remove plugin dependencies
[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.api.MdsalConsumer;
19 import org.opendaylight.ovsdb.openstack.netvirt.impl.NeutronL3Adapter;
20 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
21 import org.opendaylight.ovsdb.schema.openvswitch.Port;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
23
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import java.net.HttpURLConnection;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.concurrent.ConcurrentMap;
31
32 /**
33  * Handle requests for Neutron Port.
34  */
35 public class PortHandler extends AbstractHandler
36                          implements INeutronPortAware {
37
38     /**
39      * Logger instance.
40      */
41     static final Logger logger = LoggerFactory.getLogger(PortHandler.class);
42
43     // The implementation for each of these services is resolved by the OSGi Service Manager
44     /* TODO SB_MIGRATION
45     private volatile OvsdbConfigurationService ovsdbConfigurationService;
46     private volatile OvsdbConnectionService connectionService;*/
47     private volatile MdsalConsumer mdsalConsumer;
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         /* TODO SB_MIGRATION
157         List<Node> nodes = connectionService.getNodes();
158         for (Node node : nodes) {
159             try {
160                 ConcurrentMap<String, Row> portRows =
161                         this.ovsdbConfigurationService.getRows(node,
162                                                         ovsdbConfigurationService.getTableName(node, Port.class));
163                 if (portRows != null) {
164                     for (Row portRow : portRows.values()) {
165                         Port port = ovsdbConfigurationService.getTypedRow(node, Port.class, portRow);
166                         for (UUID interfaceUuid : port.getInterfacesColumn().getData()) {
167                             Row ifaceRow = ovsdbConfigurationService
168                                     .getRow(node,
169                                             ovsdbConfigurationService.getTableName(node, Interface.class),
170                                             interfaceUuid.toString());
171                             Interface iface = ovsdbConfigurationService.getTypedRow(node, Interface.class, ifaceRow);
172                             Map<String, String> externalIds = iface.getExternalIdsColumn().getData();
173
174                             if (externalIds == null) {
175                                 logger.trace("No external_ids seen in {}", iface.getName());
176                                 continue;
177                             }
178
179                             *//* Compare Neutron port uuid *//*
180                             String neutronPortId = externalIds.get(Constants.EXTERNAL_ID_INTERFACE_ID);
181                             if (neutronPortId == null) {
182                                 continue;
183                             }
184
185                             if (neutronPortId.equalsIgnoreCase(neutronPort.getPortUUID())) {
186                                 logger.trace("neutronPortDeleted: Delete interface {}", iface.getName());
187                                 ovsdbConfigurationService.deleteRow(node,
188                                                              ovsdbConfigurationService.getTableName(node, Port.class),
189                                                              port.getUuid().toString());
190                                 break;
191                             }
192                         }
193                     }
194                 }
195             } catch (Exception e) {
196                 logger.error("Exception during handlingNeutron port delete", e);
197             }
198         }
199         logger.debug(" PORT delete successful for tenant-id - {}, " +
200                      " network-id - {}, port-id - {}",
201                      neutronPort.getTenantID(), neutronPort.getNetworkUUID(),
202                      neutronPort.getID());
203 */
204     }
205
206     /**
207      * Process the event.
208      *
209      * @param abstractEvent the {@link org.opendaylight.ovsdb.openstack.netvirt.AbstractEvent} event to be handled.
210      * @see EventDispatcher
211      */
212     @Override
213     public void processEvent(AbstractEvent abstractEvent) {
214         if (!(abstractEvent instanceof NorthboundEvent)) {
215             logger.error("Unable to process abstract event " + abstractEvent);
216             return;
217         }
218         NorthboundEvent ev = (NorthboundEvent) abstractEvent;
219         switch (ev.getAction()) {
220             case ADD:
221                 doNeutronPortCreated(ev.getPort());
222                 break;
223             case DELETE:
224                 doNeutronPortDeleted(ev.getPort());
225                 break;
226             case UPDATE:
227                 doNeutronPortUpdated(ev.getPort());
228                 break;
229             default:
230                 logger.warn("Unable to process event action " + ev.getAction());
231                 break;
232         }
233     }
234 }