1 package org.opendaylight.netvirt.netvirt.renderers.neutron;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertNull;
6 import static org.mockito.Mockito.mock;
7 import static org.mockito.Mockito.when;
9 import com.google.common.base.Optional;
10 import java.util.ArrayList;
11 import java.util.List;
12 import org.junit.Test;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.test.AbstractDataBrokerTest;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.port.attributes.FixedIps;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.port.attributes.FixedIpsBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.Ports;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.PortBuilder;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 * Unit test for {@link NeutronPortDataProcessor}
30 public class NeutronPortDataProcessorTest extends AbstractDataBrokerTest {
31 private static final Uuid portId = new Uuid("aaaaaaaa-bbbb-cccc-dddd-123456789012");
32 private static final Uuid portId2 = new Uuid("11111111-2222-3333-4444-555555555555");
33 private static final Uuid portId3 = new Uuid("33333333-3333-3333-3333-333333333333");
34 ProviderContext session;
35 NeutronPortDataProcessor neutronPortDataProcessor;
36 boolean initialized = false;
38 private org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.ports.rev151227.ports.Port readFromMdSal(Uuid uuid) throws Exception {
39 InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.ports.rev151227.ports.Port> portIid =
40 MdsalHelper.createPortInstanceIdentifier(uuid);
42 Optional<org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.ports.rev151227.ports.Port> data =
44 .newReadOnlyTransaction()
45 .read(LogicalDatastoreType.CONFIGURATION, portIid)
50 private Port createNeutronPort(Uuid uuid, String name, boolean adminStateUp ) throws Exception {
51 String addr = "100.100.100.100";
52 List<FixedIps> ips = new ArrayList<>();
53 FixedIpsBuilder fixedIpsBuilder = new FixedIpsBuilder()
54 .setIpAddress(new IpAddress(addr.toCharArray()))
55 .setSubnetId(new Uuid("12345678-1234-1234-1234-222222222222"));
56 ips.add(fixedIpsBuilder.build());
59 return (new PortBuilder()
61 .setAdminStateUp(adminStateUp)
63 .setDeviceOwner("compute:nova")
64 .setDeviceId("12345678-1234-1234-1234-123456789012")
66 .setMacAddress(new MacAddress("00:00:01:02:03:04"))
71 private void initialize() {
73 session = mock(ProviderContext.class);
74 when(session.getSALService(DataBroker.class)).thenReturn(getDataBroker());
75 neutronPortDataProcessor = new NeutronPortDataProcessor(getDataBroker());
81 public void testRemove() throws Exception {
82 //Do some setup and initialization
86 Port neutronPort = createNeutronPort(portId2, "testRemovePort", true);
87 InstanceIdentifier<Port> instanceIdentifier = InstanceIdentifier.create(Ports.class).child(Port.class);
89 //Add the Neutron port.This should result in a Netvirt port being created, and added to mdsal.
90 neutronPortDataProcessor.add(instanceIdentifier, neutronPort);
92 //Verify the Netvirt port was added to mdsal
93 assertNotNull(readFromMdSal(neutronPort.getUuid()));
95 //Delete the Netvirt port that was just put into mdsal, and verify that it was removed from mdsal.
96 neutronPortDataProcessor.remove(instanceIdentifier, neutronPort);
97 assertNull(readFromMdSal(neutronPort.getUuid()));
101 public void testUpdate() throws Exception {
102 //Do some setup and initialization
105 //Create Neutron port
106 Port neutronPort = createNeutronPort(portId3, "testUpdatePort", true);
107 InstanceIdentifier<Port> instanceIdentifier = InstanceIdentifier.create(Ports.class).child(Port.class);
109 //Add the Neutron port. This should result in a Netvirt port being created, and added to mdsal.
110 neutronPortDataProcessor.add(instanceIdentifier, neutronPort);
112 //Verify the Netvirt port was added to mdsal
113 assertNotNull(readFromMdSal(neutronPort.getUuid()));
115 //Create a second Neutron port, with different values for "name" and "AdminStateUp"
116 Port neutronPort1 = createNeutronPort(portId3, "portUpdatedTest", false);
118 //Update the Neutron port. This should result in the netvirt port in mdsal being updated, with a new name and
120 neutronPortDataProcessor.update(instanceIdentifier, neutronPort, neutronPort1);
122 //Verify that the netvirt port was updated in mdsal
123 org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.ports.rev151227.ports.Port netvirtPort = readFromMdSal(neutronPort1.getUuid());
124 assertNotNull(netvirtPort);
125 assertEquals("Error, name not updated", netvirtPort.getName(), neutronPort1.getName());
126 assertEquals("Error, admin state not updated", netvirtPort.isAdminStateUp(), neutronPort1.isAdminStateUp());
130 public void testAdd() throws Exception {
131 //Do some setup and initialization
134 //Create Neutron port.
135 Port neutronPort = createNeutronPort(portId, "testAddPort", true);
136 InstanceIdentifier<Port> instanceIdentifier = InstanceIdentifier.create(Ports.class).child(Port.class);
138 //Add the Neutron port.This should result in a Netvirt port being created, and added to mdsal.
139 neutronPortDataProcessor.add(instanceIdentifier, neutronPort);
141 //Verify that the Netvirt port was added to mdsal, and that the contents of the Netvirt port are correct.
142 org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.ports.rev151227.ports.Port netvirtPort = readFromMdSal(neutronPort.getUuid());
143 assertNotNull(netvirtPort);
144 assertEquals("Error, status not correct", netvirtPort.getStatus(), neutronPort.getStatus());
145 assertEquals("Error, name not correct", netvirtPort.getName(), neutronPort.getName());
146 assertEquals("Error, admin state not correct", netvirtPort.isAdminStateUp(), neutronPort.isAdminStateUp());
147 assertEquals("Error, dev id is not correct", netvirtPort.getDeviceUuid().getValue(), neutronPort.getDeviceId());
148 assertEquals("Error, uuid is not correct", netvirtPort.getUuid().getValue(), neutronPort.getUuid().getValue());