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.neutron.transcriber;
11 import java.util.ArrayList;
12 import java.util.HashSet;
13 import java.util.List;
14 import java.util.Map.Entry;
17 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
18 import org.opendaylight.neutron.spi.INeutronNetworkCRUD;
19 import org.opendaylight.neutron.spi.INeutronPortCRUD;
20 import org.opendaylight.neutron.spi.INeutronSubnetCRUD;
21 import org.opendaylight.neutron.spi.Neutron_IPs;
22 import org.opendaylight.neutron.spi.NeutronCRUDInterfaces;
23 import org.opendaylight.neutron.spi.NeutronNetwork;
24 import org.opendaylight.neutron.spi.NeutronPort;
25 import org.opendaylight.neutron.spi.NeutronSubnet;
26 import org.opendaylight.neutron.spi.NeutronSubnetIPAllocationPool;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev160807.Dhcpv6Base;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev160807.Dhcpv6Off;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev160807.Dhcpv6Slaac;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev160807.Dhcpv6Stateful;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev160807.Dhcpv6Stateless;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev160807.IpVersionBase;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev160807.IpVersionV4;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev160807.IpVersionV6;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150325.Neutron;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev141002.subnet.attributes.AllocationPools;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev141002.subnet.attributes.AllocationPoolsBuilder;
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.yang.gen.v1.urn.opendaylight.neutron.subnets.rev141002.subnets.attributes.subnets.SubnetBuilder;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import org.osgi.framework.BundleContext;
44 import org.osgi.framework.ServiceRegistration;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
48 import com.google.common.collect.ImmutableBiMap;
50 public class NeutronSubnetInterface extends AbstractNeutronInterface<Subnet, NeutronSubnet> implements INeutronSubnetCRUD {
51 private static final Logger LOGGER = LoggerFactory.getLogger(NeutronSubnetInterface.class);
53 private static final ImmutableBiMap<Class<? extends IpVersionBase>,Integer> IPV_MAP
54 = new ImmutableBiMap.Builder<Class<? extends IpVersionBase>,Integer>()
55 .put(IpVersionV4.class,Integer.valueOf(4))
56 .put(IpVersionV6.class,Integer.valueOf(6))
59 private static final ImmutableBiMap<Class<? extends Dhcpv6Base>,String> DHCPV6_MAP
60 = new ImmutableBiMap.Builder<Class<? extends Dhcpv6Base>,String>()
61 .put(Dhcpv6Off.class,"off")
62 .put(Dhcpv6Stateful.class,"dhcpv6-stateful")
63 .put(Dhcpv6Slaac.class,"slaac")
64 .put(Dhcpv6Stateless.class,"dhcpv6-stateless")
67 NeutronSubnetInterface(ProviderContext providerContext) {
68 super(providerContext);
71 // IfNBSubnetCRUD methods
74 public boolean subnetExists(String uuid) {
75 Subnet subnet = readMd(createInstanceIdentifier(toMd(uuid)));
83 public NeutronSubnet getSubnet(String uuid) {
84 Subnet subnet = readMd(createInstanceIdentifier(toMd(uuid)));
88 return fromMd(subnet);
92 public List<NeutronSubnet> getAllSubnets() {
93 Set<NeutronSubnet> allSubnets = new HashSet<NeutronSubnet>();
94 Subnets subnets = readMd(createInstanceIdentifier());
95 if (subnets != null) {
96 for (Subnet subnet: subnets.getSubnet()) {
97 allSubnets.add(fromMd(subnet));
100 LOGGER.debug("Exiting getAllSubnets, Found {} OpenStackSubnets", allSubnets.size());
101 List<NeutronSubnet> ans = new ArrayList<NeutronSubnet>();
102 ans.addAll(allSubnets);
107 public boolean addSubnet(NeutronSubnet input) {
108 String id = input.getID();
109 if (subnetExists(id)) {
113 NeutronCRUDInterfaces interfaces = new NeutronCRUDInterfaces()
114 .fetchINeutronNetworkCRUD(this);
115 INeutronNetworkCRUD networkIf = interfaces.getNetworkInterface();
117 NeutronNetwork targetNet = networkIf.getNetwork(input.getNetworkUUID());
118 targetNet.addSubnet(id);
123 public boolean removeSubnet(String uuid) {
124 NeutronSubnet target = getSubnet(uuid);
125 if (target == null) {
128 removeMd(toMd(uuid));
129 NeutronCRUDInterfaces interfaces = new NeutronCRUDInterfaces()
130 .fetchINeutronNetworkCRUD(this);
131 INeutronNetworkCRUD networkIf = interfaces.getNetworkInterface();
133 NeutronNetwork targetNet = networkIf.getNetwork(target.getNetworkUUID());
134 targetNet.removeSubnet(uuid);
139 public boolean updateSubnet(String uuid, NeutronSubnet delta) {
140 if (!subnetExists(uuid)) {
143 /* note: because what we get is *not* a delta but (at this point) the updated
144 * object, this is much simpler - just replace the value and update the mdsal
150 // note: this is being set to false in preparation for deprecation and removal
152 public boolean subnetInUse(String subnetUUID) {
156 protected NeutronSubnet fromMd(Subnet subnet) {
157 NeutronSubnet result = new NeutronSubnet();
158 result.setName(subnet.getName());
159 result.setTenantID(String.valueOf(subnet.getTenantId().getValue()).replace("-",""));
160 result.setNetworkUUID(subnet.getNetworkId().getValue());
161 result.setIpVersion(IPV_MAP.get(subnet.getIpVersion()));
162 result.setCidr(subnet.getCidr());
163 result.setGatewayIP(String.valueOf(subnet.getGatewayIp().getValue()));
164 result.setIpV6RaMode(DHCPV6_MAP.get(subnet.getIpv6RaMode()));
165 result.setIpV6AddressMode(DHCPV6_MAP.get(subnet.getIpv6AddressMode()));
166 result.setEnableDHCP(subnet.isEnableDhcp());
167 if (subnet.getAllocationPools() != null) {
168 List<NeutronSubnetIPAllocationPool> allocationPools = new ArrayList<NeutronSubnetIPAllocationPool>();
169 for (AllocationPools allocationPool : subnet.getAllocationPools()) {
170 NeutronSubnetIPAllocationPool pool = new NeutronSubnetIPAllocationPool();
171 pool.setPoolStart(allocationPool.getStart());
172 pool.setPoolEnd(allocationPool.getEnd());
173 allocationPools.add(pool);
175 result.setAllocationPools(allocationPools);
177 if (subnet.getDnsNameservers() != null) {
178 List<String> dnsNameServers = new ArrayList<String>();
179 for (IpAddress dnsNameServer : subnet.getDnsNameservers()) {
180 dnsNameServers.add(String.valueOf(dnsNameServer.getValue()));
182 result.setDnsNameservers(dnsNameServers);
184 result.setID(subnet.getUuid().getValue());
185 // read through the ports and put the ones in this subnet into the internal
187 // @deprecated and will be removed in Boron
188 Set<NeutronPort> allPorts = new HashSet<NeutronPort>();
189 NeutronCRUDInterfaces interfaces = new NeutronCRUDInterfaces()
190 .fetchINeutronPortCRUD(this);
191 INeutronPortCRUD portIf = interfaces.getPortInterface();
192 for (NeutronPort port : portIf.getAllPorts()) {
193 if (port.getFixedIPs() != null) {
194 for (Neutron_IPs ip : port.getFixedIPs()) {
195 if (ip.getSubnetUUID().equals(result.getID())) {
201 List<NeutronPort> ports = new ArrayList<NeutronPort>();
202 ports.addAll(allPorts);
203 result.setPorts(ports);
207 protected Subnet toMd(NeutronSubnet subnet) {
208 SubnetBuilder subnetBuilder = new SubnetBuilder();
209 if (subnet.getName() != null) {
210 subnetBuilder.setName(subnet.getName());
212 if (subnet.getTenantID() != null) {
213 subnetBuilder.setTenantId(toUuid(subnet.getTenantID()));
215 if (subnet.getNetworkUUID() != null) {
216 subnetBuilder.setNetworkId(toUuid(subnet.getNetworkUUID()));
218 if (subnet.getIpVersion() != null) {
219 ImmutableBiMap<Integer, Class<? extends IpVersionBase>> mapper =
221 subnetBuilder.setIpVersion((Class<? extends IpVersionBase>) mapper.get(subnet
224 if (subnet.getCidr() != null) {
225 subnetBuilder.setCidr(subnet.getCidr());
227 if (subnet.getGatewayIP() != null) {
228 IpAddress ipAddress = new IpAddress(subnet.getGatewayIP()
230 subnetBuilder.setGatewayIp(ipAddress);
232 if (subnet.getIpV6RaMode() != null) {
233 ImmutableBiMap<String, Class<? extends Dhcpv6Base>> mapper =
234 DHCPV6_MAP.inverse();
235 subnetBuilder.setIpv6RaMode((Class<? extends Dhcpv6Base>) mapper.get(subnet.getIpV6RaMode()));
237 if (subnet.getIpV6AddressMode() != null) {
238 ImmutableBiMap<String, Class<? extends Dhcpv6Base>> mapper =
239 DHCPV6_MAP.inverse();
240 subnetBuilder.setIpv6AddressMode((Class<? extends Dhcpv6Base>) mapper.get(subnet.getIpV6AddressMode()));
242 subnetBuilder.setEnableDhcp(subnet.getEnableDHCP());
243 if (subnet.getAllocationPools() != null) {
244 List<AllocationPools> allocationPools = new ArrayList<AllocationPools>();
245 for (NeutronSubnetIPAllocationPool allocationPool : subnet
246 .getAllocationPools()) {
247 AllocationPoolsBuilder builder = new AllocationPoolsBuilder();
248 builder.setStart(allocationPool.getPoolStart());
249 builder.setEnd(allocationPool.getPoolEnd());
250 AllocationPools temp = builder.build();
251 allocationPools.add(temp);
253 subnetBuilder.setAllocationPools(allocationPools);
255 if (subnet.getDnsNameservers() != null) {
256 List<IpAddress> dnsNameServers = new ArrayList<IpAddress>();
257 for (String dnsNameServer : subnet.getDnsNameservers()) {
258 IpAddress ipAddress = new IpAddress(dnsNameServer.toCharArray());
259 dnsNameServers.add(ipAddress);
261 subnetBuilder.setDnsNameservers(dnsNameServers);
263 if (subnet.getID() != null) {
264 subnetBuilder.setUuid(toUuid(subnet.getID()));
266 LOGGER.warn("Attempting to write neutron subnet without UUID");
268 return subnetBuilder.build();
272 protected InstanceIdentifier<Subnet> createInstanceIdentifier(Subnet subnet) {
273 return InstanceIdentifier.create(Neutron.class).child(Subnets.class)
274 .child(Subnet.class, subnet.getKey());
277 protected InstanceIdentifier<Subnets> createInstanceIdentifier() {
278 return InstanceIdentifier.create(Neutron.class)
279 .child(Subnets.class);
283 protected Subnet toMd(String uuid) {
284 SubnetBuilder subnetBuilder = new SubnetBuilder();
285 subnetBuilder.setUuid(toUuid(uuid));
286 return subnetBuilder.build();
289 public static void registerNewInterface(BundleContext context,
290 ProviderContext providerContext,
291 List<ServiceRegistration<?>> registrations) {
292 NeutronSubnetInterface neutronSubnetInterface = new NeutronSubnetInterface(providerContext);
293 ServiceRegistration<INeutronSubnetCRUD> neutronSubnetInterfaceRegistration = context.registerService(INeutronSubnetCRUD.class, neutronSubnetInterface, null);
294 if(neutronSubnetInterfaceRegistration != null) {
295 registrations.add(neutronSubnetInterfaceRegistration);