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 com.google.common.collect.ImmutableBiMap;
12 import java.util.ArrayList;
13 import java.util.List;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.neutron.spi.INeutronSubnetCRUD;
16 import org.opendaylight.neutron.spi.NeutronRoute;
17 import org.opendaylight.neutron.spi.NeutronSubnet;
18 import org.opendaylight.neutron.spi.NeutronSubnetIpAllocationPool;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefixBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.Dhcpv6Base;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.Dhcpv6Off;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.Dhcpv6Slaac;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.Dhcpv6Stateful;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.Dhcpv6Stateless;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.IpVersionBase;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.IpVersionV4;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.IpVersionV6;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnet.attributes.AllocationPools;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnet.attributes.AllocationPoolsBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnet.attributes.HostRoutes;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnet.attributes.HostRoutesBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.Subnets;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.Subnet;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.SubnetBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.SubnetKey;
39 public final class NeutronSubnetInterface extends AbstractNeutronInterface<Subnet, Subnets, SubnetKey, NeutronSubnet>
40 implements INeutronSubnetCRUD {
41 private static final int IPV4_VERSION = 4;
42 private static final int IPV6_VERSION = 6;
44 private static final ImmutableBiMap<Class<? extends IpVersionBase>,
45 Integer> IPV_MAP = new ImmutableBiMap.Builder<Class<? extends IpVersionBase>, Integer>()
46 .put(IpVersionV4.class, Integer.valueOf(IPV4_VERSION))
47 .put(IpVersionV6.class, Integer.valueOf(IPV6_VERSION)).build();
49 private static final ImmutableBiMap<Class<? extends Dhcpv6Base>,
50 String> DHCPV6_MAP = new ImmutableBiMap.Builder<Class<? extends Dhcpv6Base>, String>()
51 .put(Dhcpv6Off.class, "off").put(Dhcpv6Stateful.class, "dhcpv6-stateful")
52 .put(Dhcpv6Slaac.class, "slaac").put(Dhcpv6Stateless.class, "dhcpv6-stateless").build();
54 NeutronSubnetInterface(DataBroker db) {
55 super(SubnetBuilder.class, db);
58 // IfNBSubnetCRUD methods
61 protected List<Subnet> getDataObjectList(Subnets subnets) {
62 return subnets.getSubnet();
65 protected NeutronSubnet fromMd(Subnet subnet) {
66 final NeutronSubnet result = new NeutronSubnet();
67 fromMdBaseAttributes(subnet, result);
68 result.setNetworkUUID(subnet.getNetworkId().getValue());
69 result.setIpVersion(IPV_MAP.get(subnet.getIpVersion()));
70 result.setCidr(String.valueOf(subnet.getCidr().getValue()));
71 if (subnet.getGatewayIp() != null) {
72 result.setGatewayIp(String.valueOf(subnet.getGatewayIp().getValue()));
74 if (subnet.getIpv6RaMode() != null) {
75 result.setIpV6RaMode(DHCPV6_MAP.get(subnet.getIpv6RaMode()));
77 if (subnet.getIpv6AddressMode() != null) {
78 result.setIpV6AddressMode(DHCPV6_MAP.get(subnet.getIpv6AddressMode()));
80 result.setEnableDHCP(subnet.isEnableDhcp());
81 if (subnet.getAllocationPools() != null) {
82 final List<NeutronSubnetIpAllocationPool> allocationPools = new ArrayList<>();
83 for (final AllocationPools allocationPool : subnet.getAllocationPools()) {
84 final NeutronSubnetIpAllocationPool pool = new NeutronSubnetIpAllocationPool();
85 pool.setPoolStart(String.valueOf(allocationPool.getStart().getValue()));
86 pool.setPoolEnd(String.valueOf(allocationPool.getEnd().getValue()));
87 allocationPools.add(pool);
89 result.setAllocationPools(allocationPools);
91 if (subnet.getDnsNameservers() != null) {
92 final List<String> dnsNameServers = new ArrayList<>();
93 for (final IpAddress dnsNameServer : subnet.getDnsNameservers()) {
94 dnsNameServers.add(String.valueOf(dnsNameServer.getValue()));
96 result.setDnsNameservers(dnsNameServers);
98 if (subnet.getHostRoutes() != null) {
99 final List<NeutronRoute> hostRoutes = new ArrayList<>();
100 for (final HostRoutes hostRoute : subnet.getHostRoutes()) {
101 final NeutronRoute nsHostRoute = new NeutronRoute();
102 nsHostRoute.setDestination(String.valueOf(hostRoute.getDestination().getValue()));
103 nsHostRoute.setNextHop(String.valueOf(hostRoute.getNexthop().getValue()));
104 hostRoutes.add(nsHostRoute);
106 result.setHostRoutes(hostRoutes);
111 protected Subnet toMd(NeutronSubnet subnet) {
112 final SubnetBuilder subnetBuilder = new SubnetBuilder();
113 toMdBaseAttributes(subnet, subnetBuilder);
114 if (subnet.getNetworkUUID() != null) {
115 subnetBuilder.setNetworkId(toUuid(subnet.getNetworkUUID()));
117 if (subnet.getIpVersion() != null) {
118 final ImmutableBiMap<Integer, Class<? extends IpVersionBase>> mapper = IPV_MAP.inverse();
119 subnetBuilder.setIpVersion((Class<? extends IpVersionBase>) mapper.get(subnet.getIpVersion()));
121 if (subnet.getCidr() != null) {
122 final IpPrefix ipPrefix = IpPrefixBuilder.getDefaultInstance(subnet.getCidr());
123 subnetBuilder.setCidr(ipPrefix);
125 if (subnet.getGatewayIp() != null) {
126 final IpAddress ipAddress = new IpAddress(subnet.getGatewayIp().toCharArray());
127 subnetBuilder.setGatewayIp(ipAddress);
129 if (subnet.getIpV6RaMode() != null) {
130 final ImmutableBiMap<String, Class<? extends Dhcpv6Base>> mapper = DHCPV6_MAP.inverse();
131 subnetBuilder.setIpv6RaMode((Class<? extends Dhcpv6Base>) mapper.get(subnet.getIpV6RaMode()));
133 if (subnet.getIpV6AddressMode() != null) {
134 final ImmutableBiMap<String, Class<? extends Dhcpv6Base>> mapper = DHCPV6_MAP.inverse();
135 subnetBuilder.setIpv6AddressMode((Class<? extends Dhcpv6Base>) mapper.get(subnet.getIpV6AddressMode()));
137 subnetBuilder.setEnableDhcp(subnet.getEnableDHCP());
138 if (subnet.getAllocationPools() != null) {
139 final List<AllocationPools> allocationPools = new ArrayList<AllocationPools>();
140 for (final NeutronSubnetIpAllocationPool allocationPool : subnet.getAllocationPools()) {
141 final AllocationPoolsBuilder builder = new AllocationPoolsBuilder();
142 builder.setStart(new IpAddress(allocationPool.getPoolStart().toCharArray()));
143 builder.setEnd(new IpAddress(allocationPool.getPoolEnd().toCharArray()));
144 final AllocationPools temp = builder.build();
145 allocationPools.add(temp);
147 subnetBuilder.setAllocationPools(allocationPools);
149 if (subnet.getDnsNameservers() != null) {
150 final List<IpAddress> dnsNameServers = new ArrayList<>();
151 for (final String dnsNameServer : subnet.getDnsNameservers()) {
152 final IpAddress ipAddress = new IpAddress(dnsNameServer.toCharArray());
153 dnsNameServers.add(ipAddress);
155 subnetBuilder.setDnsNameservers(dnsNameServers);
157 if (subnet.getHostRoutes() != null) {
158 final List<HostRoutes> hostRoutes = new ArrayList<HostRoutes>();
159 for (final NeutronRoute hostRoute : subnet.getHostRoutes()) {
160 final HostRoutesBuilder hrBuilder = new HostRoutesBuilder();
161 hrBuilder.setDestination(new IpPrefix(hostRoute.getDestination().toCharArray()));
162 hrBuilder.setNexthop(new IpAddress(hostRoute.getNextHop().toCharArray()));
163 hostRoutes.add(hrBuilder.build());
165 subnetBuilder.setHostRoutes(hostRoutes);
167 return subnetBuilder.build();