2 * Copyright (c) 2015, 2016 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.netvirt.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.assertNotNull;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.assertTrue;
17 import static org.mockito.Mockito.mock;
18 import static org.mockito.Mockito.when;
20 import java.util.List;
22 import org.junit.Test;
24 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
25 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
26 import org.opendaylight.controller.md.sal.binding.test.AbstractDataBrokerTest;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
29 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
30 import org.opendaylight.netvirt.openstack.netvirt.translator.NeutronFloatingIP;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.floatingips.attributes.floatingips.Floatingip;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.floatingips.attributes.floatingips.FloatingipBuilder;
37 * Unit test for {@link NeutronFloatingIPInterface}
39 public class NeutronFloatingIPInterfaceTest extends AbstractDataBrokerTest {
41 * UUID_VALUE used for testing different scenarios.
43 private static final String UUID_VALUE = "b9a13232-525e-4d8c-be21-cd65e3436034";
45 * FIXED_IP_ADDRESS used for testing different scenarios.
47 private static final String FIXED_IP_ADDRESS = "10.0.0.3";
49 * FLOATING_IP_ADDRESS used for testing different scenarios.
51 private static final String FLOATING_IP_ADDRESS = "172.24.4.228";
53 * STATUS used for testing different scenarios.
55 private static final String STATUS = "ACTIVE";
57 private NeutronFloatingIPInterface getTestInterface(DataBroker broker) {
58 ProviderContext providerContext = mock(ProviderContext.class);
59 when(providerContext.getSALService(DataBroker.class)).thenReturn(broker);
60 return new NeutronFloatingIPInterface(providerContext);
64 * Test that checks if @{NeutronFloatingIPInterface#floatingIPExists} is called
65 * and then checks that floating Ip exists or not.
68 public void testFloatingIPExists() throws TransactionCommitFailedException {
69 // floatingIPExists() returns true if the underlying data broker contains the node, false otherwise
70 DataBroker broker = getDataBroker();
71 NeutronFloatingIPInterface testInterface = getTestInterface(broker);
73 // First case: the underlying data broker returns nothing (we haven't inserted the IP yet)
74 assertFalse(testInterface.floatingIPExists(UUID_VALUE));
77 addTestFloatingIP(broker, testInterface);
79 // Second case: the underlying data broker returns something
80 assertTrue(testInterface.floatingIPExists(UUID_VALUE));
83 private void addTestFloatingIP(DataBroker broker, NeutronFloatingIPInterface testInterface)
84 throws TransactionCommitFailedException {
85 WriteTransaction writeTransaction = broker.newWriteOnlyTransaction();
86 Floatingip floatingip = new FloatingipBuilder().setUuid(new Uuid(UUID_VALUE)).build();
87 writeTransaction.put(LogicalDatastoreType.CONFIGURATION,
88 testInterface.createInstanceIdentifier(floatingip), floatingip);
89 writeTransaction.submit().checkedGet();
93 * Test that checks if @{NeutronFloatingIPInterface#getFloatingIP} is called
94 * and then checks that it gets floating Ip or not.
97 public void testGetFloatingIP() throws TransactionCommitFailedException {
98 // getFloatingIP() returns the floating IP if the underlying data broker contains the node, null otherwise
99 DataBroker broker = getDataBroker();
100 NeutronFloatingIPInterface testInterface = getTestInterface(broker);
102 // First case: the underlying data broker returns nothing (we haven't inserted the IP yet)
103 assertNull(testInterface.getFloatingIP(UUID_VALUE));
106 addTestFloatingIP(broker, testInterface);
108 // Second case: the underlying data broker returns something
109 final NeutronFloatingIP returnedFloatingIp = testInterface.getFloatingIP(UUID_VALUE);
110 assertNotNull(returnedFloatingIp);
111 assertEquals("UUID mismatch", UUID_VALUE, returnedFloatingIp.getID());
115 * Test that checks if @{NeutronFloatingIPInterface#getAllFloatingIPs} is called
116 * and then checks that it gets all floating Ips in a list or not.
119 public void testGetAllFloatingIPs() throws TransactionCommitFailedException {
120 // getAllFloatingIPs() returns all the floating IPs in the underlying data broker
121 DataBroker broker = getDataBroker();
122 NeutronFloatingIPInterface testInterface = getTestInterface(broker);
124 // First case: the underlying data broker returns nothing (we haven't inserted the IP yet)
125 assertTrue("Non-empty list of floating IPs", testInterface.getAllFloatingIPs().isEmpty());
128 addTestFloatingIP(broker, testInterface);
130 // Second case: the underlying data broker returns something
131 final List<NeutronFloatingIP> allFloatingIPs = testInterface.getAllFloatingIPs();
132 assertFalse("Empty list of floating IPs", allFloatingIPs.isEmpty());
133 assertEquals("Incorrect number of floating IPs", 1, allFloatingIPs.size());
134 assertEquals("UUID mismatch", UUID_VALUE, allFloatingIPs.get(0).getID());
138 * Test that checks if @{NeutronFloatingIPInterface#addFloatingIP} is called
139 * and then verifies whether floating Ip already exists in datastore if not then
140 * ensures floating ip addition by invoking MD-SAL add.
143 public void testAddFloatingIP() throws TransactionCommitFailedException {
144 // addFloatingIP() adds the given floating IP if it isn't already in the data store
145 DataBroker broker = getDataBroker();
146 NeutronFloatingIPInterface testInterface = getTestInterface(broker);
148 // First case: addFloatingIP() adds the floating IP
149 NeutronFloatingIP insertedFloatingIp = new NeutronFloatingIP();
150 insertedFloatingIp.setID(UUID_VALUE);
151 assertTrue("Floating IP already present", testInterface.addFloatingIP(insertedFloatingIp));
153 // TODO Retrieve the floating IP directly and make sure it's correct
155 // Second case: the floating IP is already present
156 assertFalse("Floating IP missing", testInterface.addFloatingIP(insertedFloatingIp));
160 * Test that checks if @{NeutronFloatingIPInterface#removeFloatingIP} is called
161 * and then verifies by reading floating ip from datastore and ensures floating ip
162 * removal by invoking MD-SAL remove.
165 public void testRemoveFloatingIP() throws TransactionCommitFailedException {
166 // removeFloatingIP() removes the given floating IP if it's present in the data store
167 DataBroker broker = getDataBroker();
168 NeutronFloatingIPInterface testInterface = getTestInterface(broker);
170 // First case: the floating IP isn't present
171 assertFalse("Floating IP present", testInterface.removeFloatingIP(UUID_VALUE));
174 addTestFloatingIP(broker, testInterface);
176 // Second case: the floating IP is present
177 assertTrue("Floating IP absent", testInterface.removeFloatingIP(UUID_VALUE));
179 // TODO Attempt to retrieve the floating IP and make sure it's absent
183 * Test that checks if @{NeutronFloatingIPInterface#updateFloatingIP} is called
184 * and then verifies by reading floating ip from datastore and ensures floating ip
185 * updation by invoking MD-SAL update.
188 public void testUpdateFloatingIP() throws TransactionCommitFailedException {
189 // updateFloatingIP() updates the given floating IP only if it's already in the data store
190 DataBroker broker = getDataBroker();
191 NeutronFloatingIPInterface testInterface = getTestInterface(broker);
193 // First case: the floating IP isn't present
194 NeutronFloatingIP testFloatingIp = new NeutronFloatingIP();
195 testFloatingIp.setID(UUID_VALUE);
196 assertFalse("Floating IP present", testInterface.updateFloatingIP(UUID_VALUE, testFloatingIp));
199 addTestFloatingIP(broker, testInterface);
201 // Second case: the floating IP is present
202 assertTrue("Floating IP absent", testInterface.updateFloatingIP(UUID_VALUE, testFloatingIp));
204 // TODO Change some attributes and make sure they're updated
208 * Test that checks if @{NeutronFloatingIPInterface#toMd} is called
209 * and then checks that it sets vales into floating Ip.
212 public void testToMd() throws Exception {
213 DataBroker broker = getDataBroker();
214 NeutronFloatingIPInterface testInterface = getTestInterface(broker);
215 NeutronFloatingIP neutronFloatingIP = new NeutronFloatingIP();
216 neutronFloatingIP.setID(UUID_VALUE);
217 neutronFloatingIP.setFloatingNetworkUUID(UUID_VALUE);
218 neutronFloatingIP.setPortUUID(UUID_VALUE);
219 neutronFloatingIP.setFixedIPAddress(FIXED_IP_ADDRESS);
220 neutronFloatingIP.setFloatingIPAddress(FLOATING_IP_ADDRESS);
221 neutronFloatingIP.setTenantUUID(UUID_VALUE);
222 neutronFloatingIP.setRouterUUID(UUID_VALUE);
223 neutronFloatingIP.setStatus(STATUS);
224 Floatingip floatingipReceived = testInterface.toMd(neutronFloatingIP);
225 assertEquals("UUID mismatch", UUID_VALUE, floatingipReceived.getUuid().getValue());
226 assertEquals("FloatingNetworkId mismatch", UUID_VALUE, floatingipReceived.getFloatingNetworkId().getValue());
227 assertEquals("Port ID mismatch", UUID_VALUE, floatingipReceived.getPortId().getValue());
228 assertEquals("Fixed IP Address mismatch", FIXED_IP_ADDRESS, String.valueOf(floatingipReceived.getFixedIpAddress().getValue()));
229 assertEquals("Floating IP Address mismatch", FLOATING_IP_ADDRESS, String.valueOf(floatingipReceived.getFloatingIpAddress().getValue()));
230 assertEquals("Tenant Id mismatch", UUID_VALUE, floatingipReceived.getTenantId().getValue());
231 assertEquals("Router Id mismatch", UUID_VALUE, floatingipReceived.getRouterId().getValue());
232 assertEquals("Status mismatch", STATUS, floatingipReceived.getStatus());
236 * Test that checks if @{NeutronFloatingIPInterface#fromMd} is called
237 * and then checks that it gets values from Floating Ip.
240 public void testFromMd() throws Exception {
241 DataBroker broker = getDataBroker();
242 NeutronFloatingIPInterface testInterface = getTestInterface(broker);
243 Floatingip actualfloatingip = new FloatingipBuilder()
244 .setUuid(new Uuid(UUID_VALUE))
246 new IpAddress(FIXED_IP_ADDRESS.toCharArray()))
247 .setFloatingIpAddress(
248 new IpAddress(FLOATING_IP_ADDRESS.toCharArray()))
249 .setFloatingNetworkId(new Uuid(UUID_VALUE))
250 .setPortId(new Uuid(UUID_VALUE))
251 .setRouterId(new Uuid(UUID_VALUE)).setStatus(STATUS)
252 .setTenantId(new Uuid(UUID_VALUE)).build();
253 NeutronFloatingIP neutronFloatingIPReceived = testInterface.fromMd(actualfloatingip);
254 assertEquals("UUID mismatch", UUID_VALUE, neutronFloatingIPReceived.getID());
255 assertEquals("FloatingNetworkId mismatch", UUID_VALUE, neutronFloatingIPReceived.getFloatingNetworkUUID());
256 assertEquals("Port ID mismatch", UUID_VALUE, neutronFloatingIPReceived.getPortUUID());
257 assertEquals("Fixed IP Address mismatch", FIXED_IP_ADDRESS, neutronFloatingIPReceived.getFixedIPAddress());
258 assertEquals("Floating IP Address mismatch", FLOATING_IP_ADDRESS, neutronFloatingIPReceived.getFloatingIPAddress());
259 assertEquals("Tenant Id mismatch", UUID_VALUE, neutronFloatingIPReceived.getTenantUUID());
260 assertEquals("Router Id mismatch", UUID_VALUE, neutronFloatingIPReceived.getRouterUUID());
261 assertEquals("Status mismatch", STATUS, neutronFloatingIPReceived.getStatus());