2 * Copyright (c) 2014 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
8 package org.opendaylight.yangtools.yang.data.composite.node.schema;
10 import static org.junit.Assert.assertNotNull;
11 import static org.opendaylight.yangtools.yang.data.impl.NodeFactory.createMutableSimpleNode;
14 import java.io.FileNotFoundException;
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;
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.MapEntryNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
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;
52 public class TestUtils {
54 private static final Logger LOG = LoggerFactory.getLogger(TestUtils.class);
55 private static final QNameModule MODULE_AUGMENT;
56 private static final QNameModule MODULE_BASE;
59 final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
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);
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());
77 for (String fileName : fileList) {
78 testFiles.add(new File(testDir, fileName));
80 return parser.parseYangModels(testFiles);
83 public static Set<Module> loadModulesFrom(final String yangPath) throws URISyntaxException {
85 return loadModules(TestUtils.class.getResource(yangPath).toURI());
86 } catch (FileNotFoundException e) {
87 LOG.error("Yang files at path: " + yangPath + " weren't loaded.");
93 public static DataSchemaNode resolveDataSchemaNode(final String searchedDataSchemaName, final Module module) {
94 assertNotNull("Module can't be null", module);
96 if (searchedDataSchemaName != null) {
97 for (DataSchemaNode dsn : module.getChildNodes()) {
98 if (dsn.getQName().getLocalName().equals(searchedDataSchemaName)) {
102 } else if (module.getChildNodes().size() == 1) {
103 return module.getChildNodes().iterator().next();
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)) {
116 } else if (modules.size() == 1) {
117 return modules.iterator().next();
123 * Prepare composite node structure according to
124 * /cnsn-to-normalized-node/simple-conainer.json
126 public static CompositeNode prepareCompositeNodeStruct() {
127 CompositeNodeBuilder<ImmutableCompositeNode> contBuilder = ImmutableCompositeNode.builder();
128 contBuilder.setQName(QName.create(MODULE_BASE, "cont"));
131 contBuilder.add(ImmutableCompositeNode.builder().setQName(QName.create(MODULE_BASE, "cont1")).build());
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());
140 contBuilder.add(ImmutableCompositeNode.builder().setQName(QName.create(MODULE_BASE, "lst1")).build());
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());
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());
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));
158 contBuilder.add(createMutableSimpleNode(QName.create(MODULE_BASE, "lf1"), null, "lf1", null, null));
161 contBuilder.add(createMutableSimpleNode(QName.create(MODULE_BASE, "lf11"), null, "value from case (cs1)", null,
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());
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,
176 contBuilder.add(anxmlCompositeBuilder.build());
179 contBuilder.add(NodeFactory.createImmutableSimpleNode(QName.create(MODULE_BASE, "anxml-simple"), null,43));
181 return contBuilder.build();
185 * Prepare composite node structure according to
186 * /cnsn-to-normalized-node/simple-conainer.json
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
194 .withNodeIdentifier(getNodeIdentifier("cont2"))
196 Builders.leafBuilder().withNodeIdentifier(getNodeIdentifier("lf21"))
197 .withValue("value in cont2/lf21").build()).build());
199 CollectionNodeBuilder<MapEntryNode, MapNode> lst1 = Builders.mapBuilder().withNodeIdentifier(
200 getNodeIdentifier("lst1"));
201 lst1.withChild(Builders.mapEntryBuilder()
202 .withNodeIdentifier(getNodeIdentifierPredicate("lst1", new HashMap<String, Object>()))
203 .withValue(Collections.<DataContainerChild<? extends PathArgument, ?>> emptyList()).build());
204 containerBuilder.withChild(lst1.build());
206 CollectionNodeBuilder<MapEntryNode, MapNode> lst2 = Builders.mapBuilder().withNodeIdentifier(
207 getNodeIdentifier("lst2"));
209 Map<String, Object> lst2_1 = new HashMap<>();
210 lst2_1.put("lf21", "some value21");
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.mapEntryBuilder().withNodeIdentifier(getNodeIdentifierPredicate("lst2", lst2_1))
216 .withValue(lst2_1_values).build());
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.mapEntryBuilder().withNodeIdentifier(getNodeIdentifierPredicate("lst2", lst2_2))
224 .withValue(lst2_2_values).build());
225 containerBuilder.withChild(lst2.build());
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());
235 containerBuilder.withChild(Builders.leafBuilder().withNodeIdentifier(getNodeIdentifier("lf1")).withValue("lf1")
238 containerBuilder.withChild(Builders
240 .withNodeIdentifier(getNodeIdentifier("chc"))
242 Builders.leafBuilder().withNodeIdentifier(getNodeIdentifier("lf11"))
243 .withValue("value from case (cs1)").build()).build());
245 Set<QName> children = new HashSet<>();
246 children.add(QName.create(MODULE_AUGMENT, "cont3"));
248 containerBuilder.withChild(Builders
249 .augmentationBuilder()
250 .withNodeIdentifier(getAugmentationIdentifier(null, null, null, children))
252 Builders.containerBuilder()
253 .withNodeIdentifier(getNodeIdentifier(MODULE_AUGMENT, "cont3"))
255 Builders.leafBuilder()
256 .withNodeIdentifier(getNodeIdentifier(MODULE_AUGMENT, "lf31"))
257 .withValue("value in leaf in augment").build()).build()).build());
259 containerBuilder.withChild(Builders
261 .withNodeIdentifier(getNodeIdentifier("anxml-composite"))
263 ImmutableCompositeNode
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))
273 .withNodeIdentifier(getNodeIdentifier("anxml-simple"))
275 NodeFactory.createImmutableSimpleNode(
276 QName.create("simple:container:yang", "2013-11-12", "anxml-simple"), null, 43))
279 ContainerNode build = containerBuilder.build();
283 private static YangInstanceIdentifier.NodeIdentifier getNodeIdentifier(final String localName) {
284 return getNodeIdentifier(MODULE_BASE, localName);
287 private static YangInstanceIdentifier.NodeIdentifier getNodeIdentifier(final QNameModule module, final String localName) {
288 return new YangInstanceIdentifier.NodeIdentifier(QName.create(module, localName));
291 private static YangInstanceIdentifier.NodeWithValue getNodeIdentifier(final String localName, final Object value) {
292 return new YangInstanceIdentifier.NodeWithValue(QName.create(MODULE_BASE, localName),
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));
303 return new YangInstanceIdentifier.NodeIdentifierWithPredicates(QName.create(MODULE_BASE, localName), predicate);
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);