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.DataTreeCandidateInputOutput.DataTreeCandidateWithVersion;
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.PathArgument;
27 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
31 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
32 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
34 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
35 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
36 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
37 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
38 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
39 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
41 public class CommitTransactionPayloadTest extends AbstractTest {
42 static final QName LEAF_SET = QName.create(TestModel.TEST_QNAME, "leaf-set");
44 private DataTreeCandidate candidate;
46 private static DataTreeCandidateNode findNode(final Collection<DataTreeCandidateNode> nodes,
47 final PathArgument arg) {
48 for (DataTreeCandidateNode node : nodes) {
49 if (arg.equals(node.getIdentifier())) {
56 private static void assertChildrenEquals(final Collection<DataTreeCandidateNode> expected,
57 final Collection<DataTreeCandidateNode> actual) {
58 // Make sure all expected nodes are there
59 for (DataTreeCandidateNode exp : expected) {
60 final DataTreeCandidateNode act = findNode(actual, exp.getIdentifier());
61 assertNotNull("missing expected child", act);
62 assertCandidateNodeEquals(exp, act);
64 // Make sure no nodes are present which are not in the expected set
65 for (DataTreeCandidateNode act : actual) {
66 final DataTreeCandidateNode exp = findNode(expected, act.getIdentifier());
67 assertNull("unexpected child", exp);
71 private static void assertCandidateEquals(final DataTreeCandidate expected,
72 final DataTreeCandidateWithVersion actual) {
73 final DataTreeCandidate candidate = actual.getCandidate();
74 assertEquals("root path", expected.getRootPath(), candidate.getRootPath());
75 assertCandidateNodeEquals(expected.getRootNode(), candidate.getRootNode());
78 private static void assertCandidateNodeEquals(final DataTreeCandidateNode expected,
79 final DataTreeCandidateNode actual) {
80 assertEquals("child type", expected.getModificationType(), actual.getModificationType());
82 switch (actual.getModificationType()) {
85 assertEquals("child identifier", expected.getIdentifier(), actual.getIdentifier());
86 assertEquals("child data", expected.getDataAfter(), actual.getDataAfter());
88 case SUBTREE_MODIFIED:
89 assertEquals("child identifier", expected.getIdentifier(), actual.getIdentifier());
90 assertChildrenEquals(expected.getChildNodes(), actual.getChildNodes());
95 fail("Unexpect root type " + actual.getModificationType());
101 public void setUp() {
103 final YangInstanceIdentifier writePath = TestModel.TEST_PATH;
104 final NormalizedNode writeData = ImmutableContainerNodeBuilder.create()
105 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TestModel.TEST_QNAME))
106 .withChild(ImmutableNodes.leafNode(TestModel.DESC_QNAME, "foo")).build();
107 candidate = DataTreeCandidates.fromNormalizedNode(writePath, writeData);
111 public void testCandidateSerialization() throws IOException {
112 final CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
113 assertEquals("payload size", 156, payload.size());
117 public void testCandidateSerDes() throws IOException {
118 final CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
119 assertCandidateEquals(candidate, payload.getCandidate().getValue());
123 public void testPayloadSerDes() throws IOException {
124 final CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
125 assertCandidateEquals(candidate, SerializationUtils.clone(payload).getCandidate().getValue());
128 @SuppressWarnings({ "rawtypes", "unchecked" })
130 public void testLeafSetEntryNodeCandidate() throws Exception {
131 YangInstanceIdentifier.NodeWithValue entryPathArg = new YangInstanceIdentifier.NodeWithValue(LEAF_SET, "one");
132 YangInstanceIdentifier leafSetEntryPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(LEAF_SET)
133 .node(entryPathArg).build();
135 NormalizedNode leafSetEntryNode = Builders.leafSetEntryBuilder().withNodeIdentifier(entryPathArg)
136 .withValue("one").build();
138 candidate = DataTreeCandidates.fromNormalizedNode(leafSetEntryPath, leafSetEntryNode);
139 CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
140 assertCandidateEquals(candidate, payload.getCandidate().getValue());
143 @SuppressWarnings({ "rawtypes", "unchecked" })
145 public void testLeafSetNodeCandidate() throws Exception {
146 YangInstanceIdentifier.NodeWithValue entryPathArg = new YangInstanceIdentifier.NodeWithValue(LEAF_SET, "one");
147 YangInstanceIdentifier leafSetPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(LEAF_SET).build();
149 LeafSetEntryNode leafSetEntryNode = Builders.leafSetEntryBuilder().withNodeIdentifier(entryPathArg)
150 .withValue("one").build();
151 NormalizedNode leafSetNode = Builders.leafSetBuilder().withNodeIdentifier(
152 new YangInstanceIdentifier.NodeIdentifier(LEAF_SET)).withChild(leafSetEntryNode).build();
154 candidate = DataTreeCandidates.fromNormalizedNode(leafSetPath, leafSetNode);
155 CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
156 assertCandidateEquals(candidate, payload.getCandidate().getValue());
159 @SuppressWarnings({ "rawtypes", "unchecked" })
161 public void testOrderedLeafSetNodeCandidate() throws Exception {
162 YangInstanceIdentifier.NodeWithValue entryPathArg = new YangInstanceIdentifier.NodeWithValue(LEAF_SET, "one");
163 YangInstanceIdentifier leafSetPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH).node(LEAF_SET).build();
165 LeafSetEntryNode leafSetEntryNode = Builders.leafSetEntryBuilder().withNodeIdentifier(entryPathArg)
166 .withValue("one").build();
167 NormalizedNode leafSetNode = Builders.orderedLeafSetBuilder().withNodeIdentifier(
168 new YangInstanceIdentifier.NodeIdentifier(LEAF_SET)).withChild(leafSetEntryNode).build();
170 candidate = DataTreeCandidates.fromNormalizedNode(leafSetPath, leafSetNode);
171 CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
172 assertCandidateEquals(candidate, payload.getCandidate().getValue());
176 public void testLeafNodeCandidate() throws Exception {
177 YangInstanceIdentifier leafPath = YangInstanceIdentifier.builder(TestModel.TEST_PATH)
178 .node(TestModel.DESC_QNAME).build();
179 LeafNode<Object> leafNode = Builders.leafBuilder().withNodeIdentifier(
180 new YangInstanceIdentifier.NodeIdentifier(TestModel.DESC_QNAME)).withValue("test").build();
182 candidate = DataTreeCandidates.fromNormalizedNode(leafPath, leafNode);
183 CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
184 assertCandidateEquals(candidate, payload.getCandidate().getValue());
188 public void testUnmodifiedRootCandidate() throws Exception {
189 final DataTree dataTree = new InMemoryDataTreeFactory().create(
190 DataTreeConfiguration.DEFAULT_CONFIGURATION, SchemaContextHelper.select(SchemaContextHelper.CARS_YANG));
192 DataTreeModification modification = dataTree.takeSnapshot().newModification();
193 modification.ready();
194 candidate = dataTree.prepare(modification);
196 CommitTransactionPayload payload = CommitTransactionPayload.create(nextTransactionId(), candidate);
197 assertCandidateEquals(candidate, payload.getCandidate().getValue());