a27a465b139f0bc7ce83c8ea925dcea11149a924
[yangtools.git] / yang / yang-data-json / src / test / java / org / opendaylight / yangtools / yang / data / json / 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.json.schema;
9
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;
13
14 import java.io.File;
15 import java.io.FileNotFoundException;
16 import java.net.URI;
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;
26 import java.util.Map;
27 import java.util.Set;
28
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;
51
52 public class TestUtils {
53
54     private static final Logger LOG = LoggerFactory.getLogger(TestUtils.class);
55
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;
60
61     static {
62         try {
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) {
66             e.printStackTrace();
67         }
68     }
69
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());
77         }
78         for (String fileName : fileList) {
79             testFiles.add(new File(testDir, fileName));
80         }
81         return parser.parseYangModels(testFiles);
82     }
83
84     public static Set<Module> loadModulesFrom(String yangPath) throws URISyntaxException {
85         try {
86             return loadModules(TestUtils.class.getResource(yangPath).toURI());
87         } catch (FileNotFoundException e) {
88             LOG.error("Yang files at path: " + yangPath + " weren't loaded.");
89         }
90
91         return null;
92     }
93
94     public static DataSchemaNode resolveDataSchemaNode(String searchedDataSchemaName, Module module) {
95         assertNotNull("Module can't be null", module);
96
97         if (searchedDataSchemaName != null) {
98             for (DataSchemaNode dsn : module.getChildNodes()) {
99                 if (dsn.getQName().getLocalName().equals(searchedDataSchemaName)) {
100                     return dsn;
101                 }
102             }
103         } else if (module.getChildNodes().size() == 1) {
104             return module.getChildNodes().iterator().next();
105         }
106         return null;
107     }
108
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)) {
114                     return m;
115                 }
116             }
117         } else if (modules.size() == 1) {
118             return modules.iterator().next();
119         }
120         return null;
121     }
122
123     /**
124      * Prepare composite node structure according to
125      * /cnsn-to-normalized-node/simple-conainer.json
126      */
127     public static CompositeNode prepareCompositeNodeStruct() {
128         MutableCompositeNode cont = createMutableCompositeNode(new QName(URI.create(NAMESPACE_BASE), revision_base,
129                 "cont"), null, null, null, null);
130
131         // cont1
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));
135
136         // cont2
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);
144
145         // lst1
146         contChilds.add(createMutableCompositeNode(new QName(URI.create(NAMESPACE_BASE), revision_base, "lst1"), cont,
147                 Collections.<Node<?>> emptyList(), null, null));
148
149         // lst2
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);
157
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);
165
166         // lflst1
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));
171
172         // lf1
173         contChilds.add(createMutableSimpleNode(new QName(URI.create(NAMESPACE_BASE), revision_base, "lf1"), cont,
174                 "lf1", null, null));
175
176         // lf11
177         contChilds.add(createMutableSimpleNode(new QName(URI.create(NAMESPACE_BASE), revision_base, "lf11"), cont,
178                 "value from case (cs1)", null, null));
179
180         // cont3
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);
188
189         cont.setValue(contChilds);
190         return cont;
191     }
192
193     /**
194      * Prepare composite node structure according to
195      * /cnsn-to-normalized-node/simple-conainer.json
196      */
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
202                 .containerBuilder()
203                 .withNodeIdentifier(getNodeIdentifier("cont2"))
204                 .withChild(
205                         Builders.leafBuilder().withNodeIdentifier(getNodeIdentifier("lf21"))
206                                 .withValue("value in cont2/lf21").build()).build());
207
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());
214
215         CollectionNodeBuilder<MapEntryNode, MapNode> lst2 = Builders.mapBuilder().withNodeIdentifier(
216                 getNodeIdentifier("lst2"));
217
218         Map<String, Object> lst2_1 = new HashMap<>();
219         lst2_1.put("lf21", "some value21");
220
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());
226
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());
235
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());
243
244         containerBuilder.withChild(Builders.leafBuilder().withNodeIdentifier(getNodeIdentifier("lf1")).withValue("lf1")
245                 .build());
246
247         containerBuilder.withChild(Builders
248                 .choiceBuilder()
249                 .withNodeIdentifier(getNodeIdentifier("chc"))
250                 .withChild(
251                         Builders.leafBuilder().withNodeIdentifier(getNodeIdentifier("lf11"))
252                                 .withValue("value from case (cs1)").build()).build());
253
254         Set<QName> children = new HashSet<>();
255         children.add(new QName(new URI(NAMESPACE_AUGMENT), revision_augment, "cont3"));
256
257         containerBuilder.withChild(Builders
258                 .augmentationBuilder()
259                 .withNodeIdentifier(getAugmentationIdentifier(null, null, null, children))
260                 .withChild(
261                         Builders.containerBuilder()
262                                 .withNodeIdentifier(getNodeIdentifier("cont3", NAMESPACE_AUGMENT, revision_augment))
263                                 .withChild(
264                                         Builders.leafBuilder()
265                                                 .withNodeIdentifier(
266                                                         getNodeIdentifier("lf31", NAMESPACE_AUGMENT, revision_augment))
267                                                 .withValue("value in leaf in augment").build()).build()).build());
268
269         ContainerNode build = containerBuilder.build();
270         return build;
271     }
272
273     private static InstanceIdentifier.NodeIdentifier getNodeIdentifier(String localName) {
274         return new InstanceIdentifier.NodeIdentifier(new QName(URI.create(NAMESPACE_BASE), revision_base, localName));
275     }
276
277     private static InstanceIdentifier.NodeIdentifier getNodeIdentifier(String localName, String namespace, Date revision) {
278         return new InstanceIdentifier.NodeIdentifier(new QName(URI.create(namespace), revision, localName));
279     }
280
281     private static InstanceIdentifier.NodeWithValue getNodeIdentifier(String localName, Object value) {
282         return new InstanceIdentifier.NodeWithValue(new QName(URI.create(NAMESPACE_BASE), revision_base, localName),
283                 value);
284     }
285
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));
291         }
292
293         return new InstanceIdentifier.NodeIdentifierWithPredicates(
294
295         new QName(URI.create(NAMESPACE_BASE), revision_base, localName), predicate);
296     }
297
298     private static InstanceIdentifier.AugmentationIdentifier getAugmentationIdentifier(String localName,
299             String namespace, Date revision, Set<QName> children) {
300         return new InstanceIdentifier.AugmentationIdentifier(null, children);
301     }
302
303 }