2 * Copyright (c) 2013, 2015 IBM Corporation 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
9 package org.opendaylight.netvirt.openstack.netvirt.translator.crud.impl;
11 import com.google.common.collect.ImmutableBiMap;
12 import java.util.ArrayList;
13 import java.util.HashSet;
14 import java.util.List;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.netvirt.openstack.netvirt.translator.NeutronPort;
18 import org.opendaylight.netvirt.openstack.netvirt.translator.NeutronSubnet;
19 import org.opendaylight.netvirt.openstack.netvirt.translator.NeutronSubnetIPAllocationPool;
20 import org.opendaylight.netvirt.openstack.netvirt.translator.Neutron_IPs;
21 import org.opendaylight.netvirt.openstack.netvirt.translator.crud.INeutronPortCRUD;
22 import org.opendaylight.netvirt.openstack.netvirt.translator.crud.INeutronSubnetCRUD;
23 import org.opendaylight.netvirt.openstack.netvirt.translator.crud.NeutronCRUDInterfaces;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.Dhcpv6Base;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.Dhcpv6Off;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.Dhcpv6Slaac;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.Dhcpv6Stateful;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.Dhcpv6Stateless;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.IpVersionBase;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.IpVersionV4;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.IpVersionV6;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnet.attributes.AllocationPools;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnet.attributes.AllocationPoolsBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.Subnets;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.Subnet;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.SubnetBuilder;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.osgi.framework.BundleContext;
42 import org.osgi.framework.ServiceRegistration;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
46 public class NeutronSubnetInterface extends AbstractNeutronInterface<Subnet, NeutronSubnet> implements INeutronSubnetCRUD {
47 private static final Logger LOGGER = LoggerFactory.getLogger(NeutronSubnetInterface.class);
49 private static final ImmutableBiMap<Class<? extends IpVersionBase>,Integer> IPV_MAP
50 = new ImmutableBiMap.Builder<Class<? extends IpVersionBase>,Integer>()
51 .put(IpVersionV4.class, 4)
52 .put(IpVersionV6.class, 6)
55 private static final ImmutableBiMap<Class<? extends Dhcpv6Base>,String> DHCPV6_MAP
56 = new ImmutableBiMap.Builder<Class<? extends Dhcpv6Base>,String>()
57 .put(Dhcpv6Off.class,"off")
58 .put(Dhcpv6Stateful.class,"dhcpv6-stateful")
59 .put(Dhcpv6Slaac.class,"slaac")
60 .put(Dhcpv6Stateless.class,"dhcpv6-stateless")
63 NeutronSubnetInterface(final DataBroker dataBroker) {
67 // IfNBSubnetCRUD methods
70 public boolean subnetExists(String uuid) {
71 Subnet subnet = readMd(createInstanceIdentifier(toMd(uuid)));
79 public NeutronSubnet getSubnet(String uuid) {
80 Subnet subnet = readMd(createInstanceIdentifier(toMd(uuid)));
84 return fromMd(subnet);
88 public List<NeutronSubnet> getAllSubnets() {
89 Set<NeutronSubnet> allSubnets = new HashSet<>();
90 Subnets subnets = readMd(createInstanceIdentifier());
91 if (subnets != null) {
92 for (Subnet subnet: subnets.getSubnet()) {
93 allSubnets.add(fromMd(subnet));
96 LOGGER.debug("Exiting getAllSubnets, Found {} OpenStackSubnets", allSubnets.size());
97 List<NeutronSubnet> ans = new ArrayList<>();
98 ans.addAll(allSubnets);
103 public boolean addSubnet(NeutronSubnet input) {
104 String id = input.getID();
105 if (subnetExists(id)) {
113 public boolean removeSubnet(String uuid) {
114 NeutronSubnet target = getSubnet(uuid);
115 if (target == null) {
118 removeMd(toMd(uuid));
123 public boolean updateSubnet(String uuid, NeutronSubnet delta) {
124 if (!subnetExists(uuid)) {
127 /* note: because what we get is *not* a delta but (at this point) the updated
128 * object, this is much simpler - just replace the value and update the mdsal
134 // note: this is being set to false in preparation for deprecation and removal
136 public boolean subnetInUse(String subnetUUID) {
140 protected NeutronSubnet fromMd(Subnet subnet) {
141 NeutronSubnet result = new NeutronSubnet();
142 result.setName(subnet.getName());
143 result.setTenantID(String.valueOf(subnet.getTenantId().getValue()).replace("-",""));
144 result.setNetworkUUID(subnet.getNetworkId().getValue());
145 result.setIpVersion(IPV_MAP.get(subnet.getIpVersion()));
146 result.setCidr(String.valueOf(subnet.getCidr().getValue()));
147 result.setIpV6RaMode(DHCPV6_MAP.get(subnet.getIpv6RaMode()));
148 result.setIpV6AddressMode(DHCPV6_MAP.get(subnet.getIpv6AddressMode()));
149 result.setEnableDHCP(subnet.isEnableDhcp());
150 if (subnet.getAllocationPools() != null) {
151 List<NeutronSubnetIPAllocationPool> allocationPools = new ArrayList<>();
152 for (AllocationPools allocationPool : subnet.getAllocationPools()) {
153 NeutronSubnetIPAllocationPool pool = new NeutronSubnetIPAllocationPool();
154 pool.setPoolStart(String.valueOf(allocationPool.getStart().getValue()));
155 pool.setPoolEnd(String.valueOf(allocationPool.getEnd().getValue()));
156 allocationPools.add(pool);
158 result.setAllocationPools(allocationPools);
160 if (subnet.getDnsNameservers() != null) {
161 List<String> dnsNameServers = new ArrayList<>();
162 for (IpAddress dnsNameServer : subnet.getDnsNameservers()) {
163 dnsNameServers.add(String.valueOf(dnsNameServer.getValue()));
165 result.setDnsNameservers(dnsNameServers);
167 result.setID(subnet.getUuid().getValue());
168 // read through the ports and put the ones in this subnet into the internal
170 // @deprecated and will be removed in Boron
171 Set<NeutronPort> allPorts = new HashSet<>();
172 NeutronCRUDInterfaces interfaces = new NeutronCRUDInterfaces()
173 .fetchINeutronPortCRUD(this);
174 INeutronPortCRUD portIf = interfaces.getPortInterface();
175 for (NeutronPort port : portIf.getAllPorts()) {
176 if (port.getDeviceOwner().equalsIgnoreCase("network:router_interface") ||
177 port.getDeviceOwner().equalsIgnoreCase("network:router_gateway")) {
178 result.setGatewayIP(String.valueOf(subnet.getGatewayIp().getValue()));
180 if (port.getFixedIPs() != null) {
181 for (Neutron_IPs ip : port.getFixedIPs()) {
182 if (ip.getSubnetUUID().equals(result.getID())) {
188 List<NeutronPort> ports = new ArrayList<>();
189 ports.addAll(allPorts);
190 result.setPorts(ports);
194 protected Subnet toMd(NeutronSubnet subnet) {
195 SubnetBuilder subnetBuilder = new SubnetBuilder();
196 if (subnet.getName() != null) {
197 subnetBuilder.setName(subnet.getName());
199 if (subnet.getTenantID() != null) {
200 subnetBuilder.setTenantId(toUuid(subnet.getTenantID()));
202 if (subnet.getNetworkUUID() != null) {
203 subnetBuilder.setNetworkId(toUuid(subnet.getNetworkUUID()));
205 if (subnet.getIpVersion() != null) {
206 ImmutableBiMap<Integer, Class<? extends IpVersionBase>> mapper =
208 subnetBuilder.setIpVersion(mapper.get(subnet
211 if (subnet.getCidr() != null) {
212 IpPrefix ipPrefix = new IpPrefix(subnet.getCidr().toCharArray());
213 subnetBuilder.setCidr(ipPrefix);
215 if (subnet.getGatewayIP() != null) {
216 IpAddress ipAddress = new IpAddress(subnet.getGatewayIP()
218 subnetBuilder.setGatewayIp(ipAddress);
220 if (subnet.getIpV6RaMode() != null) {
221 ImmutableBiMap<String, Class<? extends Dhcpv6Base>> mapper =
222 DHCPV6_MAP.inverse();
223 subnetBuilder.setIpv6RaMode(mapper.get(subnet.getIpV6RaMode()));
225 if (subnet.getIpV6AddressMode() != null) {
226 ImmutableBiMap<String, Class<? extends Dhcpv6Base>> mapper =
227 DHCPV6_MAP.inverse();
228 subnetBuilder.setIpv6AddressMode(mapper.get(subnet.getIpV6AddressMode()));
230 subnetBuilder.setEnableDhcp(subnet.getEnableDHCP());
231 if (subnet.getAllocationPools() != null) {
232 List<AllocationPools> allocationPools = new ArrayList<>();
233 for (NeutronSubnetIPAllocationPool allocationPool : subnet
234 .getAllocationPools()) {
235 AllocationPoolsBuilder builder = new AllocationPoolsBuilder();
236 builder.setStart(new IpAddress(allocationPool.getPoolStart().toCharArray()));
237 builder.setEnd(new IpAddress(allocationPool.getPoolEnd().toCharArray()));
238 AllocationPools temp = builder.build();
239 allocationPools.add(temp);
241 subnetBuilder.setAllocationPools(allocationPools);
243 if (subnet.getDnsNameservers() != null) {
244 List<IpAddress> dnsNameServers = new ArrayList<>();
245 for (String dnsNameServer : subnet.getDnsNameservers()) {
246 IpAddress ipAddress = new IpAddress(dnsNameServer.toCharArray());
247 dnsNameServers.add(ipAddress);
249 subnetBuilder.setDnsNameservers(dnsNameServers);
251 if (subnet.getID() != null) {
252 subnetBuilder.setUuid(toUuid(subnet.getID()));
254 LOGGER.warn("Attempting to write neutron subnet without UUID");
256 return subnetBuilder.build();
260 protected InstanceIdentifier<Subnet> createInstanceIdentifier(Subnet subnet) {
261 return InstanceIdentifier.create(Neutron.class).child(Subnets.class)
262 .child(Subnet.class, subnet.getKey());
265 protected InstanceIdentifier<Subnets> createInstanceIdentifier() {
266 return InstanceIdentifier.create(Neutron.class)
267 .child(Subnets.class);
271 protected Subnet toMd(String uuid) {
272 SubnetBuilder subnetBuilder = new SubnetBuilder();
273 subnetBuilder.setUuid(toUuid(uuid));
274 return subnetBuilder.build();
277 public static void registerNewInterface(BundleContext context,
278 final DataBroker dataBroker,
279 List<ServiceRegistration<?>> registrations) {
280 NeutronSubnetInterface neutronSubnetInterface = new NeutronSubnetInterface(dataBroker);
281 ServiceRegistration<INeutronSubnetCRUD> neutronSubnetInterfaceRegistration = context.registerService(INeutronSubnetCRUD.class, neutronSubnetInterface, null);
282 if(neutronSubnetInterfaceRegistration != null) {
283 registrations.add(neutronSubnetInterfaceRegistration);