Merge "L3: Add eth to br-ex"
[ovsdb.git] / openstack / net-virt / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / translator / crud / impl / 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
9 package org.opendaylight.ovsdb.openstack.netvirt.translator.crud.impl;
10
11 import java.util.ArrayList;
12 import java.util.HashSet;
13 import java.util.List;
14 import java.util.Set;
15
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;
46
47 import com.google.common.collect.ImmutableBiMap;
48
49 public class NeutronSubnetInterface extends AbstractNeutronInterface<Subnet, NeutronSubnet> implements INeutronSubnetCRUD {
50     private static final Logger LOGGER = LoggerFactory.getLogger(NeutronSubnetInterface.class);
51
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)
56             .build();
57
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")
64             .build();
65
66     NeutronSubnetInterface(ProviderContext providerContext) {
67         super(providerContext);
68     }
69
70     // IfNBSubnetCRUD methods
71
72     @Override
73     public boolean subnetExists(String uuid) {
74         Subnet subnet = readMd(createInstanceIdentifier(toMd(uuid)));
75         if (subnet == null) {
76             return false;
77         }
78         return true;
79     }
80
81     @Override
82     public NeutronSubnet getSubnet(String uuid) {
83         Subnet subnet = readMd(createInstanceIdentifier(toMd(uuid)));
84         if (subnet == null) {
85             return null;
86         }
87         return fromMd(subnet);
88     }
89
90     @Override
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));
97             }
98         }
99         LOGGER.debug("Exiting getAllSubnets, Found {} OpenStackSubnets", allSubnets.size());
100         List<NeutronSubnet> ans = new ArrayList<>();
101         ans.addAll(allSubnets);
102         return ans;
103     }
104
105     @Override
106     public boolean addSubnet(NeutronSubnet input) {
107         String id = input.getID();
108         if (subnetExists(id)) {
109             return false;
110         }
111         addMd(input);
112         NeutronCRUDInterfaces interfaces = new NeutronCRUDInterfaces()
113             .fetchINeutronNetworkCRUD(this);
114         INeutronNetworkCRUD networkIf = interfaces.getNetworkInterface();
115
116         NeutronNetwork targetNet = networkIf.getNetwork(input.getNetworkUUID());
117         targetNet.addSubnet(id);
118         return true;
119     }
120
121     @Override
122     public boolean removeSubnet(String uuid) {
123         NeutronSubnet target = getSubnet(uuid);
124         if (target == null) {
125             return false;
126         }
127         removeMd(toMd(uuid));
128         NeutronCRUDInterfaces interfaces = new NeutronCRUDInterfaces()
129             .fetchINeutronNetworkCRUD(this);
130         INeutronNetworkCRUD networkIf = interfaces.getNetworkInterface();
131
132         NeutronNetwork targetNet = networkIf.getNetwork(target.getNetworkUUID());
133         targetNet.removeSubnet(uuid);
134         return true;
135     }
136
137     @Override
138     public boolean updateSubnet(String uuid, NeutronSubnet delta) {
139         if (!subnetExists(uuid)) {
140             return false;
141         }
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
144  * with it */
145         updateMd(delta);
146         return true;
147     }
148
149 // note: this is being set to false in preparation for deprecation and removal
150     @Override
151     public boolean subnetInUse(String subnetUUID) {
152         return false;
153     }
154
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);
173             }
174             result.setAllocationPools(allocationPools);
175         }
176         if (subnet.getDnsNameservers() != null) {
177             List<String> dnsNameServers = new ArrayList<>();
178             for (IpAddress dnsNameServer : subnet.getDnsNameservers()) {
179                 dnsNameServers.add(String.valueOf(dnsNameServer.getValue()));
180             }
181             result.setDnsNameservers(dnsNameServers);
182         }
183         result.setID(subnet.getUuid().getValue());
184 // read through the ports and put the ones in this subnet into the internal
185 // myPorts object.
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())) {
195                         allPorts.add(port);
196                     }
197                 }
198             }
199         }
200         List<NeutronPort> ports = new ArrayList<>();
201         ports.addAll(allPorts);
202         result.setPorts(ports);
203         return result;
204     }
205
206     protected Subnet toMd(NeutronSubnet subnet) {
207         SubnetBuilder subnetBuilder = new SubnetBuilder();
208         if (subnet.getName() != null) {
209             subnetBuilder.setName(subnet.getName());
210         }
211         if (subnet.getTenantID() != null) {
212             subnetBuilder.setTenantId(toUuid(subnet.getTenantID()));
213         }
214         if (subnet.getNetworkUUID() != null) {
215             subnetBuilder.setNetworkId(toUuid(subnet.getNetworkUUID()));
216         }
217         if (subnet.getIpVersion() != null) {
218             ImmutableBiMap<Integer, Class<? extends IpVersionBase>> mapper =
219                     IPV_MAP.inverse();
220             subnetBuilder.setIpVersion(mapper.get(subnet
221                     .getIpVersion()));
222         }
223         if (subnet.getCidr() != null) {
224             subnetBuilder.setCidr(subnet.getCidr());
225         }
226         if (subnet.getGatewayIP() != null) {
227             IpAddress ipAddress = new IpAddress(subnet.getGatewayIP()
228                     .toCharArray());
229             subnetBuilder.setGatewayIp(ipAddress);
230         }
231         if (subnet.getIpV6RaMode() != null) {
232             ImmutableBiMap<String, Class<? extends Dhcpv6Base>> mapper =
233                     DHCPV6_MAP.inverse();
234             subnetBuilder.setIpv6RaMode(mapper.get(subnet.getIpV6RaMode()));
235         }
236         if (subnet.getIpV6AddressMode() != null) {
237             ImmutableBiMap<String, Class<? extends Dhcpv6Base>> mapper =
238                     DHCPV6_MAP.inverse();
239             subnetBuilder.setIpv6AddressMode(mapper.get(subnet.getIpV6AddressMode()));
240         }
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);
251             }
252             subnetBuilder.setAllocationPools(allocationPools);
253         }
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);
259             }
260             subnetBuilder.setDnsNameservers(dnsNameServers);
261         }
262         if (subnet.getID() != null) {
263             subnetBuilder.setUuid(toUuid(subnet.getID()));
264         } else {
265             LOGGER.warn("Attempting to write neutron subnet without UUID");
266         }
267         return subnetBuilder.build();
268     }
269
270     @Override
271     protected InstanceIdentifier<Subnet> createInstanceIdentifier(Subnet subnet) {
272         return InstanceIdentifier.create(Neutron.class).child(Subnets.class)
273                 .child(Subnet.class, subnet.getKey());
274     }
275
276     protected InstanceIdentifier<Subnets> createInstanceIdentifier() {
277         return InstanceIdentifier.create(Neutron.class)
278                 .child(Subnets.class);
279     }
280
281     @Override
282     protected Subnet toMd(String uuid) {
283         SubnetBuilder subnetBuilder = new SubnetBuilder();
284         subnetBuilder.setUuid(toUuid(uuid));
285         return subnetBuilder.build();
286     }
287
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);
295         }
296     }
297 }