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
8 package org.opendaylight.ovsdb.utils.mdsal.utils;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNull;
12 import static org.junit.Assert.fail;
14 import com.google.common.util.concurrent.FluentFuture;
15 import com.google.common.util.concurrent.FutureCallback;
16 import com.google.common.util.concurrent.MoreExecutors;
17 import java.util.Collections;
18 import java.util.Optional;
19 import java.util.concurrent.ExecutionException;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.mockito.Mockito;
24 import org.mockito.junit.MockitoJUnitRunner;
25 import org.opendaylight.mdsal.binding.api.DataBroker;
26 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractDataBrokerTest;
27 import org.opendaylight.mdsal.common.api.CommitInfo;
28 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.node.attributes.SupportingNode;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.node.attributes.SupportingNodeBuilder;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.node.attributes.SupportingNodeKey;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 @RunWith(MockitoJUnitRunner.class)
43 public class MdsalUtilsAsyncTest extends AbstractDataBrokerTest {
45 private MdsalUtilsAsync mdsalUtilsAsync;
46 private DataBroker databroker;
48 private static final TopologyId TOPOLOGY_TEST = new TopologyId("test:1");
50 private static final NodeId NODE_ID = new NodeId("test");
51 private static final NodeKey NODE_KEY = new NodeKey(NODE_ID);
52 private static final Node DATA = new NodeBuilder().withKey(NODE_KEY).setNodeId(NODE_ID).build();
54 private static final InstanceIdentifier<Node> TEST_IID = InstanceIdentifier
55 .create(NetworkTopology.class)
56 .child(Topology.class, new TopologyKey(TOPOLOGY_TEST))
57 .child(Node.class, NODE_KEY);
61 databroker = getDataBroker();
62 mdsalUtilsAsync = Mockito.spy(new MdsalUtilsAsync(databroker));
66 public void testDelete() {
67 final FluentFuture<? extends CommitInfo> fut = mdsalUtilsAsync.put(
68 LogicalDatastoreType.CONFIGURATION, TEST_IID, DATA);
69 fut.addCallback(new FutureCallback<CommitInfo>() {
72 public void onSuccess(final CommitInfo result) {
73 final FluentFuture<? extends CommitInfo> future =
74 mdsalUtilsAsync.delete(LogicalDatastoreType.CONFIGURATION, TEST_IID);
75 future.addCallback(new FutureCallback<CommitInfo>() {
78 public void onSuccess(final CommitInfo result) {
83 public void onFailure(final Throwable ex) {
84 fail(ex.getMessage());
86 }, MoreExecutors.directExecutor());
90 public void onFailure(final Throwable ex) {
91 fail(ex.getMessage());
93 }, MoreExecutors.directExecutor());
97 public void testPutWithoutCallback() {
98 final String operationDesc = "testPut";
99 final SupportingNode supportingNodeBuilder1 = new SupportingNodeBuilder().withKey(
100 new SupportingNodeKey(new NodeId("id1"), TOPOLOGY_TEST)).build();
101 final SupportingNode supportingNodeBuilder2 = new SupportingNodeBuilder().withKey(
102 new SupportingNodeKey(new NodeId("id2"), TOPOLOGY_TEST)).build();
104 final Node data1 = new NodeBuilder(DATA).setSupportingNode(
105 Collections.singletonMap(supportingNodeBuilder1.key(), supportingNodeBuilder1)).build();
106 final Node data2 = new NodeBuilder(DATA).setSupportingNode(
107 Collections.singletonMap(supportingNodeBuilder2.key(), supportingNodeBuilder2)).build();
109 mdsalUtilsAsync.put(LogicalDatastoreType.CONFIGURATION, TEST_IID, data1, operationDesc);
110 assertEquals(data1, readDS());
112 final FluentFuture<? extends CommitInfo> future = mdsalUtilsAsync.put(
113 LogicalDatastoreType.CONFIGURATION, TEST_IID, data2);
114 future.addCallback(new FutureCallback<CommitInfo>() {
117 public void onSuccess(final CommitInfo result) {
118 assertEquals(1, readDS().getSupportingNode().size());
122 public void onFailure(final Throwable ex) {
123 fail(ex.getMessage());
125 }, MoreExecutors.directExecutor());
129 public void testMerge() {
130 final String operationDesc = "testMerge";
131 final SupportingNode supportingNodeBuilder1 = new SupportingNodeBuilder().withKey(
132 new SupportingNodeKey(new NodeId("id1"), TOPOLOGY_TEST)).build();
133 final SupportingNode supportingNodeBuilder2 = new SupportingNodeBuilder().withKey(
134 new SupportingNodeKey(new NodeId("id2"), TOPOLOGY_TEST)).build();
136 final Node data1 = new NodeBuilder(DATA).setSupportingNode(
137 Collections.singletonMap(supportingNodeBuilder1.key(), supportingNodeBuilder1)).build();
138 final Node data2 = new NodeBuilder(DATA).setSupportingNode(
139 Collections.singletonMap(supportingNodeBuilder2.key(), supportingNodeBuilder2)).build();
141 mdsalUtilsAsync.merge(LogicalDatastoreType.CONFIGURATION, TEST_IID, data1, operationDesc, true);
142 assertEquals(data1, readDS());
144 final FluentFuture<? extends CommitInfo> future =
145 mdsalUtilsAsync.merge(LogicalDatastoreType.CONFIGURATION, TEST_IID, data2, true);
146 future.addCallback(new FutureCallback<CommitInfo>() {
149 public void onSuccess(final CommitInfo result) {
150 assertEquals(2, readDS().getSupportingNode().size());
154 public void onFailure(final Throwable ex) {
155 fail(ex.getMessage());
157 }, MoreExecutors.directExecutor());
161 public void testRead() {
162 final FluentFuture<? extends CommitInfo> fut =
163 mdsalUtilsAsync.put(LogicalDatastoreType.CONFIGURATION, TEST_IID, DATA);
165 fut.addCallback(new FutureCallback<CommitInfo>() {
167 public void onSuccess(final CommitInfo result) {
168 final FluentFuture<Optional<Node>> future =
169 mdsalUtilsAsync.read(LogicalDatastoreType.CONFIGURATION, TEST_IID);
170 Optional<Node> optNode;
172 optNode = future.get();
173 if (optNode.isPresent()) {
174 assertEquals(DATA, optNode.get());
176 fail("Couldn't read node");
178 } catch (InterruptedException | ExecutionException e) {
179 fail(e.getMessage());
184 public void onFailure(final Throwable ex) {
185 fail(ex.getMessage());
187 }, MoreExecutors.directExecutor());
190 private Node readDS() {
192 final Optional<Node> result = databroker.newReadOnlyTransaction().read(
193 LogicalDatastoreType.CONFIGURATION, TEST_IID).get();
194 if (result.isPresent()) {
197 } catch (InterruptedException | ExecutionException e) {
198 fail(e.getMessage());