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