Add INFO.yaml for GBP
[groupbasedpolicy.git] / neutron-vpp-mapper / src / test / java / org / opendaylight / groupbasedpolicy / neutron / vpp / mapper / processors / PortHandlerTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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.groupbasedpolicy.neutron.vpp.mapper.processors;
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.assertTrue;
15 import static org.mockito.Matchers.any;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.when;
18
19 import org.junit.Before;
20 import org.junit.Rule;
21 import org.junit.Test;
22 import org.junit.rules.ExpectedException;
23 import org.mockito.Mockito;
24 import org.mockito.invocation.InvocationOnMock;
25 import org.mockito.stubbing.Answer;
26 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
27 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
28 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
29 import org.opendaylight.controller.md.sal.binding.api.ReadTransaction;
30 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
31 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
32 import org.opendaylight.controller.md.sal.binding.test.AbstractDataBrokerTest;
33 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
34 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
35 import org.opendaylight.groupbasedpolicy.util.DataStoreHelper;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.mappings.gbp.by.neutron.mappings.base.endpoints.by.ports.BaseEndpointByPort;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425._interface.attributes._interface.type.choice.LoopbackCase;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425._interface.attributes._interface.type.choice.VhostUserCase;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.config.VppEndpoint;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.RouterKey;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.Ports;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.PortBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48
49 import com.google.common.base.Optional;
50
51 public class PortHandlerTest extends AbstractDataBrokerTest {
52
53     private DataBroker dataBroker;
54     private PortHandler portHandler;
55     private BindingTransactionChain transactionChain;
56
57     private Port port;
58     private BaseEndpointByPort bebp;
59
60     @Rule
61     public ExpectedException thrown = ExpectedException.none();
62
63     @Before
64     public void init() {
65         port = TestUtils.createValidVppPort();
66         bebp = TestUtils.createBaseEndpointByPortForPort();
67         dataBroker = Mockito.spy(getDataBroker());
68         transactionChain = mock(BindingTransactionChain.class);
69         when(dataBroker.createTransactionChain(any(PortHandler.class))).thenReturn(transactionChain);
70         portHandler = new PortHandler(dataBroker, new NodeId("default"));
71         when(transactionChain.newReadOnlyTransaction()).thenAnswer(new Answer<ReadTransaction>() {
72
73             @Override
74             public ReadTransaction answer(InvocationOnMock invocation) throws Throwable {
75                 return dataBroker.newReadOnlyTransaction();
76             }
77         });
78         when(transactionChain.newWriteOnlyTransaction()).thenAnswer(new Answer<WriteTransaction>() {
79
80             @Override
81             public WriteTransaction answer(InvocationOnMock invocation) throws Throwable {
82                 return dataBroker.newWriteOnlyTransaction();
83             }
84         });
85     }
86
87     @Test
88     public void testBuildVhostUserEndpoint() {
89         VppEndpoint vppEp = portHandler.buildVppEndpoint(port, bebp);
90         assertEquals(vppEp.getAddress(), bebp.getAddress());
91         assertEquals(vppEp.getAddressType(), bebp.getAddressType());
92         assertEquals(vppEp.getContextId(), bebp.getContextId());
93         assertEquals(vppEp.getContextType(), bebp.getContextType());
94         assertTrue(vppEp.getInterfaceTypeChoice() instanceof VhostUserCase);
95         VhostUserCase vhostUserCase = (VhostUserCase) vppEp.getInterfaceTypeChoice();
96         assertNotNull(vhostUserCase);
97         assertEquals(TestUtils.TEST_SOCKET, vhostUserCase.getSocket());
98     }
99
100     @Test
101     public void testRoutingNodeConfig() {
102         TestUtils.writeQrouter(dataBroker, new RouterKey(new Uuid(TestUtils.DUMMY_UUID)));
103         port = new PortBuilder(port).setDeviceOwner(PortHandler.ROUTER_OWNER).build();
104         VppEndpoint vppEp = portHandler.buildVppEndpoint(port, bebp);
105         assertEquals(vppEp.getVppNodeId().getValue(), TestUtils.NODE_1);
106         assertTrue(vppEp.getInterfaceTypeChoice() instanceof LoopbackCase);
107         NodeId otherNode = new NodeId("devstack-compute-1");
108         portHandler = new PortHandler(dataBroker, otherNode);
109         vppEp = portHandler.buildVppEndpoint(port, bebp);
110         assertTrue(vppEp.getInterfaceTypeChoice() instanceof LoopbackCase);
111         assertEquals(otherNode, vppEp.getVppNodeId());
112     }
113
114     @Test
115     public void testBuildVhostUserEndpoint_notValidVppEp() {
116         port = TestUtils.createNonVppPort();
117         thrown.expect(NullPointerException.class);
118         portHandler.buildVppEndpoint(port, bebp);
119     }
120
121     @Test
122     public void testCreateWildcartedPortIid() throws TransactionCommitFailedException {
123         InstanceIdentifier<Port> iid = portHandler.createWildcartedPortIid();
124         Class<?>[] expectedTypes = {Neutron.class, Ports.class, Port.class};
125         TestUtils.assertPathArgumentTypes(iid.getPathArguments(), expectedTypes);
126         assertEquals(iid.getTargetType(), Port.class);
127     }
128
129     @Test
130     public void testProcessCreatedData() throws Exception {
131         portHandler.processCreatedData(port, bebp);
132         ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction();
133         Optional<VppEndpoint> optVppEp = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
134                 TestUtils.createVppEpIid(TestUtils.createVppEndpointKey(bebp)), rTx);
135         assertTrue(optVppEp.isPresent());
136     }
137
138     @Test
139     public void testProcessCreatedData_notValidVppEp() throws Exception {
140         port = TestUtils.createNonVppPort();
141         portHandler.processCreatedData(port, bebp);
142         ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction();
143         Optional<VppEndpoint> optVppEp = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
144                 TestUtils.createVppEpIid(TestUtils.createVppEndpointKey(bebp)), rTx);
145         assertFalse(optVppEp.isPresent());
146     }
147
148     @Test
149     public void testProcessUpdated() throws Exception {
150         WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
151         putVppEp(port, bebp, tx);
152         putBaseEpByPort(port, bebp, tx);
153         DataStoreHelper.submitToDs(tx);
154         portHandler.processUpdated(port, port);
155         ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction();
156         Optional<VppEndpoint> optVppEp = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
157                 TestUtils.createVppEpIid(TestUtils.createVppEndpointKey(bebp)), rTx);
158         assertTrue(optVppEp.isPresent());
159     }
160
161     @Test
162     public void testProcessUpdated_notValidVppEpAnymore() throws Exception {
163         Port delta = TestUtils.createNonVppPort();
164         ReadWriteTransaction tx = dataBroker.newReadWriteTransaction();
165         putVppEp(port, bebp, tx);
166         putBaseEpByPort(port, bebp, tx);
167         DataStoreHelper.submitToDs(tx);
168         portHandler.processUpdated(port, delta);
169         ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction();
170         Optional<VppEndpoint> optVppEp = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
171                 TestUtils.createVppEpIid(TestUtils.createVppEndpointKey(bebp)), rTx);
172         assertFalse(optVppEp.isPresent());
173     }
174
175     @Test
176     public void testProcessDeleted() throws Exception {
177         ReadWriteTransaction tx = dataBroker.newReadWriteTransaction();
178         putVppEp(port, bebp, tx);
179         DataStoreHelper.submitToDs(tx);
180         portHandler.processDeleted(bebp);
181         ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction();
182         Optional<VppEndpoint> optVppEp = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
183                 TestUtils.createVppEpIid(TestUtils.createVppEndpointKey(bebp)), rTx);
184         assertFalse(optVppEp.isPresent());
185     }
186
187     private void putVppEp(Port port, BaseEndpointByPort bebp, WriteTransaction rwTx) {
188         rwTx.put(LogicalDatastoreType.CONFIGURATION, TestUtils.createVppEpIid(TestUtils.createVppEndpointKey(bebp)),
189                 portHandler.buildVppEndpoint(port, bebp));
190     }
191
192     private void putBaseEpByPort(Port port, BaseEndpointByPort bebp, WriteTransaction rwTx) {
193         rwTx.put(LogicalDatastoreType.OPERATIONAL, TestUtils.createBaseEpByPortIid(port.getUuid()), bebp, true);
194     }
195 }