Parsing and serialization of composite and simple node to normalized nodes
[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 final static YangModelParser parser = new YangParserImpl();
57
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;
62
63     static {
64         try {
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) {
68             e.printStackTrace();
69         }
70     }
71
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);
78         }
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));
83             }
84         }
85         return parser.parseYangModels(testFiles);
86     }
87
88     public static Set<Module> loadModulesFrom(String yangPath) {
89         try {
90             return loadModules(TestUtils.class.getResource(yangPath).getPath());
91         } catch (FileNotFoundException e) {
92             LOG.error("Yang files at path: " + yangPath + " weren't loaded.");
93         }
94
95         return null;
96     }
97
98     public static DataSchemaNode resolveDataSchemaNode(String searchedDataSchemaName, Module module) {
99         assertNotNull("Module can't be null", module);
100
101         if (searchedDataSchemaName != null) {
102             for (DataSchemaNode dsn : module.getChildNodes()) {
103                 if (dsn.getQName().getLocalName().equals(searchedDataSchemaName)) {
104                     return dsn;
105                 }
106             }
107         } else if (module.getChildNodes().size() == 1) {
108             return module.getChildNodes().iterator().next();
109         }
110         return null;
111     }
112
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)) {
118                     return m;
119                 }
120             }
121         } else if (modules.size() == 1) {
122             return modules.iterator().next();
123         }
124         return null;
125     }
126
127     /**
128      * Prepare composite node structure according to
129      * /cnsn-to-normalized-node/simple-conainer.json
130      */
131     public static CompositeNode prepareCompositeNodeStruct() {
132         MutableCompositeNode cont = createMutableCompositeNode(new QName(URI.create(NAMESPACE_BASE), revision_base,
133                 "cont"), null, null, null, null);
134
135         // cont1
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));
139
140         // cont2
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);
148
149         // lst1
150         contChilds.add(createMutableCompositeNode(new QName(URI.create(NAMESPACE_BASE), revision_base, "lst1"), cont,
151                 Collections.<Node<?>> emptyList(), null, null));
152
153         // lst2
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);
161
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);
169
170         // lflst1
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));
175
176         // lf1
177         contChilds.add(createMutableSimpleNode(new QName(URI.create(NAMESPACE_BASE), revision_base, "lf1"), cont,
178                 "lf1", null, null));
179
180         // lf11
181         contChilds.add(createMutableSimpleNode(new QName(URI.create(NAMESPACE_BASE), revision_base, "lf11"), cont,
182                 "value from case (cs1)", null, null));
183
184         // cont3
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);
192
193         cont.setValue(contChilds);
194         return cont;
195     }
196
197     /**
198      * Prepare composite node structure according to
199      * /cnsn-to-normalized-node/simple-conainer.json
200      */
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
206                 .containerBuilder()
207                 .withNodeIdentifier(getNodeIdentifier("cont2"))
208                 .withChild(
209                         Builders.leafBuilder().withNodeIdentifier(getNodeIdentifier("lf21"))
210                                 .withValue("value in cont2/lf21").build()).build());
211
212
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());
219
220         CollectionNodeBuilder<MapEntryNode, MapNode> lst2 = Builders.mapBuilder().withNodeIdentifier(
221                 getNodeIdentifier("lst2"));
222
223         Map<String, Object> lst2_1 = new HashMap<>();
224         lst2_1.put("lf21", "some value21");
225
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());
231
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());
240
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());
248
249         containerBuilder.withChild(Builders.leafBuilder().withNodeIdentifier(getNodeIdentifier("lf1")).withValue("lf1")
250                 .build());
251
252         containerBuilder.withChild(Builders
253                 .choiceBuilder()
254                 .withNodeIdentifier(getNodeIdentifier("chc"))
255                 .withChild(
256                         Builders.leafBuilder().withNodeIdentifier(getNodeIdentifier("lf11"))
257                                 .withValue("value from case (cs1)").build()).build());
258
259         Set<QName> children = new HashSet<>();
260         children.add(new QName(new URI(NAMESPACE_AUGMENT), revision_augment, "cont3"));
261
262         containerBuilder.withChild(Builders
263                 .augmentationBuilder()
264                 .withNodeIdentifier(getAugmentationIdentifier(null, null, null, children))
265                 .withChild(
266                         Builders.containerBuilder()
267                                 .withNodeIdentifier(getNodeIdentifier("cont3", NAMESPACE_AUGMENT, revision_augment))
268                                 .withChild(
269                                         Builders.leafBuilder()
270                                                 .withNodeIdentifier(
271                                                         getNodeIdentifier("lf31", NAMESPACE_AUGMENT, revision_augment))
272                                                 .withValue("value in leaf in augment").build()).build()).build());
273
274
275         ContainerNode build = containerBuilder.build();
276         return build;
277     }
278
279     private static InstanceIdentifier.NodeIdentifier getNodeIdentifier(String localName) {
280         return new InstanceIdentifier.NodeIdentifier(new QName(URI.create(NAMESPACE_BASE), revision_base, localName));
281     }
282
283     private static InstanceIdentifier.NodeIdentifier getNodeIdentifier(String localName, String namespace, Date revision) {
284         return new InstanceIdentifier.NodeIdentifier(new QName(URI.create(namespace), revision, localName));
285     }
286
287     private static InstanceIdentifier.NodeWithValue getNodeIdentifier(String localName, Object value) {
288         return new InstanceIdentifier.NodeWithValue(new QName(URI.create(NAMESPACE_BASE), revision_base, localName),
289                 value);
290     }
291
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));
297         }
298
299         return new InstanceIdentifier.NodeIdentifierWithPredicates(
300
301         new QName(URI.create(NAMESPACE_BASE), revision_base, localName), predicate);
302     }
303
304     private static InstanceIdentifier.AugmentationIdentifier getAugmentationIdentifier(String localName,
305             String namespace, Date revision, Set<QName> children) {
306         return new InstanceIdentifier.AugmentationIdentifier(null, children);
307     }
308
309 }