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 final static YangModelParser parser = new YangParserImpl();
58 private static final String NAMESPACE_BASE = "simple:container:yang";
59 private static final String NAMESPACE_AUGMENT = "augment:container:yang";
60 private static Date revision_base;
61 private static Date revision_augment;
65 revision_base = new SimpleDateFormat("yyyy-MM-dd").parse("2013-11-12");
66 revision_augment = new SimpleDateFormat("yyyy-MM-dd").parse("2014-03-19");
67 } catch (ParseException e) {
72 private static Set<Module> loadModules(String resourceDirectory) throws FileNotFoundException {
73 final File testDir = new File(resourceDirectory);
74 final String[] fileList = testDir.list();
75 final List<File> testFiles = new ArrayList<File>();
76 if (fileList == null) {
77 throw new FileNotFoundException(resourceDirectory);
79 for (int i = 0; i < fileList.length; i++) {
80 String fileName = fileList[i];
81 if (new File(testDir, fileName).isDirectory() == false) {
82 testFiles.add(new File(testDir, fileName));
85 return parser.parseYangModels(testFiles);
88 public static Set<Module> loadModulesFrom(String yangPath) {
90 return loadModules(TestUtils.class.getResource(yangPath).getPath());
91 } catch (FileNotFoundException e) {
92 LOG.error("Yang files at path: " + yangPath + " weren't loaded.");
98 public static DataSchemaNode resolveDataSchemaNode(String searchedDataSchemaName, Module module) {
99 assertNotNull("Module can't be null", module);
101 if (searchedDataSchemaName != null) {
102 for (DataSchemaNode dsn : module.getChildNodes()) {
103 if (dsn.getQName().getLocalName().equals(searchedDataSchemaName)) {
107 } else if (module.getChildNodes().size() == 1) {
108 return module.getChildNodes().iterator().next();
113 public static Module resolveModule(String searchedModuleName, Set<Module> modules) {
114 assertNotNull("Modules can't be null.", modules);
115 if (searchedModuleName != null) {
116 for (Module m : modules) {
117 if (m.getName().equals(searchedModuleName)) {
121 } else if (modules.size() == 1) {
122 return modules.iterator().next();
128 * Prepare composite node structure according to
129 * /cnsn-to-normalized-node/simple-conainer.json
131 public static CompositeNode prepareCompositeNodeStruct() {
132 MutableCompositeNode cont = createMutableCompositeNode(new QName(URI.create(NAMESPACE_BASE), revision_base,
133 "cont"), null, null, null, null);
136 List<Node<?>> contChilds = new ArrayList<>();
137 contChilds.add(createMutableCompositeNode(new QName(URI.create(NAMESPACE_BASE), revision_base, "cont1"), cont,
138 Collections.<Node<?>> emptyList(), null, null));
141 MutableCompositeNode cont2 = createMutableCompositeNode(new QName(URI.create(NAMESPACE_BASE), revision_base,
142 "cont2"), cont, null, null, null);
143 List<Node<?>> cont2Childs = new ArrayList<>();
144 cont2Childs.add(createMutableSimpleNode(new QName(URI.create(NAMESPACE_BASE), revision_base, "lf21"), cont2,
145 "value in cont2/lf21", null, null));
146 cont2.setValue(cont2Childs);
147 contChilds.add(cont2);
150 contChilds.add(createMutableCompositeNode(new QName(URI.create(NAMESPACE_BASE), revision_base, "lst1"), cont,
151 Collections.<Node<?>> emptyList(), null, null));
154 MutableCompositeNode lst2_1 = createMutableCompositeNode(new QName(URI.create(NAMESPACE_BASE), revision_base,
155 "lst2"), cont, null, null, null);
156 List<Node<?>> lst2_1Childs = new ArrayList<>();
157 lst2_1Childs.add(createMutableSimpleNode(new QName(URI.create(NAMESPACE_BASE), revision_base, "lf21"), lst2_1,
158 "some value21", null, null));
159 lst2_1.setValue(lst2_1Childs);
160 contChilds.add(lst2_1);
162 MutableCompositeNode lst2_2 = createMutableCompositeNode(new QName(URI.create(NAMESPACE_BASE), revision_base,
163 "lst2"), cont, null, null, null);
164 List<Node<?>> lst2_2Childs = new ArrayList<>();
165 lst2_2Childs.add(createMutableSimpleNode(new QName(URI.create(NAMESPACE_BASE), revision_base, "lf22"), lst2_2,
166 "some value22", null, null));
167 lst2_2.setValue(lst2_2Childs);
168 contChilds.add(lst2_2);
171 contChilds.add(createMutableSimpleNode(new QName(URI.create(NAMESPACE_BASE), revision_base, "lflst1"), cont,
172 "lflst1_1", null, null));
173 contChilds.add(createMutableSimpleNode(new QName(URI.create(NAMESPACE_BASE), revision_base, "lflst1"), cont,
174 "lflst1_2", null, null));
177 contChilds.add(createMutableSimpleNode(new QName(URI.create(NAMESPACE_BASE), revision_base, "lf1"), cont,
181 contChilds.add(createMutableSimpleNode(new QName(URI.create(NAMESPACE_BASE), revision_base, "lf11"), cont,
182 "value from case (cs1)", null, null));
185 MutableCompositeNode cont3 = createMutableCompositeNode(new QName(URI.create(NAMESPACE_AUGMENT),
186 revision_augment, "cont3"), cont, null, null, null);
187 List<Node<?>> cont3Childs = new ArrayList<>();
188 cont3Childs.add(createMutableSimpleNode(new QName(URI.create(NAMESPACE_AUGMENT), revision_augment, "lf31"),
189 cont3, "value in leaf in augment", null, null));
190 cont3.setValue(cont3Childs);
191 contChilds.add(cont3);
193 cont.setValue(contChilds);
198 * Prepare composite node structure according to
199 * /cnsn-to-normalized-node/simple-conainer.json
201 public static ContainerNode prepareNormalizedNodeStruct() throws URISyntaxException {
202 DataContainerNodeBuilder<NodeIdentifier, ContainerNode> containerBuilder = Builders.containerBuilder();
203 containerBuilder.withNodeIdentifier(getNodeIdentifier("cont"));
204 containerBuilder.withChild(Builders.containerBuilder().withNodeIdentifier(getNodeIdentifier("cont1")).build());
205 containerBuilder.withChild(Builders
207 .withNodeIdentifier(getNodeIdentifier("cont2"))
209 Builders.leafBuilder().withNodeIdentifier(getNodeIdentifier("lf21"))
210 .withValue("value in cont2/lf21").build()).build());
213 CollectionNodeBuilder<MapEntryNode, MapNode> lst1 = Builders.mapBuilder().withNodeIdentifier(
214 getNodeIdentifier("lst1"));
215 lst1.withChild(Builders.mapEntryBuilder()
216 .withNodeIdentifier(getNodeIdentifierPredicate("lst1", new HashMap<String, Object>()))
217 .withValue(Collections.<DataContainerChild<? extends PathArgument, ?>> emptyList()).build());
218 containerBuilder.withChild(lst1.build());
220 CollectionNodeBuilder<MapEntryNode, MapNode> lst2 = Builders.mapBuilder().withNodeIdentifier(
221 getNodeIdentifier("lst2"));
223 Map<String, Object> lst2_1 = new HashMap<>();
224 lst2_1.put("lf21", "some value21");
226 List<DataContainerChild<? extends PathArgument, ?>> lst2_1_values = new ArrayList<>();
227 lst2_1_values.add(Builders.leafBuilder().withNodeIdentifier(getNodeIdentifier("lf21"))
228 .withValue("some value21").build());
229 lst2.withChild(Builders.mapEntryBuilder().withNodeIdentifier(getNodeIdentifierPredicate("lst2", lst2_1))
230 .withValue(lst2_1_values).build());
232 Map<String, Object> lst2_2 = new HashMap<>();
233 lst2_2.put("lf22", "some value22");
234 List<DataContainerChild<? extends PathArgument, ?>> lst2_2_values = new ArrayList<>();
235 lst2_2_values.add(Builders.leafBuilder().withNodeIdentifier(getNodeIdentifier("lf22"))
236 .withValue("some value22").build());
237 lst2.withChild(Builders.mapEntryBuilder().withNodeIdentifier(getNodeIdentifierPredicate("lst2", lst2_2))
238 .withValue(lst2_2_values).build());
239 containerBuilder.withChild(lst2.build());
241 ListNodeBuilder<Object, LeafSetEntryNode<Object>> lflst1 = Builders.leafSetBuilder().withNodeIdentifier(
242 getNodeIdentifier("lflst1"));
243 lflst1.withChild(Builders.leafSetEntryBuilder().withNodeIdentifier(getNodeIdentifier("lflst1", "lflst1_1"))
244 .withValue("lflst1_1").build());
245 lflst1.withChild(Builders.leafSetEntryBuilder().withNodeIdentifier(getNodeIdentifier("lflst1", "lflst1_2"))
246 .withValue("lflst1_2").build());
247 containerBuilder.withChild(lflst1.build());
249 containerBuilder.withChild(Builders.leafBuilder().withNodeIdentifier(getNodeIdentifier("lf1")).withValue("lf1")
252 containerBuilder.withChild(Builders
254 .withNodeIdentifier(getNodeIdentifier("chc"))
256 Builders.leafBuilder().withNodeIdentifier(getNodeIdentifier("lf11"))
257 .withValue("value from case (cs1)").build()).build());
259 Set<QName> children = new HashSet<>();
260 children.add(new QName(new URI(NAMESPACE_AUGMENT), revision_augment, "cont3"));
262 containerBuilder.withChild(Builders
263 .augmentationBuilder()
264 .withNodeIdentifier(getAugmentationIdentifier(null, null, null, children))
266 Builders.containerBuilder()
267 .withNodeIdentifier(getNodeIdentifier("cont3", NAMESPACE_AUGMENT, revision_augment))
269 Builders.leafBuilder()
271 getNodeIdentifier("lf31", NAMESPACE_AUGMENT, revision_augment))
272 .withValue("value in leaf in augment").build()).build()).build());
275 ContainerNode build = containerBuilder.build();
279 private static InstanceIdentifier.NodeIdentifier getNodeIdentifier(String localName) {
280 return new InstanceIdentifier.NodeIdentifier(new QName(URI.create(NAMESPACE_BASE), revision_base, localName));
283 private static InstanceIdentifier.NodeIdentifier getNodeIdentifier(String localName, String namespace, Date revision) {
284 return new InstanceIdentifier.NodeIdentifier(new QName(URI.create(namespace), revision, localName));
287 private static InstanceIdentifier.NodeWithValue getNodeIdentifier(String localName, Object value) {
288 return new InstanceIdentifier.NodeWithValue(new QName(URI.create(NAMESPACE_BASE), revision_base, localName),
292 private static InstanceIdentifier.NodeIdentifierWithPredicates getNodeIdentifierPredicate(String localName,
293 Map<String, Object> keys) {
294 Map<QName, Object> predicate = new HashMap<>();
295 for (String key : keys.keySet()) {
296 predicate.put(new QName(URI.create(NAMESPACE_BASE), revision_base, key), keys.get(key));
299 return new InstanceIdentifier.NodeIdentifierWithPredicates(
301 new QName(URI.create(NAMESPACE_BASE), revision_base, localName), predicate);
304 private static InstanceIdentifier.AugmentationIdentifier getAugmentationIdentifier(String localName,
305 String namespace, Date revision, Set<QName> children) {
306 return new InstanceIdentifier.AugmentationIdentifier(null, children);