BUG 1975 - yang unkeyed list is transformed to map node
[yangtools.git] / yang / yang-data-composite-node / src / test / java / org / opendaylight / yangtools / yang / data / composite / node / schema / TestUtils.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.yangtools.yang.data.composite.node.schema;
9
10 import static org.junit.Assert.assertNotNull;
11 import static org.opendaylight.yangtools.yang.data.impl.NodeFactory.createMutableSimpleNode;
12
13 import java.io.File;
14 import java.io.FileNotFoundException;
15 import java.net.URI;
16 import java.net.URISyntaxException;
17 import java.text.ParseException;
18 import java.text.SimpleDateFormat;
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.Date;
22 import java.util.HashMap;
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Set;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.common.QNameModule;
29 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
33 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
35 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
38 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
39 import org.opendaylight.yangtools.yang.data.impl.NodeFactory;
40 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
41 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
42 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
43 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
44 import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder;
45 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
46 import org.opendaylight.yangtools.yang.model.api.Module;
47 import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
48 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52 public class TestUtils {
53
54     private static final Logger LOG = LoggerFactory.getLogger(TestUtils.class);
55     private static final QNameModule MODULE_AUGMENT;
56     private static final QNameModule MODULE_BASE;
57
58     static {
59         final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
60
61         try {
62             MODULE_BASE = QNameModule.create(URI.create("simple:container:yang"), df.parse("2013-11-12"));
63             MODULE_AUGMENT = QNameModule.create(URI.create("augment:container:yang"), df.parse("2014-03-19"));
64         } catch (ParseException e) {
65             throw new ExceptionInInitializerError(e);
66         }
67     }
68
69     public static Set<Module> loadModules(final URI resourceURI) throws FileNotFoundException {
70         final YangModelParser parser = new YangParserImpl();
71         final File testDir = new File(resourceURI);
72         final String[] fileList = testDir.list();
73         final List<File> testFiles = new ArrayList<>();
74         if (fileList == null) {
75             throw new FileNotFoundException(resourceURI.toString());
76         }
77         for (String fileName : fileList) {
78             testFiles.add(new File(testDir, fileName));
79         }
80         return parser.parseYangModels(testFiles);
81     }
82
83     public static Set<Module> loadModulesFrom(final String yangPath) throws URISyntaxException {
84         try {
85             return loadModules(TestUtils.class.getResource(yangPath).toURI());
86         } catch (FileNotFoundException e) {
87             LOG.error("Yang files at path: " + yangPath + " weren't loaded.");
88         }
89
90         return null;
91     }
92
93     public static DataSchemaNode resolveDataSchemaNode(final String searchedDataSchemaName, final Module module) {
94         assertNotNull("Module can't be null", module);
95
96         if (searchedDataSchemaName != null) {
97             for (DataSchemaNode dsn : module.getChildNodes()) {
98                 if (dsn.getQName().getLocalName().equals(searchedDataSchemaName)) {
99                     return dsn;
100                 }
101             }
102         } else if (module.getChildNodes().size() == 1) {
103             return module.getChildNodes().iterator().next();
104         }
105         return null;
106     }
107
108     public static Module resolveModule(final String searchedModuleName, final Set<Module> modules) {
109         assertNotNull("Modules can't be null.", modules);
110         if (searchedModuleName != null) {
111             for (Module m : modules) {
112                 if (m.getName().equals(searchedModuleName)) {
113                     return m;
114                 }
115             }
116         } else if (modules.size() == 1) {
117             return modules.iterator().next();
118         }
119         return null;
120     }
121
122     /**
123      * Prepare composite node structure according to
124      * /cnsn-to-normalized-node/simple-conainer.json
125      */
126     public static CompositeNode prepareCompositeNodeStruct() {
127         CompositeNodeBuilder<ImmutableCompositeNode> contBuilder = ImmutableCompositeNode.builder();
128         contBuilder.setQName(QName.create(MODULE_BASE, "cont"));
129
130         // cont1
131         contBuilder.add(ImmutableCompositeNode.builder().setQName(QName.create(MODULE_BASE, "cont1")).build());
132
133         // cont2
134         CompositeNodeBuilder<ImmutableCompositeNode> cont2 = ImmutableCompositeNode.builder().setQName(
135                 QName.create(MODULE_BASE, "cont2"));
136         cont2.add(createMutableSimpleNode(QName.create(MODULE_BASE, "lf21"), null, "value in cont2/lf21", null, null));
137         contBuilder.add(cont2.build());
138
139         // lst1
140         contBuilder.add(ImmutableCompositeNode.builder().setQName(QName.create(MODULE_BASE, "lst1")).build());
141
142         // lst2
143         CompositeNodeBuilder<ImmutableCompositeNode> lst2_1Builder = ImmutableCompositeNode.builder().setQName(
144                 QName.create(MODULE_BASE, "lst2"));
145         lst2_1Builder.add(createMutableSimpleNode(QName.create(MODULE_BASE, "lf21"), null, "some value21", null, null));
146         contBuilder.add(lst2_1Builder.build());
147
148         CompositeNodeBuilder<ImmutableCompositeNode> lst2_2Builder = ImmutableCompositeNode.builder().setQName(
149                 QName.create(MODULE_BASE, "lst2"));
150         lst2_2Builder.add(createMutableSimpleNode(QName.create(MODULE_BASE, "lf22"), null, "some value22", null, null));
151         contBuilder.add(lst2_2Builder.build());
152
153         // lflst1
154         contBuilder.add(createMutableSimpleNode(QName.create(MODULE_BASE, "lflst1"), null, "lflst1_1", null, null));
155         contBuilder.add(createMutableSimpleNode(QName.create(MODULE_BASE, "lflst1"), null, "lflst1_2", null, null));
156
157         // lf1
158         contBuilder.add(createMutableSimpleNode(QName.create(MODULE_BASE, "lf1"), null, "lf1", null, null));
159
160         // lf11
161         contBuilder.add(createMutableSimpleNode(QName.create(MODULE_BASE, "lf11"), null, "value from case (cs1)", null,
162                 null));
163
164         // cont3
165         CompositeNodeBuilder<ImmutableCompositeNode> cont3Builder = ImmutableCompositeNode.builder().setQName(
166                 QName.create(MODULE_AUGMENT, "cont3"));
167         cont3Builder.add(createMutableSimpleNode(QName.create(MODULE_AUGMENT, "lf31"), null,
168                 "value in leaf in augment", null, null));
169         contBuilder.add(cont3Builder.build());
170
171         // anxml-composite
172         CompositeNodeBuilder<ImmutableCompositeNode> anxmlCompositeBuilder = ImmutableCompositeNode.builder().setQName(
173                 QName.create(MODULE_BASE, "anxml-composite"));
174         anxmlCompositeBuilder.add(NodeFactory.createImmutableSimpleNode(QName.create(MODULE_BASE, "anxml-cont"), null,
175                 null));
176         contBuilder.add(anxmlCompositeBuilder.build());
177
178         // anxml-simple
179         contBuilder.add(NodeFactory.createImmutableSimpleNode(QName.create(MODULE_BASE, "anxml-simple"), null,43));
180
181         return contBuilder.build();
182     }
183
184     /**
185      * Prepare composite node structure according to
186      * /cnsn-to-normalized-node/simple-conainer.json
187      */
188     public static ContainerNode prepareNormalizedNodeStruct() throws URISyntaxException {
189         DataContainerNodeBuilder<NodeIdentifier, ContainerNode> containerBuilder = Builders.containerBuilder();
190         containerBuilder.withNodeIdentifier(getNodeIdentifier("cont"));
191         containerBuilder.withChild(Builders.containerBuilder().withNodeIdentifier(getNodeIdentifier("cont1")).build());
192         containerBuilder.withChild(Builders
193                 .containerBuilder()
194                 .withNodeIdentifier(getNodeIdentifier("cont2"))
195                 .withChild(
196                         Builders.leafBuilder().withNodeIdentifier(getNodeIdentifier("lf21"))
197                                 .withValue("value in cont2/lf21").build()).build());
198
199         CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> lst1 = Builders.unkeyedListBuilder().withNodeIdentifier(
200                 getNodeIdentifier("lst1"));
201         lst1.withChild(Builders.unkeyedListEntryBuilder()
202                 .withNodeIdentifier(getNodeIdentifier("lst1"))
203                 .withValue(Collections.<DataContainerChild<? extends PathArgument, ?>> emptyList()).build());
204         containerBuilder.withChild(lst1.build());
205
206         CollectionNodeBuilder<UnkeyedListEntryNode, UnkeyedListNode> lst2 = Builders.unkeyedListBuilder().withNodeIdentifier(
207                 getNodeIdentifier("lst2"));
208
209         Map<String, Object> lst2_1 = new HashMap<>();
210         lst2_1.put("lf21", "some value21");
211
212         List<DataContainerChild<? extends PathArgument, ?>> lst2_1_values = new ArrayList<>();
213         lst2_1_values.add(Builders.leafBuilder().withNodeIdentifier(getNodeIdentifier("lf21"))
214                 .withValue("some value21").build());
215         lst2.withChild(Builders.unkeyedListEntryBuilder().withNodeIdentifier(getNodeIdentifier("lst2"))
216                 .withValue(lst2_1_values).build());
217
218         Map<String, Object> lst2_2 = new HashMap<>();
219         lst2_2.put("lf22", "some value22");
220         List<DataContainerChild<? extends PathArgument, ?>> lst2_2_values = new ArrayList<>();
221         lst2_2_values.add(Builders.leafBuilder().withNodeIdentifier(getNodeIdentifier("lf22"))
222                 .withValue("some value22").build());
223         lst2.withChild(Builders.unkeyedListEntryBuilder().withNodeIdentifier(getNodeIdentifier("lst2"))
224                 .withValue(lst2_2_values).build());
225         containerBuilder.withChild(lst2.build());
226
227         ListNodeBuilder<Object, LeafSetEntryNode<Object>> lflst1 = Builders.leafSetBuilder().withNodeIdentifier(
228                 getNodeIdentifier("lflst1"));
229         lflst1.withChild(Builders.leafSetEntryBuilder().withNodeIdentifier(getNodeIdentifier("lflst1", "lflst1_1"))
230                 .withValue("lflst1_1").build());
231         lflst1.withChild(Builders.leafSetEntryBuilder().withNodeIdentifier(getNodeIdentifier("lflst1", "lflst1_2"))
232                 .withValue("lflst1_2").build());
233         containerBuilder.withChild(lflst1.build());
234
235         containerBuilder.withChild(Builders.leafBuilder().withNodeIdentifier(getNodeIdentifier("lf1")).withValue("lf1")
236                 .build());
237
238         containerBuilder.withChild(Builders
239                 .choiceBuilder()
240                 .withNodeIdentifier(getNodeIdentifier("chc"))
241                 .withChild(
242                         Builders.leafBuilder().withNodeIdentifier(getNodeIdentifier("lf11"))
243                                 .withValue("value from case (cs1)").build()).build());
244
245         Set<QName> children = new HashSet<>();
246         children.add(QName.create(MODULE_AUGMENT, "cont3"));
247
248         containerBuilder.withChild(Builders
249                 .augmentationBuilder()
250                 .withNodeIdentifier(getAugmentationIdentifier(null, null, null, children))
251                 .withChild(
252                         Builders.containerBuilder()
253                                 .withNodeIdentifier(getNodeIdentifier(MODULE_AUGMENT, "cont3"))
254                                 .withChild(
255                                         Builders.leafBuilder()
256                                                 .withNodeIdentifier(getNodeIdentifier(MODULE_AUGMENT, "lf31"))
257                                                 .withValue("value in leaf in augment").build()).build()).build());
258
259         containerBuilder.withChild(Builders
260                 .anyXmlBuilder()
261                 .withNodeIdentifier(getNodeIdentifier("anxml-composite"))
262                 .withValue(
263                         ImmutableCompositeNode
264                                 .builder()
265                                 .setQName(QName.create("simple:container:yang", "2013-11-12", "anxml-composite"))
266                                 .add(NodeFactory.createImmutableSimpleNode(
267                                         QName.create("simple:container:yang", "2013-11-12", "anxml-cont"), null, null))
268                                 .build()).build());
269
270         containerBuilder
271                 .withChild(Builders
272                         .anyXmlBuilder()
273                         .withNodeIdentifier(getNodeIdentifier("anxml-simple"))
274                         .withValue(
275                                 NodeFactory.createImmutableSimpleNode(
276                                         QName.create("simple:container:yang", "2013-11-12", "anxml-simple"), null, 43))
277                         .build());
278
279         ContainerNode build = containerBuilder.build();
280         return build;
281     }
282
283     private static YangInstanceIdentifier.NodeIdentifier getNodeIdentifier(final String localName) {
284         return getNodeIdentifier(MODULE_BASE, localName);
285     }
286
287     private static YangInstanceIdentifier.NodeIdentifier getNodeIdentifier(final QNameModule module, final String localName) {
288         return new YangInstanceIdentifier.NodeIdentifier(QName.create(module, localName));
289     }
290
291     private static YangInstanceIdentifier.NodeWithValue getNodeIdentifier(final String localName, final Object value) {
292         return new YangInstanceIdentifier.NodeWithValue(QName.create(MODULE_BASE, localName),
293                 value);
294     }
295
296     private static YangInstanceIdentifier.NodeIdentifierWithPredicates getNodeIdentifierPredicate(final String localName,
297             final Map<String, Object> keys) {
298         Map<QName, Object> predicate = new HashMap<>();
299         for (String key : keys.keySet()) {
300             predicate.put(QName.create(MODULE_BASE, key), keys.get(key));
301         }
302
303         return new YangInstanceIdentifier.NodeIdentifierWithPredicates(QName.create(MODULE_BASE, localName), predicate);
304     }
305
306     private static YangInstanceIdentifier.AugmentationIdentifier getAugmentationIdentifier(final String localName,
307             final String namespace, final Date revision, final Set<QName> children) {
308         return new YangInstanceIdentifier.AugmentationIdentifier(children);
309     }
310
311 }