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._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;
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 BaseEndpointByPort bebp;
57 public ExpectedException thrown = ExpectedException.none();
61 port = TestUtils.createValidVppPort();
62 bebp = TestUtils.createBaseEndpointByPortForPort();
63 dataBroker = Mockito.spy(getDataBroker());
64 transactionChain = mock(BindingTransactionChain.class);
65 when(dataBroker.createTransactionChain(any(PortHandler.class))).thenReturn(transactionChain);
66 portHandler = new PortHandler(dataBroker);
67 when(transactionChain.newReadOnlyTransaction()).thenAnswer(new Answer<ReadTransaction>() {
70 public ReadTransaction answer(InvocationOnMock invocation) throws Throwable {
71 return dataBroker.newReadOnlyTransaction();
74 when(transactionChain.newWriteOnlyTransaction()).thenAnswer(new Answer<WriteTransaction>() {
77 public WriteTransaction answer(InvocationOnMock invocation) throws Throwable {
78 return dataBroker.newWriteOnlyTransaction();
84 public void testBuildVhostUserEndpoint() {
85 VppEndpoint vppEp = portHandler.buildVppEndpoint(port, bebp);
86 assertEquals(vppEp.getAddress(), bebp.getAddress());
87 assertEquals(vppEp.getAddressType(), bebp.getAddressType());
88 assertEquals(vppEp.getContextId(), bebp.getContextId());
89 assertEquals(vppEp.getContextType(), bebp.getContextType());
90 assertTrue(vppEp.getInterfaceTypeChoice() instanceof VhostUserCase);
91 VhostUserCase vhostUserCase = (VhostUserCase) vppEp.getInterfaceTypeChoice();
92 assertNotNull(vhostUserCase);
93 assertEquals(TestUtils.TEST_SOCKET, vhostUserCase.getSocket());
97 public void testBuildVhostUserEndpoint_notValidVppEp() {
98 port = TestUtils.createNonVppPort();
99 thrown.expect(NullPointerException.class);
100 portHandler.buildVppEndpoint(port, bebp);
104 public void testCreateWildcartedPortIid() throws TransactionCommitFailedException {
105 InstanceIdentifier<Port> iid = portHandler.createWildcartedPortIid();
106 Class<?>[] expectedTypes = {Neutron.class, Ports.class, Port.class};
107 TestUtils.assertPathArgumentTypes(iid.getPathArguments(), expectedTypes);
108 assertEquals(iid.getTargetType(), Port.class);
112 public void testProcessCreatedData() throws Exception {
113 portHandler.processCreatedData(port, bebp);
114 ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction();
115 Optional<VppEndpoint> optVppEp = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
116 TestUtils.createVppEpIid(TestUtils.createVppEndpointKey(bebp)), rTx);
117 assertTrue(optVppEp.isPresent());
121 public void testProcessCreatedData_notValidVppEp() throws Exception {
122 port = TestUtils.createNonVppPort();
123 portHandler.processCreatedData(port, bebp);
124 ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction();
125 Optional<VppEndpoint> optVppEp = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
126 TestUtils.createVppEpIid(TestUtils.createVppEndpointKey(bebp)), rTx);
127 assertFalse(optVppEp.isPresent());
131 public void testProcessUpdated() throws Exception {
132 WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
133 putVppEp(port, bebp, tx);
134 putBaseEpByPort(port, bebp, tx);
135 DataStoreHelper.submitToDs(tx);
136 portHandler.processUpdated(port, port);
137 ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction();
138 Optional<VppEndpoint> optVppEp = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
139 TestUtils.createVppEpIid(TestUtils.createVppEndpointKey(bebp)), rTx);
140 assertTrue(optVppEp.isPresent());
144 public void testProcessUpdated_notValidVppEpAnymore() throws Exception {
145 Port delta = TestUtils.createNonVppPort();
146 ReadWriteTransaction tx = dataBroker.newReadWriteTransaction();
147 putVppEp(port, bebp, tx);
148 putBaseEpByPort(port, bebp, tx);
149 DataStoreHelper.submitToDs(tx);
150 portHandler.processUpdated(port, delta);
151 ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction();
152 Optional<VppEndpoint> optVppEp = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
153 TestUtils.createVppEpIid(TestUtils.createVppEndpointKey(bebp)), rTx);
154 assertFalse(optVppEp.isPresent());
158 public void testProcessDeleted() throws Exception {
159 ReadWriteTransaction tx = dataBroker.newReadWriteTransaction();
160 putVppEp(port, bebp, tx);
161 DataStoreHelper.submitToDs(tx);
162 portHandler.processDeleted(bebp);
163 ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction();
164 Optional<VppEndpoint> optVppEp = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
165 TestUtils.createVppEpIid(TestUtils.createVppEndpointKey(bebp)), rTx);
166 assertFalse(optVppEp.isPresent());
169 private void putVppEp(Port port, BaseEndpointByPort bebp, WriteTransaction rwTx) {
170 rwTx.put(LogicalDatastoreType.CONFIGURATION, TestUtils.createVppEpIid(TestUtils.createVppEndpointKey(bebp)),
171 portHandler.buildVppEndpoint(port, bebp));
174 private void putBaseEpByPort(Port port, BaseEndpointByPort bebp, WriteTransaction rwTx) {
175 rwTx.put(LogicalDatastoreType.OPERATIONAL, TestUtils.createBaseEpByPortIid(port.getUuid()), bebp, true);