BUG-1472: deprecated CompositeNode and friends
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / NodeHelper.java
1 /*
2  * Copyright (c) 2013 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.impl;
9
10 import groovy.lang.Binding;
11 import groovy.lang.GroovyShell;
12 import groovy.lang.Script;
13
14 import java.io.ByteArrayInputStream;
15 import java.io.ByteArrayOutputStream;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.io.InputStreamReader;
19 import java.io.PrintStream;
20 import java.io.Reader;
21 import java.io.StringWriter;
22 import java.lang.reflect.Method;
23 import java.text.MessageFormat;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Set;
27
28 import javax.xml.transform.OutputKeys;
29 import javax.xml.transform.Transformer;
30 import javax.xml.transform.TransformerException;
31 import javax.xml.transform.TransformerFactory;
32 import javax.xml.transform.dom.DOMSource;
33 import javax.xml.transform.stream.StreamResult;
34
35 import org.custommonkey.xmlunit.Diff;
36 import org.junit.Assert;
37 import org.opendaylight.yangtools.yang.common.QName;
38 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
39 import org.opendaylight.yangtools.yang.data.api.Node;
40 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeUtils;
41 import org.opendaylight.yangtools.yang.model.api.Module;
42 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
43 import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
44 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47 import org.w3c.dom.Document;
48 import org.xml.sax.SAXException;
49
50 /**
51  * @author michal.rehak
52  *
53  * @deprecated Use {@link NormalizedNodeUtils} instead.
54  */
55 @Deprecated
56 public abstract class NodeHelper {
57
58     private static final Logger LOG = LoggerFactory.getLogger(NodeHelper.class);
59
60     /** xml source of example network configuration */
61     public static final String NETWORK_XML =
62             "<network xmlns=\"urn:opendaylight:controller:network\">\n" +
63                     //"<network>\n" +
64                     "    <topologies>\n" +
65                     "        <topology>\n" +
66                     "            <topology-id>topId_01</topology-id>\n" +
67                     "            <nodes>\n" +
68                     "                <node>\n" +
69                     "                    <node-id>nodeId_02</node-id>\n" +
70                     "                    <supporting-ne>networkId_02</supporting-ne>\n" +
71                     "                    <termination-points>\n" +
72                     "                        <termination-point>\n" +
73                     "                            <tp-id>tpId_03</tp-id>\n" +
74                     "                        </termination-point>\n" +
75                     "                    </termination-points>\n" +
76                     "                </node>\n" +
77                     "                <node>\n" +
78                     "                    <node-id>nodeId_16</node-id>\n" +
79                     "                    <supporting-ne>networkId_17</supporting-ne>\n" +
80                     "                    <termination-points>\n" +
81                     "                        <termination-point>\n" +
82                     "                            <tp-id>tpId_18</tp-id>\n" +
83                     "                        </termination-point>\n" +
84                     "                    </termination-points>\n" +
85                     "                </node>\n" +
86                     "                <node>\n" +
87                     "                    <node-id>nodeId_19</node-id>\n" +
88                     "                    <supporting-ne>networkId_20</supporting-ne>\n" +
89                     "                    <termination-points>\n" +
90                     "                        <termination-point>\n" +
91                     "                            <tp-id>tpId_18</tp-id>\n" +
92                     "                        </termination-point>\n" +
93                     "                        <termination-point>\n" +
94                     "                            <tp-id>tpId_19</tp-id>\n" +
95                     "                        </termination-point>\n" +
96                     "                    </termination-points>\n" +
97                     "                </node>\n" +
98                     "            </nodes>\n" +
99                     "            <links>\n" +
100                     "                <link>\n" +
101                     "                    <link-id>linkId_04</link-id>\n" +
102                     "                    <source>\n" +
103                     "                        <source-node>nodeId_05</source-node>\n" +
104                     "                        <source-tp>tpId_06</source-tp>\n" +
105                     "                    </source>\n" +
106                     "                    <destination>\n" +
107                     "                        <dest-node>nodeId_07</dest-node>\n" +
108                     "                        <dest-tp>tpId_08</dest-tp>\n" +
109                     "                    </destination>\n" +
110                     "                </link>\n" +
111                     "                <link>\n" +
112                     "                    <link-id>linkId_11</link-id>\n" +
113                     "                    <source>\n" +
114                     "                        <source-node>nodeId_12</source-node>\n" +
115                     "                        <source-tp>tpId_13</source-tp>\n" +
116                     "                    </source>\n" +
117                     "                    <destination>\n" +
118                     "                        <dest-node>nodeId_14</dest-node>\n" +
119                     "                        <dest-tp>tpId_15</dest-tp>\n" +
120                     "                    </destination>\n" +
121                     "                </link>\n" +
122                     "            </links>\n" +
123                     "        </topology>\n" +
124                     "    </topologies>\n" +
125                     "    <network-elements>\n" +
126                     "        <network-element>\n" +
127                     "            <element-id>ntElementId_09</element-id>\n" +
128                     "        </network-element>\n" +
129                     "        <network-element>\n" +
130                     "            <element-id>ntElementId_10</element-id>\n" +
131                     "        </network-element>\n" +
132                     "    </network-elements>\n" +
133                     "</network>";
134
135     private static String domTreeString(final Document domTree) throws TransformerException {
136         TransformerFactory transformerFact = TransformerFactory.newInstance();
137         transformerFact.setAttribute("indent-number", 4);
138         Transformer transformer = transformerFact.newTransformer();
139         transformer.setOutputProperty(OutputKeys.INDENT, "yes");
140         //initialize StreamResult with File object to save to file
141         StreamResult result = new StreamResult(new StringWriter());
142         DOMSource source = new DOMSource(domTree);
143         transformer.transform(source, result);
144         return result.getWriter().toString();
145     }
146
147     /**
148      * @param domTree
149      * @param out
150      * @throws Exception
151      */
152     private static void dumpDoc(final Document domTree, final PrintStream out) throws Exception {
153         out.println(domTreeString(domTree));
154     }
155
156     public static void dumpDoc(final Document domTree, final Logger logger) throws TransformerException {
157         logger.info("{}", domTreeString(domTree));
158     }
159
160     /**
161      * @param qName
162      * @return example tree, see {@link #NETWORK_XML}
163      */
164     public static CompositeNode buildTestConfigTree(final QName qName) {
165         List<Node<?>> value = new ArrayList<Node<?>>();
166         value.add(NodeFactory.createImmutableSimpleNode(QName.create(qName, "element-id"), null, "ntElementId_09"));
167         CompositeNode ntElementNode1 = NodeFactory.createImmutableCompositeNode(QName.create(qName, "network-element"),
168                 null, value);
169         assignParentToChildren(ntElementNode1);
170
171         value = new ArrayList<Node<?>>();
172         value.add(NodeFactory.createImmutableSimpleNode(QName.create(qName, "element-id"), null, "ntElementId_10"));
173         CompositeNode ntElementNode2 = NodeFactory.createImmutableCompositeNode(QName.create(qName, "network-element"),
174                 null, value);
175         assignParentToChildren(ntElementNode2);
176
177         value = new ArrayList<Node<?>>();
178         value.add(ntElementNode1);
179         value.add(ntElementNode2);
180         CompositeNode ntElementsNode = NodeFactory.createImmutableCompositeNode(
181                 QName.create(qName, "network-elements"), null, value);
182         assignParentToChildren(ntElementsNode);
183
184         value = new ArrayList<Node<?>>();
185         value.add(NodeFactory.createImmutableSimpleNode(QName.create(qName, "dest-node"), null, "nodeId_07"));
186         value.add(NodeFactory.createImmutableSimpleNode(QName.create(qName, "dest-tp"), null, "tpId_08"));
187         CompositeNode destination = NodeFactory.createImmutableCompositeNode(
188                 QName.create(qName, "destination"), null,
189                 value);
190         assignParentToChildren(destination);
191
192         value = new ArrayList<Node<?>>();
193         value.add(NodeFactory.createImmutableSimpleNode(QName.create(qName, "source-node"), null, "nodeId_05"));
194         value.add(NodeFactory.createImmutableSimpleNode(QName.create(qName, "source-tp"), null, "tpId_06"));
195         CompositeNode source = NodeFactory.createImmutableCompositeNode(
196                 QName.create(qName, "source"), null, value);
197         assignParentToChildren(source);
198
199         value = new ArrayList<Node<?>>();
200         value.add(NodeFactory.createImmutableSimpleNode(QName.create(qName, "link-id"), null, "linkId_04"));
201         value.add(source);
202         value.add(destination);
203         CompositeNode link1 = NodeFactory.createImmutableCompositeNode(
204                 QName.create(qName, "link"), null, value);
205         assignParentToChildren(link1);
206
207         value = new ArrayList<Node<?>>();
208         value.add(NodeFactory.createImmutableSimpleNode(QName.create(qName, "dest-node"), null, "nodeId_14"));
209         value.add(NodeFactory.createImmutableSimpleNode(QName.create(qName, "dest-tp"), null, "tpId_15"));
210         destination = NodeFactory.createImmutableCompositeNode(
211                 QName.create(qName, "destination"), null, value);
212         assignParentToChildren(destination);
213
214         value = new ArrayList<Node<?>>();
215         value.add(NodeFactory.createImmutableSimpleNode(QName.create(qName, "source-node"), null, "nodeId_12"));
216         value.add(NodeFactory.createImmutableSimpleNode(QName.create(qName, "source-tp"), null, "tpId_13"));
217         source = NodeFactory.createImmutableCompositeNode(
218                 QName.create(qName, "source"), null, value);
219         assignParentToChildren(source);
220
221         value = new ArrayList<Node<?>>();
222         value.add(NodeFactory.createImmutableSimpleNode(QName.create(qName, "link-id"), null, "linkId_11"));
223         value.add(source);
224         value.add(destination);
225         CompositeNode link2 = NodeFactory.createImmutableCompositeNode(
226                 QName.create(qName, "link"), null, value);
227         assignParentToChildren(link2);
228
229         value = new ArrayList<Node<?>>();
230         value.add(link1);
231         value.add(link2);
232         CompositeNode links = NodeFactory.createImmutableCompositeNode(
233                 QName.create(qName, "links"), null, value);
234         assignParentToChildren(links);
235
236
237         value = new ArrayList<Node<?>>();
238         value.add(NodeFactory.createImmutableSimpleNode(QName.create(qName, "tp-id"), null, "tpId_03"));
239         CompositeNode terminationPointNode1 = NodeFactory.createImmutableCompositeNode(
240                 QName.create(qName, "termination-point"), null, value);
241         assignParentToChildren(terminationPointNode1);
242
243         value = new ArrayList<Node<?>>();
244         value.add(terminationPointNode1);
245         CompositeNode terminationPointsNode = NodeFactory.createImmutableCompositeNode(
246                 QName.create(qName, "termination-points"), null, value);
247         assignParentToChildren(terminationPointsNode);
248
249         value = new ArrayList<Node<?>>();
250         value.add(NodeFactory.createImmutableSimpleNode(QName.create(qName, "node-id"), null, "nodeId_02"));
251         value.add(NodeFactory.createImmutableSimpleNode(QName.create(qName, "supporting-ne"), null, "networkId_02"));
252         value.add(terminationPointsNode);
253         CompositeNode node1Node = NodeFactory.createImmutableCompositeNode(
254                 QName.create(qName, "node"), null, value);
255         assignParentToChildren(node1Node);
256
257         value = new ArrayList<Node<?>>();
258         value.add(NodeFactory.createImmutableSimpleNode(QName.create(qName, "tp-id"), null, "tpId_18"));
259         terminationPointNode1 = NodeFactory.createImmutableCompositeNode(
260                 QName.create(qName, "termination-point"),
261                 null, value);
262         assignParentToChildren(terminationPointNode1);
263
264         value = new ArrayList<Node<?>>();
265         value.add(terminationPointNode1);
266         terminationPointsNode = NodeFactory.createImmutableCompositeNode(
267                 QName.create(qName, "termination-points"),
268                 null, value);
269         assignParentToChildren(terminationPointsNode);
270
271         value = new ArrayList<Node<?>>();
272         value.add(NodeFactory.createImmutableSimpleNode(QName.create(qName, "node-id"), null, "nodeId_16"));
273         value.add(NodeFactory.createImmutableSimpleNode(QName.create(qName, "supporting-ne"), null, "networkId_17"));
274         value.add(terminationPointsNode);
275         CompositeNode node2Node = NodeFactory.createImmutableCompositeNode(
276                 QName.create(qName, "node"), null, value);
277         assignParentToChildren(node2Node);
278
279         value = new ArrayList<Node<?>>();
280         value.add(NodeFactory.createImmutableSimpleNode(QName.create(qName, "tp-id"), null, "tpId_18"));
281         terminationPointNode1 = NodeFactory.createImmutableCompositeNode(
282                 QName.create(qName, "termination-point"),
283                 null, value);
284         assignParentToChildren(terminationPointNode1);
285
286         value = new ArrayList<Node<?>>();
287         value.add(NodeFactory.createImmutableSimpleNode(QName.create(qName, "tp-id"), null, "tpId_19"));
288         CompositeNode terminationPointNode2 = NodeFactory.createImmutableCompositeNode(
289                 QName.create(qName, "termination-point"), null, value);
290         assignParentToChildren(terminationPointNode2);
291
292         value = new ArrayList<Node<?>>();
293         value.add(terminationPointNode1);
294         value.add(terminationPointNode2);
295         terminationPointsNode = NodeFactory.createImmutableCompositeNode(
296                 QName.create(qName, "termination-points"),
297                 null, value);
298         assignParentToChildren(terminationPointsNode);
299
300         value = new ArrayList<Node<?>>();
301         value.add(NodeFactory.createImmutableSimpleNode(QName.create(qName, "node-id"), null, "nodeId_19"));
302         value.add(NodeFactory.createImmutableSimpleNode(QName.create(qName, "supporting-ne"), null, "networkId_20"));
303         value.add(terminationPointsNode);
304         CompositeNode node3Node = NodeFactory.createImmutableCompositeNode(
305                 QName.create(qName, "node"), null, value);
306         assignParentToChildren(node3Node);
307
308         value = new ArrayList<Node<?>>();
309         value.add(node1Node);
310         value.add(node2Node);
311         value.add(node3Node);
312         CompositeNode nodesNode = NodeFactory.createImmutableCompositeNode(
313                 QName.create(qName, "nodes"), null, value);
314         assignParentToChildren(nodesNode);
315
316         value = new ArrayList<Node<?>>();
317         value.add(links);
318         value.add(nodesNode);
319         value.add(NodeFactory.createImmutableSimpleNode(QName.create(qName, "topology-id"), null, "topId_01"));
320         CompositeNode topology = NodeFactory.createImmutableCompositeNode(
321                 QName.create(qName, "topology"), null, value);
322         assignParentToChildren(topology);
323
324         value = new ArrayList<Node<?>>();
325         value.add(topology);
326         CompositeNode topologies = NodeFactory.createImmutableCompositeNode(
327                 QName.create(qName, "topologies"), null,
328                 value);
329         assignParentToChildren(topologies);
330
331         value = new ArrayList<Node<?>>();
332         value.add(topologies);
333         value.add(ntElementsNode);
334         CompositeNode network = NodeFactory.createImmutableCompositeNode(
335                 QName.create(qName, "network"), null, value);
336         assignParentToChildren(network);
337
338         return network;
339     }
340
341     /**
342      * @param parentNode
343      */
344     public static void assignParentToChildren(final CompositeNode parentNode) {
345         for (Node<?> child : parentNode.getValue()) {
346             ((AbstractNodeTO<?>) child).setParent(parentNode);
347         }
348     }
349
350     /**
351      * @return schema context of controller-network.yang
352      */
353     public static SchemaContext loadSchemaContext() {
354         YangModelParser yParser = new YangParserImpl();
355         List<InputStream> yangInputStreams = new ArrayList<>();
356         yangInputStreams.add(NodeHelper.class.getResourceAsStream(
357                 "/controller-network.yang"));
358         yangInputStreams.add(NodeHelper.class.getResourceAsStream(
359                 "/ietf-inet-types@2010-09-24.yang"));
360         Set<Module> modules = yParser
361                 .parseYangModelsFromStreams(yangInputStreams);
362         return yParser.resolveSchemaContext(modules);
363     }
364
365     /**
366      * @param scriptName
367      * @return tree root
368      * @throws Exception
369      */
370     public static CompositeNode loadConfigByGroovy(final String scriptName) throws Exception {
371         InputStream configStream = NodeHelper.class.getResourceAsStream(scriptName);
372         Binding binding = new Binding();
373         GroovyShell gShell = new GroovyShell(binding);
374         LOG.debug("groovy: starting script parse..  {}", scriptName);
375         Script configScript = gShell.parse(new InputStreamReader(configStream));
376         LOG.debug("groovy: starting script..  {}", scriptName);
377         configScript.run();
378         LOG.debug("groovy: digging result");
379         Object xmlGen = binding.getVariable("xmlGen");
380         LOG.debug("xmlGen = {}", xmlGen);
381         Method getter = xmlGen.getClass().getDeclaredMethod("getBuilder", new Class[0]);
382         MyNodeBuilder builder = (MyNodeBuilder) getter.invoke(xmlGen, new Object[0]);
383
384         return builder.getRootNode();
385     }
386
387     /**
388      * @param pattern , e.g.: <pre>"//{0}:network/{1}:xx[text() = 'sss']"</pre>
389      * @param nsArg , e.g.: <pre>{"uri:ns1", "uri:ns2"}</pre>
390      * @return pattern with namespaces: <pre>//uri:ns1:network/uri:ns2:xx[text() = ''sss'']"</pre>
391      */
392     public static String AddNamespaceToPattern(final String pattern, final Object... nsArg) {
393         Object[] ns = nsArg;
394         String patternNs = pattern.replaceAll("'", "''");
395         if (ns == null) {
396             ns = new Object[]{""};
397         } else {
398             // add ':' into pattern after placeholders
399             patternNs = patternNs.replaceAll("(\\{[0-9]+\\})", "$1:");
400         }
401
402         return MessageFormat.format(patternNs, ns);
403     }
404
405     /**
406      * @param tree
407      * @param xmlFile
408      * @param clazz
409      * @throws Exception
410      * @throws SAXException
411      * @throws IOException
412      */
413     public static void compareXmlTree(final Document tree, final String xmlFile, final Class<?> clazz) throws Exception,
414     SAXException, IOException {
415         ByteArrayOutputStream actualRaw = new ByteArrayOutputStream();
416         dumpDoc(tree, new PrintStream(actualRaw));
417         Reader actualReader = new InputStreamReader(new ByteArrayInputStream(actualRaw.toByteArray()));
418
419         Reader expectedReader = new InputStreamReader(clazz.getResourceAsStream(xmlFile));
420         Diff myDiff = new Diff(expectedReader, actualReader);
421         myDiff.overrideDifferenceListener(new IgnoreWhiteCharsDiffListener());
422
423         boolean similar = myDiff.similar();
424         if (! similar) {
425             LOG.info("{}", new String(actualRaw.toByteArray()));
426         }
427         Assert.assertEquals(myDiff.toString(), true, similar);
428     }
429 }