better odl <-> device communication via netconf - gbp side
[groupbasedpolicy.git] / renderers / vpp / src / test / java / org / opendaylight / groupbasedpolicy / renderer / vpp / util / GbpNetconfTransactionTest.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.renderer.vpp.util;
10
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;
19
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;
40
41 public class GbpNetconfTransactionTest {
42
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));
59
60     @Before
61     public void init() {
62         when(dataBroker.newReadOnlyTransaction()).thenReturn(rTx);
63         when(dataBroker.newReadWriteTransaction()).thenReturn(rwTx);
64     }
65
66     @Test
67     public void writeConfigCommandReattemptTest() {
68         doThrow(new IllegalStateException()).when(command).execute(rwTx);
69
70         final boolean result = GbpNetconfTransaction.write(dataBroker, command, (byte)5);
71         verify(dataBroker, times(6)).newReadWriteTransaction();
72         assertFalse(result);
73     }
74
75     @Test
76     public void writeConfigCommandTest() throws Exception {
77         when(rwTx.submit()).thenReturn(future);
78         doNothing().when(command).execute(rwTx);
79         when(future.get()).thenReturn(null);
80
81         final boolean result = GbpNetconfTransaction.write(dataBroker, command, (byte)5);
82         verify(dataBroker, times(1)).newReadWriteTransaction();
83         assertTrue(result);
84     }
85
86     @Test
87     public void writeDataReattemptTest() {
88         doThrow(new IllegalStateException()).when(rwTx).put(LogicalDatastoreType.CONFIGURATION, nodeIid, node, true);
89
90         final boolean result = GbpNetconfTransaction.write(dataBroker, nodeIid, node, (byte)5);
91         verify(dataBroker, times(6)).newReadWriteTransaction();
92         assertFalse(result);
93     }
94
95     @Test
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);
100
101         final boolean result = GbpNetconfTransaction.write(dataBroker, nodeIid, node, (byte)5);
102         verify(dataBroker, times(1)).newReadWriteTransaction();
103         assertTrue(result);
104     }
105
106     @Test
107     public void readDataReattemptTest() {
108         doThrow(new IllegalStateException()).when(rTx).read(LogicalDatastoreType.CONFIGURATION, nodeIid);
109
110         final Optional<Node> result = GbpNetconfTransaction.read(dataBroker, LogicalDatastoreType.CONFIGURATION,
111                 nodeIid, (byte)5);
112         verify(dataBroker, times(6)).newReadOnlyTransaction();
113         assertFalse(result.isPresent());
114     }
115
116     @Test
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()));
121
122         final Optional<Node> result = GbpNetconfTransaction.read(dataBroker, LogicalDatastoreType.CONFIGURATION,
123                 nodeIid, (byte)5);
124         verify(dataBroker, times(1)).newReadOnlyTransaction();
125         assertTrue(result.isPresent());
126     }
127
128     @Test
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);
135
136         final boolean result = GbpNetconfTransaction.deleteIfExists(dataBroker, command, (byte)5);
137         verify(dataBroker, times(1)).newReadOnlyTransaction();
138         assertTrue(result);
139     }
140
141     @Test
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);
149
150         final boolean result = GbpNetconfTransaction.deleteIfExists(dataBroker, command, (byte)5);
151         verify(dataBroker, times(6)).newReadWriteTransaction();
152         assertFalse(result);
153     }
154
155     @Test
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);
165
166         final boolean result = GbpNetconfTransaction.deleteIfExists(dataBroker, command, (byte)5);
167         verify(dataBroker, times(1)).newReadWriteTransaction();
168         assertTrue(result);
169     }
170
171     @Test
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);
176
177         final boolean result = GbpNetconfTransaction.deleteIfExists(dataBroker, nodeIid, (byte)5);
178         verify(dataBroker, times(1)).newReadOnlyTransaction();
179         assertTrue(result);
180     }
181
182     @Test
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);
188
189         final boolean result = GbpNetconfTransaction.deleteIfExists(dataBroker, nodeIid, (byte)5);
190         verify(dataBroker, times(6)).newReadWriteTransaction();
191         assertFalse(result);
192     }
193
194     @Test
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);
202
203         final boolean result = GbpNetconfTransaction.deleteIfExists(dataBroker, nodeIid, (byte)5);
204         verify(dataBroker, times(1)).newReadWriteTransaction();
205         assertTrue(result);
206     }
207 }