2 * Copyright (c) 2013 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.impl;
10 import groovy.lang.Binding;
11 import groovy.lang.GroovyShell;
12 import groovy.lang.Script;
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;
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;
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;
51 * @author michal.rehak
53 * @deprecated Use {@link NormalizedNodeUtils} instead.
56 public abstract class NodeHelper {
58 private static final Logger LOG = LoggerFactory.getLogger(NodeHelper.class);
60 /** xml source of example network configuration */
61 public static final String NETWORK_XML =
62 "<network xmlns=\"urn:opendaylight:controller:network\">\n" +
66 " <topology-id>topId_01</topology-id>\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" +
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" +
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" +
101 " <link-id>linkId_04</link-id>\n" +
103 " <source-node>nodeId_05</source-node>\n" +
104 " <source-tp>tpId_06</source-tp>\n" +
107 " <dest-node>nodeId_07</dest-node>\n" +
108 " <dest-tp>tpId_08</dest-tp>\n" +
109 " </destination>\n" +
112 " <link-id>linkId_11</link-id>\n" +
114 " <source-node>nodeId_12</source-node>\n" +
115 " <source-tp>tpId_13</source-tp>\n" +
118 " <dest-node>nodeId_14</dest-node>\n" +
119 " <dest-tp>tpId_15</dest-tp>\n" +
120 " </destination>\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" +
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();
152 private static void dumpDoc(final Document domTree, final PrintStream out) throws Exception {
153 out.println(domTreeString(domTree));
156 public static void dumpDoc(final Document domTree, final Logger logger) throws TransformerException {
157 logger.info("{}", domTreeString(domTree));
162 * @return example tree, see {@link #NETWORK_XML}
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"),
169 assignParentToChildren(ntElementNode1);
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"),
175 assignParentToChildren(ntElementNode2);
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);
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,
190 assignParentToChildren(destination);
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);
199 value = new ArrayList<Node<?>>();
200 value.add(NodeFactory.createImmutableSimpleNode(QName.create(qName, "link-id"), null, "linkId_04"));
202 value.add(destination);
203 CompositeNode link1 = NodeFactory.createImmutableCompositeNode(
204 QName.create(qName, "link"), null, value);
205 assignParentToChildren(link1);
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);
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);
221 value = new ArrayList<Node<?>>();
222 value.add(NodeFactory.createImmutableSimpleNode(QName.create(qName, "link-id"), null, "linkId_11"));
224 value.add(destination);
225 CompositeNode link2 = NodeFactory.createImmutableCompositeNode(
226 QName.create(qName, "link"), null, value);
227 assignParentToChildren(link2);
229 value = new ArrayList<Node<?>>();
232 CompositeNode links = NodeFactory.createImmutableCompositeNode(
233 QName.create(qName, "links"), null, value);
234 assignParentToChildren(links);
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);
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);
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);
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"),
262 assignParentToChildren(terminationPointNode1);
264 value = new ArrayList<Node<?>>();
265 value.add(terminationPointNode1);
266 terminationPointsNode = NodeFactory.createImmutableCompositeNode(
267 QName.create(qName, "termination-points"),
269 assignParentToChildren(terminationPointsNode);
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);
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"),
284 assignParentToChildren(terminationPointNode1);
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);
292 value = new ArrayList<Node<?>>();
293 value.add(terminationPointNode1);
294 value.add(terminationPointNode2);
295 terminationPointsNode = NodeFactory.createImmutableCompositeNode(
296 QName.create(qName, "termination-points"),
298 assignParentToChildren(terminationPointsNode);
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);
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);
316 value = new ArrayList<Node<?>>();
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);
324 value = new ArrayList<Node<?>>();
326 CompositeNode topologies = NodeFactory.createImmutableCompositeNode(
327 QName.create(qName, "topologies"), null,
329 assignParentToChildren(topologies);
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);
344 public static void assignParentToChildren(final CompositeNode parentNode) {
345 for (Node<?> child : parentNode.getValue()) {
346 ((AbstractNodeTO<?>) child).setParent(parentNode);
351 * @return schema context of controller-network.yang
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);
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);
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]);
384 return builder.getRootNode();
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>
392 public static String AddNamespaceToPattern(final String pattern, final Object... nsArg) {
394 String patternNs = pattern.replaceAll("'", "''");
396 ns = new Object[]{""};
398 // add ':' into pattern after placeholders
399 patternNs = patternNs.replaceAll("(\\{[0-9]+\\})", "$1:");
402 return MessageFormat.format(patternNs, ns);
410 * @throws SAXException
411 * @throws IOException
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()));
419 Reader expectedReader = new InputStreamReader(clazz.getResourceAsStream(xmlFile));
420 Diff myDiff = new Diff(expectedReader, actualReader);
421 myDiff.overrideDifferenceListener(new IgnoreWhiteCharsDiffListener());
423 boolean similar = myDiff.similar();
425 LOG.info("{}", new String(actualRaw.toByteArray()));
427 Assert.assertEquals(myDiff.toString(), true, similar);