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.rev160807.Dhcpv6Base;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev160807.Dhcpv6Off;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev160807.Dhcpv6Slaac;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev160807.Dhcpv6Stateful;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev160807.Dhcpv6Stateless;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev160807.IpVersionBase;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev160807.IpVersionV4;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev160807.IpVersionV6;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150325.Neutron;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev141002.subnet.attributes.AllocationPools;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev141002.subnets.attributes.Subnets;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev141002.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 NeutronSubnet subnet = fromMd((Subnet)newSubnet.getValue());
97 for(Object entry: subscribers){
98 INeutronSubnetAware subscriber = (INeutronSubnetAware)entry;
99 subscriber.neutronSubnetCreated(subnet);
105 private void updateSubnet(
106 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes,
107 Object[] subscribers) {
108 for (Entry<InstanceIdentifier<?>, DataObject> updateSubnet : changes.getUpdatedData().entrySet()) {
109 NeutronSubnet subnet = fromMd((Subnet)updateSubnet.getValue());
110 for(Object entry: subscribers){
111 INeutronSubnetAware subscriber = (INeutronSubnetAware)entry;
112 subscriber.neutronSubnetUpdated(subnet);
117 private void deleteSubnet(
118 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes,
119 Object[] subscribers) {
120 for (InstanceIdentifier<?> deletedSubnetPath : changes.getRemovedPaths()) {
121 NeutronSubnet subnet = fromMd((Subnet)changes.getOriginalData().get(deletedSubnetPath));
122 for(Object entry: subscribers){
123 INeutronSubnetAware subscriber = (INeutronSubnetAware)entry;
124 subscriber.neutronSubnetDeleted(subnet);
130 * This method is borrowed from NeutronSubnetInterface.java class of Neutron Northbound class.
131 * We will be utilizing similar code from other classes from the same package of neutron project.
133 private NeutronSubnet fromMd(Subnet subnet) {
134 NeutronSubnet result = new NeutronSubnet();
135 result.setName(subnet.getName());
136 result.setTenantID(String.valueOf(subnet.getTenantId().getValue()).replace("-",""));
137 result.setNetworkUUID(subnet.getNetworkId().getValue());
138 result.setIpVersion(IPV_MAP.get(subnet.getIpVersion()));
139 result.setCidr(subnet.getCidr());
140 result.setGatewayIP(String.valueOf(subnet.getGatewayIp().getValue()));
141 result.setIpV6RaMode(DHCPV6_MAP.get(subnet.getIpv6RaMode()));
142 result.setIpV6AddressMode(DHCPV6_MAP.get(subnet.getIpv6AddressMode()));
143 result.setEnableDHCP(subnet.isEnableDhcp());
144 if (subnet.getAllocationPools() != null) {
145 List<NeutronSubnetIPAllocationPool> allocationPools = new ArrayList<NeutronSubnetIPAllocationPool>();
146 for (AllocationPools allocationPool : subnet.getAllocationPools()) {
147 NeutronSubnetIPAllocationPool pool = new NeutronSubnetIPAllocationPool();
148 pool.setPoolStart(allocationPool.getStart());
149 pool.setPoolEnd(allocationPool.getEnd());
150 allocationPools.add(pool);
152 result.setAllocationPools(allocationPools);
154 if (subnet.getDnsNameservers() != null) {
155 List<String> dnsNameServers = new ArrayList<String>();
156 for (IpAddress dnsNameServer : subnet.getDnsNameservers()) {
157 dnsNameServers.add(String.valueOf(dnsNameServer.getValue()));
159 result.setDnsNameservers(dnsNameServers);
161 result.setID(subnet.getUuid().getValue());
163 // read through the ports and put the ones in this subnet into the internal
165 Set<NeutronPort> allPorts = new HashSet<NeutronPort>();
166 NeutronCRUDInterfaces interfaces = new NeutronCRUDInterfaces()
167 .fetchINeutronPortCRUD(this);
168 INeutronPortCRUD portIf = interfaces.getPortInterface();
169 for (NeutronPort port : portIf.getAllPorts()) {
170 if (port.getFixedIPs() != null) {
171 for (Neutron_IPs ip : port.getFixedIPs()) {
172 if (ip.getSubnetUUID().equals(result.getID())) {
178 List<NeutronPort> ports = new ArrayList<NeutronPort>();
179 ports.addAll(allPorts);
180 result.setPorts(ports);
185 public void close() throws Exception {
186 registration.close();