2 * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.ovsdb.openstack.netvirt.translator.iaware.impl;
10 import java.util.ArrayList;
11 import java.util.HashSet;
12 import java.util.List;
13 import java.util.Map.Entry;
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;
47 import com.google.common.collect.ImmutableBiMap;
49 public class NeutronSubnetChangeListener implements DataChangeListener, AutoCloseable{
50 private static final Logger LOG = LoggerFactory.getLogger(NeutronSubnetChangeListener.class);
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))
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")
66 private ListenerRegistration<DataChangeListener> registration;
67 private DataBroker db;
69 public NeutronSubnetChangeListener(DataBroker db){
71 InstanceIdentifier<Subnet> path = InstanceIdentifier
72 .create(Neutron.class)
75 LOG.debug("Register listener for Neutron Subnet model data changes");
77 this.db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION, path, this, DataChangeScope.ONE);
82 public void onDataChanged(
83 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes) {
84 LOG.trace("Data changes : {}",changes);
86 Object[] subscribers = NeutronIAwareUtil.getInstances(INeutronSubnetAware.class, this);
87 createSubnet(changes, subscribers);
88 updateSubnet(changes, subscribers);
89 deleteSubnet(changes, subscribers);
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);
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);
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);
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.
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);
157 result.setAllocationPools(allocationPools);
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()));
164 result.setDnsNameservers(dnsNameServers);
166 result.setID(subnet.getUuid().getValue());
168 // read through the ports and put the ones in this subnet into the internal
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())) {
183 List<NeutronPort> ports = new ArrayList<NeutronPort>();
184 ports.addAll(allPorts);
185 result.setPorts(ports);
190 public void close() throws Exception {
191 registration.close();