2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.groupbasedpolicy.neutron.vpp.mapper.processors;
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;
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;
47 import com.google.common.base.Optional;
49 public class PortHandlerTest extends AbstractDataBrokerTest {
51 private DataBroker dataBroker;
52 private PortHandler portHandler;
53 private BindingTransactionChain transactionChain;
56 private SocketInfo socketInfo;
57 private BaseEndpointByPort bebp;
60 public ExpectedException thrown = ExpectedException.none();
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>() {
74 public ReadTransaction answer(InvocationOnMock invocation) throws Throwable {
75 return dataBroker.newReadOnlyTransaction();
78 when(transactionChain.newWriteOnlyTransaction()).thenAnswer(new Answer<WriteTransaction>() {
81 public WriteTransaction answer(InvocationOnMock invocation) throws Throwable {
82 return dataBroker.newWriteOnlyTransaction();
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());
102 public void testBuildVhostUserEndpoint_notValidVppEp() {
103 port = TestUtils.createNonVppPort();
104 thrown.expect(NullPointerException.class);
105 portHandler.buildVhostUserEndpoint(port, bebp);
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);
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());
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());
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());
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());
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();
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));
187 private void putBaseEpByPort(Port port, BaseEndpointByPort bebp, WriteTransaction rwTx) {
188 rwTx.put(LogicalDatastoreType.OPERATIONAL, TestUtils.createBaseEpByPortIid(port.getUuid()), bebp, true);