50b1336d1510fa2226044250a9e8ebe516423a23
[ovsdb.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, 4)
55     .put(IpVersionV6.class, 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         if (subnet.getTenantId() != null) {
142             result.setTenantID(String.valueOf(subnet.getTenantId().getValue()).replace("-",""));
143         }
144         result.setNetworkUUID(subnet.getNetworkId().getValue());
145         result.setIpVersion(IPV_MAP.get(subnet.getIpVersion()));
146         result.setCidr(subnet.getCidr());
147         if (subnet.getGatewayIp() != null) {
148             result.setGatewayIP(String.valueOf(subnet.getGatewayIp().getValue()));
149         }
150         if (subnet.getIpv6RaMode() != null) {
151             result.setIpV6RaMode(DHCPV6_MAP.get(subnet.getIpv6RaMode()));
152         }
153         if (subnet.getIpv6AddressMode() != null) {
154             result.setIpV6AddressMode(DHCPV6_MAP.get(subnet.getIpv6AddressMode()));
155         }
156         result.setEnableDHCP(subnet.isEnableDhcp());
157         if (subnet.getAllocationPools() != null) {
158             List<NeutronSubnetIPAllocationPool> allocationPools = new ArrayList<>();
159             for (AllocationPools allocationPool : subnet.getAllocationPools()) {
160                 NeutronSubnetIPAllocationPool pool = new NeutronSubnetIPAllocationPool();
161                 pool.setPoolStart(allocationPool.getStart());
162                 pool.setPoolEnd(allocationPool.getEnd());
163                 allocationPools.add(pool);
164             }
165             result.setAllocationPools(allocationPools);
166         }
167         if (subnet.getDnsNameservers() != null) {
168             List<String> dnsNameServers = new ArrayList<>();
169             for (IpAddress dnsNameServer : subnet.getDnsNameservers()) {
170                 dnsNameServers.add(String.valueOf(dnsNameServer.getValue()));
171             }
172             result.setDnsNameservers(dnsNameServers);
173         }
174         result.setID(subnet.getUuid().getValue());
175
176         // read through the ports and put the ones in this subnet into the internal
177         // myPorts object.
178        Set<NeutronPort> allPorts = new HashSet<>();
179         NeutronCRUDInterfaces interfaces = new NeutronCRUDInterfaces()
180             .fetchINeutronPortCRUD(this);
181         INeutronPortCRUD portIf = interfaces.getPortInterface();
182         for (NeutronPort port : portIf.getAllPorts()) {
183             if (port.getFixedIPs() != null) {
184                 for (Neutron_IPs ip : port.getFixedIPs()) {
185                     if (ip.getSubnetUUID().equals(result.getID())) {
186                         allPorts.add(port);
187                     }
188                 }
189             }
190         }
191         List<NeutronPort> ports = new ArrayList<>();
192         ports.addAll(allPorts);
193         result.setPorts(ports);
194         return result;
195     }
196
197     @Override
198     public void close() throws Exception {
199         registration.close();
200     }
201
202 }