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