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.data.api.CompositeNode;
31 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
34 import org.opendaylight.yangtools.yang.data.api.MutableCompositeNode;
35 import org.opendaylight.yangtools.yang.data.api.Node;
36 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
38 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
41 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
42 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
43 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
44 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
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);
56 private static final String NAMESPACE_BASE = "simple:container:yang";
57 private static final String NAMESPACE_AUGMENT = "augment:container:yang";
58 private static Date revision_base;
59 private static Date revision_augment;
63 revision_base = new SimpleDateFormat("yyyy-MM-dd").parse("2013-11-12");
64 revision_augment = new SimpleDateFormat("yyyy-MM-dd").parse("2014-03-19");
65 } catch (ParseException e) {
70 public static Set<Module> loadModules(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(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(String searchedDataSchemaName, 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(String searchedModuleName, 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(new QName(URI.create(NAMESPACE_BASE), revision_base,
129 "cont"), null, null, null, null);
132 List<Node<?>> contChilds = new ArrayList<>();
133 contChilds.add(createMutableCompositeNode(new QName(URI.create(NAMESPACE_BASE), revision_base, "cont1"), cont,
134 Collections.<Node<?>> emptyList(), null, null));
137 MutableCompositeNode cont2 = createMutableCompositeNode(new QName(URI.create(NAMESPACE_BASE), revision_base,
138 "cont2"), cont, null, null, null);
139 List<Node<?>> cont2Childs = new ArrayList<>();
140 cont2Childs.add(createMutableSimpleNode(new QName(URI.create(NAMESPACE_BASE), revision_base, "lf21"), cont2,
141 "value in cont2/lf21", null, null));
142 cont2.setValue(cont2Childs);
143 contChilds.add(cont2);
146 contChilds.add(createMutableCompositeNode(new QName(URI.create(NAMESPACE_BASE), revision_base, "lst1"), cont,
147 Collections.<Node<?>> emptyList(), null, null));
150 MutableCompositeNode lst2_1 = createMutableCompositeNode(new QName(URI.create(NAMESPACE_BASE), revision_base,
151 "lst2"), cont, null, null, null);
152 List<Node<?>> lst2_1Childs = new ArrayList<>();
153 lst2_1Childs.add(createMutableSimpleNode(new QName(URI.create(NAMESPACE_BASE), revision_base, "lf21"), lst2_1,
154 "some value21", null, null));
155 lst2_1.setValue(lst2_1Childs);
156 contChilds.add(lst2_1);
158 MutableCompositeNode lst2_2 = createMutableCompositeNode(new QName(URI.create(NAMESPACE_BASE), revision_base,
159 "lst2"), cont, null, null, null);
160 List<Node<?>> lst2_2Childs = new ArrayList<>();
161 lst2_2Childs.add(createMutableSimpleNode(new QName(URI.create(NAMESPACE_BASE), revision_base, "lf22"), lst2_2,
162 "some value22", null, null));
163 lst2_2.setValue(lst2_2Childs);
164 contChilds.add(lst2_2);
167 contChilds.add(createMutableSimpleNode(new QName(URI.create(NAMESPACE_BASE), revision_base, "lflst1"), cont,
168 "lflst1_1", null, null));
169 contChilds.add(createMutableSimpleNode(new QName(URI.create(NAMESPACE_BASE), revision_base, "lflst1"), cont,
170 "lflst1_2", null, null));
173 contChilds.add(createMutableSimpleNode(new QName(URI.create(NAMESPACE_BASE), revision_base, "lf1"), cont,
177 contChilds.add(createMutableSimpleNode(new QName(URI.create(NAMESPACE_BASE), revision_base, "lf11"), cont,
178 "value from case (cs1)", null, null));
181 MutableCompositeNode cont3 = createMutableCompositeNode(new QName(URI.create(NAMESPACE_AUGMENT),
182 revision_augment, "cont3"), cont, null, null, null);
183 List<Node<?>> cont3Childs = new ArrayList<>();
184 cont3Childs.add(createMutableSimpleNode(new QName(URI.create(NAMESPACE_AUGMENT), revision_augment, "lf31"),
185 cont3, "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(new QName(new URI(NAMESPACE_AUGMENT), revision_augment, "cont3"));
257 containerBuilder.withChild(Builders
258 .augmentationBuilder()
259 .withNodeIdentifier(getAugmentationIdentifier(null, null, null, children))
261 Builders.containerBuilder()
262 .withNodeIdentifier(getNodeIdentifier("cont3", NAMESPACE_AUGMENT, revision_augment))
264 Builders.leafBuilder()
266 getNodeIdentifier("lf31", NAMESPACE_AUGMENT, revision_augment))
267 .withValue("value in leaf in augment").build()).build()).build());
269 ContainerNode build = containerBuilder.build();
273 private static InstanceIdentifier.NodeIdentifier getNodeIdentifier(String localName) {
274 return new InstanceIdentifier.NodeIdentifier(new QName(URI.create(NAMESPACE_BASE), revision_base, localName));
277 private static InstanceIdentifier.NodeIdentifier getNodeIdentifier(String localName, String namespace, Date revision) {
278 return new InstanceIdentifier.NodeIdentifier(new QName(URI.create(namespace), revision, localName));
281 private static InstanceIdentifier.NodeWithValue getNodeIdentifier(String localName, Object value) {
282 return new InstanceIdentifier.NodeWithValue(new QName(URI.create(NAMESPACE_BASE), revision_base, localName),
286 private static InstanceIdentifier.NodeIdentifierWithPredicates getNodeIdentifierPredicate(String localName,
287 Map<String, Object> keys) {
288 Map<QName, Object> predicate = new HashMap<>();
289 for (String key : keys.keySet()) {
290 predicate.put(new QName(URI.create(NAMESPACE_BASE), revision_base, key), keys.get(key));
293 return new InstanceIdentifier.NodeIdentifierWithPredicates(
295 new QName(URI.create(NAMESPACE_BASE), revision_base, localName), predicate);
298 private static InstanceIdentifier.AugmentationIdentifier getAugmentationIdentifier(String localName,
299 String namespace, Date revision, Set<QName> children) {
300 return new InstanceIdentifier.AugmentationIdentifier(null, children);