2 * Copyright (c) 2015 NEC 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.ovsdb.openstack.netvirt.translator.crud.impl;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertTrue;
16 import static org.mockito.Matchers.any;
17 import static org.mockito.Matchers.eq;
19 import static org.powermock.api.mockito.PowerMockito.mockStatic;
20 import static org.powermock.api.mockito.PowerMockito.mock;
21 import static org.powermock.api.mockito.PowerMockito.spy;
22 import static org.powermock.api.mockito.PowerMockito.verifyStatic;
23 import static org.powermock.api.mockito.PowerMockito.when;
25 import java.util.ArrayList;
26 import java.util.List;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
31 import org.powermock.core.classloader.annotations.PrepareForTest;
32 import org.powermock.modules.junit4.PowerMockRunner;
34 import com.google.common.base.Optional;
35 import com.google.common.util.concurrent.CheckedFuture;
37 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
38 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
39 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
40 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
41 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
42 import org.opendaylight.ovsdb.openstack.netvirt.translator.NeutronFloatingIP;
43 import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.INeutronFloatingIPCRUD;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.floatingips.attributes.Floatingips;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.floatingips.attributes.floatingips.Floatingip;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.floatingips.attributes.floatingips.FloatingipBuilder;
49 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
50 import org.osgi.framework.BundleContext;
51 import org.osgi.framework.ServiceRegistration;
54 * Unit test for {@link NeutronFloatingIPInterface}
56 @PrepareForTest({NeutronFloatingIPInterface.class})
57 @RunWith(PowerMockRunner.class)
58 public class NeutronFloatingIPInterfaceTest {
60 * UUID_VALUE used for testing different scenarios.
62 private static final String UUID_VALUE = "b9a13232-525e-4d8c-be21-cd65e3436034";
64 * FIXED_IP_ADDRESS used for testing different scenarios.
66 private static final String FIXED_IP_ADDRESS = "10.0.0.3";
68 * FLOATING_IP_ADDRESS used for testing different scenarios.
70 private static final String FLOATING_IP_ADDRESS = "172.24.4.228";
72 * STATUS used for testing different scenarios.
74 private static final String STATUS = "ACTIVE";
76 * Floatingip object reference for unit testing.
78 private Floatingip floatingip = new FloatingipBuilder().setUuid(new Uuid(UUID_VALUE)).build();
81 * Test that checks if @{NeutronFloatingIPInterface#floatingIPExists} is called
82 * and then checks that floating Ip exists or not.
85 public void testFloatingIPExists() throws Exception {
86 ProviderContext providerContext = mock(ProviderContext.class);
87 DataBroker broker = mock(DataBroker.class);
88 when(providerContext.getSALService(DataBroker.class)).thenReturn(broker);
89 ReadOnlyTransaction readOnlyTransaction = mock(ReadOnlyTransaction.class);
90 when(broker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction);
91 NeutronFloatingIPInterface neutronFloatingIPInterface = new NeutronFloatingIPInterface(providerContext);
92 // First case: floatingIPExists() is expected to return true because the datastore contains a matching floating IP.
93 CheckedFuture succeedingFuture = mock(CheckedFuture.class);
94 when(succeedingFuture.checkedGet()).thenReturn(Optional.of(floatingip));
95 when(readOnlyTransaction.read(eq(LogicalDatastoreType.CONFIGURATION), any(InstanceIdentifier.class))).thenReturn(succeedingFuture);
96 assertTrue("Should return true, when floatingIPExists success.", neutronFloatingIPInterface.floatingIPExists(UUID_VALUE));
97 // Second case: the datastore has no matching floating IP, expect false
98 CheckedFuture failingFuture = mock(CheckedFuture.class);
99 when(failingFuture.checkedGet()).thenReturn(Optional.absent());
100 when(readOnlyTransaction.read(eq(LogicalDatastoreType.CONFIGURATION), any(InstanceIdentifier.class))).thenReturn(failingFuture);
101 assertFalse("Should return false for negative case.", neutronFloatingIPInterface.floatingIPExists(UUID_VALUE));
105 * Test that checks if @{NeutronFloatingIPInterface#getFloatingIP} is called
106 * and then checks that it gets floating Ip or not.
109 public void testGetFloatingIP() throws Exception {
110 ProviderContext providerContext = mock(ProviderContext.class);
111 DataBroker broker = mock(DataBroker.class);
112 when(providerContext.getSALService(DataBroker.class)).thenReturn(broker);
113 ReadOnlyTransaction readOnlyTransaction = mock(ReadOnlyTransaction.class);
114 when(broker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction);
115 NeutronFloatingIPInterface neutronFloatingIPInterface = new NeutronFloatingIPInterface(providerContext);
116 // First case: getFloatingIP is expected to return a valid object.
117 CheckedFuture succeedingFuture = mock(CheckedFuture.class);
118 when(succeedingFuture.checkedGet()).thenReturn(Optional.of(floatingip));
119 when(readOnlyTransaction.read(eq(LogicalDatastoreType.CONFIGURATION), any(InstanceIdentifier.class))).thenReturn(succeedingFuture);
120 NeutronFloatingIP neutronFloatingIPReceived = neutronFloatingIPInterface.getFloatingIP(UUID_VALUE);
121 assertEquals("UUID mismatch", UUID_VALUE, neutronFloatingIPReceived.getID());
122 // Second case: getFloatingIP returns null object.
123 CheckedFuture failingFuture = mock(CheckedFuture.class);
124 when(failingFuture.checkedGet()).thenReturn(Optional.absent());
125 when(readOnlyTransaction.read(eq(LogicalDatastoreType.CONFIGURATION), any(InstanceIdentifier.class))).thenReturn(failingFuture);
126 assertNull("Should be null for negative case.", neutronFloatingIPInterface.getFloatingIP(UUID_VALUE));
130 * Test that checks if @{NeutronFloatingIPInterface#getAllFloatingIPs} is called
131 * and then checks that it gets all floating Ips in a list or not.
134 public void testGetAllFloatingIPs() throws Exception {
135 ProviderContext providerContext = mock(ProviderContext.class);
136 DataBroker broker = mock(DataBroker.class);
137 when(providerContext.getSALService(DataBroker.class)).thenReturn(broker);
138 ReadOnlyTransaction readOnlyTransaction = mock(ReadOnlyTransaction.class);
139 when(broker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction);
140 NeutronFloatingIPInterface neutronFloatingIPInterface = new NeutronFloatingIPInterface(providerContext);
141 // First case: getAllFloatingIPs returns a list of valid objects.
142 CheckedFuture succeedingFuture = mock(CheckedFuture.class);
143 List<Floatingip> floatingipList = new ArrayList<>();
144 floatingipList.add(floatingip);
145 Floatingips floatingips = mock(Floatingips.class);
146 NeutronFloatingIP neutronFloatingIP = mock(NeutronFloatingIP.class);
147 when(floatingips.getFloatingip()).thenReturn(floatingipList);
148 when(succeedingFuture.checkedGet()).thenReturn(Optional.of(floatingips));
149 when(readOnlyTransaction.read(eq(LogicalDatastoreType.CONFIGURATION), any(InstanceIdentifier.class))).thenReturn(succeedingFuture);
150 List<NeutronFloatingIP> actualFloatingIps = neutronFloatingIPInterface.getAllFloatingIPs();
151 assertEquals("There should be one floating IP", 1, actualFloatingIps.size());
152 assertEquals("UUID mismatch", UUID_VALUE, actualFloatingIps.get(0).getID());
153 // Second case: getAllFloatingIPs not returns a list of valid objects.
154 CheckedFuture failingFuture = mock(CheckedFuture.class);
155 when(failingFuture.checkedGet()).thenReturn(Optional.absent());
156 when(readOnlyTransaction.read(eq(LogicalDatastoreType.CONFIGURATION), any(InstanceIdentifier.class))).thenReturn(failingFuture);
157 assertTrue("Non-empty list of floating IPs", neutronFloatingIPInterface.getAllFloatingIPs().isEmpty());
161 * Test that checks if @{NeutronFloatingIPInterface#addFloatingIP} is called
162 * and then verifies whether floating Ip already exists in datastore if not then
163 * ensures floating ip addition by invoking MD-SAL add.
166 public void testAddFloatingIP() throws Exception {
167 ProviderContext providerContext = mock(ProviderContext.class);
168 DataBroker broker = mock(DataBroker.class);
169 WriteTransaction writeTransaction = mock(WriteTransaction.class);
170 NeutronFloatingIP neutronFloatingIP = mock(NeutronFloatingIP.class);
171 when(providerContext.getSALService(DataBroker.class)).thenReturn(broker);
172 ReadOnlyTransaction readOnlyTransaction = mock(ReadOnlyTransaction.class);
173 when(broker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction);
174 NeutronFloatingIPInterface neutronFloatingIPInterface = new NeutronFloatingIPInterface(providerContext);
175 when(neutronFloatingIP.getID()).thenReturn(UUID_VALUE);
176 //First case: addFloatingIP returns false, the datastore has a matching floating IP.
177 CheckedFuture succeedingFuture = mock(CheckedFuture.class);
178 when(succeedingFuture.checkedGet()).thenReturn(Optional.of(floatingip));
179 when(readOnlyTransaction.read(eq(LogicalDatastoreType.CONFIGURATION), any(InstanceIdentifier.class))).thenReturn(succeedingFuture);
180 assertFalse("Should return false, floating Ip already exists.", neutronFloatingIPInterface.addFloatingIP(neutronFloatingIP));
181 //Second case: addFloatingIP returns true, the datastore has no matching floating IP, so invokes addMd() to write on datastore.
182 CheckedFuture failingFuture = mock(CheckedFuture.class);
183 when(broker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
184 when(failingFuture.checkedGet()).thenReturn(Optional.absent());
185 when(writeTransaction.submit()).thenReturn(failingFuture);
186 when(readOnlyTransaction.read(eq(LogicalDatastoreType.CONFIGURATION), any(InstanceIdentifier.class))).thenReturn(failingFuture);
187 assertTrue("Should return true for addFloatingIP success.", neutronFloatingIPInterface.addFloatingIP(neutronFloatingIP));
191 * Test that checks if @{NeutronFloatingIPInterface#removeFloatingIP} is called
192 * and then verifies by reading floating ip from datastore and ensures floating ip
193 * removal by invoking MD-SAL remove.
196 public void testRemoveFloatingIP() throws Exception {
197 ProviderContext providerContext = mock(ProviderContext.class);
198 DataBroker broker = mock(DataBroker.class);
199 WriteTransaction writeTransaction = mock(WriteTransaction.class);
200 NeutronFloatingIP neutronFloatingIP = mock(NeutronFloatingIP.class);
201 when(providerContext.getSALService(DataBroker.class)).thenReturn(broker);
202 ReadOnlyTransaction readOnlyTransaction = mock(ReadOnlyTransaction.class);
203 when(broker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction);
204 NeutronFloatingIPInterface neutronFloatingIPInterface = new NeutronFloatingIPInterface(providerContext);
205 when(neutronFloatingIP.getID()).thenReturn(UUID_VALUE);
206 //First case: removeFloatingIP returns true by ensuring floating ip removal in datastore.
207 CheckedFuture succeedingFuture = mock(CheckedFuture.class);
208 when(succeedingFuture.checkedGet()).thenReturn(Optional.of(floatingip));
209 when(writeTransaction.submit()).thenReturn(succeedingFuture);
210 when(broker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
211 when(readOnlyTransaction.read(eq(LogicalDatastoreType.CONFIGURATION), any(InstanceIdentifier.class))).thenReturn(succeedingFuture);
212 assertTrue("Should return true for removeFloatingIP success.", neutronFloatingIPInterface.removeFloatingIP(UUID_VALUE));
213 // Second case: removeFloatingIP returns false for negative case.
214 CheckedFuture failingFuture = mock(CheckedFuture.class);
215 when(broker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
216 when(failingFuture.checkedGet()).thenReturn(Optional.absent());
217 when(writeTransaction.submit()).thenReturn(failingFuture);
218 when(readOnlyTransaction.read(eq(LogicalDatastoreType.CONFIGURATION), any(InstanceIdentifier.class))).thenReturn(failingFuture);
219 assertFalse("Should return false for negative case.", neutronFloatingIPInterface.removeFloatingIP(UUID_VALUE));
223 * Test that checks if @{NeutronFloatingIPInterface#updateFloatingIP} is called
224 * and then verifies by reading floating ip from datastore and ensures floating ip
225 * updation by invoking MD-SAL update.
228 public void testUpdateFloatingIP() throws Exception {
229 ProviderContext providerContext = mock(ProviderContext.class);
230 DataBroker broker = mock(DataBroker.class);
231 WriteTransaction writeTransaction = mock(WriteTransaction.class);
232 NeutronFloatingIP neutronFloatingIP = mock(NeutronFloatingIP.class);
233 when(providerContext.getSALService(DataBroker.class)).thenReturn(broker);
234 ReadOnlyTransaction readOnlyTransaction = mock(ReadOnlyTransaction.class);
235 when(broker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction);
236 NeutronFloatingIPInterface neutronFloatingIPInterface = new NeutronFloatingIPInterface(providerContext);
237 when(neutronFloatingIP.getID()).thenReturn(UUID_VALUE);
238 //First case: updateFloatingIP returns true by ensuring floating ip updation in datastore.
239 CheckedFuture succeedingFuture = mock(CheckedFuture.class);
240 when(succeedingFuture.checkedGet()).thenReturn(Optional.of(floatingip));
241 when(writeTransaction.submit()).thenReturn(succeedingFuture);
242 when(broker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
243 when(readOnlyTransaction.read(eq(LogicalDatastoreType.CONFIGURATION), any(InstanceIdentifier.class))).thenReturn(succeedingFuture);
244 assertTrue("Should return true for updateFloatingIP success.", neutronFloatingIPInterface.updateFloatingIP(UUID_VALUE, neutronFloatingIP));
245 //Second case: updateFloatingIP returns false for negative case.
246 CheckedFuture failingFuture = mock(CheckedFuture.class);
247 when(broker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
248 when(failingFuture.checkedGet()).thenReturn(Optional.absent());
249 when(writeTransaction.submit()).thenReturn(failingFuture);
250 when(readOnlyTransaction.read(eq(LogicalDatastoreType.CONFIGURATION), any(InstanceIdentifier.class))).thenReturn(failingFuture);
251 assertFalse("Should return false for negative case.", neutronFloatingIPInterface.updateFloatingIP(UUID_VALUE, neutronFloatingIP));
255 * Test that checks if @{NeutronFloatingIPInterface#toMd} is called
256 * and then checks that it sets vales into floating Ip.
259 public void testToMd() throws Exception {
260 ProviderContext providerContext = mock(ProviderContext.class);
261 DataBroker broker = mock(DataBroker.class);
262 when(providerContext.getSALService(DataBroker.class)).thenReturn(broker);
263 NeutronFloatingIPInterface neutronFloatingIPInterface = new NeutronFloatingIPInterface(providerContext);
264 NeutronFloatingIP neutronFloatingIP = new NeutronFloatingIP();
265 neutronFloatingIP.setID(UUID_VALUE);
266 neutronFloatingIP.setFloatingNetworkUUID(UUID_VALUE);
267 neutronFloatingIP.setPortUUID(UUID_VALUE);
268 neutronFloatingIP.setFixedIPAddress(FIXED_IP_ADDRESS);
269 neutronFloatingIP.setFloatingIPAddress(FLOATING_IP_ADDRESS);
270 neutronFloatingIP.setTenantUUID(UUID_VALUE);
271 neutronFloatingIP.setRouterUUID(UUID_VALUE);
272 neutronFloatingIP.setStatus(STATUS);
273 Floatingip floatingipReceived = neutronFloatingIPInterface.toMd(neutronFloatingIP);
274 assertEquals("UUID mismatch", UUID_VALUE, String.valueOf(floatingipReceived.getUuid().getValue()));
275 assertEquals("FloatingNetworkId mismatch", UUID_VALUE, String.valueOf(floatingipReceived.getFloatingNetworkId().getValue()));
276 assertEquals("Port ID mismatch", UUID_VALUE, String.valueOf(floatingipReceived.getPortId().getValue()));
277 assertEquals("Fixed IP Address mismatch", FIXED_IP_ADDRESS, String.valueOf(floatingipReceived.getFixedIpAddress().getValue()));
278 assertEquals("Floating IP Address mismatch", FLOATING_IP_ADDRESS, String.valueOf(floatingipReceived.getFloatingIpAddress().getValue()));
279 assertEquals("Tenant Id mismatch", UUID_VALUE, String.valueOf(floatingipReceived.getTenantId().getValue()));
280 assertEquals("Router Id mismatch", UUID_VALUE, String.valueOf(floatingipReceived.getRouterId().getValue()));
281 assertEquals("Status mismatch", STATUS, String.valueOf(floatingipReceived.getStatus()));
285 * Test that checks if @{NeutronFloatingIPInterface#fromMd} is called
286 * and then checks that it gets values from Floating Ip.
289 public void testFromMd() throws Exception {
290 ProviderContext providerContext = mock(ProviderContext.class);
291 DataBroker broker = mock(DataBroker.class);
292 when(providerContext.getSALService(DataBroker.class)).thenReturn(broker);
293 Floatingip actualfloatingip = new FloatingipBuilder()
294 .setUuid(new Uuid(UUID_VALUE))
296 new IpAddress(FIXED_IP_ADDRESS.toCharArray()))
297 .setFloatingIpAddress(
298 new IpAddress(FLOATING_IP_ADDRESS.toCharArray()))
299 .setFloatingNetworkId(new Uuid(UUID_VALUE))
300 .setPortId(new Uuid(UUID_VALUE))
301 .setRouterId(new Uuid(UUID_VALUE)).setStatus(STATUS)
302 .setTenantId(new Uuid(UUID_VALUE)).build();
303 NeutronFloatingIPInterface neutronFloatingIPInterface = new NeutronFloatingIPInterface(providerContext);
304 NeutronFloatingIP neutronFloatingIPReceived = neutronFloatingIPInterface.fromMd(actualfloatingip);
305 assertEquals("UUID mismatch", UUID_VALUE, neutronFloatingIPReceived.getID());
306 assertEquals("FloatingNetworkId mismatch", UUID_VALUE, neutronFloatingIPReceived.getFloatingNetworkUUID());
307 assertEquals("Port ID mismatch", UUID_VALUE, neutronFloatingIPReceived.getPortUUID());
308 assertEquals("Fixed IP Address mismatch", FIXED_IP_ADDRESS, neutronFloatingIPReceived.getFixedIPAddress());
309 assertEquals("Floating IP Address mismatch", FLOATING_IP_ADDRESS, neutronFloatingIPReceived.getFloatingIPAddress());
310 assertEquals("Tenant Id mismatch", UUID_VALUE, neutronFloatingIPReceived.getTenantUUID());
311 assertEquals("Router Id mismatch", UUID_VALUE, neutronFloatingIPReceived.getRouterUUID());
312 assertEquals("Status mismatch", STATUS, neutronFloatingIPReceived.getStatus());
316 * Test that checks if @{NeutronFloatingIPInterface#registerNewInterface} is called
317 * and then checks that it register service or not.
320 public void testRegisterNewInterface() throws Exception {
321 ProviderContext providerContext = mock(ProviderContext.class);
322 DataBroker broker = mock(DataBroker.class);
323 BundleContext bundleContext = mock(BundleContext.class);
324 ServiceRegistration serviceRegistration = mock(ServiceRegistration.class);
325 mockStatic(NeutronFloatingIPInterface.class);
326 List<ServiceRegistration<?>> serviceRegistrationList = new ArrayList<>();
327 serviceRegistrationList.add(serviceRegistration);
328 when(providerContext.getSALService(DataBroker.class)).thenReturn(broker);
329 NeutronFloatingIPInterface neutronFloatingIPInterface = spy(new NeutronFloatingIPInterface(providerContext));
330 when(bundleContext.registerService(INeutronFloatingIPCRUD.class, neutronFloatingIPInterface, null)).thenReturn(serviceRegistration);
331 NeutronFloatingIPInterface.registerNewInterface(bundleContext, providerContext, serviceRegistrationList);
333 NeutronFloatingIPInterface.registerNewInterface(any(BundleContext.class), any(ProviderContext.class), any(List.class));