2 * Copyright © 2016, 2017 Inocybe 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.ovsdb.utils.mdsal.utils;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.fail;
15 import com.google.common.base.Optional;
16 import com.google.common.util.concurrent.CheckedFuture;
17 import com.google.common.util.concurrent.FutureCallback;
18 import com.google.common.util.concurrent.Futures;
19 import com.google.common.util.concurrent.MoreExecutors;
20 import java.util.Collections;
21 import java.util.concurrent.ExecutionException;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.mockito.Mockito;
26 import org.mockito.runners.MockitoJUnitRunner;
27 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
28 import org.opendaylight.controller.md.sal.binding.test.AbstractDataBrokerTest;
29 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
30 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
31 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.node.attributes.SupportingNode;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.node.attributes.SupportingNodeBuilder;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.node.attributes.SupportingNodeKey;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 @RunWith(MockitoJUnitRunner.class)
46 public class MdsalUtilsAsyncTest extends AbstractDataBrokerTest {
48 private MdsalUtilsAsync mdsalUtilsAsync;
49 private DataBroker databroker;
51 private static final TopologyId TOPOLOGY_TEST = new TopologyId("test:1");
53 private static final NodeId NODE_ID = new NodeId("test");
54 private static final NodeKey NODE_KEY = new NodeKey(NODE_ID);
55 private static final Node DATA = new NodeBuilder().setKey(NODE_KEY).setNodeId(NODE_ID).build();
57 private static final InstanceIdentifier<Node> TEST_IID = InstanceIdentifier
58 .create(NetworkTopology.class)
59 .child(Topology.class, new TopologyKey(TOPOLOGY_TEST))
60 .child(Node.class, NODE_KEY);
64 databroker = getDataBroker();
65 mdsalUtilsAsync = Mockito.spy(new MdsalUtilsAsync(databroker));
69 public void testDelete() {
70 final CheckedFuture<Void, TransactionCommitFailedException> fut = mdsalUtilsAsync.put(
71 LogicalDatastoreType.CONFIGURATION, TEST_IID, DATA);
72 Futures.addCallback(fut, new FutureCallback<Void>() {
75 public void onSuccess(final Void result) {
76 final CheckedFuture<Void, TransactionCommitFailedException> future =
77 mdsalUtilsAsync.delete(LogicalDatastoreType.CONFIGURATION, TEST_IID);
78 Futures.addCallback(future, new FutureCallback<Void>() {
81 public void onSuccess(final Void result) {
86 public void onFailure(final Throwable ex) {
87 fail(ex.getMessage());
89 }, MoreExecutors.directExecutor());
93 public void onFailure(final Throwable ex) {
94 fail(ex.getMessage());
96 }, MoreExecutors.directExecutor());
100 public void testPutWithoutCallback() {
101 final String operationDesc = "testPut";
102 final SupportingNode supportingNodeBuilder1 = new SupportingNodeBuilder().setKey(
103 new SupportingNodeKey(new NodeId("id1"), TOPOLOGY_TEST)).build();
104 final SupportingNode supportingNodeBuilder2 = new SupportingNodeBuilder().setKey(
105 new SupportingNodeKey(new NodeId("id2"), TOPOLOGY_TEST)).build();
107 final Node data1 = new NodeBuilder(DATA).setSupportingNode(
108 Collections.singletonList(supportingNodeBuilder1)).build();
109 final Node data2 = new NodeBuilder(DATA).setSupportingNode(
110 Collections.singletonList(supportingNodeBuilder2)).build();
112 mdsalUtilsAsync.put(LogicalDatastoreType.CONFIGURATION, TEST_IID, data1, operationDesc);
113 assertEquals(data1, readDS());
115 final CheckedFuture<Void, TransactionCommitFailedException> future = mdsalUtilsAsync.put(
116 LogicalDatastoreType.CONFIGURATION, TEST_IID, data2);
117 Futures.addCallback(future, new FutureCallback<Void>() {
120 public void onSuccess(final Void result) {
121 assertEquals(1, readDS().getSupportingNode().size());
125 public void onFailure(final Throwable ex) {
126 fail(ex.getMessage());
128 }, MoreExecutors.directExecutor());
132 public void testMerge() {
133 final String operationDesc = "testMerge";
134 final SupportingNode supportingNodeBuilder1 = new SupportingNodeBuilder().setKey(
135 new SupportingNodeKey(new NodeId("id1"), TOPOLOGY_TEST)).build();
136 final SupportingNode supportingNodeBuilder2 = new SupportingNodeBuilder().setKey(
137 new SupportingNodeKey(new NodeId("id2"), TOPOLOGY_TEST)).build();
139 final Node data1 = new NodeBuilder(DATA).setSupportingNode(
140 Collections.singletonList(supportingNodeBuilder1)).build();
141 final Node data2 = new NodeBuilder(DATA).setSupportingNode(
142 Collections.singletonList(supportingNodeBuilder2)).build();
144 mdsalUtilsAsync.merge(LogicalDatastoreType.CONFIGURATION, TEST_IID, data1, operationDesc, true);
145 assertEquals(data1, readDS());
147 final CheckedFuture<Void, TransactionCommitFailedException> future =
148 mdsalUtilsAsync.merge(LogicalDatastoreType.CONFIGURATION, TEST_IID, data2, true);
149 Futures.addCallback(future, new FutureCallback<Void>() {
152 public void onSuccess(final Void result) {
153 assertEquals(2, readDS().getSupportingNode().size());
157 public void onFailure(final Throwable ex) {
158 fail(ex.getMessage());
160 }, MoreExecutors.directExecutor());
164 public void testRead() {
165 final CheckedFuture<Void, TransactionCommitFailedException> fut =
166 mdsalUtilsAsync.put(LogicalDatastoreType.CONFIGURATION, TEST_IID, DATA);
168 Futures.addCallback(fut, new FutureCallback<Void>() {
170 public void onSuccess(final Void result) {
171 final CheckedFuture<Optional<Node>, ReadFailedException> future =
172 mdsalUtilsAsync.read(LogicalDatastoreType.CONFIGURATION, TEST_IID);
173 Optional<Node> optNode;
175 optNode = future.get();
176 if (optNode.isPresent()) {
177 assertEquals(DATA, optNode.get());
179 fail("Couldn't read node");
181 } catch (InterruptedException | ExecutionException e) {
182 fail(e.getMessage());
187 public void onFailure(final Throwable ex) {
188 fail(ex.getMessage());
190 }, MoreExecutors.directExecutor());
193 private Node readDS() {
195 final Optional<Node> result = databroker.newReadOnlyTransaction().read(
196 LogicalDatastoreType.CONFIGURATION, TEST_IID).get();
197 if (result.isPresent()) {
200 } catch (InterruptedException | ExecutionException e) {
201 fail(e.getMessage());