Clean up logging
[ovsdb.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 java.net.HttpURLConnection;
13 import java.util.List;
14
15 import org.opendaylight.neutron.spi.INeutronNetworkAware;
16 import org.opendaylight.neutron.spi.INeutronNetworkCRUD;
17 import org.opendaylight.neutron.spi.NeutronNetwork;
18 import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
19 import org.opendaylight.ovsdb.openstack.netvirt.api.BridgeConfigurationManager;
20 import org.opendaylight.ovsdb.openstack.netvirt.api.EventDispatcher;
21 import org.opendaylight.ovsdb.openstack.netvirt.api.NodeCacheManager;
22 import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound;
23 import org.opendaylight.ovsdb.openstack.netvirt.api.TenantNetworkManager;
24 import org.opendaylight.ovsdb.openstack.netvirt.impl.NeutronL3Adapter;
25 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
28 import org.osgi.framework.BundleContext;
29 import org.osgi.framework.ServiceReference;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * Handle requests for Neutron Network.
35  */
36 public class NetworkHandler extends AbstractHandler implements INeutronNetworkAware, ConfigInterface {
37     private static final Logger LOG = LoggerFactory.getLogger(NetworkHandler.class);
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     // The implementation for each of these services is resolved by the OSGi Service Manager
43     private volatile TenantNetworkManager tenantNetworkManager;
44     private volatile BridgeConfigurationManager bridgeConfigurationManager;
45     private volatile NodeCacheManager nodeCacheManager;
46     private volatile INeutronNetworkCRUD neutronNetworkCache;
47     private volatile NeutronL3Adapter neutronL3Adapter;
48     private volatile Southbound southbound;
49
50     /**
51      * Invoked when a network creation is requested
52      * to indicate if the specified network can be created.
53      *
54      * @param network  An instance of proposed new Neutron Network object.
55      * @return A HTTP status code to the creation request.
56      */
57     @Override
58     public int canCreateNetwork(NeutronNetwork network) {
59         if (network.isShared()) {
60             LOG.error(" Network shared attribute not supported ");
61             return HttpURLConnection.HTTP_NOT_ACCEPTABLE;
62         }
63
64         return HttpURLConnection.HTTP_OK;
65     }
66
67     /**
68      * Invoked to take action after a network has been created.
69      *
70      * @param network  An instance of new Neutron Network object.
71      */
72     @Override
73     public void neutronNetworkCreated(NeutronNetwork network) {
74         enqueueEvent(new NorthboundEvent(network, Action.ADD));
75     }
76     private void doNeutronNetworkCreated(NeutronNetwork network) {
77         neutronL3Adapter.handleNeutronNetworkEvent(network, Action.ADD);
78     }
79
80     /**
81      * Invoked when a network update is requested
82      * to indicate if the specified network can be changed
83      * using the specified delta.
84      *
85      * @param delta     Updates to the network object using patch semantics.
86      * @param original  An instance of the Neutron Network object
87      *                  to be updated.
88      * @return A HTTP status code to the update request.
89      */
90     @Override
91     public int canUpdateNetwork(NeutronNetwork delta,
92                                 NeutronNetwork original) {
93         if (delta.isShared()) {
94             LOG.error(" Network shared attribute not supported ");
95             return HttpURLConnection.HTTP_NOT_ACCEPTABLE;
96         }
97
98         return HttpURLConnection.HTTP_OK;
99     }
100
101     /**
102      * Invoked to take action after a network has been updated.
103      *
104      * @param network An instance of modified Neutron Network object.
105      */
106     @Override
107     public void neutronNetworkUpdated(NeutronNetwork network) {
108         enqueueEvent(new NorthboundEvent(network, Action.UPDATE));
109     }
110     private void doNeutronNetworkUpdated(NeutronNetwork network) {
111         neutronL3Adapter.handleNeutronNetworkEvent(network, Action.UPDATE);
112     }
113
114     /**
115      * Invoked when a network deletion is requested
116      * to indicate if the specified network can be deleted.
117      *
118      * @param network  An instance of the Neutron Network object to be deleted.
119      * @return A HTTP status code to the deletion request.
120      */
121     @Override
122     public int canDeleteNetwork(NeutronNetwork network) {
123         return HttpURLConnection.HTTP_OK;
124     }
125
126     /**
127      * Invoked to take action after a network has been deleted.
128      *
129      * @param network  An instance of deleted Neutron Network object.
130      */
131     @Override
132     public void neutronNetworkDeleted(NeutronNetwork network) {
133         enqueueEvent(new NorthboundEvent(network, Action.DELETE));
134     }
135     private void doNeutronNetworkDeleted(NeutronNetwork network) {
136         neutronL3Adapter.handleNeutronNetworkEvent(network, Action.DELETE);
137
138         /* Is this the last Neutron tenant network */
139         List <NeutronNetwork> networks;
140         if (neutronNetworkCache != null) {
141             networks = neutronNetworkCache.getAllNetworks();
142             if (networks.isEmpty()) {
143                 LOG.trace("neutronNetworkDeleted: last tenant network, delete tunnel ports...");
144                 List<Node> nodes = nodeCacheManager.getNodes();
145
146                 for (Node node : nodes) {
147                     List<String> phyIfName = bridgeConfigurationManager.getAllPhysicalInterfaceNames(node);
148                     try {
149                         List<OvsdbTerminationPointAugmentation> ports = southbound.getTerminationPointsOfBridge(node);
150                         for (OvsdbTerminationPointAugmentation port : ports) {
151                             if (southbound.isTunnel(port)) {
152                                 LOG.trace("Delete tunnel interface {}", port.getName());
153                                 southbound.deleteTerminationPoint(node, port.getName());
154                             } else if (!phyIfName.isEmpty() && phyIfName.contains(port.getName())) {
155                                 LOG.trace("Delete physical interface {}", port.getName());
156                                 southbound.deleteTerminationPoint(node, port.getName());
157                             }
158                         }
159                     } catch (Exception e) {
160                         LOG.error("Exception during handlingNeutron network delete", e);
161                     }
162                 }
163             }
164         }
165         tenantNetworkManager.networkDeleted(network.getID());
166     }
167
168     /**
169      * Process the event.
170      *
171      * @param abstractEvent the {@link org.opendaylight.ovsdb.openstack.netvirt.AbstractEvent} event to be handled.
172      * @see org.opendaylight.ovsdb.openstack.netvirt.api.EventDispatcher
173      */
174     @Override
175     public void processEvent(AbstractEvent abstractEvent) {
176         if (!(abstractEvent instanceof NorthboundEvent)) {
177             LOG.error("Unable to process abstract event {}", abstractEvent);
178             return;
179         }
180         NorthboundEvent ev = (NorthboundEvent) abstractEvent;
181         switch (ev.getAction()) {
182             case ADD:
183                 doNeutronNetworkCreated(ev.getNeutronNetwork());
184                 break;
185             case UPDATE:
186                 doNeutronNetworkUpdated(ev.getNeutronNetwork());
187                 break;
188             case DELETE:
189                 doNeutronNetworkDeleted(ev.getNeutronNetwork());
190                 break;
191             default:
192                 LOG.warn("Unable to process event action {}", ev.getAction());
193                 break;
194         }
195     }
196
197     @Override
198     public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
199         tenantNetworkManager =
200                 (TenantNetworkManager) ServiceHelper.getGlobalInstance(TenantNetworkManager.class, this);
201         bridgeConfigurationManager =
202                 (BridgeConfigurationManager) ServiceHelper.getGlobalInstance(BridgeConfigurationManager.class, this);
203         nodeCacheManager =
204                 (NodeCacheManager) ServiceHelper.getGlobalInstance(NodeCacheManager.class, this);
205         neutronL3Adapter =
206                 (NeutronL3Adapter) ServiceHelper.getGlobalInstance(NeutronL3Adapter.class, this);
207         southbound =
208                 (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
209         eventDispatcher =
210                 (EventDispatcher) ServiceHelper.getGlobalInstance(EventDispatcher.class, this);
211         eventDispatcher.eventHandlerAdded(
212                 bundleContext.getServiceReference(INeutronNetworkAware.class.getName()), this);
213     }
214
215     @Override
216     public void setDependencies(Object impl) {
217         if (impl instanceof INeutronNetworkCRUD) {
218             neutronNetworkCache = (INeutronNetworkCRUD)impl;
219         }
220     }
221 }