Change handling of netconf cluster transactions
[netconf.git] / netconf / netconf-topology-singleton / src / test / java / org / opendaylight / netconf / topology / singleton / impl / tx / ReadOnlyTransactionTest.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.netconf.topology.singleton.impl.tx;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.MockitoAnnotations.initMocks;
16 import static org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologyUtils.DEFAULT_SCHEMA_REPOSITORY;
17
18 import akka.actor.ActorRef;
19 import akka.actor.ActorSystem;
20 import akka.actor.Props;
21 import akka.pattern.Patterns;
22 import akka.testkit.JavaTestKit;
23 import akka.testkit.TestActorRef;
24 import akka.util.Timeout;
25 import com.google.common.base.Optional;
26 import com.google.common.collect.Lists;
27 import com.google.common.util.concurrent.CheckedFuture;
28 import com.google.common.util.concurrent.Futures;
29 import java.net.InetAddress;
30 import java.net.InetSocketAddress;
31 import java.net.UnknownHostException;
32 import java.util.List;
33 import java.util.concurrent.TimeUnit;
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.Rule;
37 import org.junit.Test;
38 import org.junit.rules.ExpectedException;
39 import org.mockito.Mock;
40 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
41 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
42 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
43 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
44 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
45 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
46 import org.opendaylight.netconf.topology.singleton.impl.ProxyDOMDataBroker;
47 import org.opendaylight.netconf.topology.singleton.impl.actors.NetconfNodeActor;
48 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologySetup;
49 import org.opendaylight.netconf.topology.singleton.messages.CreateInitialMasterActorData;
50 import org.opendaylight.netconf.topology.singleton.messages.MasterActorDataInitialized;
51 import org.opendaylight.yangtools.yang.common.QName;
52 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
53 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
54 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
55 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
56 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
57 import scala.concurrent.Await;
58 import scala.concurrent.Future;
59 import scala.concurrent.duration.Duration;
60
61 public class ReadOnlyTransactionTest {
62     private static final Timeout TIMEOUT = new Timeout(Duration.create(5, "seconds"));
63     private static final int TIMEOUT_SEC = 5;
64     private static ActorSystem system;
65
66     @Rule
67     public final ExpectedException exception = ExpectedException.none();
68
69     private ActorRef masterRef;
70     private ProxyDOMDataBroker slaveDataBroker;
71     private List<SourceIdentifier> sourceIdentifiers;
72     @Mock
73     private DOMDataBroker deviceDataBroker;
74     @Mock
75     private DOMDataReadOnlyTransaction readTx;
76     @Mock
77     private DOMRpcService domRpcService;
78
79     @Before
80     public void setup() throws UnknownHostException {
81         initMocks(this);
82
83         system = ActorSystem.create();
84
85         final RemoteDeviceId remoteDeviceId = new RemoteDeviceId("netconf-topology",
86                 new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 9999));
87
88         final NetconfTopologySetup setup = mock(NetconfTopologySetup.class);
89         final Props props = NetconfNodeActor.props(setup, remoteDeviceId, DEFAULT_SCHEMA_REPOSITORY,
90                 DEFAULT_SCHEMA_REPOSITORY);
91
92         masterRef = TestActorRef.create(system, props, "master_read");
93
94         sourceIdentifiers = Lists.newArrayList();
95
96         //device read tx
97         readTx = mock(DOMDataReadOnlyTransaction.class);
98         doReturn(readTx).when(deviceDataBroker).newReadOnlyTransaction();
99
100         // Create slave data broker for testing proxy
101         slaveDataBroker =
102                 new ProxyDOMDataBroker(system, remoteDeviceId, masterRef, Timeout.apply(5, TimeUnit.SECONDS));
103     }
104
105     @After
106     public void teardown() {
107         JavaTestKit.shutdownActorSystem(system, null, true);
108         system = null;
109     }
110
111     @Test
112     public void testRead() throws Exception {
113
114         /* Initialize data on master */
115
116         initializeDataTest();
117
118         final YangInstanceIdentifier instanceIdentifier = YangInstanceIdentifier.EMPTY;
119         final LogicalDatastoreType storeType = LogicalDatastoreType.CONFIGURATION;
120
121         // Message: EmptyReadResponse
122
123         final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> resultEmpty =
124                 Futures.immediateCheckedFuture(Optional.absent());
125
126         doReturn(resultEmpty).when(readTx).read(storeType, instanceIdentifier);
127
128         final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> resultEmptyResponse =
129                 slaveDataBroker.newReadOnlyTransaction().read(storeType,
130                         instanceIdentifier);
131
132         final Optional<NormalizedNode<?, ?>> resultEmptyMessage =
133                 resultEmptyResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
134
135         assertEquals(resultEmptyMessage, Optional.absent());
136
137         // Message: NormalizedNodeMessage
138
139         final NormalizedNode<?, ?> outputNode = ImmutableContainerNodeBuilder.create()
140                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create("TestQname")))
141                 .withChild(ImmutableNodes.leafNode(QName.create("NodeQname"), "foo")).build();
142
143         final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> resultNormalizedNodeMessage =
144                 Futures.immediateCheckedFuture(Optional.of(outputNode));
145
146         doReturn(resultNormalizedNodeMessage).when(readTx).read(storeType, instanceIdentifier);
147
148         final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> resultNodeMessageResponse =
149                 slaveDataBroker.newReadOnlyTransaction().read(storeType, instanceIdentifier);
150
151         final Optional<NormalizedNode<?, ?>> resultNodeMessage =
152                 resultNodeMessageResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
153
154         assertTrue(resultNodeMessage.isPresent());
155         assertEquals(resultNodeMessage.get(), outputNode);
156
157         // Message: Throwable
158
159         final ReadFailedException readFailedException = new ReadFailedException("Fail", null);
160         final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> resultThrowable =
161                 Futures.immediateFailedCheckedFuture(readFailedException);
162
163         doReturn(resultThrowable).when(readTx).read(storeType, instanceIdentifier);
164
165         final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> resultThrowableResponse =
166                 slaveDataBroker.newReadOnlyTransaction().read(storeType, instanceIdentifier);
167
168         exception.expect(ReadFailedException.class);
169         resultThrowableResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
170
171     }
172
173     @Test
174     public void testExist() throws Exception {
175
176         /* Initialize data on master */
177
178         initializeDataTest();
179
180         final YangInstanceIdentifier instanceIdentifier = YangInstanceIdentifier.EMPTY;
181         final LogicalDatastoreType storeType = LogicalDatastoreType.CONFIGURATION;
182
183         // Message: True
184
185         final CheckedFuture<Boolean, ReadFailedException> resultTrue =
186                 Futures.immediateCheckedFuture(true);
187
188         doReturn(resultTrue).when(readTx).exists(storeType, instanceIdentifier);
189
190         final CheckedFuture<Boolean, ReadFailedException> trueResponse =
191                 slaveDataBroker.newReadOnlyTransaction().exists(storeType, instanceIdentifier);
192
193         final Boolean trueMessage = trueResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
194
195         assertEquals(true, trueMessage);
196
197         // Message: False
198
199         final CheckedFuture<Boolean, ReadFailedException> resultFalse = Futures.immediateCheckedFuture(false);
200
201         doReturn(resultFalse).when(readTx).exists(storeType, instanceIdentifier);
202
203         final CheckedFuture<Boolean, ReadFailedException> falseResponse =
204                 slaveDataBroker.newReadOnlyTransaction().exists(storeType,
205                         instanceIdentifier);
206
207         final Boolean falseMessage = falseResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
208
209         assertEquals(false, falseMessage);
210
211         // Message: False, result null
212
213         final CheckedFuture<Boolean, ReadFailedException> resultNull = Futures.immediateCheckedFuture(null);
214
215         doReturn(resultNull).when(readTx).exists(storeType, instanceIdentifier);
216
217         final CheckedFuture<Boolean, ReadFailedException> nullResponse =
218                 slaveDataBroker.newReadOnlyTransaction().exists(storeType,
219                         instanceIdentifier);
220
221         final Boolean nullFalseMessage = nullResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
222
223         assertEquals(false, nullFalseMessage);
224
225         // Message: Throwable
226
227         final ReadFailedException readFailedException = new ReadFailedException("Fail", null);
228         final CheckedFuture<Boolean, ReadFailedException> resultThrowable =
229                 Futures.immediateFailedCheckedFuture(readFailedException);
230
231         doReturn(resultThrowable).when(readTx).exists(storeType, instanceIdentifier);
232
233         final CheckedFuture<Boolean, ReadFailedException> resultThrowableResponse =
234                 slaveDataBroker.newReadOnlyTransaction().exists(storeType, instanceIdentifier);
235
236         exception.expect(ReadFailedException.class);
237         resultThrowableResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
238
239     }
240
241     private void initializeDataTest() throws Exception {
242         final Future<Object> initialDataToActor =
243                 Patterns.ask(masterRef, new CreateInitialMasterActorData(deviceDataBroker, sourceIdentifiers,
244                                 domRpcService), TIMEOUT);
245
246         final Object success = Await.result(initialDataToActor, TIMEOUT.duration());
247
248         assertTrue(success instanceof MasterActorDataInitialized);
249     }
250 }