b3ffb93fb2488ea8cd94b66080ec4248c21498c1
[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
65 public class NodeChangedListenerTest extends AbstractConcurrentDataBrokerTest {
66
67     private static final TopologyId PCEP_TOPOLOGY_ID = new TopologyId("pcep-topology");
68     private static final TopologyId TUNNEL_TOPOLOGY_ID = new TopologyId("tunnel-topology");
69
70     private static final String NODE1_IPV4 = "39.39.39.39";
71     private static final NodeId NODE1_ID = new NodeId("pcc://" + NODE1_IPV4);
72     private static final String LSP1_NAME = "lsp1";
73     private static final long LSP1_ID = 1;
74
75     private static final String NODE2_IPV4 = "40.40.40.40";
76     private static final NodeId NODE2_ID = new NodeId("pcc://" + NODE2_IPV4);
77     private static final String LSP2_NAME = "lsp2";
78     private static final long LSP2_ID = 2;
79
80     private static final InstanceIdentifier<Topology> PCEP_TOPO_IID = InstanceIdentifier.builder(NetworkTopology.class)
81             .child(Topology.class, new TopologyKey(PCEP_TOPOLOGY_ID)).build();
82     private static final InstanceIdentifier<Topology> TUNNEL_TOPO_IID = InstanceIdentifier
83             .builder(NetworkTopology.class).child(Topology.class, new TopologyKey(TUNNEL_TOPOLOGY_ID)).build();
84
85     private ListenerRegistration<NodeChangedListener> listenerRegistration;
86
87     @Before
88     public void setUp() throws InterruptedException, ExecutionException {
89         final WriteTransaction wTx = getDataBroker().newWriteOnlyTransaction();
90         wTx.mergeParentStructurePut(LogicalDatastoreType.OPERATIONAL, PCEP_TOPO_IID, new TopologyBuilder()
91                 .withKey(new TopologyKey(PCEP_TOPOLOGY_ID)).setNode(Collections.emptyList())
92                 .setTopologyId(PCEP_TOPOLOGY_ID).build());
93         wTx.mergeParentStructurePut(LogicalDatastoreType.OPERATIONAL, TUNNEL_TOPO_IID, new TopologyBuilder()
94                 .withKey(new TopologyKey(TUNNEL_TOPOLOGY_ID)).setTopologyId(TUNNEL_TOPOLOGY_ID).build());
95         wTx.commit().get();
96         final NodeChangedListener nodeListener = new NodeChangedListener(getDataBroker(),
97                 PCEP_TOPOLOGY_ID, TUNNEL_TOPO_IID);
98         this.listenerRegistration = getDataBroker().registerDataTreeChangeListener(DataTreeIdentifier.create(
99                 LogicalDatastoreType.OPERATIONAL, PCEP_TOPO_IID.child(Node.class)), nodeListener);
100     }
101
102     @Test
103     public void testNodeChangedListener() throws InterruptedException, ExecutionException {
104         // add node -> create two nodes with TPs and link
105         createNode(NODE1_ID, NODE1_IPV4, LSP1_NAME, LSP1_ID, NODE2_IPV4);
106         final Topology tunnelTopo = readDataOperational(getDataBroker(), TUNNEL_TOPO_IID, tunnelTopo1 -> {
107             assertNotNull(tunnelTopo1.getNode());
108             assertEquals(2, tunnelTopo1.getNode().size());
109             return tunnelTopo1;
110         });
111
112         final NodeId srcId = new NodeId("ip://" + new IpAddress(new Ipv4Address(NODE1_IPV4)));
113         final NodeId dstId = new NodeId("ip://" + new IpAddress(new Ipv4Address(NODE2_IPV4)));
114
115         final Node dst;
116         final Node src;
117
118         if (tunnelTopo.getNode().get(0).getNodeId().equals(srcId)) {
119             src = tunnelTopo.getNode().get(0);
120             dst = tunnelTopo.getNode().get(1);
121         } else {
122             src = tunnelTopo.getNode().get(1);
123             dst = tunnelTopo.getNode().get(0);
124         }
125
126         Assert.assertEquals(srcId, src.getNodeId());
127         Assert.assertEquals(dstId, dst.getNodeId());
128
129         Assert.assertEquals(1, dst.getTerminationPoint().size());
130         Assert.assertEquals(1, src.getTerminationPoint().size());
131         final TerminationPoint dstTp = dst.getTerminationPoint().get(0);
132         final TerminationPoint srcTp = src.getTerminationPoint().get(0);
133         final TpId dstNodeTpId = new TpId(dstId.getValue());
134         final TpId srcNodeTpId = new TpId(srcId.getValue());
135         Assert.assertEquals(dstNodeTpId, dstTp.getTpId());
136         Assert.assertEquals(srcNodeTpId, srcTp.getTpId());
137
138         Assert.assertEquals(1, src.getSupportingNode().size());
139         Assert.assertNull(dst.getSupportingNode());
140         final SupportingNode sNode = src.getSupportingNode().get(0);
141         Assert.assertEquals(NODE1_ID, sNode.key().getNodeRef());
142
143         Assert.assertEquals(1, tunnelTopo.getLink().size());
144         final Link link = tunnelTopo.getLink().get(0);
145         Assert.assertEquals(srcId, link.getSource().getSourceNode());
146         Assert.assertEquals(srcNodeTpId, link.getSource().getSourceTp());
147         Assert.assertEquals(dstId, link.getDestination().getDestNode());
148         Assert.assertEquals(dstNodeTpId, link.getDestination().getDestTp());
149
150         // update second node -> adds supporting node and second link
151         createNode(NODE2_ID, NODE2_IPV4, LSP2_NAME, LSP2_ID, NODE1_IPV4);
152         readDataOperational(getDataBroker(), TUNNEL_TOPO_IID, updatedNodeTopo -> {
153             assertNotNull(updatedNodeTopo.getNode());
154             Assert.assertEquals(2, updatedNodeTopo.getNode().size());
155             final Node updatedNode;
156             if (updatedNodeTopo.getNode().get(0).getNodeId().equals(srcId)) {
157                 updatedNode = updatedNodeTopo.getNode().get(1);
158             } else {
159                 updatedNode = updatedNodeTopo.getNode().get(0);
160             }
161
162             assertNotNull(updatedNode.getSupportingNode());
163             Assert.assertEquals(1, updatedNode.getSupportingNode().size());
164             final SupportingNode sNode2 = updatedNode.getSupportingNode().get(0);
165             Assert.assertEquals(NODE2_ID, sNode2.getNodeRef());
166             Assert.assertEquals(2, updatedNodeTopo.getLink().size());
167             return updatedNodeTopo;
168
169         });
170
171         readDataOperational(getDataBroker(), TUNNEL_TOPO_IID, updatedNodeTopo -> {
172             final Link link2;
173             if (updatedNodeTopo.getLink().get(0).getSource().getSourceNode().equals(srcId)) {
174                 link2 = updatedNodeTopo.getLink().get(1);
175             } else {
176                 link2 = updatedNodeTopo.getLink().get(0);
177             }
178             assertEquals(dstId, link2.getSource().getSourceNode());
179             assertEquals(dstNodeTpId, link2.getSource().getSourceTp());
180             assertEquals(srcId, link2.getDestination().getDestNode());
181             assertEquals(srcNodeTpId, link2.getDestination().getDestTp());
182             return updatedNodeTopo;
183         });
184
185         // remove nodes -> remove link
186         removeNode(NODE1_ID);
187         removeNode(NODE2_ID);
188         readDataOperational(getDataBroker(), TUNNEL_TOPO_IID, removedNodeTopo -> {
189             assertNull(removedNodeTopo.getNode());
190             assertNull(removedNodeTopo.getLink());
191             return removedNodeTopo;
192         });
193     }
194
195     @After
196     public void tearDown() {
197         this.listenerRegistration.close();
198     }
199
200     private void createNode(final NodeId nodeId, final String ipv4Address, final String lspName, final long lspId,
201             final String dstIpv4Address) throws InterruptedException, ExecutionException {
202         final NodeBuilder nodeBuilder = new NodeBuilder();
203         nodeBuilder.withKey(new NodeKey(nodeId));
204         nodeBuilder.setNodeId(nodeId);
205         final PathBuilder pathBuilder = new PathBuilder();
206         pathBuilder.withKey(new PathKey(new LspId(lspId)));
207         pathBuilder.setBandwidth(new BandwidthBuilder().setBandwidth(
208                 new Bandwidth(new byte[]{0x00, 0x00, (byte) 0xff, (byte) 0xff})).build());
209         pathBuilder.addAugmentation(Path1.class, new Path1Builder().setLsp(new LspBuilder().setTlvs(new TlvsBuilder()
210                 .setLspIdentifiers(new LspIdentifiersBuilder().setAddressFamily(new Ipv4CaseBuilder().setIpv4(
211                         new Ipv4Builder().setIpv4TunnelSenderAddress(new Ipv4AddressNoZone(ipv4Address))
212                                 .setIpv4ExtendedTunnelId(new Ipv4ExtendedTunnelId(ipv4Address))
213                                 .setIpv4TunnelEndpointAddress(new Ipv4AddressNoZone(dstIpv4Address))
214                                 .build()).build()).build()).build()).setAdministrative(true)
215                 .setDelegate(true).build()).build());
216         final ReportedLsp reportedLps = new ReportedLspBuilder().withKey(new ReportedLspKey(lspName)).setPath(
217                 Collections.singletonList(pathBuilder.build())).build();
218         final Node1Builder node1Builder = new Node1Builder();
219         node1Builder.setPathComputationClient(new PathComputationClientBuilder()
220                 .setStateSync(PccSyncState.Synchronized)
221                 .setReportedLsp(Lists.newArrayList(reportedLps))
222                 .setIpAddress(new IpAddress(new Ipv4Address(ipv4Address)))
223                 .build());
224         nodeBuilder.addAugmentation(Node1.class, node1Builder.build());
225         final WriteTransaction wTx = getDataBroker().newWriteOnlyTransaction();
226         wTx.put(LogicalDatastoreType.OPERATIONAL, PCEP_TOPO_IID.builder().child(Node.class,
227                 new NodeKey(nodeId)).build(), nodeBuilder.build());
228         wTx.commit().get();
229     }
230
231     private void removeNode(final NodeId nodeId) throws InterruptedException, ExecutionException {
232         final WriteTransaction wTx = getDataBroker().newWriteOnlyTransaction();
233         wTx.delete(LogicalDatastoreType.OPERATIONAL, PCEP_TOPO_IID.builder()
234                 .child(Node.class, new NodeKey(nodeId)).build());
235         wTx.commit().get();
236     }
237 }