Migrate to stringValue()
[neutron.git] / transcriber / src / main / java / org / opendaylight / neutron / transcriber / NeutronSubnetInterface.java
1 /*
2  * Copyright (c) 2013, 2015 IBM Corporation and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.neutron.transcriber;
9
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;
41
42 @Singleton
43 @OsgiServiceProvider(classes = INeutronSubnetCRUD.class)
44 public final class NeutronSubnetInterface extends AbstractNeutronInterface<Subnet, Subnets, SubnetKey, NeutronSubnet>
45         implements INeutronSubnetCRUD {
46
47     private static final int IPV4_VERSION = 4;
48     private static final int IPV6_VERSION = 6;
49
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();
54
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();
59
60     @Inject
61     public NeutronSubnetInterface(DataBroker db) {
62         super(SubnetBuilder.class, db);
63     }
64
65     // IfNBSubnetCRUD methods
66
67     @Override
68     protected List<Subnet> getDataObjectList(Subnets subnets) {
69         return subnets.getSubnet();
70     }
71
72     @Override
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());
81         }
82         if (subnet.getIpv6RaMode() != null) {
83             result.setIpV6RaMode(DHCPV6_MAP.get(subnet.getIpv6RaMode()));
84         }
85         if (subnet.getIpv6AddressMode() != null) {
86             result.setIpV6AddressMode(DHCPV6_MAP.get(subnet.getIpv6AddressMode()));
87         }
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);
96             }
97             result.setAllocationPools(allocationPools);
98         }
99         if (subnet.getDnsNameservers() != null) {
100             final List<String> dnsNameServers = new ArrayList<>();
101             for (final IpAddress dnsNameServer : subnet.getDnsNameservers()) {
102                 dnsNameServers.add(dnsNameServer.stringValue());
103             }
104             result.setDnsNameservers(dnsNameServers);
105         }
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);
113             }
114             result.setHostRoutes(hostRoutes);
115         }
116         return result;
117     }
118
119     @Override
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()));
125         }
126         if (subnet.getIpVersion() != null) {
127             final ImmutableBiMap<Integer, Class<? extends IpVersionBase>> mapper = IPV_MAP.inverse();
128             subnetBuilder.setIpVersion(mapper.get(subnet.getIpVersion()));
129         }
130         if (subnet.getCidr() != null) {
131             final IpPrefix ipPrefix = IpPrefixBuilder.getDefaultInstance(subnet.getCidr());
132             subnetBuilder.setCidr(ipPrefix);
133         }
134         if (subnet.getGatewayIp() != null) {
135             final IpAddress ipAddress = IpAddressBuilder.getDefaultInstance(subnet.getGatewayIp());
136             subnetBuilder.setGatewayIp(ipAddress);
137         }
138         if (subnet.getIpV6RaMode() != null) {
139             final ImmutableBiMap<String, Class<? extends Dhcpv6Base>> mapper = DHCPV6_MAP.inverse();
140             subnetBuilder.setIpv6RaMode(mapper.get(subnet.getIpV6RaMode()));
141         }
142         if (subnet.getIpV6AddressMode() != null) {
143             final ImmutableBiMap<String, Class<? extends Dhcpv6Base>> mapper = DHCPV6_MAP.inverse();
144             subnetBuilder.setIpv6AddressMode(mapper.get(subnet.getIpV6AddressMode()));
145         }
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);
155             }
156             subnetBuilder.setAllocationPools(allocationPools);
157         }
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);
163             }
164             subnetBuilder.setDnsNameservers(dnsNameServers);
165         }
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());
173             }
174             subnetBuilder.setHostRoutes(hostRoutes);
175         }
176         return subnetBuilder.build();
177     }
178 }