81da6503d76daffde580d96fbbe20b576adf6011
[netvirt.git] / openstack / net-virt / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / NetworkHandler.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, Hsin-Yi Shen
9  */
10 package org.opendaylight.ovsdb.openstack.netvirt;
11
12 import org.opendaylight.controller.networkconfig.neutron.INeutronNetworkAware;
13 import org.opendaylight.controller.networkconfig.neutron.INeutronNetworkCRUD;
14 import org.opendaylight.controller.networkconfig.neutron.NeutronNetwork;
15 import org.opendaylight.controller.sal.core.Node;
16 import org.opendaylight.ovsdb.lib.notation.Row;
17 import org.opendaylight.ovsdb.openstack.netvirt.api.BridgeConfigurationManager;
18 import org.opendaylight.ovsdb.openstack.netvirt.api.ConfigurationService;
19 import org.opendaylight.ovsdb.openstack.netvirt.api.TenantNetworkManager;
20 import org.opendaylight.ovsdb.plugin.IConnectionServiceInternal;
21 import org.opendaylight.ovsdb.plugin.OvsdbConfigService;
22 import org.opendaylight.ovsdb.plugin.OvsdbInventoryListener;
23 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
24
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 import java.net.HttpURLConnection;
29 import java.util.List;
30 import java.util.concurrent.ConcurrentMap;
31
32 /**
33  * Handle requests for Neutron Network.
34  */
35 public class NetworkHandler extends AbstractHandler
36                             implements INeutronNetworkAware {
37
38     public static final String NETWORK_TYPE_VXLAN = "vxlan";
39     public static final String NETWORK_TYPE_GRE = "gre";
40     public static final String NETWORK_TYPE_VLAN = "vlan";
41
42     /**
43      * Logger instance.
44      */
45     static final Logger logger = LoggerFactory.getLogger(NetworkHandler.class);
46
47     // The implementation for each of these services is resolved by the OSGi Service Manager
48     private volatile TenantNetworkManager tenantNetworkManager;
49     private volatile BridgeConfigurationManager bridgeConfigurationManager;
50     private volatile ConfigurationService configurationService;
51     private volatile OvsdbConfigService ovsdbConfigService;
52     private volatile IConnectionServiceInternal connectionService;
53     private volatile INeutronNetworkCRUD neutronNetworkCache;
54     private volatile OvsdbInventoryListener inventoryListener;
55
56     /**
57      * Invoked when a network creation is requested
58      * to indicate if the specified network can be created.
59      *
60      * @param network  An instance of proposed new Neutron Network object.
61      * @return A HTTP status code to the creation request.
62      */
63     @Override
64     public int canCreateNetwork(NeutronNetwork network) {
65         if (network.isShared()) {
66             logger.error(" Network shared attribute not supported ");
67             return HttpURLConnection.HTTP_NOT_ACCEPTABLE;
68         }
69
70         return HttpURLConnection.HTTP_CREATED;
71     }
72
73     /**
74      * Invoked to take action after a network has been created.
75      *
76      * @param network  An instance of new Neutron Network object.
77      */
78     @Override
79     public void neutronNetworkCreated(NeutronNetwork network) {
80         int result = HttpURLConnection.HTTP_BAD_REQUEST;
81         logger.trace("neutronNetworkCreated: network: {}", network);
82         result = canCreateNetwork(network);
83         if (result != HttpURLConnection.HTTP_CREATED) {
84             logger.debug("Network creation failed {} ", result);
85             return;
86         }
87
88     }
89
90     /**
91      * Invoked when a network update is requested
92      * to indicate if the specified network can be changed
93      * using the specified delta.
94      *
95      * @param delta     Updates to the network object using patch semantics.
96      * @param original  An instance of the Neutron Network object
97      *                  to be updated.
98      * @return A HTTP status code to the update request.
99      */
100     @Override
101     public int canUpdateNetwork(NeutronNetwork delta,
102                                 NeutronNetwork original) {
103         logger.trace("canUpdateNetwork: network delta {} --- original {}", delta, original);
104         return HttpURLConnection.HTTP_OK;
105     }
106
107     /**
108      * Invoked to take action after a network has been updated.
109      *
110      * @param network An instance of modified Neutron Network object.
111      */
112     @Override
113     public void neutronNetworkUpdated(NeutronNetwork network) {
114         logger.trace("neutronNetworkUpdated: network: {}", network);
115         return;
116     }
117
118     /**
119      * Invoked when a network deletion is requested
120      * to indicate if the specified network can be deleted.
121      *
122      * @param network  An instance of the Neutron Network object to be deleted.
123      * @return A HTTP status code to the deletion request.
124      */
125     @Override
126     public int canDeleteNetwork(NeutronNetwork network) {
127         return HttpURLConnection.HTTP_OK;
128     }
129
130     /**
131      * Invoked to take action after a network has been deleted.
132      *
133      * @param network  An instance of deleted Neutron Network object.
134      */
135     @Override
136     public void neutronNetworkDeleted(NeutronNetwork network) {
137
138         int result = canDeleteNetwork(network);
139         logger.trace("canDeleteNetwork: network: {}", network);
140         if  (result != HttpURLConnection.HTTP_OK) {
141             logger.error(" deleteNetwork validation failed for result - {} ",
142                     result);
143             return;
144         }
145         /* Is this the last Neutron tenant network */
146         List <NeutronNetwork> networks;
147         if (neutronNetworkCache != null) {
148             networks = neutronNetworkCache.getAllNetworks();
149             if (networks.isEmpty()) {
150                 logger.trace("neutronNetworkDeleted: last tenant network, delete tunnel ports...");
151                 List<Node> nodes = connectionService.getNodes();
152
153                 for (Node node : nodes) {
154                     List<String> phyIfName = bridgeConfigurationManager.getAllPhysicalInterfaceNames(node);
155                     try {
156                         ConcurrentMap<String, Row> interfaces = this.ovsdbConfigService.getRows(node, ovsdbConfigService.getTableName(node, Interface.class));
157                         if (interfaces != null) {
158                             for (String intfUUID : interfaces.keySet()) {
159                                 Interface intf = ovsdbConfigService.getTypedRow(node, Interface.class, interfaces.get(intfUUID));
160                                 String intfType = intf.getTypeColumn().getData();
161                                 if (intfType.equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VXLAN) || intfType.equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_GRE)) {
162                                     /* delete tunnel ports on this node */
163                                     logger.trace("Delete tunnel intf {}", intf);
164                                     inventoryListener.rowRemoved(node, intf.getSchema().getName(), intfUUID,
165                                             intf.getRow(), null);
166                                 } else if (!phyIfName.isEmpty() && phyIfName.contains(intf.getName())) {
167                                     logger.trace("Delete physical intf {}", intf);
168                                     inventoryListener.rowRemoved(node, intf.getSchema().getName(), intfUUID,
169                                             intf.getRow(), null);
170                                 }
171                             }
172                         }
173                     } catch (Exception e) {
174                         logger.error("Exception during handlingNeutron network delete");
175                     }
176                 }
177             }
178         }
179         tenantNetworkManager.networkDeleted(network.getID());
180     }
181 }