2 * Copyright (c) 2016 Cisco Systems, Inc. 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.controller.cluster.datastore.persisted;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.fail;
15 import java.io.IOException;
16 import java.util.Collection;
17 import org.apache.commons.lang3.SerializationUtils;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.opendaylight.controller.cluster.datastore.AbstractTest;
21 import org.opendaylight.controller.cluster.datastore.persisted.CommitTransactionPayload.CandidateTransaction;
22 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
23 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
29 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
30 import org.opendaylight.yangtools.yang.data.tree.api.DataTree;
31 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
32 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidateNode;
33 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
34 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeModification;
35 import org.opendaylight.yangtools.yang.data.tree.impl.di.InMemoryDataTreeFactory;
36 import org.opendaylight.yangtools.yang.data.tree.spi.DataTreeCandidates;
38 public class CommitTransactionPayloadTest extends AbstractTest {
39 static final QName LEAF_SET = QName.create(TestModel.TEST_QNAME, "leaf-set");
41 private DataTreeCandidate candidate;
43 private static DataTreeCandidateNode findNode(final Collection<DataTreeCandidateNode> nodes,
44 final PathArgument arg) {
45 for (DataTreeCandidateNode node : nodes) {
46 if (arg.equals(node.name())) {
53 private static void assertChildrenEquals(final Collection<DataTreeCandidateNode> expected,
54 final Collection<DataTreeCandidateNode> actual) {
55 // Make sure all expected nodes are there
56 for (DataTreeCandidateNode exp : expected) {
57 final DataTreeCandidateNode act = findNode(actual, exp.name());
58 assertNotNull("missing expected child", act);
59 assertCandidateNodeEquals(exp, act);
61 // Make sure no nodes are present which are not in the expected set
62 for (DataTreeCandidateNode act : actual) {
63 final DataTreeCandidateNode exp = findNode(expected, act.name());
64 assertNull("unexpected child", exp);
68 private static void assertCandidateEquals(final DataTreeCandidate expected, final CandidateTransaction actual) {
69 final var candidate = actual.candidate();
70 assertEquals("root path", expected.getRootPath(), candidate.getRootPath());
71 assertCandidateNodeEquals(expected.getRootNode(), candidate.getRootNode());
74 private static void assertCandidateNodeEquals(final DataTreeCandidateNode expected,
75 final DataTreeCandidateNode actual) {
76 assertEquals("child type", expected.modificationType(), actual.modificationType());
78 switch (actual.modificationType()) {
81 assertEquals("child identifier", expected.name(), actual.name());
82 assertEquals("child data", expected.dataAfter(), actual.dataAfter());
84 case SUBTREE_MODIFIED:
85 assertEquals("child identifier", expected.name(), actual.name());
86 assertChildrenEquals(expected.childNodes(), actual.childNodes());
91 fail("Unexpect root type " + actual.modificationType());
99 candidate = DataTreeCandidates.fromNormalizedNode(TestModel.TEST_PATH, ImmutableNodes.newContainerBuilder()
100 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
101 .withChild(ImmutableNodes.leafNode(TestModel.DESC_QNAME, "foo"))
106 public void testCandidateSerialization() throws IOException {
107 final CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
108 assertEquals("payload size", 156, payload.size());
109 assertEquals("serialized size", 242, SerializationUtils.serialize(payload).length);
113 public void testCandidateSerDes() throws IOException {
114 final CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
115 assertCandidateEquals(candidate, payload.getCandidate());
119 public void testPayloadSerDes() throws IOException {
120 final CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
121 assertCandidateEquals(candidate, SerializationUtils.clone(payload).getCandidate());
125 public void testLeafSetEntryNodeCandidate() throws Exception {
126 NodeWithValue<String> entryPathArg = new NodeWithValue<>(LEAF_SET, "one");
127 YangInstanceIdentifier leafSetEntryPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(LEAF_SET)
128 .node(entryPathArg).build();
130 candidate = DataTreeCandidates.fromNormalizedNode(leafSetEntryPath, ImmutableNodes.leafSetEntry(entryPathArg));
131 CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
132 assertCandidateEquals(candidate, payload.getCandidate());
136 public void testLeafSetNodeCandidate() throws Exception {
137 YangInstanceIdentifier leafSetPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(LEAF_SET).build();
139 candidate = DataTreeCandidates.fromNormalizedNode(leafSetPath, ImmutableNodes.newSystemLeafSetBuilder()
140 .withNodeIdentifier(new NodeIdentifier(LEAF_SET))
141 .withChild(ImmutableNodes.leafSetEntry(LEAF_SET, "one"))
143 CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
144 assertCandidateEquals(candidate, payload.getCandidate());
148 public void testOrderedLeafSetNodeCandidate() throws Exception {
149 YangInstanceIdentifier leafSetPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(LEAF_SET).build();
151 candidate = DataTreeCandidates.fromNormalizedNode(leafSetPath, ImmutableNodes.newUserLeafSetBuilder()
152 .withNodeIdentifier(new NodeIdentifier(LEAF_SET))
153 .withChild(ImmutableNodes.leafSetEntry(LEAF_SET, "one"))
155 CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
156 assertCandidateEquals(candidate, payload.getCandidate());
160 public void testLeafNodeCandidate() throws Exception {
161 YangInstanceIdentifier leafPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH)
162 .node(TestModel.DESC_QNAME).build();
164 candidate = DataTreeCandidates.fromNormalizedNode(leafPath,
165 ImmutableNodes.leafNode(TestModel.DESC_QNAME, "test"));
166 CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
167 assertCandidateEquals(candidate, payload.getCandidate());
171 public void testUnmodifiedRootCandidate() throws Exception {
172 final DataTree dataTree = new InMemoryDataTreeFactory().create(
173 DataTreeConfiguration.DEFAULT_CONFIGURATION, SchemaContextHelper.select(SchemaContextHelper.CARS_YANG));
175 DataTreeModification modification = dataTree.takeSnapshot().newModification();
176 modification.ready();
177 candidate = dataTree.prepare(modification);
179 CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
180 assertCandidateEquals(candidate, payload.getCandidate());