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.topology.provider;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.assertTrue;
16 import static org.opendaylight.protocol.pcep.pcc.mock.MsgBuilderUtil.createLspTlvs;
18 import com.google.common.base.Optional;
19 import com.google.common.collect.Lists;
20 import java.net.InetAddress;
21 import java.net.UnknownHostException;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.concurrent.ExecutionException;
25 import java.util.concurrent.Future;
26 import org.junit.Assert;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.opendaylight.protocol.pcep.PCEPCloseTermination;
30 import org.opendaylight.protocol.pcep.TerminationReason;
31 import org.opendaylight.protocol.pcep.pcc.mock.MsgBuilderUtil;
32 import org.opendaylight.protocol.pcep.spi.AbstractMessageParser;
33 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.topology.rev140113.NetworkTopologyRef;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev131126.Pcinitiate;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev131126.pcinitiate.message.pcinitiate.message.Requests;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Arguments1;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Arguments1Builder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Arguments2;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Arguments2Builder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Arguments3;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Arguments3Builder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.OperationalStatus;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Pcrpt;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.PcrptBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Pcupd;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.PlspId;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.SymbolicPathName;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Tlvs1;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Tlvs1Builder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.identifiers.tlv.LspIdentifiersBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.LspBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.lsp.Tlvs;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.pcrpt.message.PcrptMessageBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.pcrpt.message.pcrpt.message.Reports;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.pcrpt.message.pcrpt.message.ReportsBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.pcrpt.message.pcrpt.message.reports.PathBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.pcupd.message.pcupd.message.Updates;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.stateful.capability.tlv.StatefulBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.symbolic.path.name.tlv.SymbolicPathNameBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv4CaseBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.ipv4._case.Ipv4Builder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.object.EndpointsObjBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.EroBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.ero.Subobject;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.Open;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.TlvsBuilder;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.ErrorObject;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rp.object.Rp;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.LspId;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.AddLspInput;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.AddLspInputBuilder;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.AddLspOutput;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.EnsureLspOperationalInput;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.EnsureLspOperationalInputBuilder;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.FailureType;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.Node1;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.OperationResult;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.RemoveLspInput;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.RemoveLspInputBuilder;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.UpdateLspInput;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.UpdateLspInputBuilder;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.add.lsp.args.ArgumentsBuilder;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.PathComputationClient;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.path.computation.client.ReportedLsp;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.path.computation.client.reported.lsp.Path;
90 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
91 import org.opendaylight.yangtools.yang.common.RpcResult;
93 public class Stateful07TopologySessionListenerTest extends AbstractPCEPSessionTest<Stateful07TopologySessionListenerFactory> {
95 private Stateful07TopologySessionListener listener;
98 public void setUp() throws Exception {
100 this.listener = (Stateful07TopologySessionListener) this.manager.getSessionListener();
104 public void testStateful07TopologySessionListener() throws Exception {
105 this.listener.onSessionUp(this.session);
108 this.topologyRpcs.addLsp(createAddLspInput());
109 assertEquals(1, this.receivedMsgs.size());
110 assertTrue(this.receivedMsgs.get(0) instanceof Pcinitiate);
111 final Pcinitiate pcinitiate = (Pcinitiate) this.receivedMsgs.get(0);
112 final Requests req = pcinitiate.getPcinitiateMessage().getRequests().get(0);
113 final long srpId = req.getSrp().getOperationId().getValue();
114 final InetAddress inetAddress = InetAddress.getByName(TEST_ADDRESS);
115 final Tlvs tlvs = createLspTlvs(req.getLsp().getPlspId().getValue(), true,
116 inetAddress, inetAddress, inetAddress);
117 final Pcrpt pcRpt = MsgBuilderUtil.createPcRtpMessage(new LspBuilder(req.getLsp()).setTlvs(tlvs).setPlspId(new PlspId(1L)).setSync(false).setRemove(false).setOperational(OperationalStatus.Active).build(), Optional.of(MsgBuilderUtil.createSrp(srpId)), MsgBuilderUtil.createPath(req.getEro().getSubobject()));
118 final Pcrpt esm = MsgBuilderUtil.createPcRtpMessage(new LspBuilder().setSync(false).build(), Optional.of(MsgBuilderUtil.createSrp(0L)), null);
119 this.listener.onMessage(this.session, esm);
121 final Optional<Topology> topoOptional = getTopology();
122 assertTrue(topoOptional.isPresent());
123 Topology topology = topoOptional.get();
124 assertEquals(1, topology.getNode().size());
125 final Node1 node = topology.getNode().get(0).getAugmentation(Node1.class);
127 PathComputationClient pcc = node.getPathComputationClient();
128 assertEquals(TEST_ADDRESS, pcc.getIpAddress().getIpv4Address().getValue());
129 // reported lsp so far empty, has not received response (PcRpt) yet
130 assertTrue(pcc.getReportedLsp().isEmpty());
131 this.listener.onMessage(this.session, pcRpt);
133 topology = getTopology().get();
134 pcc = topology.getNode().get(0).getAugmentation(Node1.class).getPathComputationClient();
135 assertEquals(1, pcc.getReportedLsp().size());
136 ReportedLsp reportedLsp = pcc.getReportedLsp().get(0);
137 assertEquals(TEST_LSP_NAME, reportedLsp.getName());
138 assertEquals(1, reportedLsp.getPath().size());
139 Path path = reportedLsp.getPath().get(0);
140 assertEquals(1, path.getEro().getSubobject().size());
141 assertEquals(ERO_IP_PREFIX, getLastEroIpPrefix(path.getEro()));
144 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.update.lsp.args.ArgumentsBuilder updArgsBuilder = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.update.lsp.args.ArgumentsBuilder();
145 updArgsBuilder.setEro(createEroWithIpPrefixes(Lists.newArrayList(ERO_IP_PREFIX, DST_IP_PREFIX)));
146 updArgsBuilder.addAugmentation(Arguments3.class, new Arguments3Builder().setLsp(new LspBuilder().setDelegate(true).setAdministrative(true).build()).build());
147 final UpdateLspInput update = new UpdateLspInputBuilder().setArguments(updArgsBuilder.build()).setName(TEST_LSP_NAME).setNetworkTopologyRef(new NetworkTopologyRef(TOPO_IID)).setNode(NODE_ID).build();
148 this.topologyRpcs.updateLsp(update);
149 assertEquals(2, this.receivedMsgs.size());
150 assertTrue(this.receivedMsgs.get(1) instanceof Pcupd);
151 final Pcupd updateMsg = (Pcupd) this.receivedMsgs.get(1);
152 final Updates upd = updateMsg.getPcupdMessage().getUpdates().get(0);
153 final long srpId2 = upd.getSrp().getOperationId().getValue();
154 final Tlvs tlvs2 = createLspTlvs(upd.getLsp().getPlspId().getValue(), false,
155 InetAddress.getByName(NEW_DESTINATION_ADDRESS), inetAddress, inetAddress);
156 final Pcrpt pcRpt2 = MsgBuilderUtil.createPcRtpMessage(new LspBuilder(upd.getLsp()).setTlvs(tlvs2).setSync(true).setRemove(false).setOperational(OperationalStatus.Active).build(), Optional.of(MsgBuilderUtil.createSrp(srpId2)), MsgBuilderUtil.createPath(upd.getPath().getEro().getSubobject()));
157 this.listener.onMessage(this.session, pcRpt2);
159 topology = getTopology().get();
160 pcc = topology.getNode().get(0).getAugmentation(Node1.class).getPathComputationClient();
161 assertEquals(1, pcc.getReportedLsp().size());
162 reportedLsp = pcc.getReportedLsp().get(0);
163 assertEquals(TEST_LSP_NAME, reportedLsp.getName());
164 assertEquals(1, reportedLsp.getPath().size());
165 path = reportedLsp.getPath().get(0);
166 assertEquals(2, path.getEro().getSubobject().size());
167 assertEquals(DST_IP_PREFIX, getLastEroIpPrefix(path.getEro()));
169 // ensure-operational
170 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.ensure.lsp.operational.args.ArgumentsBuilder ensureArgs = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.ensure.lsp.operational.args.ArgumentsBuilder();
171 ensureArgs.addAugmentation(Arguments1.class, new Arguments1Builder().setOperational(OperationalStatus.Active).build());
172 final EnsureLspOperationalInput ensure = new EnsureLspOperationalInputBuilder().setArguments(ensureArgs.build()).setName(TEST_LSP_NAME).setNetworkTopologyRef(new NetworkTopologyRef(TOPO_IID)).setNode(NODE_ID).build();
173 final OperationResult result = this.topologyRpcs.ensureLspOperational(ensure).get().getResult();
175 assertNull(result.getFailure());
178 final RemoveLspInput remove = new RemoveLspInputBuilder().setName(TEST_LSP_NAME).setNetworkTopologyRef(new NetworkTopologyRef(TOPO_IID)).setNode(NODE_ID).build();
179 this.topologyRpcs.removeLsp(remove);
180 assertEquals(3, this.receivedMsgs.size());
181 assertTrue(this.receivedMsgs.get(2) instanceof Pcinitiate);
182 final Pcinitiate pcinitiate2 = (Pcinitiate) this.receivedMsgs.get(2);
183 final Requests req2 = pcinitiate2.getPcinitiateMessage().getRequests().get(0);
184 final long srpId3 = req2.getSrp().getOperationId().getValue();
185 final Tlvs tlvs3 = createLspTlvs(req2.getLsp().getPlspId().getValue(), false,
186 inetAddress, inetAddress, inetAddress);
187 final Pcrpt pcRpt3 = MsgBuilderUtil.createPcRtpMessage(new LspBuilder(req2.getLsp()).setTlvs(tlvs3).setRemove(true).setSync(true).setOperational(OperationalStatus.Down).build(), Optional.of(MsgBuilderUtil.createSrp(srpId3)), MsgBuilderUtil.createPath(Collections.<Subobject>emptyList()));
188 this.listener.onMessage(this.session, pcRpt3);
189 // check if lsp was removed
190 topology = getTopology().get();
191 pcc = topology.getNode().get(0).getAugmentation(Node1.class).getPathComputationClient();
192 assertEquals(0, pcc.getReportedLsp().size());
196 public void testOnUnhandledErrorMessage() {
197 final Message errorMsg = AbstractMessageParser.createErrorMsg(PCEPErrors.NON_ZERO_PLSPID, Optional.<Rp>absent());
198 this.listener.onSessionUp(this.session);
199 Assert.assertTrue(this.listener.onMessage(Optional.<AbstractTopologySessionListener.MessageContext>absent().orNull(), errorMsg));
203 public void testOnErrorMessage() throws InterruptedException, ExecutionException {
204 final Message errorMsg = MsgBuilderUtil.createErrorMsg(PCEPErrors.NON_ZERO_PLSPID, 1L);
205 this.listener.onSessionUp(this.session);
206 final Future<RpcResult<AddLspOutput>> futureOutput = this.topologyRpcs.addLsp(createAddLspInput());
207 this.listener.onMessage(this.session, errorMsg);
209 final AddLspOutput output = futureOutput.get().getResult();
210 Assert.assertEquals(FailureType.Failed ,output.getFailure());
211 Assert.assertEquals(1, output.getError().size());
212 final ErrorObject err = output.getError().get(0).getErrorObject();
213 Assert.assertEquals(PCEPErrors.NON_ZERO_PLSPID.getErrorType(), err.getType().shortValue());
214 Assert.assertEquals(PCEPErrors.NON_ZERO_PLSPID.getErrorValue(), err.getValue().shortValue());
218 public void testOnSessionDown() throws InterruptedException, ExecutionException {
219 this.listener.onSessionUp(this.session);
221 final Future<RpcResult<AddLspOutput>> futureOutput = this.topologyRpcs.addLsp(createAddLspInput());
222 this.listener.onSessionDown(this.session, new IllegalArgumentException());
223 final AddLspOutput output = futureOutput.get().getResult();
224 // deal with unsent request after session down
225 Assert.assertEquals(FailureType.Unsent, output.getFailure());
229 public void testOnSessionTermination() throws UnknownHostException, InterruptedException, ExecutionException {
230 this.listener.onSessionUp(this.session);
233 this.topologyRpcs.addLsp(createAddLspInput());
234 final Pcinitiate pcinitiate = (Pcinitiate) this.receivedMsgs.get(0);
235 final Requests req = pcinitiate.getPcinitiateMessage().getRequests().get(0);
236 final long srpId = req.getSrp().getOperationId().getValue();
237 final InetAddress inetAddress = InetAddress.getByName(TEST_ADDRESS);
238 final Tlvs tlvs = createLspTlvs(req.getLsp().getPlspId().getValue(), true,
239 inetAddress, inetAddress, inetAddress);
240 final Pcrpt pcRpt = MsgBuilderUtil.createPcRtpMessage(new LspBuilder(req.getLsp()).setTlvs(tlvs).setSync(true).setRemove(false).setOperational(OperationalStatus.Active).build(), Optional.of(MsgBuilderUtil.createSrp(srpId)), MsgBuilderUtil.createPath(req.getEro().getSubobject()));
241 this.listener.onMessage(this.session, pcRpt);
242 Assert.assertEquals(1, getTopology().get().getNode().size());
244 // node should be removed after termination
245 this.listener.onSessionTerminated(this.session, new PCEPCloseTermination(TerminationReason.Unknown));
246 Assert.assertEquals(0, getTopology().get().getNode().size());
250 public void testUnknownLsp() throws Exception {
251 final List<Reports> reports = Lists.newArrayList(new ReportsBuilder().setPath(new PathBuilder().setEro(new EroBuilder().build()).build()).setLsp(
252 new LspBuilder().setPlspId(new PlspId(5L)).setSync(false).setRemove(false).setTlvs(
253 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.lsp.TlvsBuilder().setLspIdentifiers(new LspIdentifiersBuilder().setLspId(new LspId(1L)).build()).setSymbolicPathName(
254 new SymbolicPathNameBuilder().setPathName(new SymbolicPathName(new byte[] { 22, 34 })).build()).build()).build()).build());
255 final Pcrpt rptmsg = new PcrptBuilder().setPcrptMessage(new PcrptMessageBuilder().setReports(reports).build()).build();
256 this.session.sessionUp();
257 this.session.handleMessage(rptmsg);
258 final Topology topology = getTopology().get();
259 assertFalse(topology.getNode().isEmpty());
263 protected Open getLocalPref() {
264 return new OpenBuilder(super.getLocalPref()).setTlvs(new TlvsBuilder().addAugmentation(Tlvs1.class, new Tlvs1Builder().setStateful(new StatefulBuilder().build()).build()).build()).build();
267 private AddLspInput createAddLspInput() {
268 final ArgumentsBuilder argsBuilder = new ArgumentsBuilder();
269 final Ipv4CaseBuilder ipv4Builder = new Ipv4CaseBuilder();
270 ipv4Builder.setIpv4(new Ipv4Builder().setSourceIpv4Address(new Ipv4Address(TEST_ADDRESS)).setDestinationIpv4Address(new Ipv4Address(TEST_ADDRESS)).build());
271 argsBuilder.setEndpointsObj(new EndpointsObjBuilder().setAddressFamily(ipv4Builder.build()).build());
272 argsBuilder.setEro(createEroWithIpPrefixes(Lists.newArrayList(ERO_IP_PREFIX)));
273 argsBuilder.addAugmentation(Arguments2.class, new Arguments2Builder().setLsp(new LspBuilder().setDelegate(true).setAdministrative(true).build()).build());
274 return new AddLspInputBuilder().setName(TEST_LSP_NAME).setArguments(argsBuilder.build()).setNetworkTopologyRef(new NetworkTopologyRef(TOPO_IID)).setNode(NODE_ID).build();