Use new Uint* constants
[bgpcep.git] / pcep / tunnel / tunnel-provider / src / test / java / org / opendaylight / bgpcep / pcep / tunnel / provider / NodeChangedListenerTest.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.bgpcep.pcep.tunnel.provider;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.opendaylight.protocol.util.CheckTestUtil.readDataOperational;
14
15 import com.google.common.collect.Lists;
16 import java.util.Collections;
17 import java.util.concurrent.ExecutionException;
18 import org.junit.After;
19 import org.junit.Assert;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
23 import org.opendaylight.mdsal.binding.api.WriteTransaction;
24 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractConcurrentDataBrokerTest;
25 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.Path1;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.Path1Builder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.lsp.identifiers.tlv.LspIdentifiersBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.lsp.identifiers.tlv.lsp.identifiers.address.family.Ipv4CaseBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.lsp.identifiers.tlv.lsp.identifiers.address.family.ipv4._case.Ipv4Builder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.lsp.object.LspBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev181109.lsp.object.lsp.TlvsBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.bandwidth.object.BandwidthBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.Ipv4ExtendedTunnelId;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.LspId;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev181109.Node1;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev181109.Node1Builder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev181109.PccSyncState;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev181109.pcep.client.attributes.PathComputationClientBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev181109.pcep.client.attributes.path.computation.client.ReportedLsp;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev181109.pcep.client.attributes.path.computation.client.ReportedLspBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev181109.pcep.client.attributes.path.computation.client.ReportedLspKey;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev181109.pcep.client.attributes.path.computation.client.reported.lsp.PathBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev181109.pcep.client.attributes.path.computation.client.reported.lsp.PathKey;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
50 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
51 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
52 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
53 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
54 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
55 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
56 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
57 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
58 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
59 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
60 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
61 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.node.attributes.SupportingNode;
62 import org.opendaylight.yangtools.concepts.ListenerRegistration;
63 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
64 import org.opendaylight.yangtools.yang.common.Uint32;
65
66 public class NodeChangedListenerTest extends AbstractConcurrentDataBrokerTest {
67
68     private static final TopologyId PCEP_TOPOLOGY_ID = new TopologyId("pcep-topology");
69     private static final TopologyId TUNNEL_TOPOLOGY_ID = new TopologyId("tunnel-topology");
70
71     private static final String NODE1_IPV4 = "39.39.39.39";
72     private static final NodeId NODE1_ID = new NodeId("pcc://" + NODE1_IPV4);
73     private static final String LSP1_NAME = "lsp1";
74     private static final Uint32 LSP1_ID = Uint32.ONE;
75
76     private static final String NODE2_IPV4 = "40.40.40.40";
77     private static final NodeId NODE2_ID = new NodeId("pcc://" + NODE2_IPV4);
78     private static final String LSP2_NAME = "lsp2";
79     private static final Uint32 LSP2_ID = Uint32.TWO;
80
81     private static final InstanceIdentifier<Topology> PCEP_TOPO_IID = InstanceIdentifier.builder(NetworkTopology.class)
82             .child(Topology.class, new TopologyKey(PCEP_TOPOLOGY_ID)).build();
83     private static final InstanceIdentifier<Topology> TUNNEL_TOPO_IID = InstanceIdentifier
84             .builder(NetworkTopology.class).child(Topology.class, new TopologyKey(TUNNEL_TOPOLOGY_ID)).build();
85
86     private ListenerRegistration<NodeChangedListener> listenerRegistration;
87
88     @Before
89     public void setUp() throws InterruptedException, ExecutionException {
90         final WriteTransaction wTx = getDataBroker().newWriteOnlyTransaction();
91         wTx.mergeParentStructurePut(LogicalDatastoreType.OPERATIONAL, PCEP_TOPO_IID, new TopologyBuilder()
92                 .withKey(new TopologyKey(PCEP_TOPOLOGY_ID)).setNode(Collections.emptyList())
93                 .setTopologyId(PCEP_TOPOLOGY_ID).build());
94         wTx.mergeParentStructurePut(LogicalDatastoreType.OPERATIONAL, TUNNEL_TOPO_IID, new TopologyBuilder()
95                 .withKey(new TopologyKey(TUNNEL_TOPOLOGY_ID)).setTopologyId(TUNNEL_TOPOLOGY_ID).build());
96         wTx.commit().get();
97         final NodeChangedListener nodeListener = new NodeChangedListener(getDataBroker(),
98                 PCEP_TOPOLOGY_ID, TUNNEL_TOPO_IID);
99         this.listenerRegistration = getDataBroker().registerDataTreeChangeListener(DataTreeIdentifier.create(
100                 LogicalDatastoreType.OPERATIONAL, PCEP_TOPO_IID.child(Node.class)), nodeListener);
101     }
102
103     @Test
104     public void testNodeChangedListener() throws InterruptedException, ExecutionException {
105         // add node -> create two nodes with TPs and link
106         createNode(NODE1_ID, NODE1_IPV4, LSP1_NAME, LSP1_ID, NODE2_IPV4);
107         final Topology tunnelTopo = readDataOperational(getDataBroker(), TUNNEL_TOPO_IID, tunnelTopo1 -> {
108             assertNotNull(tunnelTopo1.getNode());
109             assertEquals(2, tunnelTopo1.getNode().size());
110             return tunnelTopo1;
111         });
112
113         final NodeId srcId = new NodeId("ip://" + new IpAddress(new Ipv4Address(NODE1_IPV4)));
114         final NodeId dstId = new NodeId("ip://" + new IpAddress(new Ipv4Address(NODE2_IPV4)));
115
116         final Node dst;
117         final Node src;
118
119         if (tunnelTopo.getNode().get(0).getNodeId().equals(srcId)) {
120             src = tunnelTopo.getNode().get(0);
121             dst = tunnelTopo.getNode().get(1);
122         } else {
123             src = tunnelTopo.getNode().get(1);
124             dst = tunnelTopo.getNode().get(0);
125         }
126
127         Assert.assertEquals(srcId, src.getNodeId());
128         Assert.assertEquals(dstId, dst.getNodeId());
129
130         Assert.assertEquals(1, dst.getTerminationPoint().size());
131         Assert.assertEquals(1, src.getTerminationPoint().size());
132         final TerminationPoint dstTp = dst.getTerminationPoint().get(0);
133         final TerminationPoint srcTp = src.getTerminationPoint().get(0);
134         final TpId dstNodeTpId = new TpId(dstId.getValue());
135         final TpId srcNodeTpId = new TpId(srcId.getValue());
136         Assert.assertEquals(dstNodeTpId, dstTp.getTpId());
137         Assert.assertEquals(srcNodeTpId, srcTp.getTpId());
138
139         Assert.assertEquals(1, src.getSupportingNode().size());
140         Assert.assertNull(dst.getSupportingNode());
141         final SupportingNode sNode = src.getSupportingNode().get(0);
142         Assert.assertEquals(NODE1_ID, sNode.key().getNodeRef());
143
144         Assert.assertEquals(1, tunnelTopo.getLink().size());
145         final Link link = tunnelTopo.getLink().get(0);
146         Assert.assertEquals(srcId, link.getSource().getSourceNode());
147         Assert.assertEquals(srcNodeTpId, link.getSource().getSourceTp());
148         Assert.assertEquals(dstId, link.getDestination().getDestNode());
149         Assert.assertEquals(dstNodeTpId, link.getDestination().getDestTp());
150
151         // update second node -> adds supporting node and second link
152         createNode(NODE2_ID, NODE2_IPV4, LSP2_NAME, LSP2_ID, NODE1_IPV4);
153         readDataOperational(getDataBroker(), TUNNEL_TOPO_IID, updatedNodeTopo -> {
154             assertNotNull(updatedNodeTopo.getNode());
155             Assert.assertEquals(2, updatedNodeTopo.getNode().size());
156             final Node updatedNode;
157             if (updatedNodeTopo.getNode().get(0).getNodeId().equals(srcId)) {
158                 updatedNode = updatedNodeTopo.getNode().get(1);
159             } else {
160                 updatedNode = updatedNodeTopo.getNode().get(0);
161             }
162
163             assertNotNull(updatedNode.getSupportingNode());
164             Assert.assertEquals(1, updatedNode.getSupportingNode().size());
165             final SupportingNode sNode2 = updatedNode.getSupportingNode().get(0);
166             Assert.assertEquals(NODE2_ID, sNode2.getNodeRef());
167             Assert.assertEquals(2, updatedNodeTopo.getLink().size());
168             return updatedNodeTopo;
169
170         });
171
172         readDataOperational(getDataBroker(), TUNNEL_TOPO_IID, updatedNodeTopo -> {
173             final Link link2;
174             if (updatedNodeTopo.getLink().get(0).getSource().getSourceNode().equals(srcId)) {
175                 link2 = updatedNodeTopo.getLink().get(1);
176             } else {
177                 link2 = updatedNodeTopo.getLink().get(0);
178             }
179             assertEquals(dstId, link2.getSource().getSourceNode());
180             assertEquals(dstNodeTpId, link2.getSource().getSourceTp());
181             assertEquals(srcId, link2.getDestination().getDestNode());
182             assertEquals(srcNodeTpId, link2.getDestination().getDestTp());
183             return updatedNodeTopo;
184         });
185
186         // remove nodes -> remove link
187         removeNode(NODE1_ID);
188         removeNode(NODE2_ID);
189         readDataOperational(getDataBroker(), TUNNEL_TOPO_IID, removedNodeTopo -> {
190             assertNull(removedNodeTopo.getNode());
191             assertNull(removedNodeTopo.getLink());
192             return removedNodeTopo;
193         });
194     }
195
196     @After
197     public void tearDown() {
198         this.listenerRegistration.close();
199     }
200
201     private void createNode(final NodeId nodeId, final String ipv4Address, final String lspName, final Uint32 lspId,
202             final String dstIpv4Address) throws InterruptedException, ExecutionException {
203         final NodeBuilder nodeBuilder = new NodeBuilder();
204         nodeBuilder.withKey(new NodeKey(nodeId));
205         nodeBuilder.setNodeId(nodeId);
206         final PathBuilder pathBuilder = new PathBuilder();
207         pathBuilder.withKey(new PathKey(new LspId(lspId)));
208         pathBuilder.setBandwidth(new BandwidthBuilder().setBandwidth(
209                 new Bandwidth(new byte[]{0x00, 0x00, (byte) 0xff, (byte) 0xff})).build());
210         pathBuilder.addAugmentation(Path1.class, new Path1Builder().setLsp(new LspBuilder().setTlvs(new TlvsBuilder()
211                 .setLspIdentifiers(new LspIdentifiersBuilder().setAddressFamily(new Ipv4CaseBuilder().setIpv4(
212                         new Ipv4Builder().setIpv4TunnelSenderAddress(new Ipv4AddressNoZone(ipv4Address))
213                                 .setIpv4ExtendedTunnelId(new Ipv4ExtendedTunnelId(ipv4Address))
214                                 .setIpv4TunnelEndpointAddress(new Ipv4AddressNoZone(dstIpv4Address))
215                                 .build()).build()).build()).build()).setAdministrative(true)
216                 .setDelegate(true).build()).build());
217         final ReportedLsp reportedLps = new ReportedLspBuilder().withKey(new ReportedLspKey(lspName)).setPath(
218                 Collections.singletonList(pathBuilder.build())).build();
219         final Node1Builder node1Builder = new Node1Builder();
220         node1Builder.setPathComputationClient(new PathComputationClientBuilder()
221                 .setStateSync(PccSyncState.Synchronized)
222                 .setReportedLsp(Lists.newArrayList(reportedLps))
223                 .setIpAddress(new IpAddress(new Ipv4Address(ipv4Address)))
224                 .build());
225         nodeBuilder.addAugmentation(Node1.class, node1Builder.build());
226         final WriteTransaction wTx = getDataBroker().newWriteOnlyTransaction();
227         wTx.put(LogicalDatastoreType.OPERATIONAL, PCEP_TOPO_IID.builder().child(Node.class,
228                 new NodeKey(nodeId)).build(), nodeBuilder.build());
229         wTx.commit().get();
230     }
231
232     private void removeNode(final NodeId nodeId) throws InterruptedException, ExecutionException {
233         final WriteTransaction wTx = getDataBroker().newWriteOnlyTransaction();
234         wTx.delete(LogicalDatastoreType.OPERATIONAL, PCEP_TOPO_IID.builder()
235                 .child(Node.class, new NodeKey(nodeId)).build());
236         wTx.commit().get();
237     }
238 }