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 java.util.List;
18 import org.junit.Test;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
21 import org.opendaylight.controller.md.sal.binding.test.AbstractDataBrokerTest;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
24 import org.opendaylight.netvirt.openstack.netvirt.translator.NeutronFloatingIP;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.floatingips.attributes.floatingips.Floatingip;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.floatingips.attributes.floatingips.FloatingipBuilder;
31 * Unit test for {@link NeutronFloatingIPInterface}
33 public class NeutronFloatingIPInterfaceTest extends AbstractDataBrokerTest {
35 * UUID_VALUE used for testing different scenarios.
37 private static final String UUID_VALUE = "b9a13232-525e-4d8c-be21-cd65e3436034";
39 * FIXED_IP_ADDRESS used for testing different scenarios.
41 private static final String FIXED_IP_ADDRESS = "10.0.0.3";
43 * FLOATING_IP_ADDRESS used for testing different scenarios.
45 private static final String FLOATING_IP_ADDRESS = "172.24.4.228";
47 * STATUS used for testing different scenarios.
49 private static final String STATUS = "ACTIVE";
51 private NeutronFloatingIPInterface getTestInterface(DataBroker broker) {
52 return new NeutronFloatingIPInterface(broker);
56 * Test that checks if @{NeutronFloatingIPInterface#floatingIPExists} is called
57 * and then checks that floating Ip exists or not.
60 public void testFloatingIPExists() throws TransactionCommitFailedException {
61 // floatingIPExists() returns true if the underlying data broker contains the node, false otherwise
62 DataBroker broker = getDataBroker();
63 NeutronFloatingIPInterface testInterface = getTestInterface(broker);
65 // First case: the underlying data broker returns nothing (we haven't inserted the IP yet)
66 assertFalse(testInterface.floatingIPExists(UUID_VALUE));
69 addTestFloatingIP(broker, testInterface);
71 // Second case: the underlying data broker returns something
72 assertTrue(testInterface.floatingIPExists(UUID_VALUE));
75 private void addTestFloatingIP(DataBroker broker, NeutronFloatingIPInterface testInterface)
76 throws TransactionCommitFailedException {
77 WriteTransaction writeTransaction = broker.newWriteOnlyTransaction();
78 Floatingip floatingip = new FloatingipBuilder().setUuid(new Uuid(UUID_VALUE)).build();
79 writeTransaction.put(LogicalDatastoreType.CONFIGURATION,
80 testInterface.createInstanceIdentifier(floatingip), floatingip);
81 writeTransaction.submit().checkedGet();
85 * Test that checks if @{NeutronFloatingIPInterface#getFloatingIP} is called
86 * and then checks that it gets floating Ip or not.
89 public void testGetFloatingIP() throws TransactionCommitFailedException {
90 // getFloatingIP() returns the floating IP if the underlying data broker contains the node, null otherwise
91 DataBroker broker = getDataBroker();
92 NeutronFloatingIPInterface testInterface = getTestInterface(broker);
94 // First case: the underlying data broker returns nothing (we haven't inserted the IP yet)
95 assertNull(testInterface.getFloatingIP(UUID_VALUE));
98 addTestFloatingIP(broker, testInterface);
100 // Second case: the underlying data broker returns something
101 final NeutronFloatingIP returnedFloatingIp = testInterface.getFloatingIP(UUID_VALUE);
102 assertNotNull(returnedFloatingIp);
103 assertEquals("UUID mismatch", UUID_VALUE, returnedFloatingIp.getID());
107 * Test that checks if @{NeutronFloatingIPInterface#getAllFloatingIPs} is called
108 * and then checks that it gets all floating Ips in a list or not.
111 public void testGetAllFloatingIPs() throws TransactionCommitFailedException {
112 // getAllFloatingIPs() returns all the floating IPs in the underlying data broker
113 DataBroker broker = getDataBroker();
114 NeutronFloatingIPInterface testInterface = getTestInterface(broker);
116 // First case: the underlying data broker returns nothing (we haven't inserted the IP yet)
117 assertTrue("Non-empty list of floating IPs", testInterface.getAllFloatingIPs().isEmpty());
120 addTestFloatingIP(broker, testInterface);
122 // Second case: the underlying data broker returns something
123 final List<NeutronFloatingIP> allFloatingIPs = testInterface.getAllFloatingIPs();
124 assertFalse("Empty list of floating IPs", allFloatingIPs.isEmpty());
125 assertEquals("Incorrect number of floating IPs", 1, allFloatingIPs.size());
126 assertEquals("UUID mismatch", UUID_VALUE, allFloatingIPs.get(0).getID());
130 * Test that checks if @{NeutronFloatingIPInterface#addFloatingIP} is called
131 * and then verifies whether floating Ip already exists in datastore if not then
132 * ensures floating ip addition by invoking MD-SAL add.
135 public void testAddFloatingIP() throws TransactionCommitFailedException {
136 // addFloatingIP() adds the given floating IP if it isn't already in the data store
137 DataBroker broker = getDataBroker();
138 NeutronFloatingIPInterface testInterface = getTestInterface(broker);
140 // First case: addFloatingIP() adds the floating IP
141 NeutronFloatingIP insertedFloatingIp = new NeutronFloatingIP();
142 insertedFloatingIp.setID(UUID_VALUE);
143 assertTrue("Floating IP already present", testInterface.addFloatingIP(insertedFloatingIp));
145 // TODO Retrieve the floating IP directly and make sure it's correct
147 // Second case: the floating IP is already present
148 assertFalse("Floating IP missing", testInterface.addFloatingIP(insertedFloatingIp));
152 * Test that checks if @{NeutronFloatingIPInterface#removeFloatingIP} is called
153 * and then verifies by reading floating ip from datastore and ensures floating ip
154 * removal by invoking MD-SAL remove.
157 public void testRemoveFloatingIP() throws TransactionCommitFailedException {
158 // removeFloatingIP() removes the given floating IP if it's present in the data store
159 DataBroker broker = getDataBroker();
160 NeutronFloatingIPInterface testInterface = getTestInterface(broker);
162 // First case: the floating IP isn't present
163 assertFalse("Floating IP present", testInterface.removeFloatingIP(UUID_VALUE));
166 addTestFloatingIP(broker, testInterface);
168 // Second case: the floating IP is present
169 assertTrue("Floating IP absent", testInterface.removeFloatingIP(UUID_VALUE));
171 // TODO Attempt to retrieve the floating IP and make sure it's absent
175 * Test that checks if @{NeutronFloatingIPInterface#updateFloatingIP} is called
176 * and then verifies by reading floating ip from datastore and ensures floating ip
177 * updation by invoking MD-SAL update.
180 public void testUpdateFloatingIP() throws TransactionCommitFailedException {
181 // updateFloatingIP() updates the given floating IP only if it's already in the data store
182 DataBroker broker = getDataBroker();
183 NeutronFloatingIPInterface testInterface = getTestInterface(broker);
185 // First case: the floating IP isn't present
186 NeutronFloatingIP testFloatingIp = new NeutronFloatingIP();
187 testFloatingIp.setID(UUID_VALUE);
188 assertFalse("Floating IP present", testInterface.updateFloatingIP(UUID_VALUE, testFloatingIp));
191 addTestFloatingIP(broker, testInterface);
193 // Second case: the floating IP is present
194 assertTrue("Floating IP absent", testInterface.updateFloatingIP(UUID_VALUE, testFloatingIp));
196 // TODO Change some attributes and make sure they're updated
200 * Test that checks if @{NeutronFloatingIPInterface#toMd} is called
201 * and then checks that it sets vales into floating Ip.
204 public void testToMd() throws Exception {
205 DataBroker broker = getDataBroker();
206 NeutronFloatingIPInterface testInterface = getTestInterface(broker);
207 NeutronFloatingIP neutronFloatingIP = new NeutronFloatingIP();
208 neutronFloatingIP.setID(UUID_VALUE);
209 neutronFloatingIP.setFloatingNetworkUUID(UUID_VALUE);
210 neutronFloatingIP.setPortUUID(UUID_VALUE);
211 neutronFloatingIP.setFixedIPAddress(FIXED_IP_ADDRESS);
212 neutronFloatingIP.setFloatingIPAddress(FLOATING_IP_ADDRESS);
213 neutronFloatingIP.setTenantUUID(UUID_VALUE);
214 neutronFloatingIP.setRouterUUID(UUID_VALUE);
215 neutronFloatingIP.setStatus(STATUS);
216 Floatingip floatingipReceived = testInterface.toMd(neutronFloatingIP);
217 assertEquals("UUID mismatch", UUID_VALUE, floatingipReceived.getUuid().getValue());
218 assertEquals("FloatingNetworkId mismatch", UUID_VALUE, floatingipReceived.getFloatingNetworkId().getValue());
219 assertEquals("Port ID mismatch", UUID_VALUE, floatingipReceived.getPortId().getValue());
220 assertEquals("Fixed IP Address mismatch", FIXED_IP_ADDRESS, String.valueOf(floatingipReceived.getFixedIpAddress().getValue()));
221 assertEquals("Floating IP Address mismatch", FLOATING_IP_ADDRESS, String.valueOf(floatingipReceived.getFloatingIpAddress().getValue()));
222 assertEquals("Tenant Id mismatch", UUID_VALUE, floatingipReceived.getTenantId().getValue());
223 assertEquals("Router Id mismatch", UUID_VALUE, floatingipReceived.getRouterId().getValue());
224 assertEquals("Status mismatch", STATUS, floatingipReceived.getStatus());
228 * Test that checks if @{NeutronFloatingIPInterface#fromMd} is called
229 * and then checks that it gets values from Floating Ip.
232 public void testFromMd() throws Exception {
233 DataBroker broker = getDataBroker();
234 NeutronFloatingIPInterface testInterface = getTestInterface(broker);
235 Floatingip actualfloatingip = new FloatingipBuilder()
236 .setUuid(new Uuid(UUID_VALUE))
238 new IpAddress(FIXED_IP_ADDRESS.toCharArray()))
239 .setFloatingIpAddress(
240 new IpAddress(FLOATING_IP_ADDRESS.toCharArray()))
241 .setFloatingNetworkId(new Uuid(UUID_VALUE))
242 .setPortId(new Uuid(UUID_VALUE))
243 .setRouterId(new Uuid(UUID_VALUE)).setStatus(STATUS)
244 .setTenantId(new Uuid(UUID_VALUE)).build();
245 NeutronFloatingIP neutronFloatingIPReceived = testInterface.fromMd(actualfloatingip);
246 assertEquals("UUID mismatch", UUID_VALUE, neutronFloatingIPReceived.getID());
247 assertEquals("FloatingNetworkId mismatch", UUID_VALUE, neutronFloatingIPReceived.getFloatingNetworkUUID());
248 assertEquals("Port ID mismatch", UUID_VALUE, neutronFloatingIPReceived.getPortUUID());
249 assertEquals("Fixed IP Address mismatch", FIXED_IP_ADDRESS, neutronFloatingIPReceived.getFixedIPAddress());
250 assertEquals("Floating IP Address mismatch", FLOATING_IP_ADDRESS, neutronFloatingIPReceived.getFloatingIPAddress());
251 assertEquals("Tenant Id mismatch", UUID_VALUE, neutronFloatingIPReceived.getTenantUUID());
252 assertEquals("Router Id mismatch", UUID_VALUE, neutronFloatingIPReceived.getRouterUUID());
253 assertEquals("Status mismatch", STATUS, neutronFloatingIPReceived.getStatus());