1 package org.opendaylight.yangtools.it.yang.runtime.tests;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertNotNull;
6 import static org.junit.Assert.assertTrue;
8 import java.util.HashSet;
11 import org.junit.Before;
12 import org.junit.Test;
13 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.LinkId;
14 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
15 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopologyBuilder;
16 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
17 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
18 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
19 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.Source;
20 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.SourceBuilder;
21 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
22 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkBuilder;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkKey;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.opendaylight.yangtools.yang.binding.util.DataObjectReadingUtil;
30 import com.google.common.base.Optional;
31 import com.google.common.collect.ImmutableList;
33 public class BindingReadingTest {
35 private static final TopologyId TOPOLOGY_FOO_ID = new TopologyId("foo:topo");
36 private static final TopologyId TOPOLOGY_BAR_ID = new TopologyId("bar:topo");
37 private static final TopologyId TOPOLOGY_BAZ_ID = new TopologyId("baz:topo");
38 private static final LinkId LINK_BAR_ID = new LinkId("bar:link:1");
39 private static final NodeId SOURCE_NODE_ID = new NodeId("node:id");
40 private static final TpId SOURCE_TP_ID = new TpId("source:tp");
41 private static final InstanceIdentifier<NetworkTopology> NETWORK_TOPOLOGY_PATH = InstanceIdentifier.builder(
42 NetworkTopology.class).build();
43 private NetworkTopology networkModel;
44 private Link linkModel;
46 private static final InstanceIdentifier<Topology> TOPOLOGY_BAR_PATH = NETWORK_TOPOLOGY_PATH //
47 .child(Topology.class, new TopologyKey(TOPOLOGY_BAR_ID));
49 private static final InstanceIdentifier<Link> LINK_BAR_PATH = NETWORK_TOPOLOGY_PATH.builder() //
50 .child(Topology.class, new TopologyKey(TOPOLOGY_BAR_ID)) //
51 .child(Link.class, new LinkKey(LINK_BAR_ID)) //
54 private static final InstanceIdentifier<Link> WILDCARDED_LINK_PATH = NETWORK_TOPOLOGY_PATH.builder() //
55 .child(Topology.class) //
59 private static final InstanceIdentifier<Source> ABSOLUTE_SOURCE_PATH = TOPOLOGY_BAR_PATH.builder() //
60 .child(Link.class, new LinkKey(LINK_BAR_ID)) //
61 .child(Source.class) //
64 private static final InstanceIdentifier<Source> WILDCARDED_SOURCE_PATH = NETWORK_TOPOLOGY_PATH.builder() //
65 .child(Topology.class) //
66 .child(Link.class, new LinkKey(LINK_BAR_ID)) //
67 .child(Source.class) //
72 * Creates network topology model with three topologies:
74 * Where bar has 1 link, and baz has 2 links.
78 public void createTopology() {
79 linkModel = new LinkBuilder() //
80 .setLinkId(LINK_BAR_ID) //
81 .setSource(new SourceBuilder() //
82 .setSourceNode(SOURCE_NODE_ID) //
83 .setSourceTp(SOURCE_TP_ID) //
86 networkModel = new NetworkTopologyBuilder().setTopology(ImmutableList.<Topology> builder() //
87 .add(new TopologyBuilder() //
88 .setTopologyId(TOPOLOGY_FOO_ID) //
89 .setServerProvided(true) //
91 .add(new TopologyBuilder() //
92 .setTopologyId(TOPOLOGY_BAR_ID) //
93 .setServerProvided(false) //
94 .setLink(ImmutableList.<Link> builder() //
98 .add(new TopologyBuilder() //
100 .add(new TopologyBuilder() //
101 .setTopologyId(TOPOLOGY_BAZ_ID)//
102 .setLink(ImmutableList.<Link> builder() //
103 .add(new LinkBuilder().setLinkId(new LinkId("link:2")).build()) //
104 .add(new LinkBuilder().setLinkId(new LinkId("link:3")).build()) //
112 public void testContainerRead() {
113 Optional<Source> source = DataObjectReadingUtil.readData(linkModel, Source.class);
114 assertNotNull(source);
115 assertEquals(linkModel.getSource(), source.get());
119 public void testInstanceIdentifierRead() {
120 Map<InstanceIdentifier<Source>, Source> source = DataObjectReadingUtil.readData(networkModel,
121 NETWORK_TOPOLOGY_PATH, ABSOLUTE_SOURCE_PATH);
122 assertNotNull(source);
123 Source potentialSource = source.get(ABSOLUTE_SOURCE_PATH);
124 assertEquals(linkModel.getSource(), potentialSource);
128 public void testInstanceIdentifierReadWildcarded() {
129 Topology topology = DataObjectReadingUtil.readData(networkModel, NETWORK_TOPOLOGY_PATH, TOPOLOGY_BAR_PATH).get(
131 Map<InstanceIdentifier<Source>, Source> source = DataObjectReadingUtil.readData(topology, TOPOLOGY_BAR_PATH,
132 WILDCARDED_SOURCE_PATH);
133 assertNotNull(source);
134 Source potentialSource = source.get(ABSOLUTE_SOURCE_PATH);
135 assertEquals(linkModel.getSource(), potentialSource);
139 public void testInstanceIdentifierReadNonExistingValue() {
140 InstanceIdentifier<Source> sourcePath = NETWORK_TOPOLOGY_PATH.builder() //
141 .child(Topology.class, new TopologyKey(TOPOLOGY_BAZ_ID)) //
142 .child(Link.class, new LinkKey(LINK_BAR_ID)) //
143 .child(Source.class) //
145 Map<InstanceIdentifier<Source>, Source> source = DataObjectReadingUtil.readData(networkModel,
146 NETWORK_TOPOLOGY_PATH, sourcePath);
147 assertNotNull(source);
148 assertTrue(source.isEmpty());
152 public void testWildcardedListRead() {
153 Topology topology = DataObjectReadingUtil.readData(networkModel, NETWORK_TOPOLOGY_PATH, TOPOLOGY_BAR_PATH).get(TOPOLOGY_BAR_PATH);
155 Map<InstanceIdentifier<Link>, Link> potentialLinks = DataObjectReadingUtil.readData(topology, TOPOLOGY_BAR_PATH, WILDCARDED_LINK_PATH);
156 assertFalse(potentialLinks.isEmpty());
157 assertEquals(1, potentialLinks.size());
158 assertEquals(linkModel, potentialLinks.get(LINK_BAR_PATH));
162 public void testTwoWildcardsListRead() {
164 Map<InstanceIdentifier<Link>, Link> potentialLinks = DataObjectReadingUtil.readData(networkModel, NETWORK_TOPOLOGY_PATH, WILDCARDED_LINK_PATH);
165 assertFalse(potentialLinks.isEmpty());
166 assertEquals(3, potentialLinks.size());
167 assertEquals(linkModel, potentialLinks.get(LINK_BAR_PATH));
168 HashSet<Link> allLinks = new HashSet<>(potentialLinks.values());
169 assertEquals(3, allLinks.size());
170 for(InstanceIdentifier<Link> key : potentialLinks.keySet()) {
171 assertFalse("Returned instance identifier must not be wildcarded.", key.isWildcarded());