Shift Builder<P> from toInstance() to build()
[yangtools.git] / restconf / restconf-util / src / test / java / org / opendaylight / yangtools / restconf / utils / Bug1196Test.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.restconf.utils;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12
13 import java.io.InputStream;
14 import java.util.AbstractMap.SimpleEntry;
15 import java.util.Map.Entry;
16
17 import javassist.ClassPool;
18
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.topology.unix.rev131222.network.topology.topology.node.path.computation.client.reported.lsp.lsp.tlvs.vs.tlv.vendor.payload.unix.UnixSubTlvs;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.ReportedLsp1;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.Lsp;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.lsp.Tlvs;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.network.topology.topology.node.path.computation.client.reported.lsp.lsp.tlvs.vs.tlv.vendor.payload.linux.LinuxSubTlvs;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vs.tlv.VsTlv;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.Node1;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.PathComputationClient;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.path.computation.client.ReportedLsp;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
35 import org.opendaylight.yangtools.sal.binding.generator.impl.ModuleInfoBackedContext;
36 import org.opendaylight.yangtools.sal.binding.generator.impl.RuntimeGeneratedMappingServiceImpl;
37 import org.opendaylight.yangtools.yang.binding.DataObject;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
40 import org.opendaylight.yangtools.yang.common.QName;
41 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
42 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
43
44 public class Bug1196Test {
45
46     private static final InstanceIdentifier<PathComputationClient> PATH_TO_CLIENT = InstanceIdentifier.builder(NetworkTopology.class)
47             .child(Topology.class)
48             .child(Node.class)
49             .augmentation(Node1.class)
50             .child(PathComputationClient.class)
51             .build();
52     private RuntimeGeneratedMappingServiceImpl mappingService;
53
54     @Before
55     public void setup() {
56         this.mappingService = new RuntimeGeneratedMappingServiceImpl(new ClassPool());
57
58         final ModuleInfoBackedContext moduleInfo = ModuleInfoBackedContext.create();
59         moduleInfo.addModuleInfos(BindingReflections.loadModuleInfos());
60         this.mappingService.onGlobalContextUpdated(moduleInfo.tryToCreateSchemaContext().get());
61     }
62
63     @Test
64     public void testXmlDataToDataObjectLinuxCase() {
65         final InstanceIdentifier<Topology> instanceIdentifier = InstanceIdentifier.builder(NetworkTopology.class)
66                 .child(Topology.class, new TopologyKey(new TopologyId("example-pcep-topology"))).build();
67         final InputStream is = this.getClass().getClassLoader().getResourceAsStream("topology-bug1196-linux.xml");
68         final DataSchemaNode dataSchema = RestconfUtils.toRestconfIdentifier(instanceIdentifier, this.mappingService,
69                 this.mappingService.getSchemaContext()).getValue();
70         Topology topology = (Topology) RestconfUtils.dataObjectFromInputStream(instanceIdentifier, is,
71                 this.mappingService.getSchemaContext(), this.mappingService, dataSchema);
72         assertNotNull(topology);
73         assertNotNull(topology.getNode());
74         assertEquals(1, topology.getNode().size());
75         Node node = topology.getNode().get(0);
76         Node1 node1 = node.getAugmentation(Node1.class);
77         assertNotNull(node1);
78         final PathComputationClient pcc = node1.getPathComputationClient();
79         final Lsp lsp = pcc.getReportedLsp().get(0).getAugmentation(ReportedLsp1.class).getLsp();
80         org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.lsp.Tlvs tlvs = lsp.getTlvs();
81         assertNotNull(tlvs);
82         VsTlv vsTlv = tlvs.getVsTlv();
83         assertNotNull(vsTlv.getVendorPayload());
84
85         Entry<org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, CompositeNode> domPcc = mappingService.toDataDom(new SimpleEntry<InstanceIdentifier<?>,DataObject>(PATH_TO_CLIENT,pcc));
86         CompositeNode domPccValue = domPcc.getValue();
87         assertNotNull(domPccValue);
88         CompositeNode domPccTlvs = getFirstReportedLspVsTlvs(domPccValue);
89         assertNotNull(domPccTlvs);
90         assertNotNull(domPccTlvs.getFirstCompositeByName(LinuxSubTlvs.QNAME));
91
92     }
93
94     private CompositeNode getFirstReportedLspVsTlvs(final CompositeNode domPccValue) {
95         return domPccValue.getFirstCompositeByName(ReportedLsp.QNAME).getFirstCompositeByName(Lsp.QNAME).getFirstCompositeByName(Tlvs.QNAME).getFirstCompositeByName(QName.create(Tlvs.QNAME,VsTlv.QNAME.getLocalName()));
96     }
97
98     @Test
99     public void testXmlDataToDataObjectUnixCase() {
100         final InstanceIdentifier<Topology> instanceIdentifier = InstanceIdentifier.builder(NetworkTopology.class)
101                 .child(Topology.class, new TopologyKey(new TopologyId("example-pcep-topology"))).build();
102         final InputStream is = this.getClass().getClassLoader().getResourceAsStream("topology-bug1196-unix.xml");
103         final DataSchemaNode dataSchema = RestconfUtils.toRestconfIdentifier(instanceIdentifier, this.mappingService,
104                 this.mappingService.getSchemaContext()).getValue();
105         Topology topology = (Topology) RestconfUtils.dataObjectFromInputStream(instanceIdentifier, is,
106                 this.mappingService.getSchemaContext(), this.mappingService, dataSchema);
107         assertNotNull(topology);
108         assertNotNull(topology.getNode());
109         assertEquals(1, topology.getNode().size());
110         Node node = topology.getNode().get(0);
111         Node1 node1 = node.getAugmentation(Node1.class);
112         assertNotNull(node1);
113         final PathComputationClient pcc = node1.getPathComputationClient();
114         final Lsp lsp = pcc.getReportedLsp().get(0).getAugmentation(ReportedLsp1.class).getLsp();
115         org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.lsp.Tlvs tlvs = lsp.getTlvs();
116         assertNotNull(tlvs);
117         VsTlv vsTlv = tlvs.getVsTlv();
118         assertNotNull(vsTlv.getVendorPayload());
119
120         Entry<org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier, CompositeNode> domPcc = mappingService.toDataDom(new SimpleEntry<InstanceIdentifier<?>,DataObject>(PATH_TO_CLIENT,pcc));
121         CompositeNode domPccValue = domPcc.getValue();
122         assertNotNull(domPccValue);
123         CompositeNode domPccTlvs = getFirstReportedLspVsTlvs(domPccValue);
124         assertNotNull(domPccTlvs);
125         assertNotNull(domPccTlvs.getFirstCompositeByName(UnixSubTlvs.QNAME));
126     }
127
128 }