Fixed some major sonar issues in yang-validation-tool
[yangtools.git] / integration-test / yang-runtime-tests / src / test / java / org / opendaylight / yangtools / it / yang / runtime / tests / BindingReadingTest.java
1 package org.opendaylight.yangtools.it.yang.runtime.tests;
2
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;
7
8 import java.util.HashSet;
9 import java.util.Map;
10
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;
29
30 import com.google.common.base.Optional;
31 import com.google.common.collect.ImmutableList;
32
33 public class BindingReadingTest {
34
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;
45
46     private static final InstanceIdentifier<Topology> TOPOLOGY_BAR_PATH = NETWORK_TOPOLOGY_PATH //
47             .child(Topology.class, new TopologyKey(TOPOLOGY_BAR_ID));
48
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)) //
52             .build();
53
54     private static final InstanceIdentifier<Link> WILDCARDED_LINK_PATH = NETWORK_TOPOLOGY_PATH.builder() //
55             .child(Topology.class) //
56             .child(Link.class) //
57             .build();
58
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) //
62             .build();
63
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) //
68             .build();
69
70     /**
71      *
72      * Creates network topology model with three topologies:
73      * foo,bar and baz.
74      * Where bar has 1 link, and baz has 2 links.
75      *
76      */
77     @Before
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) //
84                         .build()) //
85                 .build();
86         networkModel = new NetworkTopologyBuilder().setTopology(ImmutableList.<Topology> builder() //
87                 .add(new TopologyBuilder() //
88                         .setTopologyId(TOPOLOGY_FOO_ID) //
89                         .setServerProvided(true) //
90                         .build()) //
91                 .add(new TopologyBuilder() //
92                         .setTopologyId(TOPOLOGY_BAR_ID) //
93                         .setServerProvided(false) //
94                         .setLink(ImmutableList.<Link> builder() //
95                                 .add(linkModel) //
96                                 .build()) //
97                         .build()) //
98                 .add(new TopologyBuilder() //
99                         .build())//
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()) //
105                                 .build()) //
106                         .build()) //
107                 .build()) //
108                 .build(); //
109     }
110
111     @Test
112     public void testContainerRead() {
113         Optional<Source> source = DataObjectReadingUtil.readData(linkModel, Source.class);
114         assertNotNull(source);
115         assertEquals(linkModel.getSource(), source.get());
116     }
117
118     @Test
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);
125     }
126
127     @Test
128     public void testInstanceIdentifierReadWildcarded() {
129         Topology topology = DataObjectReadingUtil.readData(networkModel, NETWORK_TOPOLOGY_PATH, TOPOLOGY_BAR_PATH).get(
130                 TOPOLOGY_BAR_PATH);
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);
136     }
137
138     @Test
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) //
144                 .build();
145         Map<InstanceIdentifier<Source>, Source> source = DataObjectReadingUtil.readData(networkModel,
146                 NETWORK_TOPOLOGY_PATH, sourcePath);
147         assertNotNull(source);
148         assertTrue(source.isEmpty());
149     }
150
151     @Test
152     public void testWildcardedListRead() {
153         Topology topology = DataObjectReadingUtil.readData(networkModel, NETWORK_TOPOLOGY_PATH, TOPOLOGY_BAR_PATH).get(TOPOLOGY_BAR_PATH);
154
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));
159     }
160
161     @Test
162     public void testTwoWildcardsListRead() {
163
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());
172         }
173
174     }
175
176 }