L3: Add eth to br-ex
[netvirt.git] / openstack / net-virt / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / translator / iaware / impl / NeutronSubnetChangeListener.java
1 /*
2  * Copyright (c) 2015 Brocade Communications Systems, 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 package org.opendaylight.ovsdb.openstack.netvirt.translator.iaware.impl;
9
10 import java.util.ArrayList;
11 import java.util.HashSet;
12 import java.util.List;
13 import java.util.Map.Entry;
14 import java.util.Set;
15
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
18 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
19 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.ovsdb.openstack.netvirt.translator.NeutronPort;
22 import org.opendaylight.ovsdb.openstack.netvirt.translator.NeutronSubnet;
23 import org.opendaylight.ovsdb.openstack.netvirt.translator.NeutronSubnetIPAllocationPool;
24 import org.opendaylight.ovsdb.openstack.netvirt.translator.Neutron_IPs;
25 import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.INeutronPortCRUD;
26 import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.NeutronCRUDInterfaces;
27 import org.opendaylight.ovsdb.openstack.netvirt.translator.iaware.INeutronSubnetAware;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.Dhcpv6Base;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.Dhcpv6Off;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.Dhcpv6Slaac;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.Dhcpv6Stateful;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.Dhcpv6Stateless;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.IpVersionBase;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.IpVersionV4;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.IpVersionV6;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnet.attributes.AllocationPools;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.Subnets;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.Subnet;
41 import org.opendaylight.yangtools.concepts.ListenerRegistration;
42 import org.opendaylight.yangtools.yang.binding.DataObject;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 import com.google.common.collect.ImmutableBiMap;
48
49 public class NeutronSubnetChangeListener implements DataChangeListener, AutoCloseable{
50     private static final Logger LOG = LoggerFactory.getLogger(NeutronSubnetChangeListener.class);
51
52     private static final ImmutableBiMap<Class<? extends IpVersionBase>,Integer> IPV_MAP
53     = new ImmutableBiMap.Builder<Class<? extends IpVersionBase>,Integer>()
54     .put(IpVersionV4.class,Integer.valueOf(4))
55     .put(IpVersionV6.class,Integer.valueOf(6))
56     .build();
57
58     private static final ImmutableBiMap<Class<? extends Dhcpv6Base>,String> DHCPV6_MAP
59     = new ImmutableBiMap.Builder<Class<? extends Dhcpv6Base>,String>()
60     .put(Dhcpv6Off.class,"off")
61     .put(Dhcpv6Stateful.class,"dhcpv6-stateful")
62     .put(Dhcpv6Slaac.class,"slaac")
63     .put(Dhcpv6Stateless.class,"dhcpv6-stateless")
64     .build();
65
66     private ListenerRegistration<DataChangeListener> registration;
67     private DataBroker db;
68
69     public NeutronSubnetChangeListener(DataBroker db){
70         this.db = db;
71         InstanceIdentifier<Subnet> path = InstanceIdentifier
72                 .create(Neutron.class)
73                 .child(Subnets.class)
74                 .child(Subnet.class);
75         LOG.debug("Register listener for Neutron Subnet model data changes");
76         registration =
77                 this.db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION, path, this, DataChangeScope.ONE);
78
79     }
80
81     @Override
82     public void onDataChanged(
83             AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
84         LOG.trace("Data changes : {}",changes);
85
86         Object[] subscribers = NeutronIAwareUtil.getInstances(INeutronSubnetAware.class, this);
87         createSubnet(changes, subscribers);
88         updateSubnet(changes, subscribers);
89         deleteSubnet(changes, subscribers);
90     }
91
92     private void createSubnet(
93             AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes,
94             Object[] subscribers) {
95         for (Entry<InstanceIdentifier<?>, DataObject> newSubnet : changes.getCreatedData().entrySet()) {
96                 if(newSubnet.getValue() instanceof Subnet){
97                 NeutronSubnet subnet = fromMd((Subnet)newSubnet.getValue());
98                 for(Object entry: subscribers){
99                     INeutronSubnetAware subscriber = (INeutronSubnetAware)entry;
100                     subscriber.neutronSubnetCreated(subnet);
101                 }
102                 }
103         }
104     }
105
106     private void updateSubnet(
107             AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes,
108             Object[] subscribers) {
109         for (Entry<InstanceIdentifier<?>, DataObject> updateSubnet : changes.getUpdatedData().entrySet()) {
110                 if(updateSubnet.getValue() instanceof Subnet){
111                 NeutronSubnet subnet = fromMd((Subnet)updateSubnet.getValue());
112                 for(Object entry: subscribers){
113                     INeutronSubnetAware subscriber = (INeutronSubnetAware)entry;
114                     subscriber.neutronSubnetUpdated(subnet);
115                 }
116                 }
117         }
118     }
119
120     private void deleteSubnet(
121             AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes,
122             Object[] subscribers) {
123         for (InstanceIdentifier<?> deletedSubnetPath : changes.getRemovedPaths()) {
124                 if(deletedSubnetPath.getTargetType().equals(Subnet.class)){
125                 NeutronSubnet subnet = fromMd((Subnet)changes.getOriginalData().get(deletedSubnetPath));
126                 for(Object entry: subscribers){
127                     INeutronSubnetAware subscriber = (INeutronSubnetAware)entry;
128                     subscriber.neutronSubnetDeleted(subnet);
129                 }
130                 }
131         }
132     }
133
134     /*
135      * This method is borrowed from NeutronSubnetInterface.java class of Neutron Northbound class.
136      * We will be utilizing similar code from other classes from the same package of neutron project.
137      */
138     private NeutronSubnet fromMd(Subnet subnet) {
139         NeutronSubnet result = new NeutronSubnet();
140         result.setName(subnet.getName());
141         result.setTenantID(String.valueOf(subnet.getTenantId().getValue()).replace("-",""));
142         result.setNetworkUUID(subnet.getNetworkId().getValue());
143         result.setIpVersion(IPV_MAP.get(subnet.getIpVersion()));
144         result.setCidr(subnet.getCidr());
145         result.setGatewayIP(String.valueOf(subnet.getGatewayIp().getValue()));
146         result.setIpV6RaMode(DHCPV6_MAP.get(subnet.getIpv6RaMode()));
147         result.setIpV6AddressMode(DHCPV6_MAP.get(subnet.getIpv6AddressMode()));
148         result.setEnableDHCP(subnet.isEnableDhcp());
149         if (subnet.getAllocationPools() != null) {
150             List<NeutronSubnetIPAllocationPool> allocationPools = new ArrayList<NeutronSubnetIPAllocationPool>();
151             for (AllocationPools allocationPool : subnet.getAllocationPools()) {
152                 NeutronSubnetIPAllocationPool pool = new NeutronSubnetIPAllocationPool();
153                 pool.setPoolStart(allocationPool.getStart());
154                 pool.setPoolEnd(allocationPool.getEnd());
155                 allocationPools.add(pool);
156             }
157             result.setAllocationPools(allocationPools);
158         }
159         if (subnet.getDnsNameservers() != null) {
160             List<String> dnsNameServers = new ArrayList<String>();
161             for (IpAddress dnsNameServer : subnet.getDnsNameservers()) {
162                 dnsNameServers.add(String.valueOf(dnsNameServer.getValue()));
163             }
164             result.setDnsNameservers(dnsNameServers);
165         }
166         result.setID(subnet.getUuid().getValue());
167
168         // read through the ports and put the ones in this subnet into the internal
169         // myPorts object.
170        Set<NeutronPort> allPorts = new HashSet<NeutronPort>();
171         NeutronCRUDInterfaces interfaces = new NeutronCRUDInterfaces()
172             .fetchINeutronPortCRUD(this);
173         INeutronPortCRUD portIf = interfaces.getPortInterface();
174         for (NeutronPort port : portIf.getAllPorts()) {
175             if (port.getFixedIPs() != null) {
176                 for (Neutron_IPs ip : port.getFixedIPs()) {
177                     if (ip.getSubnetUUID().equals(result.getID())) {
178                         allPorts.add(port);
179                     }
180                 }
181             }
182         }
183         List<NeutronPort> ports = new ArrayList<NeutronPort>();
184         ports.addAll(allPorts);
185         result.setPorts(ports);
186         return result;
187     }
188
189     @Override
190     public void close() throws Exception {
191         registration.close();
192     }
193
194 }