a706438466f5c87aba2b21aca60aa27d176e7486
[netvirt.git] / openstack / net-virt / src / main / java / org / opendaylight / netvirt / openstack / netvirt / translator / crud / impl / NeutronFloatingIPInterface.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 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.netvirt.openstack.netvirt.translator.NeutronFloatingIP;
18 import org.opendaylight.netvirt.openstack.netvirt.translator.crud.INeutronFloatingIPCRUD;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.floatingips.attributes.Floatingips;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.floatingips.attributes.floatingips.Floatingip;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.floatingips.attributes.floatingips.FloatingipBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.osgi.framework.BundleContext;
26 import org.osgi.framework.ServiceRegistration;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 public class NeutronFloatingIPInterface extends AbstractNeutronInterface<Floatingip, NeutronFloatingIP> implements INeutronFloatingIPCRUD {
31     private static final Logger LOGGER = LoggerFactory.getLogger(NeutronFloatingIPInterface.class);
32
33     NeutronFloatingIPInterface(ProviderContext providerContext) {
34         super(providerContext);
35     }
36
37     // IfNBFloatingIPCRUD interface methods
38
39     @Override
40     public boolean floatingIPExists(String uuid) {
41         Floatingip fip = readMd(createInstanceIdentifier(toMd(uuid)));
42         return (fip != null);
43     }
44
45     @Override
46     public NeutronFloatingIP getFloatingIP(String uuid) {
47         Floatingip fip = readMd(createInstanceIdentifier(toMd(uuid)));
48         if (fip == null) {
49             return null;
50         }
51         return fromMd(fip);
52     }
53
54     @Override
55     public List<NeutronFloatingIP> getAllFloatingIPs() {
56         Set<NeutronFloatingIP> allIPs = new HashSet<>();
57         Floatingips fips = readMd(createInstanceIdentifier());
58         if (fips != null) {
59             for (Floatingip fip: fips.getFloatingip()) {
60                 allIPs.add(fromMd(fip));
61             }
62         }
63         LOGGER.debug("Exiting getAllFloatingIPs, Found {} FloatingIPs", allIPs.size());
64         List<NeutronFloatingIP> ans = new ArrayList<>();
65         ans.addAll(allIPs);
66         return ans;
67     }
68
69     @Override
70     public boolean addFloatingIP(NeutronFloatingIP input) {
71         if (floatingIPExists(input.getID())) {
72             return false;
73         }
74         return addMd(input);
75     }
76
77     @Override
78     public boolean removeFloatingIP(String uuid) {
79         NeutronFloatingIP fip = getFloatingIP(uuid);
80         if (fip == null) {
81             return false;
82         }
83         return removeMd(toMd(uuid));
84     }
85
86     @Override
87     public boolean updateFloatingIP(String uuid, NeutronFloatingIP delta) {
88         NeutronFloatingIP target = getFloatingIP(uuid);
89         if (target == null) {
90             return false;
91         }
92         delta.setPortUUID(target.getPortUUID());
93         delta.setFixedIPAddress(target.getFixedIPAddress());
94         return updateMd(delta);
95     }
96
97     @Override
98     protected Floatingip toMd(String uuid) {
99         FloatingipBuilder floatingipBuilder = new FloatingipBuilder();
100         floatingipBuilder.setUuid(toUuid(uuid));
101         return floatingipBuilder.build();
102     }
103
104     @Override
105     protected Floatingip toMd(NeutronFloatingIP floatingIp) {
106         FloatingipBuilder floatingipBuilder = new FloatingipBuilder();
107         if (floatingIp.getFixedIPAddress() != null) {
108             floatingipBuilder.setFixedIpAddress(new IpAddress(floatingIp.getFixedIPAddress().toCharArray()));
109         }
110         if(floatingIp.getFloatingIPAddress() != null) {
111             floatingipBuilder.setFloatingIpAddress(new IpAddress(floatingIp.getFloatingIPAddress().toCharArray()));
112         }
113         if (floatingIp.getFloatingNetworkUUID() != null) {
114             floatingipBuilder.setFloatingNetworkId(toUuid(floatingIp.getFloatingNetworkUUID()));
115         }
116         if (floatingIp.getPortUUID() != null) {
117             floatingipBuilder.setPortId(toUuid(floatingIp.getPortUUID()));
118         }
119         if (floatingIp.getRouterUUID() != null) {
120             floatingipBuilder.setRouterId(toUuid(floatingIp.getRouterUUID()));
121         }
122         if (floatingIp.getStatus() != null) {
123             floatingipBuilder.setStatus(floatingIp.getStatus());
124         }
125         if (floatingIp.getTenantUUID() != null) {
126             floatingipBuilder.setTenantId(toUuid(floatingIp.getTenantUUID()));
127         }
128         if (floatingIp.getID() != null) {
129             floatingipBuilder.setUuid(toUuid(floatingIp.getID()));
130         }
131         else {
132             LOGGER.warn("Attempting to write neutron floating IP without UUID");
133         }
134         return floatingipBuilder.build();
135     }
136
137     protected NeutronFloatingIP fromMd(Floatingip fip) {
138         NeutronFloatingIP result = new NeutronFloatingIP();
139         result.setID(String.valueOf(fip.getUuid().getValue()));
140         if (fip.getFloatingNetworkId() != null) {
141             result.setFloatingNetworkUUID(String.valueOf(fip.getFloatingNetworkId().getValue()));
142         }
143         if (fip.getPortId() != null) {
144             result.setPortUUID(String.valueOf(fip.getPortId().getValue()));
145         }
146         if (fip.getFixedIpAddress() != null ) {
147             result.setFixedIPAddress(String.valueOf(fip.getFixedIpAddress().getValue()));
148         }
149         if (fip.getFloatingIpAddress() != null) {
150             result.setFloatingIPAddress(String.valueOf(fip.getFloatingIpAddress().getValue()));
151         }
152         if (fip.getTenantId() != null) {
153             result.setTenantUUID(String.valueOf(fip.getTenantId().getValue()));
154         }
155         if (fip.getRouterId() != null) {
156             result.setRouterUUID(String.valueOf(fip.getRouterId().getValue()));
157         }
158         result.setStatus(fip.getStatus());
159         return result;
160     }
161
162     @Override
163     protected InstanceIdentifier<Floatingip> createInstanceIdentifier(
164             Floatingip item) {
165         return InstanceIdentifier.create(Neutron.class)
166                 .child(Floatingips.class)
167                 .child(Floatingip.class,item.getKey());
168     }
169
170     protected InstanceIdentifier<Floatingips> createInstanceIdentifier() {
171         return InstanceIdentifier.create(Neutron.class)
172                 .child(Floatingips.class);
173     }
174
175     public static void registerNewInterface(BundleContext context,
176                                             ProviderContext providerContext,
177                                             List<ServiceRegistration<?>> registrations) {
178         NeutronFloatingIPInterface neutronFloatingIPInterface = new NeutronFloatingIPInterface(providerContext);
179         ServiceRegistration<INeutronFloatingIPCRUD> neutronFloatingIPInterfaceRegistration = context.registerService(INeutronFloatingIPCRUD.class, neutronFloatingIPInterface, null);
180         if (neutronFloatingIPInterfaceRegistration != null) {
181             registrations.add(neutronFloatingIPInterfaceRegistration);
182         }
183     }
184 }