3 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7 * and is available at http://www.eclipse.org/legal/epl-v10.html
10 package org.opendaylight.controller.cluster.datastore.util;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Lists;
14 import com.google.common.collect.Maps;
15 import com.google.common.collect.Sets;
16 import org.custommonkey.xmlunit.Diff;
17 import org.custommonkey.xmlunit.XMLUnit;
18 import org.junit.Test;
19 import org.opendaylight.controller.protobuff.messages.common.SimpleNormalizedNodeMessage;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
27 import org.opendaylight.yangtools.yang.data.impl.codec.xml.XmlDocumentUtils;
28 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
29 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
30 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
31 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
32 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeBuilder;
33 import org.opendaylight.yangtools.yang.data.impl.schema.transform.dom.DomUtils;
34 import org.opendaylight.yangtools.yang.data.impl.schema.transform.dom.parser.DomToNormalizedNodeParserFactory;
35 import org.opendaylight.yangtools.yang.data.impl.schema.transform.dom.serializer.DomFromNormalizedNodeSerializerFactory;
36 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
37 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
39 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.Module;
41 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
42 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.w3c.dom.Document;
46 import org.w3c.dom.Element;
47 import org.xml.sax.SAXException;
49 import javax.xml.parsers.DocumentBuilder;
50 import javax.xml.parsers.DocumentBuilderFactory;
51 import javax.xml.parsers.ParserConfigurationException;
52 import javax.xml.transform.OutputKeys;
53 import javax.xml.transform.Transformer;
54 import javax.xml.transform.TransformerException;
55 import javax.xml.transform.TransformerFactory;
56 import javax.xml.transform.TransformerFactoryConfigurationError;
57 import javax.xml.transform.dom.DOMSource;
58 import javax.xml.transform.stream.StreamResult;
59 import java.io.ByteArrayInputStream;
60 import java.io.IOException;
61 import java.io.InputStream;
62 import java.io.StringWriter;
64 import java.text.ParseException;
65 import java.text.SimpleDateFormat;
66 import java.util.ArrayList;
67 import java.util.Collection;
68 import java.util.Collections;
69 import java.util.Date;
70 import java.util.List;
76 * Two of the testcases in the yangtools/yang-data-impl are leveraged (with modification) to create
77 * the serialization of NormalizedNode using the ProtocolBuffer
84 public class NormalizedNodeXmlConverterTest {
85 private static final Logger logger = LoggerFactory
86 .getLogger(NormalizedNodeXmlConverterTest.class);
87 public static final String NAMESPACE =
88 "urn:opendaylight:params:xml:ns:yang:controller:test";
89 private static Date revision;
90 private ContainerNode expectedNode;
91 private ContainerSchemaNode containerNode;
92 private String xmlPath;
96 revision = new SimpleDateFormat("yyyy-MM-dd").parse("2014-03-13");
97 } catch (ParseException e) {
98 throw new RuntimeException(e);
102 public static DataSchemaNode getSchemaNode(final SchemaContext context,
103 final String moduleName, final String childNodeName) {
104 for (Module module : context.getModules()) {
105 if (module.getName().equals(moduleName)) {
106 DataSchemaNode found =
107 findChildNode(module.getChildNodes(), childNodeName);
108 Preconditions.checkState(found != null, "Unable to find %s",
113 throw new IllegalStateException("Unable to find child node "
117 static DataSchemaNode findChildNode(
118 final Collection<DataSchemaNode> children, final String name) {
119 List<DataNodeContainer> containers = Lists.newArrayList();
121 for (DataSchemaNode dataSchemaNode : children) {
122 if (dataSchemaNode.getQName().getLocalName().equals(name)) {
123 return dataSchemaNode;
125 if (dataSchemaNode instanceof DataNodeContainer) {
126 containers.add((DataNodeContainer) dataSchemaNode);
127 } else if (dataSchemaNode instanceof ChoiceNode) {
128 containers.addAll(((ChoiceNode) dataSchemaNode).getCases());
132 for (DataNodeContainer container : containers) {
133 DataSchemaNode retVal = findChildNode(container.getChildNodes(), name);
134 if (retVal != null) {
142 public static YangInstanceIdentifier.NodeIdentifier getNodeIdentifier(
143 final String localName) {
144 return new YangInstanceIdentifier.NodeIdentifier(QName.create(
145 URI.create(NAMESPACE), revision, localName));
148 public static YangInstanceIdentifier.AugmentationIdentifier getAugmentIdentifier(
149 final String... childNames) {
150 Set<QName> qn = Sets.newHashSet();
152 for (String childName : childNames) {
153 qn.add(getNodeIdentifier(childName).getNodeType());
156 return new YangInstanceIdentifier.AugmentationIdentifier(qn);
160 public static ContainerNode augmentChoiceExpectedNode() {
162 DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> b =
163 Builders.containerBuilder();
164 b.withNodeIdentifier(getNodeIdentifier("container"));
168 .withNodeIdentifier(getNodeIdentifier("ch2"))
170 Builders.leafBuilder()
171 .withNodeIdentifier(getNodeIdentifier("c2Leaf")).withValue("2")
176 .withNodeIdentifier(getNodeIdentifier("c2DeepChoice"))
181 getNodeIdentifier("c2DeepChoiceCase1Leaf2"))
182 .withValue("2").build()).build()).build());
186 .withNodeIdentifier(getNodeIdentifier("ch3"))
188 Builders.leafBuilder()
189 .withNodeIdentifier(getNodeIdentifier("c3Leaf")).withValue("3")
193 .augmentationBuilder()
194 .withNodeIdentifier(getAugmentIdentifier("augLeaf"))
196 Builders.leafBuilder()
197 .withNodeIdentifier(getNodeIdentifier("augLeaf"))
198 .withValue("augment").build()).build());
201 .augmentationBuilder()
202 .withNodeIdentifier(getAugmentIdentifier("ch"))
206 .withNodeIdentifier(getNodeIdentifier("ch"))
208 Builders.leafBuilder()
209 .withNodeIdentifier(getNodeIdentifier("c1Leaf"))
210 .withValue("1").build())
213 .augmentationBuilder()
215 getAugmentIdentifier("c1Leaf_AnotherAugment",
221 getNodeIdentifier("c1Leaf_AnotherAugment"))
222 .withValue("1").build())
227 getNodeIdentifier("deepChoice"))
232 getNodeIdentifier("deepLeafc1"))
233 .withValue("1").build()).build())
234 .build()).build()).build());
241 public void init(final String yangPath, final String xmlPath,
242 final ContainerNode expectedNode) throws Exception {
243 SchemaContext schema = parseTestSchema(yangPath);
244 this.xmlPath = xmlPath;
246 (ContainerSchemaNode) getSchemaNode(schema, "test", "container");
247 this.expectedNode = expectedNode;
250 SchemaContext parseTestSchema(final String yangPath) throws Exception {
252 YangParserImpl yangParserImpl = new YangParserImpl();
254 NormalizedNodeXmlConverterTest.class.getResourceAsStream(yangPath);
255 ArrayList<InputStream> al = new ArrayList<InputStream>();
257 Set<Module> modules = yangParserImpl.parseYangModelsFromStreams(al);
258 return yangParserImpl.resolveSchemaContext(modules);
264 public void testConversionWithAugmentChoice() throws Exception {
265 init("/augment_choice.yang", "/augment_choice.xml",
266 augmentChoiceExpectedNode());
267 Document doc = loadDocument(xmlPath);
269 ContainerNode built =
270 DomToNormalizedNodeParserFactory
271 .getInstance(DomUtils.defaultValueCodecProvider())
272 .getContainerNodeParser()
273 .parse(Collections.singletonList(doc.getDocumentElement()),
276 if (expectedNode != null) {
277 junit.framework.Assert.assertEquals(expectedNode, built);
280 logger.info("{}", built);
282 Iterable<Element> els =
283 DomFromNormalizedNodeSerializerFactory
284 .getInstance(XmlDocumentUtils.getDocument(),
285 DomUtils.defaultValueCodecProvider())
286 .getContainerNodeSerializer().serialize(containerNode, built);
288 Element el = els.iterator().next();
290 XMLUnit.setIgnoreWhitespace(true);
291 XMLUnit.setIgnoreComments(true);
293 System.out.println(toString(doc.getDocumentElement()));
294 System.out.println(toString(el));
296 new Diff(XMLUnit.buildControlDocument(toString(doc.getDocumentElement())),
297 XMLUnit.buildTestDocument(toString(el))).similar();
300 private static ContainerNode listLeafListWithAttributes() {
301 DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> b =
302 Builders.containerBuilder();
303 b.withNodeIdentifier(getNodeIdentifier("container"));
305 CollectionNodeBuilder<MapEntryNode, MapNode> listBuilder =
306 Builders.mapBuilder().withNodeIdentifier(getNodeIdentifier("list"));
308 Map<QName, Object> predicates = Maps.newHashMap();
309 predicates.put(getNodeIdentifier("uint32InList").getNodeType(), 3L);
311 DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> list1Builder =
312 Builders.mapEntryBuilder().withNodeIdentifier(
313 new YangInstanceIdentifier.NodeIdentifierWithPredicates(
314 getNodeIdentifier("list").getNodeType(), predicates));
315 NormalizedNodeBuilder<YangInstanceIdentifier.NodeIdentifier, Object, LeafNode<Object>> uint32InListBuilder =
316 Builders.leafBuilder().withNodeIdentifier(
317 getNodeIdentifier("uint32InList"));
319 list1Builder.withChild(uint32InListBuilder.withValue(3L).build());
321 listBuilder.withChild(list1Builder.build());
322 b.withChild(listBuilder.build());
324 NormalizedNodeBuilder<YangInstanceIdentifier.NodeIdentifier, Object, LeafNode<Object>> booleanBuilder =
325 Builders.leafBuilder().withNodeIdentifier(getNodeIdentifier("boolean"));
326 booleanBuilder.withValue(false);
327 b.withChild(booleanBuilder.build());
329 ListNodeBuilder<Object, LeafSetEntryNode<Object>> leafListBuilder =
330 Builders.leafSetBuilder().withNodeIdentifier(
331 getNodeIdentifier("leafList"));
333 NormalizedNodeBuilder<YangInstanceIdentifier.NodeWithValue, Object, LeafSetEntryNode<Object>> leafList1Builder =
334 Builders.leafSetEntryBuilder().withNodeIdentifier(
335 new YangInstanceIdentifier.NodeWithValue(getNodeIdentifier(
336 "leafList").getNodeType(), "a"));
338 leafList1Builder.withValue("a");
340 leafListBuilder.withChild(leafList1Builder.build());
341 b.withChild(leafListBuilder.build());
348 public void testConversionWithAttributes() throws Exception {
349 init("/test.yang", "/simple_xml_with_attributes.xml",
350 listLeafListWithAttributes());
351 Document doc = loadDocument(xmlPath);
353 ContainerNode built =
354 DomToNormalizedNodeParserFactory
355 .getInstance(DomUtils.defaultValueCodecProvider())
356 .getContainerNodeParser()
357 .parse(Collections.singletonList(doc.getDocumentElement()),
360 if (expectedNode != null) {
361 junit.framework.Assert.assertEquals(expectedNode, built);
364 logger.info("{}", built);
366 Iterable<Element> els =
367 DomFromNormalizedNodeSerializerFactory
368 .getInstance(XmlDocumentUtils.getDocument(),
369 DomUtils.defaultValueCodecProvider())
370 .getContainerNodeSerializer().serialize(containerNode, built);
372 Element el = els.iterator().next();
374 XMLUnit.setIgnoreWhitespace(true);
375 XMLUnit.setIgnoreComments(true);
377 System.out.println(toString(doc.getDocumentElement()));
378 System.out.println(toString(el));
380 new Diff(XMLUnit.buildControlDocument(toString(doc.getDocumentElement())),
381 XMLUnit.buildTestDocument(toString(el))).similar();
385 private Document loadDocument(final String xmlPath) throws Exception {
386 InputStream resourceAsStream =
387 NormalizedNodeXmlConverterTest.class.getResourceAsStream(xmlPath);
389 Document currentConfigElement = readXmlToDocument(resourceAsStream);
390 Preconditions.checkNotNull(currentConfigElement);
391 return currentConfigElement;
394 private static final DocumentBuilderFactory BUILDERFACTORY;
397 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
398 factory.setNamespaceAware(true);
399 factory.setCoalescing(true);
400 factory.setIgnoringElementContentWhitespace(true);
401 factory.setIgnoringComments(true);
402 BUILDERFACTORY = factory;
405 private Document readXmlToDocument(final InputStream xmlContent)
406 throws IOException, SAXException {
407 DocumentBuilder dBuilder;
409 dBuilder = BUILDERFACTORY.newDocumentBuilder();
410 } catch (ParserConfigurationException e) {
411 throw new RuntimeException("Failed to parse XML document", e);
413 Document doc = dBuilder.parse(xmlContent);
415 doc.getDocumentElement().normalize();
419 public static String toString(final Element xml) {
421 Transformer transformer =
422 TransformerFactory.newInstance().newTransformer();
423 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
425 StreamResult result = new StreamResult(new StringWriter());
426 DOMSource source = new DOMSource(xml);
427 transformer.transform(source, result);
429 return result.getWriter().toString();
430 } catch (IllegalArgumentException | TransformerFactoryConfigurationError
431 | TransformerException e) {
432 throw new RuntimeException("Unable to serialize xml element " + xml, e);
437 public void testConversionToNormalizedXml() throws Exception {
438 SimpleNormalizedNodeMessage.NormalizedNodeXml nnXml =
439 EncoderDecoderUtil.encode(parseTestSchema("/augment_choice.yang"),
440 augmentChoiceExpectedNode());
441 Document expectedDoc = loadDocument("/augment_choice.xml");
442 Document convertedDoc =
443 EncoderDecoderUtil.factory.newDocumentBuilder().parse(
444 new ByteArrayInputStream(nnXml.getXmlString().getBytes("utf-8")));
445 System.out.println(toString(convertedDoc.getDocumentElement()));
446 XMLUnit.setIgnoreWhitespace(true);
447 XMLUnit.setIgnoreComments(true);
448 new Diff(XMLUnit.buildControlDocument(toString(expectedDoc
449 .getDocumentElement())),
450 XMLUnit.buildTestDocument(toString(convertedDoc.getDocumentElement())))
452 System.out.println(toString(expectedDoc.getDocumentElement()));
458 public void testConversionFromXmlToNormalizedNode() throws Exception {
459 SimpleNormalizedNodeMessage.NormalizedNodeXml nnXml =
460 EncoderDecoderUtil.encode(parseTestSchema("/test.yang"),
461 listLeafListWithAttributes());
462 Document expectedDoc = loadDocument("/simple_xml_with_attributes.xml");
463 Document convertedDoc =
464 EncoderDecoderUtil.factory.newDocumentBuilder().parse(
465 new ByteArrayInputStream(nnXml.getXmlString().getBytes("utf-8")));
466 System.out.println(toString(convertedDoc.getDocumentElement()));
467 XMLUnit.setIgnoreWhitespace(true);
468 XMLUnit.setIgnoreComments(true);
469 new Diff(XMLUnit.buildControlDocument(toString(expectedDoc
470 .getDocumentElement())),
471 XMLUnit.buildTestDocument(toString(convertedDoc.getDocumentElement())))
473 System.out.println(toString(expectedDoc.getDocumentElement()));
475 // now we will try to convert xml back to normalize node.
477 (ContainerNode) EncoderDecoderUtil.decode(
478 parseTestSchema("/test.yang"), nnXml);
479 junit.framework.Assert.assertEquals(listLeafListWithAttributes(), cn);