Add blueprint wiring for openstack/net-virt
[netvirt.git] / openstack / net-virt / src / test / java / org / opendaylight / netvirt / openstack / netvirt / translator / crud / impl / NeutronFloatingIPInterfaceTest.java
1 /*
2  * Copyright (c) 2015, 2016 NEC 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 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;
16
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;
29
30 /**
31  * Unit test for {@link NeutronFloatingIPInterface}
32  */
33 public class NeutronFloatingIPInterfaceTest extends AbstractDataBrokerTest {
34     /**
35      * UUID_VALUE used for testing different scenarios.
36      */
37     private static final String UUID_VALUE = "b9a13232-525e-4d8c-be21-cd65e3436034";
38     /**
39      * FIXED_IP_ADDRESS used for testing different scenarios.
40      */
41     private static final String FIXED_IP_ADDRESS = "10.0.0.3";
42     /**
43      * FLOATING_IP_ADDRESS used for testing different scenarios.
44      */
45     private static final String FLOATING_IP_ADDRESS = "172.24.4.228";
46     /**
47      * STATUS used for testing different scenarios.
48      */
49     private static final String STATUS = "ACTIVE";
50
51     private NeutronFloatingIPInterface getTestInterface(DataBroker broker) {
52         return new NeutronFloatingIPInterface(broker);
53     }
54
55     /**
56      * Test that checks if @{NeutronFloatingIPInterface#floatingIPExists} is called
57      * and then checks that floating Ip exists or not.
58      */
59     @Test
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);
64
65         // First case: the underlying data broker returns nothing (we haven't inserted the IP yet)
66         assertFalse(testInterface.floatingIPExists(UUID_VALUE));
67
68         // Add an IP
69         addTestFloatingIP(broker, testInterface);
70
71         // Second case: the underlying data broker returns something
72         assertTrue(testInterface.floatingIPExists(UUID_VALUE));
73     }
74
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();
82     }
83
84     /**
85      * Test that checks if @{NeutronFloatingIPInterface#getFloatingIP} is called
86      * and then checks that it gets floating Ip or not.
87      */
88     @Test
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);
93
94         // First case: the underlying data broker returns nothing (we haven't inserted the IP yet)
95         assertNull(testInterface.getFloatingIP(UUID_VALUE));
96
97         // Add an IP
98         addTestFloatingIP(broker, testInterface);
99
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());
104     }
105
106     /**
107      * Test that checks if @{NeutronFloatingIPInterface#getAllFloatingIPs} is called
108      * and then checks that it gets all floating Ips in a list or not.
109      */
110     @Test
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);
115
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());
118
119         // Add an IP
120         addTestFloatingIP(broker, testInterface);
121
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());
127     }
128
129     /**
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.
133      */
134     @Test
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);
139
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));
144
145         // TODO Retrieve the floating IP directly and make sure it's correct
146
147         // Second case: the floating IP is already present
148         assertFalse("Floating IP missing", testInterface.addFloatingIP(insertedFloatingIp));
149     }
150
151     /**
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.
155      */
156     @Test
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);
161
162         // First case: the floating IP isn't present
163         assertFalse("Floating IP present", testInterface.removeFloatingIP(UUID_VALUE));
164
165         // Add an IP
166         addTestFloatingIP(broker, testInterface);
167
168         // Second case: the floating IP is present
169         assertTrue("Floating IP absent", testInterface.removeFloatingIP(UUID_VALUE));
170
171         // TODO Attempt to retrieve the floating IP and make sure it's absent
172     }
173
174     /**
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.
178      */
179     @Test
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);
184
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));
189
190         // Add an IP
191         addTestFloatingIP(broker, testInterface);
192
193         // Second case: the floating IP is present
194         assertTrue("Floating IP absent", testInterface.updateFloatingIP(UUID_VALUE, testFloatingIp));
195
196         // TODO Change some attributes and make sure they're updated
197     }
198
199     /**
200      * Test that checks if @{NeutronFloatingIPInterface#toMd} is called
201      * and then checks that it sets vales into floating Ip.
202      */
203     @Test
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());
225     }
226
227     /**
228      * Test that checks if @{NeutronFloatingIPInterface#fromMd} is called
229      * and then checks that it gets values from Floating Ip.
230      */
231     @Test
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))
237                 .setFixedIpAddress(
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());
254     }
255 }