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
8 package org.opendaylight.neutron.transcriber;
10 import com.google.common.collect.ImmutableBiMap;
11 import java.util.ArrayList;
12 import java.util.List;
13 import javax.inject.Inject;
14 import javax.inject.Singleton;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.neutron.spi.INeutronSubnetCRUD;
17 import org.opendaylight.neutron.spi.NeutronRoute;
18 import org.opendaylight.neutron.spi.NeutronSubnet;
19 import org.opendaylight.neutron.spi.NeutronSubnetIpAllocationPool;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressBuilder;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefixBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.Dhcpv6Base;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.Dhcpv6Off;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.Dhcpv6Slaac;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.Dhcpv6Stateful;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.Dhcpv6Stateless;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.IpVersionBase;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.IpVersionV4;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.IpVersionV6;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnet.attributes.AllocationPools;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnet.attributes.AllocationPoolsBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnet.attributes.HostRoutes;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnet.attributes.HostRoutesBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.Subnets;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.Subnet;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.SubnetBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.SubnetKey;
40 import org.ops4j.pax.cdi.api.OsgiServiceProvider;
43 @OsgiServiceProvider(classes = INeutronSubnetCRUD.class)
44 public final class NeutronSubnetInterface extends AbstractNeutronInterface<Subnet, Subnets, SubnetKey, NeutronSubnet>
45 implements INeutronSubnetCRUD {
47 private static final int IPV4_VERSION = 4;
48 private static final int IPV6_VERSION = 6;
50 private static final ImmutableBiMap<Class<? extends IpVersionBase>,
51 Integer> IPV_MAP = new ImmutableBiMap.Builder<Class<? extends IpVersionBase>, Integer>()
52 .put(IpVersionV4.class, Integer.valueOf(IPV4_VERSION))
53 .put(IpVersionV6.class, Integer.valueOf(IPV6_VERSION)).build();
55 private static final ImmutableBiMap<Class<? extends Dhcpv6Base>,
56 String> DHCPV6_MAP = new ImmutableBiMap.Builder<Class<? extends Dhcpv6Base>, String>()
57 .put(Dhcpv6Off.class, "off").put(Dhcpv6Stateful.class, "dhcpv6-stateful")
58 .put(Dhcpv6Slaac.class, "slaac").put(Dhcpv6Stateless.class, "dhcpv6-stateless").build();
61 public NeutronSubnetInterface(DataBroker db) {
62 super(SubnetBuilder.class, db);
65 // IfNBSubnetCRUD methods
68 protected List<Subnet> getDataObjectList(Subnets subnets) {
69 return subnets.getSubnet();
73 protected NeutronSubnet fromMd(Subnet subnet) {
74 final NeutronSubnet result = new NeutronSubnet();
75 fromMdBaseAttributes(subnet, result);
76 result.setNetworkUUID(subnet.getNetworkId().getValue());
77 result.setIpVersion(IPV_MAP.get(subnet.getIpVersion()));
78 result.setCidr(subnet.getCidr().stringValue());
79 if (subnet.getGatewayIp() != null) {
80 result.setGatewayIp(subnet.getGatewayIp().stringValue());
82 if (subnet.getIpv6RaMode() != null) {
83 result.setIpV6RaMode(DHCPV6_MAP.get(subnet.getIpv6RaMode()));
85 if (subnet.getIpv6AddressMode() != null) {
86 result.setIpV6AddressMode(DHCPV6_MAP.get(subnet.getIpv6AddressMode()));
88 result.setEnableDHCP(subnet.isEnableDhcp());
89 if (subnet.getAllocationPools() != null) {
90 final List<NeutronSubnetIpAllocationPool> allocationPools = new ArrayList<>();
91 for (final AllocationPools allocationPool : subnet.getAllocationPools()) {
92 final NeutronSubnetIpAllocationPool pool = new NeutronSubnetIpAllocationPool();
93 pool.setPoolStart(allocationPool.getStart().stringValue());
94 pool.setPoolEnd(allocationPool.getEnd().stringValue());
95 allocationPools.add(pool);
97 result.setAllocationPools(allocationPools);
99 if (subnet.getDnsNameservers() != null) {
100 final List<String> dnsNameServers = new ArrayList<>();
101 for (final IpAddress dnsNameServer : subnet.getDnsNameservers()) {
102 dnsNameServers.add(dnsNameServer.stringValue());
104 result.setDnsNameservers(dnsNameServers);
106 if (subnet.getHostRoutes() != null) {
107 final List<NeutronRoute> hostRoutes = new ArrayList<>();
108 for (final HostRoutes hostRoute : subnet.getHostRoutes()) {
109 final NeutronRoute nsHostRoute = new NeutronRoute();
110 nsHostRoute.setDestination(hostRoute.getDestination().stringValue());
111 nsHostRoute.setNextHop(hostRoute.getNexthop().stringValue());
112 hostRoutes.add(nsHostRoute);
114 result.setHostRoutes(hostRoutes);
120 protected Subnet toMd(NeutronSubnet subnet) {
121 final SubnetBuilder subnetBuilder = new SubnetBuilder();
122 toMdBaseAttributes(subnet, subnetBuilder);
123 if (subnet.getNetworkUUID() != null) {
124 subnetBuilder.setNetworkId(toUuid(subnet.getNetworkUUID()));
126 if (subnet.getIpVersion() != null) {
127 final ImmutableBiMap<Integer, Class<? extends IpVersionBase>> mapper = IPV_MAP.inverse();
128 subnetBuilder.setIpVersion(mapper.get(subnet.getIpVersion()));
130 if (subnet.getCidr() != null) {
131 final IpPrefix ipPrefix = IpPrefixBuilder.getDefaultInstance(subnet.getCidr());
132 subnetBuilder.setCidr(ipPrefix);
134 if (subnet.getGatewayIp() != null) {
135 final IpAddress ipAddress = IpAddressBuilder.getDefaultInstance(subnet.getGatewayIp());
136 subnetBuilder.setGatewayIp(ipAddress);
138 if (subnet.getIpV6RaMode() != null) {
139 final ImmutableBiMap<String, Class<? extends Dhcpv6Base>> mapper = DHCPV6_MAP.inverse();
140 subnetBuilder.setIpv6RaMode(mapper.get(subnet.getIpV6RaMode()));
142 if (subnet.getIpV6AddressMode() != null) {
143 final ImmutableBiMap<String, Class<? extends Dhcpv6Base>> mapper = DHCPV6_MAP.inverse();
144 subnetBuilder.setIpv6AddressMode(mapper.get(subnet.getIpV6AddressMode()));
146 subnetBuilder.setEnableDhcp(subnet.getEnableDHCP());
147 if (subnet.getAllocationPools() != null) {
148 final List<AllocationPools> allocationPools = new ArrayList<>();
149 for (final NeutronSubnetIpAllocationPool allocationPool : subnet.getAllocationPools()) {
150 final AllocationPoolsBuilder builder = new AllocationPoolsBuilder();
151 builder.setStart(IpAddressBuilder.getDefaultInstance(allocationPool.getPoolStart()));
152 builder.setEnd(IpAddressBuilder.getDefaultInstance(allocationPool.getPoolEnd()));
153 final AllocationPools temp = builder.build();
154 allocationPools.add(temp);
156 subnetBuilder.setAllocationPools(allocationPools);
158 if (subnet.getDnsNameservers() != null) {
159 final List<IpAddress> dnsNameServers = new ArrayList<>();
160 for (final String dnsNameServer : subnet.getDnsNameservers()) {
161 final IpAddress ipAddress = IpAddressBuilder.getDefaultInstance(dnsNameServer);
162 dnsNameServers.add(ipAddress);
164 subnetBuilder.setDnsNameservers(dnsNameServers);
166 if (subnet.getHostRoutes() != null) {
167 final List<HostRoutes> hostRoutes = new ArrayList<>();
168 for (final NeutronRoute hostRoute : subnet.getHostRoutes()) {
169 final HostRoutesBuilder hrBuilder = new HostRoutesBuilder();
170 hrBuilder.setDestination(IpPrefixBuilder.getDefaultInstance(hostRoute.getDestination()));
171 hrBuilder.setNexthop(IpAddressBuilder.getDefaultInstance(hostRoute.getNextHop()));
172 hostRoutes.add(hrBuilder.build());
174 subnetBuilder.setHostRoutes(hostRoutes);
176 return subnetBuilder.build();