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.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;
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.config.vpp.endpoint._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;
45 import com.google.common.base.Optional;
47 public class PortHandlerTest extends AbstractDataBrokerTest {
49 private DataBroker dataBroker;
50 private PortHandler portHandler;
51 private BindingTransactionChain transactionChain;
54 private SocketInfo socketInfo;
55 private BaseEndpointByPort bebp;
58 public ExpectedException thrown = ExpectedException.none();
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>() {
72 public ReadTransaction answer(InvocationOnMock invocation) throws Throwable {
73 return dataBroker.newReadOnlyTransaction();
76 when(transactionChain.newWriteOnlyTransaction()).thenAnswer(new Answer<WriteTransaction>() {
79 public WriteTransaction answer(InvocationOnMock invocation) throws Throwable {
80 return dataBroker.newWriteOnlyTransaction();
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());
100 public void testBuildVhostUserEndpoint_notValidVppEp() {
101 port = TestUtils.createNonVppPort();
102 thrown.expect(NullPointerException.class);
103 portHandler.buildVppEndpoint(port, bebp);
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);
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());
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());
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());
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());
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());
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));
177 private void putBaseEpByPort(Port port, BaseEndpointByPort bebp, WriteTransaction rwTx) {
178 rwTx.put(LogicalDatastoreType.OPERATIONAL, TestUtils.createBaseEpByPortIid(port.getUuid()), bebp, true);