2 * Copyright (c) 2015 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
9 package org.opendaylight.controller.cluster.datastore.utils;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
14 import com.google.common.base.Optional;
15 import com.google.common.collect.ImmutableList;
16 import com.google.common.util.concurrent.CheckedFuture;
17 import java.util.Collection;
18 import java.util.concurrent.ExecutionException;
19 import java.util.concurrent.Executors;
20 import org.junit.Test;
21 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
22 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
23 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
26 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
27 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
28 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
29 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
30 import org.opendaylight.yangtools.yang.common.QName;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
35 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
38 public class NormalizedNodeAggregatorTest {
41 public void testAggregate() throws InterruptedException, ExecutionException, ReadFailedException,
42 DataValidationFailedException {
43 SchemaContext schemaContext = SchemaContextHelper.full();
44 NormalizedNode<?, ?> expectedNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
45 NormalizedNode<?, ?> expectedNode2 = ImmutableNodes.containerNode(CarsModel.CARS_QNAME);
47 Optional<NormalizedNode<?, ?>> optional = NormalizedNodeAggregator.aggregate(YangInstanceIdentifier.EMPTY,
49 Optional.<NormalizedNode<?, ?>>of(getRootNode(expectedNode1, schemaContext)),
50 Optional.<NormalizedNode<?, ?>>of(getRootNode(expectedNode2, schemaContext))),
51 schemaContext, LogicalDatastoreType.CONFIGURATION);
54 NormalizedNode<?,?> normalizedNode = optional.get();
56 assertTrue("Expect value to be a Collection", normalizedNode.getValue() instanceof Collection);
58 @SuppressWarnings("unchecked")
59 Collection<NormalizedNode<?,?>> collection = (Collection<NormalizedNode<?,?>>) normalizedNode.getValue();
61 for (NormalizedNode<?,?> node : collection) {
62 assertTrue("Expected " + node + " to be a ContainerNode", node instanceof ContainerNode);
65 assertTrue("Child with QName = " + TestModel.TEST_QNAME + " not found",
66 findChildWithQName(collection, TestModel.TEST_QNAME) != null);
68 assertEquals(expectedNode1, findChildWithQName(collection, TestModel.TEST_QNAME));
70 assertTrue("Child with QName = " + CarsModel.BASE_QNAME + " not found",
71 findChildWithQName(collection, CarsModel.BASE_QNAME) != null);
73 assertEquals(expectedNode2, findChildWithQName(collection, CarsModel.BASE_QNAME));
77 public static NormalizedNode<?, ?> getRootNode(NormalizedNode<?, ?> moduleNode, SchemaContext schemaContext)
78 throws ReadFailedException, ExecutionException, InterruptedException {
79 try (InMemoryDOMDataStore store = new InMemoryDOMDataStore("test", Executors.newSingleThreadExecutor())) {
80 store.onGlobalContextUpdated(schemaContext);
82 DOMStoreWriteTransaction writeTransaction = store.newWriteOnlyTransaction();
84 writeTransaction.merge(YangInstanceIdentifier.of(moduleNode.getNodeType()), moduleNode);
86 DOMStoreThreePhaseCommitCohort ready = writeTransaction.ready();
88 ready.canCommit().get();
89 ready.preCommit().get();
92 DOMStoreReadTransaction readTransaction = store.newReadOnlyTransaction();
94 CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read = readTransaction
95 .read(YangInstanceIdentifier.EMPTY);
97 Optional<NormalizedNode<?, ?>> nodeOptional = read.checkedGet();
99 return nodeOptional.get();
103 public static NormalizedNode<?,?> findChildWithQName(Collection<NormalizedNode<?, ?>> collection, QName qname) {
104 for (NormalizedNode<?, ?> node : collection) {
105 if (node.getNodeType().equals(qname)) {