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