Policy exclusions & parallel netconf transactions
[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 java.util.AbstractMap;
21 import java.util.concurrent.locks.ReentrantLock;
22
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
26 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
27 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
28 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
29 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
30 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
31 import org.opendaylight.groupbasedpolicy.renderer.vpp.commands.AbstractInterfaceCommand;
32 import org.opendaylight.groupbasedpolicy.renderer.vpp.commands.LoopbackCommand;
33 import org.opendaylight.vbd.impl.transaction.VbdNetconfTransaction;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceBuilder;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42
43 import com.google.common.base.Optional;
44 import com.google.common.util.concurrent.CheckedFuture;
45
46 public class GbpNetconfTransactionTest {
47
48     private final String INTERFACE_KEY = "interface-key";
49     private final String NODE_ID = "node-id";
50     private final DataBroker dataBroker = mock(DataBroker.class);
51     private final ReadWriteTransaction rwTx = mock(ReadWriteTransaction.class);
52     private final ReadOnlyTransaction rTx = mock(ReadOnlyTransaction.class);
53     private final Node node = mock(Node.class);
54     @SuppressWarnings("unchecked")
55     private final InstanceIdentifier<Node> nodeIid = mock(InstanceIdentifier.class);
56     @SuppressWarnings("unchecked")
57     private final CheckedFuture<Void, TransactionCommitFailedException> future = mock(CheckedFuture.class);
58     @SuppressWarnings("unchecked")
59     private final CheckedFuture<Optional<Node>, ReadFailedException> futureNode = mock(CheckedFuture.class);
60     @SuppressWarnings("unchecked")
61     private final CheckedFuture<Optional<Interface>, ReadFailedException> futureInterface = mock(CheckedFuture.class);
62     private final AbstractInterfaceCommand command = mock(LoopbackCommand.class);
63     private final InterfaceBuilder interfaceBuilder = new InterfaceBuilder().setKey(new InterfaceKey(INTERFACE_KEY));
64
65     @Before
66     public void init() {
67         when(dataBroker.newReadOnlyTransaction()).thenReturn(rTx);
68         when(dataBroker.newReadWriteTransaction()).thenReturn(rwTx);
69         VbdNetconfTransaction.NODE_DATA_BROKER_MAP.put(nodeIid,
70                 new AbstractMap.SimpleEntry(dataBroker, new ReentrantLock()));
71     }
72
73     @Test
74     public void writeConfigCommandReattemptTest() {
75         doThrow(new IllegalStateException()).when(command).execute(rwTx);
76
77         final boolean result = GbpNetconfTransaction.netconfSyncedWrite(nodeIid, command, (byte) 5);
78         verify(dataBroker, times(6)).newReadWriteTransaction();
79         assertFalse(result);
80     }
81
82     @Test
83     public void writeConfigCommandTest() throws Exception {
84         when(rwTx.submit()).thenReturn(future);
85         doNothing().when(command).execute(rwTx);
86         when(future.get()).thenReturn(null);
87
88         final boolean result = GbpNetconfTransaction.netconfSyncedWrite(nodeIid, command, (byte)5);
89         verify(dataBroker, times(1)).newReadWriteTransaction();
90         assertTrue(result);
91     }
92
93     @Test
94     public void writeDataReattemptTest() {
95         doThrow(new IllegalStateException()).when(rwTx).put(LogicalDatastoreType.CONFIGURATION, nodeIid, node, true);
96
97         final boolean result = GbpNetconfTransaction.netconfSyncedWrite(nodeIid, nodeIid, node, (byte) 5);
98         verify(dataBroker, times(6)).newReadWriteTransaction();
99         assertFalse(result);
100     }
101
102     @Test
103     public void writeDataTest() throws Exception {
104         when(rwTx.submit()).thenReturn(future);
105         doNothing().when(rwTx).put(LogicalDatastoreType.CONFIGURATION, nodeIid, node, true);
106         when(future.get()).thenReturn(null);
107
108         final boolean result = GbpNetconfTransaction.netconfSyncedWrite(nodeIid, nodeIid, node, (byte) 5);
109         verify(dataBroker, times(1)).newReadWriteTransaction();
110         assertTrue(result);
111     }
112
113     @Test
114     public void readDataReattemptTest() {
115         doThrow(new IllegalStateException()).when(rTx).read(LogicalDatastoreType.CONFIGURATION, nodeIid);
116
117         final Optional<Node> result = GbpNetconfTransaction.read(nodeIid, LogicalDatastoreType.CONFIGURATION,
118                 nodeIid, (byte)5);
119         verify(dataBroker, times(6)).newReadOnlyTransaction();
120         assertFalse(result.isPresent());
121     }
122
123     @Test
124     public void readDataTest() throws Exception {
125         when(rTx.read(LogicalDatastoreType.CONFIGURATION, nodeIid)).thenReturn(futureNode);
126         when(futureNode.get()).thenReturn(Optional.of(new NodeBuilder()
127                 .setKey(new NodeKey(new NodeId(NODE_ID))).build()));
128
129         final Optional<Node> result = GbpNetconfTransaction.read(nodeIid, LogicalDatastoreType.CONFIGURATION,
130                 nodeIid, (byte)5);
131         verify(dataBroker, times(1)).newReadOnlyTransaction();
132         assertTrue(result.isPresent());
133     }
134
135     @Test
136     public void deleteConfigCommandMissingDataTest() throws Exception {
137         final InstanceIdentifier<Interface> iid = VppIidFactory.getInterfaceIID(interfaceBuilder.getKey());
138         when(command.getInterfaceBuilder()).thenReturn(interfaceBuilder);
139         when(command.getIid()).thenReturn(iid);
140         when(rTx.read(LogicalDatastoreType.CONFIGURATION, iid)).thenReturn(futureInterface);
141         when(futureInterface.get()).thenReturn(Optional.absent());
142         doThrow(new IllegalStateException()).when(command).execute(rwTx);
143
144         final boolean result = GbpNetconfTransaction.netconfSyncedDelete(nodeIid, command, (byte)5);
145         verify(dataBroker, times(1)).newReadOnlyTransaction();
146         assertTrue(result);
147     }
148
149     @Test
150     public void deleteConfigCommandReattemptTest() throws Exception {
151         final InstanceIdentifier<Interface> iid = VppIidFactory.getInterfaceIID(interfaceBuilder.getKey());
152         when(command.getInterfaceBuilder()).thenReturn(interfaceBuilder);
153         when(command.getIid()).thenReturn(iid);
154         when(rTx.read(LogicalDatastoreType.CONFIGURATION, iid)).thenReturn(futureInterface);
155         when(futureInterface.get()).thenReturn(Optional.of(new InterfaceBuilder()
156                 .setKey(new InterfaceKey(INTERFACE_KEY)).build()));
157         doThrow(new IllegalStateException()).when(command).execute(rwTx);
158         final boolean result = GbpNetconfTransaction.netconfSyncedDelete(nodeIid, command, (byte)5);
159         verify(dataBroker, times(6)).newReadWriteTransaction();
160         assertFalse(result);
161     }
162
163     @Test
164     public void deleteConfigCommandTest() throws Exception {
165         final InstanceIdentifier<Interface> iid = VppIidFactory.getInterfaceIID(interfaceBuilder.getKey());
166         when(command.getInterfaceBuilder()).thenReturn(interfaceBuilder);
167         when(command.getIid()).thenReturn(iid);
168         when(rTx.read(LogicalDatastoreType.CONFIGURATION, iid)).thenReturn(futureInterface);
169         when(futureInterface.get()).thenReturn(Optional.of(new InterfaceBuilder()
170                 .setKey(new InterfaceKey(INTERFACE_KEY)).build()));
171         when(rwTx.submit()).thenReturn(future);
172         doNothing().when(command).execute(rwTx);
173         when(future.get()).thenReturn(null);
174
175         final boolean result = GbpNetconfTransaction.netconfSyncedDelete(nodeIid, command, (byte)5);
176         verify(dataBroker, times(1)).newReadWriteTransaction();
177         assertTrue(result);
178     }
179
180     @Test
181     public void deleteDataMissingDataTest() throws Exception {
182         when(rTx.read(LogicalDatastoreType.CONFIGURATION, nodeIid)).thenReturn(futureNode);
183         when(futureNode.get()).thenReturn(Optional.absent());
184         doThrow(new IllegalStateException()).when(command).execute(rwTx);
185
186         final boolean result = GbpNetconfTransaction.netconfSyncedDelete(nodeIid, nodeIid, (byte)5);
187         verify(dataBroker, times(1)).newReadOnlyTransaction();
188         assertTrue(result);
189     }
190
191     @Test
192     public void deleteDataReattemptTest() throws Exception {
193         when(rTx.read(LogicalDatastoreType.CONFIGURATION, nodeIid)).thenReturn(futureNode);
194         when(futureNode.get()).thenReturn(Optional.of(new NodeBuilder()
195                 .setKey(new NodeKey(new NodeId(NODE_ID))).build()));
196         doThrow(new IllegalStateException()).when(rwTx).delete(LogicalDatastoreType.CONFIGURATION, nodeIid);
197
198         final boolean result = GbpNetconfTransaction.netconfSyncedDelete(nodeIid, nodeIid, (byte)5);
199         verify(dataBroker, times(6)).newReadWriteTransaction();
200         assertFalse(result);
201     }
202
203     @Test
204     public void deleteDataTest() throws Exception {
205         when(rTx.read(LogicalDatastoreType.CONFIGURATION, nodeIid)).thenReturn(futureNode);
206         when(futureNode.get()).thenReturn(Optional.of(new NodeBuilder()
207                 .setKey(new NodeKey(new NodeId(NODE_ID))).build()));
208         when(rwTx.submit()).thenReturn(future);
209         doNothing().when(rwTx).delete(LogicalDatastoreType.CONFIGURATION, nodeIid);
210         when(future.get()).thenReturn(null);
211
212         final boolean result = GbpNetconfTransaction.netconfSyncedDelete(nodeIid, nodeIid, (byte)5);
213         verify(dataBroker, times(1)).newReadWriteTransaction();
214         assertTrue(result);
215     }
216 }