Add blueprint wiring for openstack/net-virt
[netvirt.git] / openstack / net-virt / src / main / java / org / opendaylight / netvirt / 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.netvirt.openstack.netvirt.translator.crud.impl;
10
11 import com.google.common.collect.ImmutableBiMap;
12 import java.util.ArrayList;
13 import java.util.HashSet;
14 import java.util.List;
15 import java.util.Set;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.netvirt.openstack.netvirt.translator.NeutronPort;
18 import org.opendaylight.netvirt.openstack.netvirt.translator.NeutronSubnet;
19 import org.opendaylight.netvirt.openstack.netvirt.translator.NeutronSubnetIPAllocationPool;
20 import org.opendaylight.netvirt.openstack.netvirt.translator.Neutron_IPs;
21 import org.opendaylight.netvirt.openstack.netvirt.translator.crud.INeutronPortCRUD;
22 import org.opendaylight.netvirt.openstack.netvirt.translator.crud.INeutronSubnetCRUD;
23 import org.opendaylight.netvirt.openstack.netvirt.translator.crud.NeutronCRUDInterfaces;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.Dhcpv6Base;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.Dhcpv6Off;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.Dhcpv6Slaac;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.Dhcpv6Stateful;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.Dhcpv6Stateless;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.IpVersionBase;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.IpVersionV4;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.IpVersionV6;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnet.attributes.AllocationPools;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnet.attributes.AllocationPoolsBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.Subnets;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.Subnet;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.SubnetBuilder;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.osgi.framework.BundleContext;
42 import org.osgi.framework.ServiceRegistration;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 public class NeutronSubnetInterface extends AbstractNeutronInterface<Subnet, NeutronSubnet> implements INeutronSubnetCRUD {
47     private static final Logger LOGGER = LoggerFactory.getLogger(NeutronSubnetInterface.class);
48
49     private static final ImmutableBiMap<Class<? extends IpVersionBase>,Integer> IPV_MAP
50             = new ImmutableBiMap.Builder<Class<? extends IpVersionBase>,Integer>()
51             .put(IpVersionV4.class, 4)
52             .put(IpVersionV6.class, 6)
53             .build();
54
55     private static final ImmutableBiMap<Class<? extends Dhcpv6Base>,String> DHCPV6_MAP
56             = new ImmutableBiMap.Builder<Class<? extends Dhcpv6Base>,String>()
57             .put(Dhcpv6Off.class,"off")
58             .put(Dhcpv6Stateful.class,"dhcpv6-stateful")
59             .put(Dhcpv6Slaac.class,"slaac")
60             .put(Dhcpv6Stateless.class,"dhcpv6-stateless")
61             .build();
62
63     NeutronSubnetInterface(final DataBroker dataBroker) {
64         super(dataBroker);
65     }
66
67     // IfNBSubnetCRUD methods
68
69     @Override
70     public boolean subnetExists(String uuid) {
71         Subnet subnet = readMd(createInstanceIdentifier(toMd(uuid)));
72         if (subnet == null) {
73             return false;
74         }
75         return true;
76     }
77
78     @Override
79     public NeutronSubnet getSubnet(String uuid) {
80         Subnet subnet = readMd(createInstanceIdentifier(toMd(uuid)));
81         if (subnet == null) {
82             return null;
83         }
84         return fromMd(subnet);
85     }
86
87     @Override
88     public List<NeutronSubnet> getAllSubnets() {
89         Set<NeutronSubnet> allSubnets = new HashSet<>();
90         Subnets subnets = readMd(createInstanceIdentifier());
91         if (subnets != null) {
92             for (Subnet subnet: subnets.getSubnet()) {
93                 allSubnets.add(fromMd(subnet));
94             }
95         }
96         LOGGER.debug("Exiting getAllSubnets, Found {} OpenStackSubnets", allSubnets.size());
97         List<NeutronSubnet> ans = new ArrayList<>();
98         ans.addAll(allSubnets);
99         return ans;
100     }
101
102     @Override
103     public boolean addSubnet(NeutronSubnet input) {
104         String id = input.getID();
105         if (subnetExists(id)) {
106             return false;
107         }
108         addMd(input);
109         return true;
110     }
111
112     @Override
113     public boolean removeSubnet(String uuid) {
114         NeutronSubnet target = getSubnet(uuid);
115         if (target == null) {
116             return false;
117         }
118         removeMd(toMd(uuid));
119         return true;
120     }
121
122     @Override
123     public boolean updateSubnet(String uuid, NeutronSubnet delta) {
124         if (!subnetExists(uuid)) {
125             return false;
126         }
127 /* note: because what we get is *not* a delta but (at this point) the updated
128  * object, this is much simpler - just replace the value and update the mdsal
129  * with it */
130         updateMd(delta);
131         return true;
132     }
133
134 // note: this is being set to false in preparation for deprecation and removal
135     @Override
136     public boolean subnetInUse(String subnetUUID) {
137         return false;
138     }
139
140     protected NeutronSubnet fromMd(Subnet subnet) {
141         NeutronSubnet result = new NeutronSubnet();
142         result.setName(subnet.getName());
143         result.setTenantID(String.valueOf(subnet.getTenantId().getValue()).replace("-",""));
144         result.setNetworkUUID(subnet.getNetworkId().getValue());
145         result.setIpVersion(IPV_MAP.get(subnet.getIpVersion()));
146         result.setCidr(String.valueOf(subnet.getCidr().getValue()));
147         result.setIpV6RaMode(DHCPV6_MAP.get(subnet.getIpv6RaMode()));
148         result.setIpV6AddressMode(DHCPV6_MAP.get(subnet.getIpv6AddressMode()));
149         result.setEnableDHCP(subnet.isEnableDhcp());
150         if (subnet.getAllocationPools() != null) {
151             List<NeutronSubnetIPAllocationPool> allocationPools = new ArrayList<>();
152             for (AllocationPools allocationPool : subnet.getAllocationPools()) {
153                 NeutronSubnetIPAllocationPool pool = new NeutronSubnetIPAllocationPool();
154                 pool.setPoolStart(String.valueOf(allocationPool.getStart().getValue()));
155                 pool.setPoolEnd(String.valueOf(allocationPool.getEnd().getValue()));
156                 allocationPools.add(pool);
157             }
158             result.setAllocationPools(allocationPools);
159         }
160         if (subnet.getDnsNameservers() != null) {
161             List<String> dnsNameServers = new ArrayList<>();
162             for (IpAddress dnsNameServer : subnet.getDnsNameservers()) {
163                 dnsNameServers.add(String.valueOf(dnsNameServer.getValue()));
164             }
165             result.setDnsNameservers(dnsNameServers);
166         }
167         result.setID(subnet.getUuid().getValue());
168 // read through the ports and put the ones in this subnet into the internal
169 // myPorts object.
170 // @deprecated and will be removed in Boron
171         Set<NeutronPort> allPorts = new HashSet<>();
172         NeutronCRUDInterfaces interfaces = new NeutronCRUDInterfaces()
173             .fetchINeutronPortCRUD(this);
174         INeutronPortCRUD portIf = interfaces.getPortInterface();
175         for (NeutronPort port : portIf.getAllPorts()) {
176             if (port.getDeviceOwner().equalsIgnoreCase("network:router_interface") ||
177                 port.getDeviceOwner().equalsIgnoreCase("network:router_gateway")) {
178                 result.setGatewayIP(String.valueOf(subnet.getGatewayIp().getValue()));
179             }
180             if (port.getFixedIPs() != null) {
181                 for (Neutron_IPs ip : port.getFixedIPs()) {
182                     if (ip.getSubnetUUID().equals(result.getID())) {
183                         allPorts.add(port);
184                     }
185                 }
186             }
187         }
188         List<NeutronPort> ports = new ArrayList<>();
189         ports.addAll(allPorts);
190         result.setPorts(ports);
191         return result;
192     }
193
194     protected Subnet toMd(NeutronSubnet subnet) {
195         SubnetBuilder subnetBuilder = new SubnetBuilder();
196         if (subnet.getName() != null) {
197             subnetBuilder.setName(subnet.getName());
198         }
199         if (subnet.getTenantID() != null) {
200             subnetBuilder.setTenantId(toUuid(subnet.getTenantID()));
201         }
202         if (subnet.getNetworkUUID() != null) {
203             subnetBuilder.setNetworkId(toUuid(subnet.getNetworkUUID()));
204         }
205         if (subnet.getIpVersion() != null) {
206             ImmutableBiMap<Integer, Class<? extends IpVersionBase>> mapper =
207                     IPV_MAP.inverse();
208             subnetBuilder.setIpVersion(mapper.get(subnet
209                     .getIpVersion()));
210         }
211         if (subnet.getCidr() != null) {
212             IpPrefix ipPrefix = new IpPrefix(subnet.getCidr().toCharArray());
213             subnetBuilder.setCidr(ipPrefix);
214         }
215         if (subnet.getGatewayIP() != null) {
216             IpAddress ipAddress = new IpAddress(subnet.getGatewayIP()
217                     .toCharArray());
218             subnetBuilder.setGatewayIp(ipAddress);
219         }
220         if (subnet.getIpV6RaMode() != null) {
221             ImmutableBiMap<String, Class<? extends Dhcpv6Base>> mapper =
222                     DHCPV6_MAP.inverse();
223             subnetBuilder.setIpv6RaMode(mapper.get(subnet.getIpV6RaMode()));
224         }
225         if (subnet.getIpV6AddressMode() != null) {
226             ImmutableBiMap<String, Class<? extends Dhcpv6Base>> mapper =
227                     DHCPV6_MAP.inverse();
228             subnetBuilder.setIpv6AddressMode(mapper.get(subnet.getIpV6AddressMode()));
229         }
230         subnetBuilder.setEnableDhcp(subnet.getEnableDHCP());
231         if (subnet.getAllocationPools() != null) {
232             List<AllocationPools> allocationPools = new ArrayList<>();
233             for (NeutronSubnetIPAllocationPool allocationPool : subnet
234                     .getAllocationPools()) {
235                 AllocationPoolsBuilder builder = new AllocationPoolsBuilder();
236                 builder.setStart(new IpAddress(allocationPool.getPoolStart().toCharArray()));
237                 builder.setEnd(new IpAddress(allocationPool.getPoolEnd().toCharArray()));
238                 AllocationPools temp = builder.build();
239                 allocationPools.add(temp);
240             }
241             subnetBuilder.setAllocationPools(allocationPools);
242         }
243         if (subnet.getDnsNameservers() != null) {
244             List<IpAddress> dnsNameServers = new ArrayList<>();
245             for (String dnsNameServer : subnet.getDnsNameservers()) {
246                 IpAddress ipAddress = new IpAddress(dnsNameServer.toCharArray());
247                 dnsNameServers.add(ipAddress);
248             }
249             subnetBuilder.setDnsNameservers(dnsNameServers);
250         }
251         if (subnet.getID() != null) {
252             subnetBuilder.setUuid(toUuid(subnet.getID()));
253         } else {
254             LOGGER.warn("Attempting to write neutron subnet without UUID");
255         }
256         return subnetBuilder.build();
257     }
258
259     @Override
260     protected InstanceIdentifier<Subnet> createInstanceIdentifier(Subnet subnet) {
261         return InstanceIdentifier.create(Neutron.class).child(Subnets.class)
262                 .child(Subnet.class, subnet.getKey());
263     }
264
265     protected InstanceIdentifier<Subnets> createInstanceIdentifier() {
266         return InstanceIdentifier.create(Neutron.class)
267                 .child(Subnets.class);
268     }
269
270     @Override
271     protected Subnet toMd(String uuid) {
272         SubnetBuilder subnetBuilder = new SubnetBuilder();
273         subnetBuilder.setUuid(toUuid(uuid));
274         return subnetBuilder.build();
275     }
276
277     public static void registerNewInterface(BundleContext context,
278                                             final DataBroker dataBroker,
279                                             List<ServiceRegistration<?>> registrations) {
280         NeutronSubnetInterface neutronSubnetInterface = new NeutronSubnetInterface(dataBroker);
281         ServiceRegistration<INeutronSubnetCRUD> neutronSubnetInterfaceRegistration = context.registerService(INeutronSubnetCRUD.class, neutronSubnetInterface, null);
282         if(neutronSubnetInterfaceRegistration != null) {
283             registrations.add(neutronSubnetInterfaceRegistration);
284         }
285     }
286 }