7e2eadf2cefd01b1cb9d75f69650f9383cc9e91f
[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.neutron.vpp.mapper.SocketInfo;
36 import org.opendaylight.groupbasedpolicy.util.DataStoreHelper;
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.config.VppEndpoint;
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.neutron.ports.rev150712.ports.attributes.Ports;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44
45 import com.google.common.base.Optional;
46
47 public class PortHandlerTest extends AbstractDataBrokerTest {
48
49     private DataBroker dataBroker;
50     private PortHandler portHandler;
51     private BindingTransactionChain transactionChain;
52
53     private Port port;
54     private SocketInfo socketInfo;
55     private BaseEndpointByPort bebp;
56
57     @Rule
58     public ExpectedException thrown = ExpectedException.none();
59
60     @Before
61     public void init() {
62         port = TestUtils.createValidVppPort();
63         bebp = TestUtils.createBaseEndpointByPortForPort();
64         socketInfo = new SocketInfo("/tmp/", "_socket");
65         dataBroker = Mockito.spy(getDataBroker());
66         transactionChain = mock(BindingTransactionChain.class);
67         when(dataBroker.createTransactionChain(any(PortHandler.class))).thenReturn(transactionChain);
68         portHandler = new PortHandler(dataBroker, socketInfo);
69         when(transactionChain.newReadOnlyTransaction()).thenAnswer(new Answer<ReadTransaction>() {
70
71             @Override
72             public ReadTransaction answer(InvocationOnMock invocation) throws Throwable {
73                 return dataBroker.newReadOnlyTransaction();
74             }
75         });
76         when(transactionChain.newWriteOnlyTransaction()).thenAnswer(new Answer<WriteTransaction>() {
77
78             @Override
79             public WriteTransaction answer(InvocationOnMock invocation) throws Throwable {
80                 return dataBroker.newWriteOnlyTransaction();
81             }
82         });
83     }
84
85     @Test
86     public void testBuildVhostUserEndpoint() {
87         VppEndpoint vppEp = portHandler.buildVppEndpoint(port, bebp);
88         assertEquals(vppEp.getAddress(), bebp.getAddress());
89         assertEquals(vppEp.getAddressType(), bebp.getAddressType());
90         assertEquals(vppEp.getContextId(), bebp.getContextId());
91         assertEquals(vppEp.getContextType(), bebp.getContextType());
92         assertTrue(vppEp.getInterfaceTypeChoice() instanceof VhostUserCase);
93         VhostUserCase vhostUserCase = (VhostUserCase) vppEp.getInterfaceTypeChoice();
94         assertNotNull(vhostUserCase);
95         assertEquals(vhostUserCase.getSocket(), socketInfo.getSocketPath() + socketInfo.getSocketPrefix()
96                 + bebp.getPortId().getValue());
97     }
98
99     @Test
100     public void testBuildVhostUserEndpoint_notValidVppEp() {
101         port = TestUtils.createNonVppPort();
102         thrown.expect(NullPointerException.class);
103         portHandler.buildVppEndpoint(port, bebp);
104     }
105
106     @Test
107     public void testCreateWildcartedPortIid() throws TransactionCommitFailedException {
108         InstanceIdentifier<Port> iid = portHandler.createWildcartedPortIid();
109         Class<?>[] expectedTypes = {Neutron.class, Ports.class, Port.class};
110         TestUtils.assertPathArgumentTypes(iid.getPathArguments(), expectedTypes);
111         assertEquals(iid.getTargetType(), Port.class);
112     }
113
114     @Test
115     public void testProcessCreatedData() throws Exception {
116         portHandler.processCreatedData(port, bebp);
117         ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction();
118         Optional<VppEndpoint> optVppEp = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
119                 TestUtils.createVppEpIid(TestUtils.createVppEndpointKey(bebp)), rTx);
120         assertTrue(optVppEp.isPresent());
121     }
122
123     @Test
124     public void testProcessCreatedData_notValidVppEp() throws Exception {
125         port = TestUtils.createNonVppPort();
126         portHandler.processCreatedData(port, bebp);
127         ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction();
128         Optional<VppEndpoint> optVppEp = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
129                 TestUtils.createVppEpIid(TestUtils.createVppEndpointKey(bebp)), rTx);
130         assertFalse(optVppEp.isPresent());
131     }
132
133     @Test
134     public void testProcessUpdated() throws Exception {
135         WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
136         putVppEp(port, bebp, tx);
137         putBaseEpByPort(port, bebp, tx);
138         DataStoreHelper.submitToDs(tx);
139         portHandler.processUpdated(port, port);
140         ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction();
141         Optional<VppEndpoint> optVppEp = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
142                 TestUtils.createVppEpIid(TestUtils.createVppEndpointKey(bebp)), rTx);
143         assertTrue(optVppEp.isPresent());
144     }
145
146     @Test
147     public void testProcessUpdated_notValidVppEpAnymore() throws Exception {
148         Port delta = TestUtils.createNonVppPort();
149         ReadWriteTransaction tx = dataBroker.newReadWriteTransaction();
150         putVppEp(port, bebp, tx);
151         putBaseEpByPort(port, bebp, tx);
152         DataStoreHelper.submitToDs(tx);
153         portHandler.processUpdated(port, delta);
154         ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction();
155         Optional<VppEndpoint> optVppEp = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
156                 TestUtils.createVppEpIid(TestUtils.createVppEndpointKey(bebp)), rTx);
157         assertFalse(optVppEp.isPresent());
158     }
159
160     @Test
161     public void testProcessDeleted() throws Exception {
162         ReadWriteTransaction tx = dataBroker.newReadWriteTransaction();
163         putVppEp(port, bebp, tx);
164         DataStoreHelper.submitToDs(tx);
165         portHandler.processDeleted(bebp);
166         ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction();
167         Optional<VppEndpoint> optVppEp = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
168                 TestUtils.createVppEpIid(TestUtils.createVppEndpointKey(bebp)), rTx);
169         assertFalse(optVppEp.isPresent());
170     }
171
172     private void putVppEp(Port port, BaseEndpointByPort bebp, WriteTransaction rwTx) {
173         rwTx.put(LogicalDatastoreType.CONFIGURATION, TestUtils.createVppEpIid(TestUtils.createVppEndpointKey(bebp)),
174                 portHandler.buildVppEndpoint(port, bebp));
175     }
176
177     private void putBaseEpByPort(Port port, BaseEndpointByPort bebp, WriteTransaction rwTx) {
178         rwTx.put(LogicalDatastoreType.OPERATIONAL, TestUtils.createBaseEpByPortIid(port.getUuid()), bebp, true);
179     }
180 }