Merge "Adding Pax-Exam infra with a basic IT for plugin"
[netvirt.git] / neutron / src / main / java / org / opendaylight / ovsdb / neutron / 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.neutron;
11
12 import java.net.HttpURLConnection;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.concurrent.ConcurrentMap;
16
17 import org.opendaylight.controller.networkconfig.neutron.INeutronNetworkCRUD;
18 import org.opendaylight.controller.networkconfig.neutron.INeutronPortAware;
19 import org.opendaylight.controller.networkconfig.neutron.NeutronNetwork;
20 import org.opendaylight.controller.networkconfig.neutron.NeutronPort;
21 import org.opendaylight.controller.sal.core.Node;
22 import org.opendaylight.controller.sal.utils.ServiceHelper;
23 import org.opendaylight.ovsdb.lib.table.Interface;
24 import org.opendaylight.ovsdb.lib.table.internal.Table;
25 import org.opendaylight.ovsdb.plugin.IConnectionServiceInternal;
26 import org.opendaylight.ovsdb.plugin.OVSDBInventoryListener;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Handle requests for Neutron Port.
32  */
33 public class PortHandler extends BaseHandler
34                          implements INeutronPortAware {
35
36     /**
37      * Logger instance.
38      */
39     static final Logger logger = LoggerFactory.getLogger(PortHandler.class);
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_CREATED;
51     }
52
53     /**
54      * Invoked to take action after a port has been created.
55      *
56      * @param port An instance of new Neutron Port object.
57      */
58     @Override
59     public void neutronPortCreated(NeutronPort port) {
60         int result = canCreatePort(port);
61         if (result != HttpURLConnection.HTTP_CREATED) {
62             logger.error(" Port create validation failed result - {} ", result);
63             return;
64         }
65
66         logger.debug(" Port-ADD successful for tenant-id - {}," +
67                      " network-id - {}, port-id - {}, result - {} ",
68                      port.getTenantID(), port.getNetworkUUID(),
69                      port.getID(), result);
70     }
71
72     /**
73      * Invoked when a port update is requested
74      * to indicate if the specified port can be changed
75      * using the specified delta.
76      *
77      * @param delta    Updates to the port object using patch semantics.
78      * @param original An instance of the Neutron Port object
79      *                  to be updated.
80      * @return A HTTP status code to the update request.
81      */
82     @Override
83     public int canUpdatePort(NeutronPort delta,
84                              NeutronPort original) {
85         int result = HttpURLConnection.HTTP_OK;
86         /**
87          * To basic validation of the request
88          */
89
90         if ((original == null) || (delta == null)) {
91             logger.error("port object not specified");
92             return HttpURLConnection.HTTP_BAD_REQUEST;
93         }
94         return result;
95     }
96
97     /**
98      * Invoked to take action after a port has been updated.
99      *
100      * @param port An instance of modified Neutron Port object.
101      */
102     @Override
103     public void neutronPortUpdated(NeutronPort port) {
104     }
105
106     /**
107      * Invoked when a port deletion is requested
108      * to indicate if the specified port can be deleted.
109      *
110      * @param port     An instance of the Neutron Port object to be deleted.
111      * @return A HTTP status code to the deletion request.
112      */
113     @Override
114     public int canDeletePort(NeutronPort port) {
115         return HttpURLConnection.HTTP_OK;
116     }
117
118     /**
119      * Invoked to take action after a port has been deleted.
120      *
121      * @param port  An instance of deleted Neutron Port object.
122      */
123     @Override
124     public void neutronPortDeleted(NeutronPort port) {
125
126         int result = canDeletePort(port);
127         if  (result != HttpURLConnection.HTTP_OK) {
128             logger.error(" deletePort validation failed - result {} ", result);
129             return;
130         }
131
132         IConnectionServiceInternal connectionService = (IConnectionServiceInternal)ServiceHelper.getGlobalInstance(IConnectionServiceInternal.class, this);
133         INeutronNetworkCRUD neutronNetworkService = (INeutronNetworkCRUD)ServiceHelper.getGlobalInstance(INeutronNetworkCRUD.class, this);
134         NeutronNetwork neutronNetwork = neutronNetworkService.getNetwork(port.getNetworkUUID());
135         OVSDBInventoryListener inventoryListener = (OVSDBInventoryListener)ServiceHelper.getGlobalInstance(OVSDBInventoryListener.class, this);
136         List<Node> nodes = connectionService.getNodes();
137         for (Node node : nodes) {
138             try {
139                 ConcurrentMap<String, Table<?>> interfaces = this.ovsdbConfigService.getRows(node, Interface.NAME.getName());
140                 if (interfaces != null) {
141                     for (String intfUUID : interfaces.keySet()) {
142                         Interface intf = (Interface) interfaces.get(intfUUID);
143                         Map<String, String> externalIds = intf.getExternal_ids();
144                         if (externalIds == null) {
145                             logger.trace("No external_ids seen in {}", intf);
146                             continue;
147                         }
148                         /* Compare Neutron port uuid */
149                         String neutronPortId = externalIds.get(TenantNetworkManager.EXTERNAL_ID_INTERFACE_ID);
150                         if (neutronPortId == null) {
151                             continue;
152                         }
153                         if (neutronPortId.equalsIgnoreCase(port.getPortUUID())) {
154                             logger.trace("neutronPortDeleted: Delete interface {}", intf.getName());
155                             inventoryListener.rowRemoved(node, Interface.NAME.getName(), intfUUID,
156                                                          intf, neutronNetwork);
157                             break;
158                         }
159                     }
160                 }
161             } catch (Exception e) {
162                 logger.error("Exception during handlingNeutron network delete");
163             }
164         }
165         logger.debug(" PORT delete successful for tenant-id - {}, " +
166                      " network-id - {}, port-id - {}",
167                      port.getTenantID(), port.getNetworkUUID(),
168                      port.getID());
169
170     }
171 }