Add blueprint wiring for neutron renderer
[netvirt.git] / netvirt / renderers / neutron / src / test / java / org / opendaylight / netvirt / netvirt / renderers / neutron / NeutronPortDataProcessorTest.java
1 package org.opendaylight.netvirt.netvirt.renderers.neutron;
2
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;
8
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;
26
27 /**
28  * Unit test for {@link NeutronPortDataProcessor}
29  */
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;
37
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);
41
42         Optional<org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.ports.rev151227.ports.Port> data =
43             getDataBroker()
44             .newReadOnlyTransaction()
45             .read(LogicalDatastoreType.CONFIGURATION, portIid)
46             .get();
47         return data.orNull();
48     }
49
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());
57
58
59         return (new PortBuilder()
60                 .setStatus("Up")
61                 .setAdminStateUp(adminStateUp)
62                 .setName(name)
63                 .setDeviceOwner("compute:nova")
64                 .setDeviceId("12345678-1234-1234-1234-123456789012")
65                 .setUuid(uuid)
66                 .setMacAddress(new MacAddress("00:00:01:02:03:04"))
67                 .setFixedIps(ips)
68                 .build());
69     }
70
71     private void initialize() {
72         if (!initialized) {
73             session = mock(ProviderContext.class);
74             when(session.getSALService(DataBroker.class)).thenReturn(getDataBroker());
75             neutronPortDataProcessor = new NeutronPortDataProcessor(getDataBroker());
76             initialized = true;
77         }
78     }
79
80     @Test
81     public void testRemove() throws Exception {
82         //Do some setup and initialization
83         initialize();
84
85         //Create Neutron port
86         Port neutronPort = createNeutronPort(portId2, "testRemovePort", true);
87         InstanceIdentifier<Port> instanceIdentifier = InstanceIdentifier.create(Ports.class).child(Port.class);
88
89         //Add the Neutron port.This should result in a Netvirt port being created, and added to mdsal.
90         neutronPortDataProcessor.add(instanceIdentifier, neutronPort);
91
92         //Verify the Netvirt port was added to mdsal
93         assertNotNull(readFromMdSal(neutronPort.getUuid()));
94
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()));
98     }
99
100     @Test
101     public void testUpdate() throws Exception {
102         //Do some setup and initialization
103         initialize();
104
105         //Create Neutron port
106         Port neutronPort = createNeutronPort(portId3, "testUpdatePort", true);
107         InstanceIdentifier<Port> instanceIdentifier = InstanceIdentifier.create(Ports.class).child(Port.class);
108
109         //Add the Neutron port. This should result in a Netvirt port being created, and added to mdsal.
110         neutronPortDataProcessor.add(instanceIdentifier, neutronPort);
111
112         //Verify the Netvirt port was added to mdsal
113         assertNotNull(readFromMdSal(neutronPort.getUuid()));
114
115         //Create a second Neutron port, with different values for "name" and "AdminStateUp"
116         Port neutronPort1 = createNeutronPort(portId3, "portUpdatedTest", false);
117
118         //Update the Neutron port. This should result in the netvirt port in mdsal being updated, with a new name and
119         //admin state
120         neutronPortDataProcessor.update(instanceIdentifier, neutronPort, neutronPort1);
121
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());
127     }
128
129     @Test
130     public void testAdd() throws Exception {
131         //Do some setup and initialization
132         initialize();
133
134         //Create Neutron port.
135         Port neutronPort = createNeutronPort(portId, "testAddPort", true);
136         InstanceIdentifier<Port> instanceIdentifier = InstanceIdentifier.create(Ports.class).child(Port.class);
137
138         //Add the Neutron port.This should result in a Netvirt port being created, and added to mdsal.
139         neutronPortDataProcessor.add(instanceIdentifier, neutronPort);
140
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());
149     }
150
151 }