667887ac805f3c8ea538df89da2d22d147b62ade
[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  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
54  *
55  * This program and the accompanying materials are made available under the
56  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
57  * and is available at http://www.eclipse.org/legal/epl-v10.html
58  */
59 public class ListProcessingAndOrderingTest extends AbstractDataServiceTest {
60
61     private static final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier DOM_UNORDERED_LIST_PATH = org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
62             .builder(Lists.QNAME).node(UnorderedContainer.QNAME).node(UnorderedList.QNAME).build();
63
64     private static final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier DOM_ORDERED_LIST_PATH = org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
65             .builder(Lists.QNAME).node(OrderedContainer.QNAME).node(OrderedList.QNAME).build();
66
67
68     private static final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier DOM_UNKEYED_LIST_PATH = org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
69             .builder(Lists.QNAME).node(UnkeyedContainer.QNAME).node(UnkeyedList.QNAME).build();
70
71     private static final InstanceIdentifier<UnorderedContainer> UNORDERED_CONTAINER_PATH = InstanceIdentifier.builder(Lists.class).child(UnorderedContainer.class).build();
72     private static final InstanceIdentifier<OrderedContainer> ORDERED_CONTAINER_PATH = InstanceIdentifier.builder(Lists.class).child(OrderedContainer.class).build();
73     private static final InstanceIdentifier<UnkeyedContainer> UNKEYED_CONTAINER_PATH = InstanceIdentifier.builder(Lists.class).child(UnkeyedContainer.class).build();
74
75     private static final UnorderedListKey UNORDERED_FOO_KEY = new UnorderedListKey("foo");
76     private static final UnorderedListKey UNORDERED_BAR_KEY = new UnorderedListKey("bar");
77
78     private static final InstanceIdentifier<UnorderedList> UNORDERED_FOO_PATH = UNORDERED_CONTAINER_PATH.child(UnorderedList.class,UNORDERED_FOO_KEY);
79     private static final InstanceIdentifier<UnorderedList> UNORDERED_BAR_PATH = UNORDERED_CONTAINER_PATH.child(UnorderedList.class,UNORDERED_BAR_KEY);
80
81     private static final OrderedListKey ORDERED_FOO_KEY = new OrderedListKey("foo");
82     private static final OrderedListKey ORDERED_BAR_KEY = new OrderedListKey("bar");
83     private static final InstanceIdentifier<OrderedList> ORDERED_FOO_PATH = ORDERED_CONTAINER_PATH.child(OrderedList.class,ORDERED_FOO_KEY);
84     private static final InstanceIdentifier<OrderedList> ORDERED_BAR_PATH = ORDERED_CONTAINER_PATH.child(OrderedList.class,ORDERED_BAR_KEY);
85
86
87     @Test
88     public void unorderedListReadWriteTest() throws InterruptedException, ExecutionException {
89         DataModificationTransaction tx = baDataService.beginTransaction();
90
91         tx.putOperationalData(UNORDERED_FOO_PATH, createUnordered("foo"));
92         tx.putOperationalData(UNORDERED_BAR_PATH, createUnordered("bar"));
93         assertedCommit(tx);
94
95         NormalizedNode<?, ?> data = resolveDataAsserted(DOM_UNORDERED_LIST_PATH);
96         assertTrue(data instanceof MapNode);
97         assertFalse(data instanceof OrderedMapNode);
98
99         assertXmlRepresentation(UNORDERED_CONTAINER_PATH, "foo","bar");
100     }
101
102
103
104     @Test
105     public void orderedListReadWriteTest() throws InterruptedException, ExecutionException {
106         DataModificationTransaction tx = baDataService.beginTransaction();
107
108         tx.putOperationalData(ORDERED_FOO_PATH, createOrdered("foo"));
109         tx.putOperationalData(ORDERED_BAR_PATH, createOrdered("bar"));
110         assertedCommit(tx);
111         NormalizedNode<?, ?> data = resolveDataAsserted(DOM_ORDERED_LIST_PATH);
112         assertTrue(data instanceof MapNode);
113         assertTrue(data instanceof OrderedMapNode);
114
115         assertXmlRepresentation(ORDERED_CONTAINER_PATH, "foo","bar");
116
117     }
118
119
120
121     @Test
122     public void unkeyedListReadWriteTest() throws InterruptedException, ExecutionException {
123         DataModificationTransaction tx = baDataService.beginTransaction();
124
125         ImmutableList<UnkeyedList> unkeyedItems= ImmutableList.<UnkeyedList>builder()
126                 .add(createUnkeyed("foo"))
127                 .add(createUnkeyed("bar"))
128                 .build();
129
130         tx.putOperationalData(UNKEYED_CONTAINER_PATH, new UnkeyedContainerBuilder().setUnkeyedList(unkeyedItems).build());
131         assertedCommit(tx);
132         NormalizedNode<?, ?> data = resolveDataAsserted(DOM_UNKEYED_LIST_PATH);
133         assertFalse(data instanceof MapNode);
134         assertTrue(data instanceof UnkeyedListNode);
135
136         assertXmlRepresentation(UNKEYED_CONTAINER_PATH, "foo","bar");
137     }
138
139     private NormalizedNode<?, ?> resolveDataAsserted(
140             final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier domPath) {
141
142         try (DOMDataReadOnlyTransaction readTx = testContext.getDomAsyncDataBroker().newReadOnlyTransaction()){
143             ListenableFuture<Optional<NormalizedNode<?, ?>>> data = readTx.read(LogicalDatastoreType.OPERATIONAL, domPath);
144             Optional<NormalizedNode<?, ?>> potential = data.get();
145             assertTrue(potential.isPresent());
146             return potential.get();
147         } catch (InterruptedException | ExecutionException e) {
148             throw new RuntimeException(e);
149         }
150     }
151
152     private static UnorderedList createUnordered(final String name) {
153         return new UnorderedListBuilder().setName(name).setValue(createValue(name)).build();
154     }
155
156     private static OrderedList createOrdered(final String name) {
157         return new OrderedListBuilder().setName(name).setValue(createValue(name)).build();
158     }
159
160     private static UnkeyedList createUnkeyed(final String name) {
161         return new UnkeyedListBuilder().setName(name).setValue(createValue(name)).build();
162     }
163
164     private static String createValue(final String name) {
165         return name + "-" + name.hashCode();
166     }
167
168     private static void assertedCommit(final DataModificationTransaction tx) throws InterruptedException, ExecutionException {
169         RpcResult<TransactionStatus> result = tx.commit().get();
170         assertTrue(result.isSuccessful());
171         assertEquals(TransactionStatus.COMMITED,result.getResult());
172     }
173
174     @SuppressWarnings("deprecation")
175     private void assertXmlRepresentation(final InstanceIdentifier<?> containerPath, final String... childNameValues) {
176
177         org.opendaylight.yangtools.yang.data.api.InstanceIdentifier domPath = testContext.getBindingToDomMappingService().toDataDom(containerPath);
178         CompositeNode compositeNode = testContext.getDomDataBroker().readOperationalData(domPath);
179         assertNotNull(compositeNode);
180
181         Set<String> childValues = new HashSet<>();
182         Collections.addAll(childValues, childNameValues);
183
184         for(Node<?> child : compositeNode.getChildren()) {
185             assertTrue(child instanceof CompositeNode);
186             CompositeNode compChild = (CompositeNode) child;
187             String nameLeafValue = (String) compChild.getSimpleNodesByName("name").get(0).getValue();
188             String valueLeafValue = (String) compChild.getSimpleNodesByName("value").get(0).getValue();
189
190             assertEquals(createValue(nameLeafValue), valueLeafValue);
191             childValues.remove(nameLeafValue);
192         }
193         assertTrue(childValues.isEmpty());
194     }
195
196 }

©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.