Merge "Add UT for SouthboundMapper and SouthboundProvider"
[netvirt.git] / openstack / net-virt / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / SubnetHandler.java
1 /*
2  * Copyright (c) 2013, 2015 Red Hat, Inc. and others. All rights reserved.
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
9 package org.opendaylight.ovsdb.openstack.netvirt;
10
11 import java.net.HttpURLConnection;
12
13 import org.opendaylight.neutron.spi.INeutronSubnetAware;
14 import org.opendaylight.neutron.spi.NeutronSubnet;
15 import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
16 import org.opendaylight.ovsdb.openstack.netvirt.api.EventDispatcher;
17 import org.opendaylight.ovsdb.openstack.netvirt.impl.NeutronL3Adapter;
18 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
19 import org.osgi.framework.ServiceReference;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 import com.google.common.base.Preconditions;
24
25 public class SubnetHandler extends AbstractHandler implements INeutronSubnetAware, ConfigInterface {
26
27     private static final Logger LOG = LoggerFactory.getLogger(SubnetHandler.class);
28
29     // The implementation for each of these services is resolved by the OSGi Service Manager
30     private volatile NeutronL3Adapter neutronL3Adapter;
31
32     @Override
33     public int canCreateSubnet(NeutronSubnet subnet) {
34         return HttpURLConnection.HTTP_OK;
35     }
36
37     @Override
38     public void neutronSubnetCreated(NeutronSubnet subnet) {
39         enqueueEvent(new NorthboundEvent(subnet, Action.ADD));
40     }
41
42     @Override
43     public int canUpdateSubnet(NeutronSubnet delta, NeutronSubnet original) {
44         return HttpURLConnection.HTTP_OK;
45     }
46
47     @Override
48     public void neutronSubnetUpdated(NeutronSubnet subnet) {
49         enqueueEvent(new NorthboundEvent(subnet, Action.UPDATE));
50     }
51
52     @Override
53     public int canDeleteSubnet(NeutronSubnet subnet) {
54         return HttpURLConnection.HTTP_OK;
55     }
56
57     @Override
58     public void neutronSubnetDeleted(NeutronSubnet subnet) {
59         enqueueEvent(new NorthboundEvent(subnet, Action.DELETE));
60     }
61
62     /**
63      * Process the event.
64      *
65      * @param abstractEvent the {@link org.opendaylight.ovsdb.openstack.netvirt.AbstractEvent} event to be handled.
66      * @see org.opendaylight.ovsdb.openstack.netvirt.api.EventDispatcher
67      */
68     @Override
69     public void processEvent(AbstractEvent abstractEvent) {
70         if (!(abstractEvent instanceof NorthboundEvent)) {
71             LOG.error("Unable to process abstract event {}", abstractEvent);
72             return;
73         }
74         NorthboundEvent ev = (NorthboundEvent) abstractEvent;
75         switch (ev.getAction()) {
76             case ADD:
77                 // fall through
78             case DELETE:
79                 // fall through
80             case UPDATE:
81                 Preconditions.checkNotNull(neutronL3Adapter);
82                 neutronL3Adapter.handleNeutronSubnetEvent(ev.getSubnet(), ev.getAction());
83                 break;
84             default:
85                 LOG.warn("Unable to process event action {}", ev.getAction());
86                 break;
87         }
88     }
89
90     @Override
91     public void setDependencies(ServiceReference serviceReference) {
92         neutronL3Adapter =
93                 (NeutronL3Adapter) ServiceHelper.getGlobalInstance(NeutronL3Adapter.class, this);
94         eventDispatcher =
95                 (EventDispatcher) ServiceHelper.getGlobalInstance(EventDispatcher.class, this);
96         eventDispatcher.eventHandlerAdded(serviceReference, this);
97     }
98
99     @Override
100     public void setDependencies(Object impl) {
101
102     }
103 }