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.ovsdb.openstack.netvirt.translator.crud.impl;
11 import java.util.ArrayList;
12 import java.util.HashSet;
13 import java.util.List;
16 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
17 import org.opendaylight.ovsdb.openstack.netvirt.translator.NeutronNetwork;
18 import org.opendaylight.ovsdb.openstack.netvirt.translator.NeutronPort;
19 import org.opendaylight.ovsdb.openstack.netvirt.translator.NeutronSubnet;
20 import org.opendaylight.ovsdb.openstack.netvirt.translator.NeutronSubnetIPAllocationPool;
21 import org.opendaylight.ovsdb.openstack.netvirt.translator.Neutron_IPs;
22 import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.INeutronNetworkCRUD;
23 import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.INeutronPortCRUD;
24 import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.INeutronSubnetCRUD;
25 import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.NeutronCRUDInterfaces;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.Dhcpv6Base;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.Dhcpv6Off;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.Dhcpv6Slaac;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.Dhcpv6Stateful;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.Dhcpv6Stateless;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.IpVersionBase;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.IpVersionV4;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.IpVersionV6;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnet.attributes.AllocationPools;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnet.attributes.AllocationPoolsBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.Subnets;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.Subnet;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.SubnetBuilder;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.osgi.framework.BundleContext;
43 import org.osgi.framework.ServiceRegistration;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
47 import com.google.common.collect.ImmutableBiMap;
49 public class NeutronSubnetInterface extends AbstractNeutronInterface<Subnet, NeutronSubnet> implements INeutronSubnetCRUD {
50 private static final Logger LOGGER = LoggerFactory.getLogger(NeutronSubnetInterface.class);
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)
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 NeutronSubnetInterface(ProviderContext providerContext) {
67 super(providerContext);
70 // IfNBSubnetCRUD methods
73 public boolean subnetExists(String uuid) {
74 Subnet subnet = readMd(createInstanceIdentifier(toMd(uuid)));
82 public NeutronSubnet getSubnet(String uuid) {
83 Subnet subnet = readMd(createInstanceIdentifier(toMd(uuid)));
87 return fromMd(subnet);
91 public List<NeutronSubnet> getAllSubnets() {
92 Set<NeutronSubnet> allSubnets = new HashSet<>();
93 Subnets subnets = readMd(createInstanceIdentifier());
94 if (subnets != null) {
95 for (Subnet subnet: subnets.getSubnet()) {
96 allSubnets.add(fromMd(subnet));
99 LOGGER.debug("Exiting getAllSubnets, Found {} OpenStackSubnets", allSubnets.size());
100 List<NeutronSubnet> ans = new ArrayList<>();
101 ans.addAll(allSubnets);
106 public boolean addSubnet(NeutronSubnet input) {
107 String id = input.getID();
108 if (subnetExists(id)) {
112 NeutronCRUDInterfaces interfaces = new NeutronCRUDInterfaces()
113 .fetchINeutronNetworkCRUD(this);
114 INeutronNetworkCRUD networkIf = interfaces.getNetworkInterface();
116 NeutronNetwork targetNet = networkIf.getNetwork(input.getNetworkUUID());
117 targetNet.addSubnet(id);
122 public boolean removeSubnet(String uuid) {
123 NeutronSubnet target = getSubnet(uuid);
124 if (target == null) {
127 removeMd(toMd(uuid));
128 NeutronCRUDInterfaces interfaces = new NeutronCRUDInterfaces()
129 .fetchINeutronNetworkCRUD(this);
130 INeutronNetworkCRUD networkIf = interfaces.getNetworkInterface();
132 NeutronNetwork targetNet = networkIf.getNetwork(target.getNetworkUUID());
133 targetNet.removeSubnet(uuid);
138 public boolean updateSubnet(String uuid, NeutronSubnet delta) {
139 if (!subnetExists(uuid)) {
142 /* note: because what we get is *not* a delta but (at this point) the updated
143 * object, this is much simpler - just replace the value and update the mdsal
149 // note: this is being set to false in preparation for deprecation and removal
151 public boolean subnetInUse(String subnetUUID) {
155 protected NeutronSubnet fromMd(Subnet subnet) {
156 NeutronSubnet result = new NeutronSubnet();
157 result.setName(subnet.getName());
158 result.setTenantID(String.valueOf(subnet.getTenantId().getValue()).replace("-",""));
159 result.setNetworkUUID(subnet.getNetworkId().getValue());
160 result.setIpVersion(IPV_MAP.get(subnet.getIpVersion()));
161 result.setCidr(subnet.getCidr());
162 result.setGatewayIP(String.valueOf(subnet.getGatewayIp().getValue()));
163 result.setIpV6RaMode(DHCPV6_MAP.get(subnet.getIpv6RaMode()));
164 result.setIpV6AddressMode(DHCPV6_MAP.get(subnet.getIpv6AddressMode()));
165 result.setEnableDHCP(subnet.isEnableDhcp());
166 if (subnet.getAllocationPools() != null) {
167 List<NeutronSubnetIPAllocationPool> allocationPools = new ArrayList<>();
168 for (AllocationPools allocationPool : subnet.getAllocationPools()) {
169 NeutronSubnetIPAllocationPool pool = new NeutronSubnetIPAllocationPool();
170 pool.setPoolStart(allocationPool.getStart());
171 pool.setPoolEnd(allocationPool.getEnd());
172 allocationPools.add(pool);
174 result.setAllocationPools(allocationPools);
176 if (subnet.getDnsNameservers() != null) {
177 List<String> dnsNameServers = new ArrayList<>();
178 for (IpAddress dnsNameServer : subnet.getDnsNameservers()) {
179 dnsNameServers.add(String.valueOf(dnsNameServer.getValue()));
181 result.setDnsNameservers(dnsNameServers);
183 result.setID(subnet.getUuid().getValue());
184 // read through the ports and put the ones in this subnet into the internal
186 // @deprecated and will be removed in Boron
187 Set<NeutronPort> allPorts = new HashSet<>();
188 NeutronCRUDInterfaces interfaces = new NeutronCRUDInterfaces()
189 .fetchINeutronPortCRUD(this);
190 INeutronPortCRUD portIf = interfaces.getPortInterface();
191 for (NeutronPort port : portIf.getAllPorts()) {
192 if (port.getFixedIPs() != null) {
193 for (Neutron_IPs ip : port.getFixedIPs()) {
194 if (ip.getSubnetUUID().equals(result.getID())) {
200 List<NeutronPort> ports = new ArrayList<>();
201 ports.addAll(allPorts);
202 result.setPorts(ports);
206 protected Subnet toMd(NeutronSubnet subnet) {
207 SubnetBuilder subnetBuilder = new SubnetBuilder();
208 if (subnet.getName() != null) {
209 subnetBuilder.setName(subnet.getName());
211 if (subnet.getTenantID() != null) {
212 subnetBuilder.setTenantId(toUuid(subnet.getTenantID()));
214 if (subnet.getNetworkUUID() != null) {
215 subnetBuilder.setNetworkId(toUuid(subnet.getNetworkUUID()));
217 if (subnet.getIpVersion() != null) {
218 ImmutableBiMap<Integer, Class<? extends IpVersionBase>> mapper =
220 subnetBuilder.setIpVersion(mapper.get(subnet
223 if (subnet.getCidr() != null) {
224 subnetBuilder.setCidr(subnet.getCidr());
226 if (subnet.getGatewayIP() != null) {
227 IpAddress ipAddress = new IpAddress(subnet.getGatewayIP()
229 subnetBuilder.setGatewayIp(ipAddress);
231 if (subnet.getIpV6RaMode() != null) {
232 ImmutableBiMap<String, Class<? extends Dhcpv6Base>> mapper =
233 DHCPV6_MAP.inverse();
234 subnetBuilder.setIpv6RaMode(mapper.get(subnet.getIpV6RaMode()));
236 if (subnet.getIpV6AddressMode() != null) {
237 ImmutableBiMap<String, Class<? extends Dhcpv6Base>> mapper =
238 DHCPV6_MAP.inverse();
239 subnetBuilder.setIpv6AddressMode(mapper.get(subnet.getIpV6AddressMode()));
241 subnetBuilder.setEnableDhcp(subnet.getEnableDHCP());
242 if (subnet.getAllocationPools() != null) {
243 List<AllocationPools> allocationPools = new ArrayList<>();
244 for (NeutronSubnetIPAllocationPool allocationPool : subnet
245 .getAllocationPools()) {
246 AllocationPoolsBuilder builder = new AllocationPoolsBuilder();
247 builder.setStart(allocationPool.getPoolStart());
248 builder.setEnd(allocationPool.getPoolEnd());
249 AllocationPools temp = builder.build();
250 allocationPools.add(temp);
252 subnetBuilder.setAllocationPools(allocationPools);
254 if (subnet.getDnsNameservers() != null) {
255 List<IpAddress> dnsNameServers = new ArrayList<>();
256 for (String dnsNameServer : subnet.getDnsNameservers()) {
257 IpAddress ipAddress = new IpAddress(dnsNameServer.toCharArray());
258 dnsNameServers.add(ipAddress);
260 subnetBuilder.setDnsNameservers(dnsNameServers);
262 if (subnet.getID() != null) {
263 subnetBuilder.setUuid(toUuid(subnet.getID()));
265 LOGGER.warn("Attempting to write neutron subnet without UUID");
267 return subnetBuilder.build();
271 protected InstanceIdentifier<Subnet> createInstanceIdentifier(Subnet subnet) {
272 return InstanceIdentifier.create(Neutron.class).child(Subnets.class)
273 .child(Subnet.class, subnet.getKey());
276 protected InstanceIdentifier<Subnets> createInstanceIdentifier() {
277 return InstanceIdentifier.create(Neutron.class)
278 .child(Subnets.class);
282 protected Subnet toMd(String uuid) {
283 SubnetBuilder subnetBuilder = new SubnetBuilder();
284 subnetBuilder.setUuid(toUuid(uuid));
285 return subnetBuilder.build();
288 public static void registerNewInterface(BundleContext context,
289 ProviderContext providerContext,
290 List<ServiceRegistration<?>> registrations) {
291 NeutronSubnetInterface neutronSubnetInterface = new NeutronSubnetInterface(providerContext);
292 ServiceRegistration<INeutronSubnetCRUD> neutronSubnetInterfaceRegistration = context.registerService(INeutronSubnetCRUD.class, neutronSubnetInterface, null);
293 if(neutronSubnetInterfaceRegistration != null) {
294 registrations.add(neutronSubnetInterfaceRegistration);