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.json.schema;
10 import static org.junit.Assert.assertNotNull;
11 import static org.opendaylight.yangtools.yang.data.impl.NodeFactory.createMutableCompositeNode;
12 import static org.opendaylight.yangtools.yang.data.impl.NodeFactory.createMutableSimpleNode;
15 import java.io.FileNotFoundException;
17 import java.net.URISyntaxException;
18 import java.text.ParseException;
19 import java.text.SimpleDateFormat;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.Date;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.List;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.common.QNameModule;
31 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
35 import org.opendaylight.yangtools.yang.data.api.MutableCompositeNode;
36 import org.opendaylight.yangtools.yang.data.api.Node;
37 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
39 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
42 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
43 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
44 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
45 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
46 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
47 import org.opendaylight.yangtools.yang.model.api.Module;
48 import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
49 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
53 public class TestUtils {
55 private static final Logger LOG = LoggerFactory.getLogger(TestUtils.class);
56 private static final QNameModule MODULE_AUGMENT;
57 private static final QNameModule MODULE_BASE;
60 final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
63 MODULE_BASE = QNameModule.create(URI.create("simple:container:yang"), df.parse("2013-11-12"));
64 MODULE_AUGMENT = QNameModule.create(URI.create("augment:container:yang"), df.parse("2014-03-19"));
65 } catch (ParseException e) {
66 throw new ExceptionInInitializerError(e);
70 public static Set<Module> loadModules(final URI resourceURI) throws FileNotFoundException {
71 final YangModelParser parser = new YangParserImpl();
72 final File testDir = new File(resourceURI);
73 final String[] fileList = testDir.list();
74 final List<File> testFiles = new ArrayList<>();
75 if (fileList == null) {
76 throw new FileNotFoundException(resourceURI.toString());
78 for (String fileName : fileList) {
79 testFiles.add(new File(testDir, fileName));
81 return parser.parseYangModels(testFiles);
84 public static Set<Module> loadModulesFrom(final String yangPath) throws URISyntaxException {
86 return loadModules(TestUtils.class.getResource(yangPath).toURI());
87 } catch (FileNotFoundException e) {
88 LOG.error("Yang files at path: " + yangPath + " weren't loaded.");
94 public static DataSchemaNode resolveDataSchemaNode(final String searchedDataSchemaName, final Module module) {
95 assertNotNull("Module can't be null", module);
97 if (searchedDataSchemaName != null) {
98 for (DataSchemaNode dsn : module.getChildNodes()) {
99 if (dsn.getQName().getLocalName().equals(searchedDataSchemaName)) {
103 } else if (module.getChildNodes().size() == 1) {
104 return module.getChildNodes().iterator().next();
109 public static Module resolveModule(final String searchedModuleName, final Set<Module> modules) {
110 assertNotNull("Modules can't be null.", modules);
111 if (searchedModuleName != null) {
112 for (Module m : modules) {
113 if (m.getName().equals(searchedModuleName)) {
117 } else if (modules.size() == 1) {
118 return modules.iterator().next();
124 * Prepare composite node structure according to
125 * /cnsn-to-normalized-node/simple-conainer.json
127 public static CompositeNode prepareCompositeNodeStruct() {
128 MutableCompositeNode cont = createMutableCompositeNode(QName.create(MODULE_BASE, "cont"), null, null, null,
132 List<Node<?>> contChilds = new ArrayList<>();
133 contChilds.add(createMutableCompositeNode(QName.create(MODULE_BASE, "cont1"),
135 Collections.<Node<?>> emptyList(), null, null));
138 MutableCompositeNode cont2 = createMutableCompositeNode(QName.create(MODULE_BASE, "cont2"), cont, null, null,
140 List<Node<?>> cont2Childs = new ArrayList<>();
141 cont2Childs.add(createMutableSimpleNode(QName.create(MODULE_BASE, "lf21"), cont2,
142 "value in cont2/lf21", null, null));
143 cont2.setValue(cont2Childs);
144 contChilds.add(cont2);
147 contChilds.add(createMutableCompositeNode(QName.create(MODULE_BASE, "lst1"), cont,
148 Collections.<Node<?>> emptyList(), null, null));
151 MutableCompositeNode lst2_1 = createMutableCompositeNode(QName.create(MODULE_BASE, "lst2"), cont, null, null,
153 List<Node<?>> lst2_1Childs = new ArrayList<>();
155 .add(createMutableSimpleNode(QName.create(MODULE_BASE, "lf21"),
157 "some value21", null, null));
158 lst2_1.setValue(lst2_1Childs);
159 contChilds.add(lst2_1);
161 MutableCompositeNode lst2_2 = createMutableCompositeNode(QName.create(MODULE_BASE, "lst2"), cont, null, null,
163 List<Node<?>> lst2_2Childs = new ArrayList<>();
165 .add(createMutableSimpleNode(QName.create(MODULE_BASE, "lf22"), lst2_2, "some value22", null, null));
166 lst2_2.setValue(lst2_2Childs);
167 contChilds.add(lst2_2);
170 contChilds.add(createMutableSimpleNode(QName.create(MODULE_BASE, "lflst1"), cont, "lflst1_1", null, null));
171 contChilds.add(createMutableSimpleNode(QName.create(MODULE_BASE, "lflst1"), cont, "lflst1_2", null, null));
174 contChilds.add(createMutableSimpleNode(QName.create(MODULE_BASE, "lf1"), cont, "lf1", null, null));
177 contChilds.add(createMutableSimpleNode(QName.create(MODULE_BASE, "lf11"), cont, "value from case (cs1)", null,
181 MutableCompositeNode cont3 = createMutableCompositeNode(QName.create(MODULE_AUGMENT, "cont3"), cont, null,
183 List<Node<?>> cont3Childs = new ArrayList<>();
184 cont3Childs.add(createMutableSimpleNode(QName.create(MODULE_AUGMENT, "lf31"), cont3,
185 "value in leaf in augment", null, null));
186 cont3.setValue(cont3Childs);
187 contChilds.add(cont3);
189 cont.setValue(contChilds);
194 * Prepare composite node structure according to
195 * /cnsn-to-normalized-node/simple-conainer.json
197 public static ContainerNode prepareNormalizedNodeStruct() throws URISyntaxException {
198 DataContainerNodeBuilder<NodeIdentifier, ContainerNode> containerBuilder = Builders.containerBuilder();
199 containerBuilder.withNodeIdentifier(getNodeIdentifier("cont"));
200 containerBuilder.withChild(Builders.containerBuilder().withNodeIdentifier(getNodeIdentifier("cont1")).build());
201 containerBuilder.withChild(Builders
203 .withNodeIdentifier(getNodeIdentifier("cont2"))
205 Builders.leafBuilder().withNodeIdentifier(getNodeIdentifier("lf21"))
206 .withValue("value in cont2/lf21").build()).build());
208 CollectionNodeBuilder<MapEntryNode, MapNode> lst1 = Builders.mapBuilder().withNodeIdentifier(
209 getNodeIdentifier("lst1"));
210 lst1.withChild(Builders.mapEntryBuilder()
211 .withNodeIdentifier(getNodeIdentifierPredicate("lst1", new HashMap<String, Object>()))
212 .withValue(Collections.<DataContainerChild<? extends PathArgument, ?>> emptyList()).build());
213 containerBuilder.withChild(lst1.build());
215 CollectionNodeBuilder<MapEntryNode, MapNode> lst2 = Builders.mapBuilder().withNodeIdentifier(
216 getNodeIdentifier("lst2"));
218 Map<String, Object> lst2_1 = new HashMap<>();
219 lst2_1.put("lf21", "some value21");
221 List<DataContainerChild<? extends PathArgument, ?>> lst2_1_values = new ArrayList<>();
222 lst2_1_values.add(Builders.leafBuilder().withNodeIdentifier(getNodeIdentifier("lf21"))
223 .withValue("some value21").build());
224 lst2.withChild(Builders.mapEntryBuilder().withNodeIdentifier(getNodeIdentifierPredicate("lst2", lst2_1))
225 .withValue(lst2_1_values).build());
227 Map<String, Object> lst2_2 = new HashMap<>();
228 lst2_2.put("lf22", "some value22");
229 List<DataContainerChild<? extends PathArgument, ?>> lst2_2_values = new ArrayList<>();
230 lst2_2_values.add(Builders.leafBuilder().withNodeIdentifier(getNodeIdentifier("lf22"))
231 .withValue("some value22").build());
232 lst2.withChild(Builders.mapEntryBuilder().withNodeIdentifier(getNodeIdentifierPredicate("lst2", lst2_2))
233 .withValue(lst2_2_values).build());
234 containerBuilder.withChild(lst2.build());
236 ListNodeBuilder<Object, LeafSetEntryNode<Object>> lflst1 = Builders.leafSetBuilder().withNodeIdentifier(
237 getNodeIdentifier("lflst1"));
238 lflst1.withChild(Builders.leafSetEntryBuilder().withNodeIdentifier(getNodeIdentifier("lflst1", "lflst1_1"))
239 .withValue("lflst1_1").build());
240 lflst1.withChild(Builders.leafSetEntryBuilder().withNodeIdentifier(getNodeIdentifier("lflst1", "lflst1_2"))
241 .withValue("lflst1_2").build());
242 containerBuilder.withChild(lflst1.build());
244 containerBuilder.withChild(Builders.leafBuilder().withNodeIdentifier(getNodeIdentifier("lf1")).withValue("lf1")
247 containerBuilder.withChild(Builders
249 .withNodeIdentifier(getNodeIdentifier("chc"))
251 Builders.leafBuilder().withNodeIdentifier(getNodeIdentifier("lf11"))
252 .withValue("value from case (cs1)").build()).build());
254 Set<QName> children = new HashSet<>();
255 children.add(QName.create(MODULE_AUGMENT, "cont3"));
257 containerBuilder.withChild(Builders
258 .augmentationBuilder()
259 .withNodeIdentifier(getAugmentationIdentifier(null, null, null, children))
261 Builders.containerBuilder()
262 .withNodeIdentifier(getNodeIdentifier(MODULE_AUGMENT, "cont3"))
264 Builders.leafBuilder()
265 .withNodeIdentifier(getNodeIdentifier(MODULE_AUGMENT, "lf31"))
266 .withValue("value in leaf in augment").build()).build()).build());
268 ContainerNode build = containerBuilder.build();
272 private static YangInstanceIdentifier.NodeIdentifier getNodeIdentifier(final String localName) {
273 return getNodeIdentifier(MODULE_BASE, localName);
276 private static YangInstanceIdentifier.NodeIdentifier getNodeIdentifier(final QNameModule module, final String localName) {
277 return new YangInstanceIdentifier.NodeIdentifier(QName.create(module, localName));
280 private static YangInstanceIdentifier.NodeWithValue getNodeIdentifier(final String localName, final Object value) {
281 return new YangInstanceIdentifier.NodeWithValue(QName.create(MODULE_BASE, localName),
285 private static YangInstanceIdentifier.NodeIdentifierWithPredicates getNodeIdentifierPredicate(final String localName,
286 final Map<String, Object> keys) {
287 Map<QName, Object> predicate = new HashMap<>();
288 for (String key : keys.keySet()) {
289 predicate.put(QName.create(MODULE_BASE, key), keys.get(key));
292 return new YangInstanceIdentifier.NodeIdentifierWithPredicates(QName.create(MODULE_BASE, localName), predicate);
295 private static YangInstanceIdentifier.AugmentationIdentifier getAugmentationIdentifier(final String localName,
296 final String namespace, final Date revision, final Set<QName> children) {
297 return new YangInstanceIdentifier.AugmentationIdentifier(children);