cd68559ae5304eb721771981069c312333b33868
[controller.git] / opendaylight / md-sal / sal-binding-dom-it / src / test / java / org / opendaylight / controller / md / sal / binding / data / ListProcessingAndOrderingTest.java
1 /*
2  * Copyright (c) 2014 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.md.sal.binding.data;
9
10 import static junit.framework.Assert.assertEquals;
11 import static junit.framework.Assert.assertFalse;
12 import static junit.framework.Assert.assertNotNull;
13 import static junit.framework.Assert.assertTrue;
14
15 import java.util.Collections;
16 import java.util.HashSet;
17 import java.util.Set;
18 import java.util.concurrent.ExecutionException;
19
20 import org.junit.Test;
21 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
24 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
25 import org.opendaylight.controller.sal.binding.test.AbstractDataServiceTest;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.store.rev140422.Lists;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.store.rev140422.lists.OrderedContainer;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.store.rev140422.lists.UnkeyedContainer;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.store.rev140422.lists.UnkeyedContainerBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.store.rev140422.lists.UnorderedContainer;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.store.rev140422.lists.ordered.container.OrderedList;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.store.rev140422.lists.ordered.container.OrderedListBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.store.rev140422.lists.ordered.container.OrderedListKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.store.rev140422.lists.unkeyed.container.UnkeyedList;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.store.rev140422.lists.unkeyed.container.UnkeyedListBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.store.rev140422.lists.unordered.container.UnorderedList;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.store.rev140422.lists.unordered.container.UnorderedListBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.store.rev140422.lists.unordered.container.UnorderedListKey;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.opendaylight.yangtools.yang.common.RpcResult;
41 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
42 import org.opendaylight.yangtools.yang.data.api.Node;
43 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
44 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
45 import org.opendaylight.yangtools.yang.data.api.schema.OrderedMapNode;
46 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
47
48 import com.google.common.base.Optional;
49 import com.google.common.collect.ImmutableList;
50 import com.google.common.util.concurrent.ListenableFuture;
51
52 /*
53  * FIXME: THis test should be moved to sal-binding-broker and rewriten
54  * to use new DataBroker API
55  */
56 @SuppressWarnings("deprecation")
57 public class ListProcessingAndOrderingTest extends AbstractDataServiceTest {
58
59     private static final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier DOM_UNORDERED_LIST_PATH = org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
60             .builder(Lists.QNAME).node(UnorderedContainer.QNAME).node(UnorderedList.QNAME).build();
61
62     private static final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier DOM_ORDERED_LIST_PATH = org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
63             .builder(Lists.QNAME).node(OrderedContainer.QNAME).node(OrderedList.QNAME).build();
64
65
66     private static final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier DOM_UNKEYED_LIST_PATH = org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
67             .builder(Lists.QNAME).node(UnkeyedContainer.QNAME).node(UnkeyedList.QNAME).build();
68
69     private static final InstanceIdentifier<UnorderedContainer> UNORDERED_CONTAINER_PATH = InstanceIdentifier.builder(Lists.class).child(UnorderedContainer.class).build();
70     private static final InstanceIdentifier<OrderedContainer> ORDERED_CONTAINER_PATH = InstanceIdentifier.builder(Lists.class).child(OrderedContainer.class).build();
71     private static final InstanceIdentifier<UnkeyedContainer> UNKEYED_CONTAINER_PATH = InstanceIdentifier.builder(Lists.class).child(UnkeyedContainer.class).build();
72
73     private static final UnorderedListKey UNORDERED_FOO_KEY = new UnorderedListKey("foo");
74     private static final UnorderedListKey UNORDERED_BAR_KEY = new UnorderedListKey("bar");
75
76     private static final InstanceIdentifier<UnorderedList> UNORDERED_FOO_PATH = UNORDERED_CONTAINER_PATH.child(UnorderedList.class,UNORDERED_FOO_KEY);
77     private static final InstanceIdentifier<UnorderedList> UNORDERED_BAR_PATH = UNORDERED_CONTAINER_PATH.child(UnorderedList.class,UNORDERED_BAR_KEY);
78
79     private static final OrderedListKey ORDERED_FOO_KEY = new OrderedListKey("foo");
80     private static final OrderedListKey ORDERED_BAR_KEY = new OrderedListKey("bar");
81     private static final InstanceIdentifier<OrderedList> ORDERED_FOO_PATH = ORDERED_CONTAINER_PATH.child(OrderedList.class,ORDERED_FOO_KEY);
82     private static final InstanceIdentifier<OrderedList> ORDERED_BAR_PATH = ORDERED_CONTAINER_PATH.child(OrderedList.class,ORDERED_BAR_KEY);
83
84
85     @Test
86     public void unorderedListReadWriteTest() throws InterruptedException, ExecutionException {
87         DataModificationTransaction tx = baDataService.beginTransaction();
88
89         tx.putOperationalData(UNORDERED_FOO_PATH, createUnordered("foo"));
90         tx.putOperationalData(UNORDERED_BAR_PATH, createUnordered("bar"));
91         assertedCommit(tx);
92
93         NormalizedNode<?, ?> data = resolveDataAsserted(DOM_UNORDERED_LIST_PATH);
94         assertTrue(data instanceof MapNode);
95         assertFalse(data instanceof OrderedMapNode);
96
97         assertXmlRepresentation(UNORDERED_CONTAINER_PATH, "foo","bar");
98     }
99
100
101
102     @Test
103     public void orderedListReadWriteTest() throws InterruptedException, ExecutionException {
104         DataModificationTransaction tx = baDataService.beginTransaction();
105
106         tx.putOperationalData(ORDERED_FOO_PATH, createOrdered("foo"));
107         tx.putOperationalData(ORDERED_BAR_PATH, createOrdered("bar"));
108         assertedCommit(tx);
109         NormalizedNode<?, ?> data = resolveDataAsserted(DOM_ORDERED_LIST_PATH);
110         assertTrue(data instanceof MapNode);
111         assertTrue(data instanceof OrderedMapNode);
112
113         assertXmlRepresentation(ORDERED_CONTAINER_PATH, "foo","bar");
114
115     }
116
117
118
119     @Test
120     public void unkeyedListReadWriteTest() throws InterruptedException, ExecutionException {
121         DataModificationTransaction tx = baDataService.beginTransaction();
122
123         ImmutableList<UnkeyedList> unkeyedItems= ImmutableList.<UnkeyedList>builder()
124                 .add(createUnkeyed("foo"))
125                 .add(createUnkeyed("bar"))
126                 .build();
127
128         tx.putOperationalData(UNKEYED_CONTAINER_PATH, new UnkeyedContainerBuilder().setUnkeyedList(unkeyedItems).build());
129         assertedCommit(tx);
130         NormalizedNode<?, ?> data = resolveDataAsserted(DOM_UNKEYED_LIST_PATH);
131         assertFalse(data instanceof MapNode);
132         assertTrue(data instanceof UnkeyedListNode);
133
134         assertXmlRepresentation(UNKEYED_CONTAINER_PATH, "foo","bar");
135     }
136
137     private NormalizedNode<?, ?> resolveDataAsserted(
138             final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier domPath) {
139
140         try (DOMDataReadOnlyTransaction readTx = testContext.getDomAsyncDataBroker().newReadOnlyTransaction()){
141             ListenableFuture<Optional<NormalizedNode<?, ?>>> data = readTx.read(LogicalDatastoreType.OPERATIONAL, domPath);
142             Optional<NormalizedNode<?, ?>> potential = data.get();
143             assertTrue(potential.isPresent());
144             return potential.get();
145         } catch (InterruptedException | ExecutionException e) {
146             throw new RuntimeException(e);
147         }
148     }
149
150     private static UnorderedList createUnordered(final String name) {
151         return new UnorderedListBuilder().setName(name).setValue(createValue(name)).build();
152     }
153
154     private static OrderedList createOrdered(final String name) {
155         return new OrderedListBuilder().setName(name).setValue(createValue(name)).build();
156     }
157
158     private static UnkeyedList createUnkeyed(final String name) {
159         return new UnkeyedListBuilder().setName(name).setValue(createValue(name)).build();
160     }
161
162     private static String createValue(final String name) {
163         return name + "-" + name.hashCode();
164     }
165
166     private static void assertedCommit(final DataModificationTransaction tx) throws InterruptedException, ExecutionException {
167         RpcResult<TransactionStatus> result = tx.commit().get();
168         assertTrue(result.isSuccessful());
169         assertEquals(TransactionStatus.COMMITED,result.getResult());
170     }
171
172     private void assertXmlRepresentation(final InstanceIdentifier<?> containerPath, final String... childNameValues) {
173
174         org.opendaylight.yangtools.yang.data.api.InstanceIdentifier domPath = testContext.getBindingToDomMappingService().toDataDom(containerPath);
175         CompositeNode compositeNode = testContext.getDomDataBroker().readOperationalData(domPath);
176         assertNotNull(compositeNode);
177
178         Set<String> childValues = new HashSet<>();
179         Collections.addAll(childValues, childNameValues);
180
181         for(Node<?> child : compositeNode.getChildren()) {
182             assertTrue(child instanceof CompositeNode);
183             CompositeNode compChild = (CompositeNode) child;
184             String nameLeafValue = (String) compChild.getSimpleNodesByName("name").get(0).getValue();
185             String valueLeafValue = (String) compChild.getSimpleNodesByName("value").get(0).getValue();
186
187             assertEquals(createValue(nameLeafValue), valueLeafValue);
188             childValues.remove(nameLeafValue);
189         }
190         assertTrue(childValues.isEmpty());
191     }
192
193 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.