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.renderer.vpp.util;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Mockito.doNothing;
14 import static org.mockito.Mockito.doThrow;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.times;
17 import static org.mockito.Mockito.verify;
18 import static org.mockito.Mockito.when;
20 import com.google.common.base.Optional;
21 import com.google.common.util.concurrent.CheckedFuture;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
25 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
26 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
29 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
30 import org.opendaylight.groupbasedpolicy.renderer.vpp.commands.ConfigCommand;
31 import org.opendaylight.groupbasedpolicy.renderer.vpp.commands.LoopbackCommand;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceBuilder;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 public class GbpNetconfTransactionTest {
43 private final String INTERFACE_KEY = "interface-key";
44 private final String NODE_ID = "node-id";
45 private final DataBroker dataBroker = mock(DataBroker.class);
46 private final ReadWriteTransaction rwTx = mock(ReadWriteTransaction.class);
47 private final ReadOnlyTransaction rTx = mock(ReadOnlyTransaction.class);
48 private final Node node = mock(Node.class);
49 @SuppressWarnings("unchecked")
50 private final InstanceIdentifier<Node> nodeIid = mock(InstanceIdentifier.class);
51 @SuppressWarnings("unchecked")
52 private final CheckedFuture<Void, TransactionCommitFailedException> future = mock(CheckedFuture.class);
53 @SuppressWarnings("unchecked")
54 private final CheckedFuture<Optional<Node>, ReadFailedException> futureNode = mock(CheckedFuture.class);
55 @SuppressWarnings("unchecked")
56 private final CheckedFuture<Optional<Interface>, ReadFailedException> futureInterface = mock(CheckedFuture.class);
57 private final ConfigCommand command = mock(LoopbackCommand.class);
58 private final InterfaceBuilder interfaceBuilder = new InterfaceBuilder().setKey(new InterfaceKey(INTERFACE_KEY));
62 when(dataBroker.newReadOnlyTransaction()).thenReturn(rTx);
63 when(dataBroker.newReadWriteTransaction()).thenReturn(rwTx);
67 public void writeConfigCommandReattemptTest() {
68 doThrow(new IllegalStateException()).when(command).execute(rwTx);
70 final boolean result = GbpNetconfTransaction.write(dataBroker, command, (byte)5);
71 verify(dataBroker, times(6)).newReadWriteTransaction();
76 public void writeConfigCommandTest() throws Exception {
77 when(rwTx.submit()).thenReturn(future);
78 doNothing().when(command).execute(rwTx);
79 when(future.get()).thenReturn(null);
81 final boolean result = GbpNetconfTransaction.write(dataBroker, command, (byte)5);
82 verify(dataBroker, times(1)).newReadWriteTransaction();
87 public void writeDataReattemptTest() {
88 doThrow(new IllegalStateException()).when(rwTx).put(LogicalDatastoreType.CONFIGURATION, nodeIid, node, true);
90 final boolean result = GbpNetconfTransaction.write(dataBroker, nodeIid, node, (byte)5);
91 verify(dataBroker, times(6)).newReadWriteTransaction();
96 public void writeDataTest() throws Exception {
97 when(rwTx.submit()).thenReturn(future);
98 doNothing().when(rwTx).put(LogicalDatastoreType.CONFIGURATION, nodeIid, node, true);
99 when(future.get()).thenReturn(null);
101 final boolean result = GbpNetconfTransaction.write(dataBroker, nodeIid, node, (byte)5);
102 verify(dataBroker, times(1)).newReadWriteTransaction();
107 public void readDataReattemptTest() {
108 doThrow(new IllegalStateException()).when(rTx).read(LogicalDatastoreType.CONFIGURATION, nodeIid);
110 final Optional<Node> result = GbpNetconfTransaction.read(dataBroker, LogicalDatastoreType.CONFIGURATION,
112 verify(dataBroker, times(6)).newReadOnlyTransaction();
113 assertFalse(result.isPresent());
117 public void readDataTest() throws Exception {
118 when(rTx.read(LogicalDatastoreType.CONFIGURATION, nodeIid)).thenReturn(futureNode);
119 when(futureNode.get()).thenReturn(Optional.of(new NodeBuilder()
120 .setKey(new NodeKey(new NodeId(NODE_ID))).build()));
122 final Optional<Node> result = GbpNetconfTransaction.read(dataBroker, LogicalDatastoreType.CONFIGURATION,
124 verify(dataBroker, times(1)).newReadOnlyTransaction();
125 assertTrue(result.isPresent());
129 public void deleteConfigCommandMissingDataTest() throws Exception {
130 final InstanceIdentifier<Interface> iid = VppIidFactory.getInterfaceIID(interfaceBuilder.getKey());
131 when(command.getInterfaceBuilder()).thenReturn(interfaceBuilder);
132 when(rTx.read(LogicalDatastoreType.CONFIGURATION, iid)).thenReturn(futureInterface);
133 when(futureInterface.get()).thenReturn(Optional.absent());
134 doThrow(new IllegalStateException()).when(command).execute(rwTx);
136 final boolean result = GbpNetconfTransaction.deleteIfExists(dataBroker, command, (byte)5);
137 verify(dataBroker, times(1)).newReadOnlyTransaction();
142 public void deleteConfigCommandReattemptTest() throws Exception {
143 final InstanceIdentifier<Interface> iid = VppIidFactory.getInterfaceIID(interfaceBuilder.getKey());
144 when(command.getInterfaceBuilder()).thenReturn(interfaceBuilder);
145 when(rTx.read(LogicalDatastoreType.CONFIGURATION, iid)).thenReturn(futureInterface);
146 when(futureInterface.get()).thenReturn(Optional.of(new InterfaceBuilder()
147 .setKey(new InterfaceKey(INTERFACE_KEY)).build()));
148 doThrow(new IllegalStateException()).when(command).execute(rwTx);
150 final boolean result = GbpNetconfTransaction.deleteIfExists(dataBroker, command, (byte)5);
151 verify(dataBroker, times(6)).newReadWriteTransaction();
156 public void deleteConfigCommandTest() throws Exception {
157 final InstanceIdentifier<Interface> iid = VppIidFactory.getInterfaceIID(interfaceBuilder.getKey());
158 when(command.getInterfaceBuilder()).thenReturn(interfaceBuilder);
159 when(rTx.read(LogicalDatastoreType.CONFIGURATION, iid)).thenReturn(futureInterface);
160 when(futureInterface.get()).thenReturn(Optional.of(new InterfaceBuilder()
161 .setKey(new InterfaceKey(INTERFACE_KEY)).build()));
162 when(rwTx.submit()).thenReturn(future);
163 doNothing().when(command).execute(rwTx);
164 when(future.get()).thenReturn(null);
166 final boolean result = GbpNetconfTransaction.deleteIfExists(dataBroker, command, (byte)5);
167 verify(dataBroker, times(1)).newReadWriteTransaction();
172 public void deleteDataMissingDataTest() throws Exception {
173 when(rTx.read(LogicalDatastoreType.CONFIGURATION, nodeIid)).thenReturn(futureNode);
174 when(futureNode.get()).thenReturn(Optional.absent());
175 doThrow(new IllegalStateException()).when(command).execute(rwTx);
177 final boolean result = GbpNetconfTransaction.deleteIfExists(dataBroker, nodeIid, (byte)5);
178 verify(dataBroker, times(1)).newReadOnlyTransaction();
183 public void deleteDataReattemptTest() throws Exception {
184 when(rTx.read(LogicalDatastoreType.CONFIGURATION, nodeIid)).thenReturn(futureNode);
185 when(futureNode.get()).thenReturn(Optional.of(new NodeBuilder()
186 .setKey(new NodeKey(new NodeId(NODE_ID))).build()));
187 doThrow(new IllegalStateException()).when(rwTx).delete(LogicalDatastoreType.CONFIGURATION, nodeIid);
189 final boolean result = GbpNetconfTransaction.deleteIfExists(dataBroker, nodeIid, (byte)5);
190 verify(dataBroker, times(6)).newReadWriteTransaction();
195 public void deleteDataTest() throws Exception {
196 when(rTx.read(LogicalDatastoreType.CONFIGURATION, nodeIid)).thenReturn(futureNode);
197 when(futureNode.get()).thenReturn(Optional.of(new NodeBuilder()
198 .setKey(new NodeKey(new NodeId(NODE_ID))).build()));
199 when(rwTx.submit()).thenReturn(future);
200 doNothing().when(rwTx).delete(LogicalDatastoreType.CONFIGURATION, nodeIid);
201 when(future.get()).thenReturn(null);
203 final boolean result = GbpNetconfTransaction.deleteIfExists(dataBroker, nodeIid, (byte)5);
204 verify(dataBroker, times(1)).newReadWriteTransaction();