Composite node <-> normalized node serializer and deserializer
[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.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;
52
53 public class TestUtils {
54
55     private static final Logger LOG = LoggerFactory.getLogger(TestUtils.class);
56     private static final QNameModule MODULE_AUGMENT;
57     private static final QNameModule MODULE_BASE;
58
59     static {
60         final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
61
62         try {
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);
67         }
68     }
69
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());
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(final 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(final String searchedDataSchemaName, final 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(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)) {
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(QName.create(MODULE_BASE, "cont"), null, null, null,
129                 null);
130
131         // cont1
132         List<Node<?>> contChilds = new ArrayList<>();
133         contChilds.add(createMutableCompositeNode(QName.create(MODULE_BASE, "cont1"),
134                 cont,
135                 Collections.<Node<?>> emptyList(), null, null));
136
137         // cont2
138         MutableCompositeNode cont2 = createMutableCompositeNode(QName.create(MODULE_BASE, "cont2"), cont, null, null,
139                 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);
145
146         // lst1
147         contChilds.add(createMutableCompositeNode(QName.create(MODULE_BASE, "lst1"), cont,
148                 Collections.<Node<?>> emptyList(), null, null));
149
150         // lst2
151         MutableCompositeNode lst2_1 = createMutableCompositeNode(QName.create(MODULE_BASE, "lst2"), cont, null, null,
152                 null);
153         List<Node<?>> lst2_1Childs = new ArrayList<>();
154         lst2_1Childs
155         .add(createMutableSimpleNode(QName.create(MODULE_BASE, "lf21"),
156                 lst2_1,
157                 "some value21", null, null));
158         lst2_1.setValue(lst2_1Childs);
159         contChilds.add(lst2_1);
160
161         MutableCompositeNode lst2_2 = createMutableCompositeNode(QName.create(MODULE_BASE, "lst2"), cont, null, null,
162                 null);
163         List<Node<?>> lst2_2Childs = new ArrayList<>();
164         lst2_2Childs
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);
168
169         // lflst1
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));
172
173         // lf1
174         contChilds.add(createMutableSimpleNode(QName.create(MODULE_BASE, "lf1"), cont, "lf1", null, null));
175
176         // lf11
177         contChilds.add(createMutableSimpleNode(QName.create(MODULE_BASE, "lf11"), cont, "value from case (cs1)", null,
178                 null));
179
180         // cont3
181         MutableCompositeNode cont3 = createMutableCompositeNode(QName.create(MODULE_AUGMENT, "cont3"), cont, null,
182                 null, 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);
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(QName.create(MODULE_AUGMENT, "cont3"));
256
257         containerBuilder.withChild(Builders
258                 .augmentationBuilder()
259                 .withNodeIdentifier(getAugmentationIdentifier(null, null, null, children))
260                 .withChild(
261                         Builders.containerBuilder()
262                         .withNodeIdentifier(getNodeIdentifier(MODULE_AUGMENT, "cont3"))
263                         .withChild(
264                                 Builders.leafBuilder()
265                                 .withNodeIdentifier(getNodeIdentifier(MODULE_AUGMENT, "lf31"))
266                                 .withValue("value in leaf in augment").build()).build()).build());
267
268         ContainerNode build = containerBuilder.build();
269         return build;
270     }
271
272     private static YangInstanceIdentifier.NodeIdentifier getNodeIdentifier(final String localName) {
273         return getNodeIdentifier(MODULE_BASE, localName);
274     }
275
276     private static YangInstanceIdentifier.NodeIdentifier getNodeIdentifier(final QNameModule module, final String localName) {
277         return new YangInstanceIdentifier.NodeIdentifier(QName.create(module, localName));
278     }
279
280     private static YangInstanceIdentifier.NodeWithValue getNodeIdentifier(final String localName, final Object value) {
281         return new YangInstanceIdentifier.NodeWithValue(QName.create(MODULE_BASE, localName),
282                 value);
283     }
284
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));
290         }
291
292         return new YangInstanceIdentifier.NodeIdentifierWithPredicates(QName.create(MODULE_BASE, localName), predicate);
293     }
294
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);
298     }
299
300 }