Improve segmented journal actor metrics
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / utils / NormalizedNodeAggregatorTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.datastore.utils;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12
13 import com.google.common.collect.ImmutableList;
14 import java.util.Collection;
15 import java.util.Optional;
16 import java.util.concurrent.ExecutionException;
17 import java.util.concurrent.Executors;
18 import org.junit.Test;
19 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
20 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
21 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
22 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
23 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction;
24 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
25 import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
26 import org.opendaylight.mdsal.dom.store.inmemory.InMemoryDOMDataStore;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
32 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
33 import org.opendaylight.yangtools.yang.data.tree.api.DataValidationFailedException;
34 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
35
36 public class NormalizedNodeAggregatorTest {
37
38     @Test
39     public void testAggregate() throws InterruptedException, ExecutionException, DataValidationFailedException {
40         EffectiveModelContext schemaContext = SchemaContextHelper.full();
41         NormalizedNode expectedNode1 = ImmutableNodes.newContainerBuilder()
42             .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
43             .build();
44         NormalizedNode expectedNode2 = ImmutableNodes.newContainerBuilder()
45             .withNodeIdentifier(new NodeIdentifier(CarsModel.CARS_QNAME))
46             .build();
47
48         Optional<NormalizedNode> optional = NormalizedNodeAggregator.aggregate(YangInstanceIdentifier.of(),
49                 ImmutableList.of(
50                         Optional.<NormalizedNode>of(getRootNode(expectedNode1, schemaContext)),
51                         Optional.<NormalizedNode>of(getRootNode(expectedNode2, schemaContext))),
52                 schemaContext, LogicalDatastoreType.CONFIGURATION);
53
54
55         NormalizedNode normalizedNode = optional.orElseThrow();
56
57         assertTrue("Expect value to be a Collection", normalizedNode.body() instanceof Collection);
58
59         @SuppressWarnings("unchecked")
60         Collection<NormalizedNode> collection = (Collection<NormalizedNode>) normalizedNode.body();
61
62         for (NormalizedNode node : collection) {
63             assertTrue("Expected " + node + " to be a ContainerNode", node instanceof ContainerNode);
64         }
65
66         assertTrue("Child with QName = " + TestModel.TEST_QNAME + " not found",
67                 findChildWithQName(collection, TestModel.TEST_QNAME) != null);
68
69         assertEquals(expectedNode1, findChildWithQName(collection, TestModel.TEST_QNAME));
70
71         assertTrue("Child with QName = " + CarsModel.BASE_QNAME + " not found",
72                 findChildWithQName(collection, CarsModel.BASE_QNAME) != null);
73
74         assertEquals(expectedNode2, findChildWithQName(collection, CarsModel.BASE_QNAME));
75
76     }
77
78     public static NormalizedNode getRootNode(final NormalizedNode moduleNode,
79             final EffectiveModelContext schemaContext) throws ExecutionException, InterruptedException {
80         try (InMemoryDOMDataStore store = new InMemoryDOMDataStore("test", Executors.newSingleThreadExecutor())) {
81             store.onModelContextUpdated(schemaContext);
82
83             DOMStoreWriteTransaction writeTransaction = store.newWriteOnlyTransaction();
84
85             writeTransaction.merge(YangInstanceIdentifier.of(moduleNode.name().getNodeType()), moduleNode);
86
87             DOMStoreThreePhaseCommitCohort ready = writeTransaction.ready();
88
89             ready.canCommit().get();
90             ready.preCommit().get();
91             ready.commit().get();
92
93             DOMStoreReadTransaction readTransaction = store.newReadOnlyTransaction();
94
95             return readTransaction.read(YangInstanceIdentifier.of()).get().orElseThrow();
96         }
97     }
98
99     public static NormalizedNode findChildWithQName(final Collection<NormalizedNode> collection,
100             final QName qname) {
101         for (NormalizedNode node : collection) {
102             if (node.name().getNodeType().equals(qname)) {
103                 return node;
104             }
105         }
106
107         return null;
108     }
109 }