Update of mapping neutron port to vpp endpoint
[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.assertTrue;
14 import static org.junit.Assert.assertNotNull;
15 import static org.mockito.Mockito.any;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.times;
18 import static org.mockito.Mockito.verify;
19 import static org.mockito.Mockito.when;
20
21 import org.junit.Before;
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.junit.rules.ExpectedException;
25 import org.mockito.Mockito;
26 import org.mockito.invocation.InvocationOnMock;
27 import org.mockito.stubbing.Answer;
28 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
29 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
30 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
31 import org.opendaylight.controller.md.sal.binding.api.ReadTransaction;
32 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
33 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
34 import org.opendaylight.controller.md.sal.binding.test.AbstractDataBrokerTest;
35 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
36 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
37 import org.opendaylight.groupbasedpolicy.neutron.vpp.mapper.SocketInfo;
38 import org.opendaylight.groupbasedpolicy.util.DataStoreHelper;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.mappings.gbp.by.neutron.mappings.base.endpoints.by.ports.BaseEndpointByPort;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.config.VppEndpoint;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.config.vpp.endpoint._interface.type.choice.VhostUserCase;
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.rev150712.Neutron;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46
47 import com.google.common.base.Optional;
48
49 public class PortHandlerTest extends AbstractDataBrokerTest {
50
51     private DataBroker dataBroker;
52     private PortHandler portHandler;
53     private BindingTransactionChain transactionChain;
54
55     private Port port;
56     private SocketInfo socketInfo;
57     private BaseEndpointByPort bebp;
58
59     @Rule
60     public ExpectedException thrown = ExpectedException.none();
61
62     @Before
63     public void init() {
64         port = TestUtils.createValidVppPort();
65         bebp = TestUtils.createBaseEndpointByPortForPort();
66         socketInfo = new SocketInfo("/tmp/", "_socket");
67         dataBroker = Mockito.spy(getDataBroker());
68         transactionChain = mock(BindingTransactionChain.class);
69         when(dataBroker.createTransactionChain(any(PortHandler.class))).thenReturn(transactionChain);
70         portHandler = new PortHandler(dataBroker, socketInfo);
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.buildVhostUserEndpoint(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(vhostUserCase.getSocket(), socketInfo.getSocketPath() + socketInfo.getSocketPrefix()
98                 + bebp.getPortId().getValue());
99     }
100
101     @Test
102     public void testBuildVhostUserEndpoint_notValidVppEp() {
103         port = TestUtils.createNonVppPort();
104         thrown.expect(NullPointerException.class);
105         portHandler.buildVhostUserEndpoint(port, bebp);
106     }
107
108     @Test
109     public void testCreateWildcartedPortIid() throws TransactionCommitFailedException {
110         InstanceIdentifier<Port> iid = portHandler.createWildcartedPortIid();
111         Class<?>[] expectedTypes = {Neutron.class, Ports.class, Port.class};
112         TestUtils.assertPathArgumentTypes(iid.getPathArguments(), expectedTypes);
113         assertEquals(iid.getTargetType(), Port.class);
114     }
115
116     @Test
117     public void testProcessCreatedData() throws Exception {
118         portHandler.processCreatedData(port, bebp);
119         ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction();
120         Optional<VppEndpoint> optVppEp = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
121                 TestUtils.createVppEpIid(TestUtils.createVppEndpointKey(bebp)), rTx);
122         assertTrue(optVppEp.isPresent());
123     }
124
125     @Test
126     public void testProcessCreatedData_notValidVppEp() throws Exception {
127         port = TestUtils.createNonVppPort();
128         portHandler.processCreatedData(port, bebp);
129         ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction();
130         Optional<VppEndpoint> optVppEp = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
131                 TestUtils.createVppEpIid(TestUtils.createVppEndpointKey(bebp)), rTx);
132         assertFalse(optVppEp.isPresent());
133     }
134
135     @Test
136     public void testProcessUpdated() throws Exception {
137         WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
138         putVppEp(port, bebp, tx);
139         putBaseEpByPort(port, bebp, tx);
140         DataStoreHelper.submitToDs(tx);
141         portHandler.processUpdated(port, port);
142         ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction();
143         Optional<VppEndpoint> optVppEp = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
144                 TestUtils.createVppEpIid(TestUtils.createVppEndpointKey(bebp)), rTx);
145         verify(transactionChain).newReadOnlyTransaction();
146         verify(transactionChain, times(2)).newWriteOnlyTransaction();
147         assertTrue(optVppEp.isPresent());
148     }
149
150     @Test
151     public void testProcessUpdated_notValidVppEpAnymore() throws Exception {
152         Port delta = TestUtils.createNonVppPort();
153         ReadWriteTransaction tx = dataBroker.newReadWriteTransaction();
154         putVppEp(port, bebp, tx);
155         putBaseEpByPort(port, bebp, tx);
156         DataStoreHelper.submitToDs(tx);
157         portHandler.processUpdated(port, delta);
158         ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction();
159         Optional<VppEndpoint> optVppEp = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
160                 TestUtils.createVppEpIid(TestUtils.createVppEndpointKey(bebp)), rTx);
161         // looks for existing Vpp endpoint
162         verify(transactionChain).newReadOnlyTransaction();
163         // only removes former valid vpp endpoint
164         verify(transactionChain).newWriteOnlyTransaction();
165         assertFalse(optVppEp.isPresent());
166     }
167
168     @Test
169     public void testProcessDeleted() throws Exception {
170         ReadWriteTransaction tx = dataBroker.newReadWriteTransaction();
171         putVppEp(port, bebp, tx);
172         DataStoreHelper.submitToDs(tx);
173         portHandler.processDeleted(bebp);
174         ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction();
175         Optional<VppEndpoint> optVppEp = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
176                 TestUtils.createVppEpIid(TestUtils.createVppEndpointKey(bebp)), rTx);
177         assertFalse(optVppEp.isPresent());
178         verify(transactionChain).newReadOnlyTransaction();
179         verify(transactionChain).newWriteOnlyTransaction();
180     }
181
182     private void putVppEp(Port port, BaseEndpointByPort bebp, WriteTransaction rwTx) {
183         rwTx.put(LogicalDatastoreType.CONFIGURATION, TestUtils.createVppEpIid(TestUtils.createVppEndpointKey(bebp)),
184                 portHandler.buildVhostUserEndpoint(port, bebp));
185     }
186
187     private void putBaseEpByPort(Port port, BaseEndpointByPort bebp, WriteTransaction rwTx) {
188         rwTx.put(LogicalDatastoreType.OPERATIONAL, TestUtils.createBaseEpByPortIid(port.getUuid()), bebp, true);
189     }
190 }