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;
13 import com.google.common.base.Optional;
14 import com.google.common.collect.ImmutableList;
15 import com.google.common.util.concurrent.CheckedFuture;
16 import java.util.Collection;
17 import java.util.concurrent.ExecutionException;
18 import java.util.concurrent.Executors;
19 import org.junit.Test;
20 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
21 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
22 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
23 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
24 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
25 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
26 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
27 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
28 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
34 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37 public class NormalizedNodeAggregatorTest {
40 public void testAggregate() throws InterruptedException, ExecutionException, ReadFailedException, DataValidationFailedException {
41 SchemaContext schemaContext = SchemaContextHelper.full();
42 NormalizedNode<?, ?> expectedNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
43 NormalizedNode<?, ?> expectedNode2 = ImmutableNodes.containerNode(CarsModel.CARS_QNAME);
45 Optional<NormalizedNode<?, ?>> optional = NormalizedNodeAggregator.aggregate(YangInstanceIdentifier.EMPTY,
47 Optional.<NormalizedNode<?, ?>>of(getRootNode(expectedNode1, schemaContext)),
48 Optional.<NormalizedNode<?, ?>>of(getRootNode(expectedNode2, schemaContext))),
49 schemaContext, LogicalDatastoreType.CONFIGURATION);
52 NormalizedNode<?,?> normalizedNode = optional.get();
54 assertTrue("Expect value to be a Collection", normalizedNode.getValue() instanceof Collection);
56 Collection<NormalizedNode<?,?>> collection = (Collection<NormalizedNode<?,?>>) normalizedNode.getValue();
58 for(NormalizedNode<?,?> node : collection){
59 assertTrue("Expected " + node + " to be a ContainerNode", node instanceof ContainerNode);
62 assertTrue("Child with QName = " + TestModel.TEST_QNAME + " not found",
63 findChildWithQName(collection, TestModel.TEST_QNAME) != null);
65 assertEquals(expectedNode1, findChildWithQName(collection, TestModel.TEST_QNAME));
67 assertTrue("Child with QName = " + CarsModel.BASE_QNAME + " not found",
68 findChildWithQName(collection, CarsModel.BASE_QNAME) != null);
70 assertEquals(expectedNode2, findChildWithQName(collection, CarsModel.BASE_QNAME));
74 public static NormalizedNode<?, ?> getRootNode(NormalizedNode<?, ?> moduleNode, SchemaContext schemaContext)
75 throws ReadFailedException, ExecutionException, InterruptedException {
76 try (InMemoryDOMDataStore store = new InMemoryDOMDataStore("test", Executors.newSingleThreadExecutor())) {
77 store.onGlobalContextUpdated(schemaContext);
79 DOMStoreWriteTransaction writeTransaction = store.newWriteOnlyTransaction();
81 writeTransaction.merge(YangInstanceIdentifier.of(moduleNode.getNodeType()), moduleNode);
83 DOMStoreThreePhaseCommitCohort ready = writeTransaction.ready();
85 ready.canCommit().get();
86 ready.preCommit().get();
89 DOMStoreReadTransaction readTransaction = store.newReadOnlyTransaction();
91 CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read = readTransaction
92 .read(YangInstanceIdentifier.EMPTY);
94 Optional<NormalizedNode<?, ?>> nodeOptional = read.checkedGet();
96 return nodeOptional.get();
100 public static NormalizedNode<?,?> findChildWithQName(Collection<NormalizedNode<?, ?>> collection, QName qName) {
101 for(NormalizedNode<?,?> node : collection){
102 if(node.getNodeType().equals(qName)){