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