2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.bgpcep.pcep.tunnel.provider;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.opendaylight.protocol.util.CheckUtil.readDataOperational;
15 import com.google.common.collect.Lists;
16 import java.util.Collections;
17 import org.junit.After;
18 import org.junit.Assert;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
22 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
23 import org.opendaylight.controller.md.sal.binding.test.AbstractConcurrentDataBrokerTest;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
26 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
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.rev171025.Path1;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.Path1Builder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.lsp.identifiers.tlv.LspIdentifiersBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.lsp.identifiers.tlv.lsp.identifiers.address.family.Ipv4CaseBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.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.rev171025.lsp.object.LspBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.lsp.object.lsp.TlvsBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.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.rev171025.Node1;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.Node1Builder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.PccSyncState;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.pcep.client.attributes.PathComputationClientBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.pcep.client.attributes.path.computation.client.ReportedLsp;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.pcep.client.attributes.path.computation.client.ReportedLspBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.pcep.client.attributes.path.computation.client.ReportedLspKey;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.pcep.client.attributes.path.computation.client.reported.lsp.PathBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.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;
65 public class NodeChangedListenerTest extends AbstractConcurrentDataBrokerTest {
67 private static final TopologyId PCEP_TOPOLOGY_ID = new TopologyId("pcep-topology");
68 private static final TopologyId TUNNEL_TOPOLOGY_ID = new TopologyId("tunnel-topology");
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;
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;
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();
85 private ListenerRegistration<NodeChangedListener> listenerRegistration;
88 public void setUp() throws TransactionCommitFailedException {
89 final WriteTransaction wTx = getDataBroker().newWriteOnlyTransaction();
90 wTx.put(LogicalDatastoreType.OPERATIONAL, PCEP_TOPO_IID, new TopologyBuilder()
91 .setKey(new TopologyKey(PCEP_TOPOLOGY_ID)).setNode(Lists.newArrayList())
92 .setTopologyId(PCEP_TOPOLOGY_ID).build(), true);
93 wTx.put(LogicalDatastoreType.OPERATIONAL, TUNNEL_TOPO_IID, new TopologyBuilder()
94 .setKey(new TopologyKey(TUNNEL_TOPOLOGY_ID)).setTopologyId(TUNNEL_TOPOLOGY_ID).build(), true);
95 wTx.submit().checkedGet();
96 final NodeChangedListener nodeListener = new NodeChangedListener(getDataBroker(),
97 PCEP_TOPOLOGY_ID, TUNNEL_TOPO_IID);
98 this.listenerRegistration = getDataBroker().registerDataTreeChangeListener(new DataTreeIdentifier<>(
99 LogicalDatastoreType.OPERATIONAL, PCEP_TOPO_IID.child(Node.class)), nodeListener);
103 public void testNodeChangedListener() throws ReadFailedException, TransactionCommitFailedException {
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());
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)));
118 if (tunnelTopo.getNode().get(0).getNodeId().equals(srcId)) {
119 src = tunnelTopo.getNode().get(0);
120 dst = tunnelTopo.getNode().get(1);
122 src = tunnelTopo.getNode().get(1);
123 dst = tunnelTopo.getNode().get(0);
126 Assert.assertEquals(srcId, src.getNodeId());
127 Assert.assertEquals(dstId, dst.getNodeId());
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());
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.getKey().getNodeRef());
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());
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);
159 updatedNode = updatedNodeTopo.getNode().get(0);
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;
171 readDataOperational(getDataBroker(), TUNNEL_TOPO_IID, updatedNodeTopo -> {
173 if (updatedNodeTopo.getLink().get(0).getSource().getSourceNode().equals(srcId)) {
174 link2 = updatedNodeTopo.getLink().get(1);
176 link2 = updatedNodeTopo.getLink().get(0);
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;
185 // remove nodes -> remove link
186 removeNode(NODE1_ID);
187 removeNode(NODE2_ID);
188 readDataOperational(getDataBroker(), TUNNEL_TOPO_IID, removedNodeTopo -> {
189 assertEquals(0, removedNodeTopo.getNode().size());
190 assertEquals(0, removedNodeTopo.getLink().size());
191 return removedNodeTopo;
196 public void tearDown() {
197 this.listenerRegistration.close();
200 private void createNode(final NodeId nodeId, final String ipv4Address, final String lspName, final long lspId,
201 final String dstIpv4Address) throws TransactionCommitFailedException {
202 final NodeBuilder nodeBuilder = new NodeBuilder();
203 nodeBuilder.setKey(new NodeKey(nodeId));
204 nodeBuilder.setNodeId(nodeId);
205 final PathBuilder pathBuilder = new PathBuilder();
206 pathBuilder.setKey(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 Ipv4Address(ipv4Address)).setIpv4ExtendedTunnelId(
212 new Ipv4ExtendedTunnelId(ipv4Address)).setIpv4TunnelEndpointAddress(new Ipv4Address(dstIpv4Address))
213 .build()).build()).build()).build()).setAdministrative(true).setDelegate(true).build()).build());
214 final ReportedLsp reportedLps = new ReportedLspBuilder().setKey(new ReportedLspKey(lspName)).setPath(
215 Collections.singletonList(pathBuilder.build())).build();
216 final Node1Builder node1Builder = new Node1Builder();
217 node1Builder.setPathComputationClient(new PathComputationClientBuilder().setStateSync(PccSyncState.Synchronized)
218 .setReportedLsp(Lists.newArrayList(reportedLps)).setIpAddress(new IpAddress(new Ipv4Address(ipv4Address)))
220 nodeBuilder.addAugmentation(Node1.class, node1Builder.build());
221 final WriteTransaction wTx = getDataBroker().newWriteOnlyTransaction();
222 wTx.put(LogicalDatastoreType.OPERATIONAL, PCEP_TOPO_IID.builder().child(Node.class,
223 new NodeKey(nodeId)).build(), nodeBuilder.build());
224 wTx.submit().checkedGet();
227 private void removeNode(final NodeId nodeId) throws TransactionCommitFailedException {
228 final WriteTransaction wTx = getDataBroker().newWriteOnlyTransaction();
229 wTx.delete(LogicalDatastoreType.OPERATIONAL, PCEP_TOPO_IID.builder().child(Node.class, new NodeKey(nodeId)).build());
230 wTx.submit().checkedGet();