401a3d9a6d55637e66cd16990d15f603d1689463
[bgpcep.git] / pcep / topology-provider / src / test / java / org / opendaylight / bgpcep / pcep / topology / provider / Stateful07TopologySessionListenerTest.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
9 package org.opendaylight.bgpcep.pcep.topology.provider;
10
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.junit.Assert.fail;
17 import static org.mockito.Mockito.times;
18 import static org.mockito.Mockito.verify;
19 import static org.opendaylight.protocol.pcep.pcc.mock.spi.MsgBuilderUtil.createLspTlvs;
20 import static org.opendaylight.protocol.util.CheckUtil.checkNull;
21 import static org.opendaylight.protocol.util.CheckUtil.readData;
22
23 import com.google.common.base.Optional;
24 import com.google.common.collect.Lists;
25 import java.net.UnknownHostException;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.concurrent.ExecutionException;
29 import java.util.concurrent.Future;
30 import java.util.concurrent.TimeUnit;
31 import java.util.concurrent.TimeoutException;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.opendaylight.controller.config.yang.pcep.topology.provider.SessionState;
35 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
36 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
37 import org.opendaylight.protocol.pcep.PCEPCloseTermination;
38 import org.opendaylight.protocol.pcep.PCEPSession;
39 import org.opendaylight.protocol.pcep.TerminationReason;
40 import org.opendaylight.protocol.pcep.pcc.mock.spi.MsgBuilderUtil;
41 import org.opendaylight.protocol.pcep.spi.AbstractMessageParser;
42 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.topology.rev140113.NetworkTopologyRef;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev131126.Pcinitiate;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev131126.Stateful1;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev131126.Stateful1Builder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev131126.pcinitiate.message.pcinitiate.message.Requests;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Arguments1;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Arguments1Builder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Arguments2;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Arguments2Builder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Arguments3;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Arguments3Builder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.OperationalStatus;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Pcrpt;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.PcrptBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Pcupd;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.PlspId;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.SymbolicPathName;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Tlvs1;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Tlvs1Builder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.identifiers.tlv.LspIdentifiersBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.LspBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.object.lsp.Tlvs;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.pcrpt.message.PcrptMessageBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.pcrpt.message.pcrpt.message.Reports;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.pcrpt.message.pcrpt.message.ReportsBuilder;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.pcrpt.message.pcrpt.message.reports.PathBuilder;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.pcupd.message.pcupd.message.Updates;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.stateful.capability.tlv.StatefulBuilder;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.symbolic.path.name.tlv.SymbolicPathNameBuilder;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv4CaseBuilder;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.ipv4._case.Ipv4Builder;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.object.EndpointsObjBuilder;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.EroBuilder;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.Open;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.TlvsBuilder;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.ErrorObject;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.LspId;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.AddLspInput;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.AddLspInputBuilder;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.AddLspOutput;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.EnsureLspOperationalInput;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.EnsureLspOperationalInputBuilder;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.FailureType;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.OperationResult;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.RemoveLspInput;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.RemoveLspInputBuilder;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.UpdateLspInput;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.UpdateLspInputBuilder;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.UpdateLspOutput;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.add.lsp.args.ArgumentsBuilder;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.path.computation.client.ReportedLsp;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.path.computation.client.reported.lsp.Path;
98 import org.opendaylight.yangtools.yang.common.RpcResult;
99
100 public class Stateful07TopologySessionListenerTest extends AbstractPCEPSessionTest<Stateful07TopologySessionListenerFactory> {
101
102     private final String TUNNEL_NAME = "pcc_" + this.testAddress + "_tunnel_0";
103
104     private Stateful07TopologySessionListener listener;
105
106     private PCEPSession session;
107
108     @Override
109     @Before
110     public void setUp() throws Exception {
111         super.setUp();
112         this.listener = (Stateful07TopologySessionListener) getSessionListener();
113         this.session = getPCEPSession(getLocalPref(), getRemotePref());
114     }
115
116     @Test
117     public void testStateful07TopologySessionListener() throws Exception {
118         this.listener.onSessionUp(this.session);
119
120         assertEquals(this.testAddress, this.listener.getPeerId());
121         final SessionState state = this.listener.getSessionState();
122         assertNotNull(state);
123         assertEquals(DEAD_TIMER, state.getLocalPref().getDeadtimer().shortValue());
124         assertEquals(KEEP_ALIVE, state.getLocalPref().getKeepalive().shortValue());
125         assertEquals(0, state.getLocalPref().getSessionId().intValue());
126         assertEquals(this.testAddress, state.getLocalPref().getIpAddress());
127         assertEquals(DEAD_TIMER, state.getPeerPref().getDeadtimer().shortValue());
128         assertEquals(KEEP_ALIVE, state.getPeerPref().getKeepalive().shortValue());
129         assertEquals(0, state.getPeerPref().getSessionId().intValue());
130         assertEquals(this.testAddress, state.getPeerPref().getIpAddress());
131
132         // add-lsp
133         this.topologyRpcs.addLsp(createAddLspInput());
134         assertEquals(1, this.receivedMsgs.size());
135         assertTrue(this.receivedMsgs.get(0) instanceof Pcinitiate);
136         final Pcinitiate pcinitiate = (Pcinitiate) this.receivedMsgs.get(0);
137         final Requests req = pcinitiate.getPcinitiateMessage().getRequests().get(0);
138         final long srpId = req.getSrp().getOperationId().getValue();
139         final Tlvs tlvs = createLspTlvs(req.getLsp().getPlspId().getValue(), true,
140             this.testAddress, this.testAddress, this.testAddress, Optional.absent());
141         final Pcrpt pcRpt = MsgBuilderUtil.createPcRtpMessage(new LspBuilder(req.getLsp())
142             .setTlvs(tlvs).setPlspId(new PlspId(1L)).setSync(false).setRemove(false)
143             .setOperational(OperationalStatus.Active).build(), Optional.of(MsgBuilderUtil.createSrp(srpId)),
144             MsgBuilderUtil.createPath(req.getEro().getSubobject()));
145         final Pcrpt esm = MsgBuilderUtil.createPcRtpMessage(new LspBuilder().setSync(false).build(),
146             Optional.of(MsgBuilderUtil.createSrp(0L)), null);
147         this.listener.onMessage(this.session, esm);
148         readData(getDataBroker(), this.pathComputationClientIId, pcc -> {
149             assertEquals(this.testAddress, pcc.getIpAddress().getIpv4Address().getValue());
150             // reported lsp so far empty, has not received response (PcRpt) yet
151             assertTrue(pcc.getReportedLsp().isEmpty());
152             return pcc;
153         });
154
155         this.listener.onMessage(this.session, pcRpt);
156         // check created lsp
157         readData(getDataBroker(), this.pathComputationClientIId, pcc -> {
158             assertEquals(1, pcc.getReportedLsp().size());
159             final ReportedLsp reportedLsp = pcc.getReportedLsp().get(0);
160             assertEquals(this.TUNNEL_NAME, reportedLsp.getName());
161             assertEquals(1, reportedLsp.getPath().size());
162             final Path path = reportedLsp.getPath().get(0);
163             assertEquals(1, path.getEro().getSubobject().size());
164             assertEquals(this.eroIpPrefix, getLastEroIpPrefix(path.getEro()));
165             return pcc;
166         });
167
168         // check stats
169         assertEquals(1, this.listener.getDelegatedLspsCount().intValue());
170         assertTrue(this.listener.getSynchronized());
171         assertTrue(this.listener.getStatefulMessages().getLastReceivedRptMsgTimestamp() > 0);
172         assertEquals(2, this.listener.getStatefulMessages().getReceivedRptMsgCount().intValue());
173         assertEquals(1, this.listener.getStatefulMessages().getSentInitMsgCount().intValue());
174         assertEquals(0, this.listener.getStatefulMessages().getSentUpdMsgCount().intValue());
175         assertNotNull(this.listener.getSessionState());
176
177         // update-lsp
178         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.update.lsp.args
179             .ArgumentsBuilder updArgsBuilder = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.
180             topology.pcep.rev131024.update.lsp.args.ArgumentsBuilder();
181         updArgsBuilder.setEro(createEroWithIpPrefixes(Lists.newArrayList(this.eroIpPrefix, this.dstIpPrefix)));
182         updArgsBuilder.addAugmentation(Arguments3.class, new Arguments3Builder().setLsp(new LspBuilder()
183             .setDelegate(true).setAdministrative(true).build()).build());
184         final UpdateLspInput update = new UpdateLspInputBuilder().setArguments(updArgsBuilder.build())
185             .setName(this.TUNNEL_NAME).setNetworkTopologyRef(new NetworkTopologyRef(TOPO_IID))
186             .setNode(this.nodeId).build();
187         this.topologyRpcs.updateLsp(update);
188         assertEquals(2, this.receivedMsgs.size());
189         assertTrue(this.receivedMsgs.get(1) instanceof Pcupd);
190         final Pcupd updateMsg = (Pcupd) this.receivedMsgs.get(1);
191         final Updates upd = updateMsg.getPcupdMessage().getUpdates().get(0);
192         final long srpId2 = upd.getSrp().getOperationId().getValue();
193         final Tlvs tlvs2 = createLspTlvs(upd.getLsp().getPlspId().getValue(), false,
194             this.newDestinationAddress, this.testAddress, this.testAddress, Optional.absent());
195         final Pcrpt pcRpt2 = MsgBuilderUtil.createPcRtpMessage(new LspBuilder(upd.getLsp()).setTlvs(tlvs2)
196             .setSync(true).setRemove(false).setOperational(OperationalStatus.Active).build(),
197             Optional.of(MsgBuilderUtil.createSrp(srpId2)), MsgBuilderUtil.createPath(upd.getPath()
198                 .getEro().getSubobject()));
199         this.listener.onMessage(this.session, pcRpt2);
200
201         //check updated lsp
202         readData(getDataBroker(), this.pathComputationClientIId, pcc -> {
203             assertEquals(1, pcc.getReportedLsp().size());
204             final ReportedLsp reportedLsp = pcc.getReportedLsp().get(0);
205             assertEquals(this.TUNNEL_NAME, reportedLsp.getName());
206             assertEquals(1, reportedLsp.getPath().size());
207             final Path path = reportedLsp.getPath().get(0);
208             assertEquals(2, path.getEro().getSubobject().size());
209             assertEquals(this.dstIpPrefix, getLastEroIpPrefix(path.getEro()));
210             return pcc;
211         });
212
213         // check stats
214         assertEquals(1, this.listener.getDelegatedLspsCount().intValue());
215         assertTrue(this.listener.getSynchronized());
216         assertTrue(this.listener.getStatefulMessages().getLastReceivedRptMsgTimestamp() > 0);
217         assertEquals(3, this.listener.getStatefulMessages().getReceivedRptMsgCount().intValue());
218         assertEquals(1, this.listener.getStatefulMessages().getSentInitMsgCount().intValue());
219         assertEquals(1, this.listener.getStatefulMessages().getSentUpdMsgCount().intValue());
220         assertTrue(this.listener.getReplyTime().getAverageTime() > 0);
221         assertTrue(this.listener.getReplyTime().getMaxTime() > 0);
222         assertFalse(this.listener.getPeerCapabilities().getActive());
223         assertTrue(this.listener.getPeerCapabilities().getInstantiation());
224         assertTrue(this.listener.getPeerCapabilities().getStateful());
225
226         // ensure-operational
227         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.ensure.lsp.
228             operational.args.ArgumentsBuilder ensureArgs = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.
229             xml.ns.yang.topology.pcep.rev131024.ensure.lsp.operational.args.ArgumentsBuilder();
230         ensureArgs.addAugmentation(Arguments1.class, new Arguments1Builder().setOperational(OperationalStatus.Active)
231             .build());
232         final EnsureLspOperationalInput ensure = new EnsureLspOperationalInputBuilder().setArguments(ensureArgs.build())
233             .setName(this.TUNNEL_NAME).setNetworkTopologyRef(new NetworkTopologyRef(TOPO_IID))
234             .setNode(this.nodeId).build();
235         final OperationResult result = this.topologyRpcs.ensureLspOperational(ensure).get().getResult();
236         //check result
237         assertNull(result.getFailure());
238
239         // remove-lsp
240         final RemoveLspInput remove = new RemoveLspInputBuilder().setName(this.TUNNEL_NAME)
241             .setNetworkTopologyRef(new NetworkTopologyRef(TOPO_IID)).setNode(this.nodeId).build();
242         this.topologyRpcs.removeLsp(remove);
243         assertEquals(3, this.receivedMsgs.size());
244         assertTrue(this.receivedMsgs.get(2) instanceof Pcinitiate);
245         final Pcinitiate pcinitiate2 = (Pcinitiate) this.receivedMsgs.get(2);
246         final Requests req2 = pcinitiate2.getPcinitiateMessage().getRequests().get(0);
247         final long srpId3 = req2.getSrp().getOperationId().getValue();
248         final Tlvs tlvs3 = createLspTlvs(req2.getLsp().getPlspId().getValue(), false,
249             this.testAddress, this.testAddress, this.testAddress, Optional.absent());
250         final Pcrpt pcRpt3 = MsgBuilderUtil.createPcRtpMessage(new LspBuilder(req2.getLsp()).setTlvs(tlvs3)
251             .setRemove(true).setSync(true).setOperational(OperationalStatus.Down).build(),
252             Optional.of(MsgBuilderUtil.createSrp(srpId3)), MsgBuilderUtil.createPath(Collections.emptyList()));
253         this.listener.onMessage(this.session, pcRpt3);
254
255         // check if lsp was removed
256         readData(getDataBroker(), this.pathComputationClientIId, pcc -> {
257             assertEquals(0, pcc.getReportedLsp().size());
258             return pcc;
259         });
260         // check stats
261         assertEquals(0, this.listener.getDelegatedLspsCount().intValue());
262         assertTrue(this.listener.getSynchronized());
263         assertTrue(this.listener.getStatefulMessages().getLastReceivedRptMsgTimestamp() > 0);
264         assertEquals(4, this.listener.getStatefulMessages().getReceivedRptMsgCount().intValue());
265         assertEquals(2, this.listener.getStatefulMessages().getSentInitMsgCount().intValue());
266         assertEquals(1, this.listener.getStatefulMessages().getSentUpdMsgCount().intValue());
267         this.listener.resetStats();
268         assertEquals(0, this.listener.getStatefulMessages().getLastReceivedRptMsgTimestamp().longValue());
269         assertEquals(0, this.listener.getStatefulMessages().getReceivedRptMsgCount().intValue());
270         assertEquals(0, this.listener.getStatefulMessages().getSentInitMsgCount().intValue());
271         assertEquals(0, this.listener.getStatefulMessages().getSentUpdMsgCount().intValue());
272         assertEquals(0, this.listener.getReplyTime().getAverageTime().longValue());
273         assertEquals(0, this.listener.getReplyTime().getMaxTime().longValue());
274         assertEquals(0, this.listener.getReplyTime().getMinTime().longValue());
275     }
276
277     @Test
278     public void testOnUnhandledErrorMessage() {
279         final Message errorMsg = AbstractMessageParser.createErrorMsg(PCEPErrors.NON_ZERO_PLSPID, Optional.absent());
280         this.listener.onSessionUp(this.session);
281         assertTrue(this.listener.onMessage(Optional.<AbstractTopologySessionListener.MessageContext>absent().orNull(), errorMsg));
282     }
283
284     @Test
285     public void testOnErrorMessage() throws InterruptedException, ExecutionException {
286         final Message errorMsg = MsgBuilderUtil.createErrorMsg(PCEPErrors.NON_ZERO_PLSPID, 1L);
287         this.listener.onSessionUp(this.session);
288         final Future<RpcResult<AddLspOutput>> futureOutput = this.topologyRpcs.addLsp(createAddLspInput());
289         this.listener.onMessage(this.session, errorMsg);
290
291         final AddLspOutput output = futureOutput.get().getResult();
292         assertEquals(FailureType.Failed ,output.getFailure());
293         assertEquals(1, output.getError().size());
294         final ErrorObject err = output.getError().get(0).getErrorObject();
295         assertEquals(PCEPErrors.NON_ZERO_PLSPID.getErrorType(), err.getType().shortValue());
296         assertEquals(PCEPErrors.NON_ZERO_PLSPID.getErrorValue(), err.getValue().shortValue());
297     }
298
299     @Test
300     public void testOnSessionDown() throws InterruptedException, ExecutionException {
301         this.listener.onSessionUp(this.session);
302         verify(this.listenerReg, times(0)).close();
303         // send request
304         final Future<RpcResult<AddLspOutput>> futureOutput = this.topologyRpcs.addLsp(createAddLspInput());
305         this.listener.onSessionDown(this.session, new IllegalArgumentException());
306         verify(this.listenerReg, times(1)).close();
307         final AddLspOutput output = futureOutput.get().getResult();
308         // deal with unsent request after session down
309         assertEquals(FailureType.Unsent, output.getFailure());
310     }
311
312     /**
313      * All the pcep session registration should be closed when the session manager is closed
314      * @throws InterruptedException
315      * @throws ExecutionException
316      * @throws TransactionCommitFailedException
317      */
318     @Test
319     public void testOnServerSessionManagerDown() throws InterruptedException, ExecutionException,
320         TransactionCommitFailedException {
321         this.listener.onSessionUp(this.session);
322         verify(this.listenerReg, times(0)).close();
323         // send request
324         final Future<RpcResult<AddLspOutput>> futureOutput = this.topologyRpcs.addLsp(createAddLspInput());
325         this.manager.close();
326         verify(this.listenerReg, times(1)).close();
327         final AddLspOutput output = futureOutput.get().getResult();
328         // deal with unsent request after session down
329         assertEquals(FailureType.Unsent, output.getFailure());
330     }
331
332     /**
333      * Verify the PCEP session should not be up when server session manager is down,
334      * otherwise it would be a problem when the session is up while it's not registered with session manager
335      * @throws InterruptedException
336      * @throws ExecutionException
337      * @throws TransactionCommitFailedException
338      */
339     @Test
340     public void testOnServerSessionManagerUnstarted() throws InterruptedException, ExecutionException,
341         TransactionCommitFailedException, ReadFailedException {
342         this.manager.close();
343         // the registration should not be closed since it's never initialized
344         verify(this.listenerReg, times(0)).close();
345         this.listener.onSessionUp(this.session);
346         // verify the session was NOT added to topology
347         checkNull(getDataBroker(), TOPO_IID);
348         // still, the session should not be registered and thus close() is never called
349         verify(this.listenerReg, times(0)).close();
350         // send request
351         final Future<RpcResult<AddLspOutput>> futureOutput = this.topologyRpcs.addLsp(createAddLspInput());
352         final AddLspOutput output = futureOutput.get().getResult();
353         // deal with unsent request after session down
354         assertEquals(FailureType.Unsent, output.getFailure());
355     }
356
357     @Test
358     public void testOnSessionTermination() throws Exception {
359         this.listener.onSessionUp(this.session);
360         verify(this.listenerReg, times(0)).close();
361
362         // create node
363         this.topologyRpcs.addLsp(createAddLspInput());
364         final Pcinitiate pcinitiate = (Pcinitiate) this.receivedMsgs.get(0);
365         final Requests req = pcinitiate.getPcinitiateMessage().getRequests().get(0);
366         final long srpId = req.getSrp().getOperationId().getValue();
367         final Tlvs tlvs = createLspTlvs(req.getLsp().getPlspId().getValue(), true,
368             this.testAddress, this.testAddress, this.testAddress, Optional.absent());
369         final Pcrpt pcRpt = MsgBuilderUtil.createPcRtpMessage(new LspBuilder(req.getLsp()).setTlvs(tlvs).setSync(true)
370             .setRemove(false).setOperational(OperationalStatus.Active).build(),
371             Optional.of(MsgBuilderUtil.createSrp(srpId)), MsgBuilderUtil.createPath(req.getEro().getSubobject()));
372         this.listener.onMessage(this.session, pcRpt);
373         readData(getDataBroker(), TOPO_IID, topology -> {
374             assertEquals(1, topology.getNode().size());
375             return topology;
376         });
377
378         // node should be removed after termination
379         this.listener.onSessionTerminated(this.session, new PCEPCloseTermination(TerminationReason.UNKNOWN));
380         verify(this.listenerReg, times(1)).close();
381         checkNull(getDataBroker(), this.pathComputationClientIId);
382     }
383
384     @Test
385     public void testUnknownLsp() throws Exception {
386         final List<Reports> reports = Lists.newArrayList(new ReportsBuilder().setPath(new PathBuilder()
387             .setEro(new EroBuilder().build()).build()).setLsp(new LspBuilder().setPlspId(new PlspId(5L))
388             .setSync(false).setRemove(false).setTlvs(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.
389                 yang.pcep.ietf.stateful.rev131222.lsp.object.lsp.TlvsBuilder().setLspIdentifiers(
390                     new LspIdentifiersBuilder().setLspId(new LspId(1L)).build()).setSymbolicPathName(
391                         new SymbolicPathNameBuilder().setPathName(new SymbolicPathName(new byte[] { 22, 34 }))
392                             .build()).build()).build()).build());
393         final Pcrpt rptmsg = new PcrptBuilder().setPcrptMessage(new PcrptMessageBuilder().setReports(reports).build())
394             .build();
395         this.listener.onSessionUp(this.session);
396         this.listener.onMessage(this.session, rptmsg);
397         readData(getDataBroker(), TOPO_IID, node -> {
398             assertFalse(node.getNode().isEmpty());
399             return node;
400         });
401     }
402
403     @Test
404     public void testUpdateUnknownLsp() throws InterruptedException, ExecutionException {
405         this.listener.onSessionUp(this.session);
406         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.update.lsp.args
407             .ArgumentsBuilder updArgsBuilder = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.
408             topology.pcep.rev131024.update.lsp.args.ArgumentsBuilder();
409         updArgsBuilder.setEro(createEroWithIpPrefixes(Lists.newArrayList(this.eroIpPrefix, this.dstIpPrefix)));
410         updArgsBuilder.addAugmentation(Arguments3.class, new Arguments3Builder().setLsp(new LspBuilder()
411             .setDelegate(true).setAdministrative(true).build()).build());
412         final UpdateLspInput update = new UpdateLspInputBuilder().setArguments(updArgsBuilder.build())
413             .setName(this.TUNNEL_NAME).setNetworkTopologyRef(new NetworkTopologyRef(TOPO_IID)).setNode(this.nodeId)
414             .build();
415         final UpdateLspOutput result = this.topologyRpcs.updateLsp(update).get().getResult();
416         assertEquals(FailureType.Unsent, result.getFailure());
417         assertEquals(1, result.getError().size());
418         final ErrorObject errorObject = result.getError().get(0).getErrorObject();
419         assertNotNull(errorObject);
420         assertEquals(PCEPErrors.UNKNOWN_PLSP_ID, PCEPErrors.forValue(errorObject.getType(), errorObject.getValue()));
421     }
422
423     @Test
424     public void testRemoveUnknownLsp() throws InterruptedException, ExecutionException {
425         this.listener.onSessionUp(this.session);
426         final RemoveLspInput remove = new RemoveLspInputBuilder().setName(this.TUNNEL_NAME).setNetworkTopologyRef(
427             new NetworkTopologyRef(TOPO_IID)).setNode(this.nodeId).build();
428         final OperationResult result = this.topologyRpcs.removeLsp(remove).get().getResult();
429         assertEquals(FailureType.Unsent, result.getFailure());
430         assertEquals(1, result.getError().size());
431         final ErrorObject errorObject = result.getError().get(0).getErrorObject();
432         assertNotNull(errorObject);
433         assertEquals(PCEPErrors.UNKNOWN_PLSP_ID, PCEPErrors.forValue(errorObject.getType(), errorObject.getValue()));
434     }
435
436     @Test
437     public void testAddAlreadyExistingLsp() throws UnknownHostException, InterruptedException, ExecutionException {
438         this.listener.onSessionUp(this.session);
439         this.topologyRpcs.addLsp(createAddLspInput());
440         assertEquals(1, this.receivedMsgs.size());
441         assertTrue(this.receivedMsgs.get(0) instanceof Pcinitiate);
442         final Pcinitiate pcinitiate = (Pcinitiate) this.receivedMsgs.get(0);
443         final Requests req = pcinitiate.getPcinitiateMessage().getRequests().get(0);
444         final long srpId = req.getSrp().getOperationId().getValue();
445         final Tlvs tlvs = createLspTlvs(req.getLsp().getPlspId().getValue(), true,
446             this.testAddress, this.testAddress, this.testAddress, Optional.absent());
447         final Pcrpt pcRpt = MsgBuilderUtil.createPcRtpMessage(new LspBuilder(req.getLsp()).setTlvs(tlvs)
448             .setPlspId(new PlspId(1L)).setSync(false).setRemove(false).setOperational(OperationalStatus.Active)
449             .build(), Optional.of(MsgBuilderUtil.createSrp(srpId)), MsgBuilderUtil.createPath(req.getEro()
450             .getSubobject()));
451         this.listener.onMessage(this.session, pcRpt);
452
453         //try to add already existing LSP
454         final AddLspOutput result = this.topologyRpcs.addLsp(createAddLspInput()).get().getResult();
455         assertEquals(FailureType.Unsent, result.getFailure());
456         assertEquals(1, result.getError().size());
457         final ErrorObject errorObject = result.getError().get(0).getErrorObject();
458         assertNotNull(errorObject);
459         assertEquals(PCEPErrors.USED_SYMBOLIC_PATH_NAME, PCEPErrors.forValue(errorObject.getType(),
460             errorObject.getValue()));
461     }
462
463     @Test
464     public void testPccResponseTimeout() throws InterruptedException, ExecutionException {
465         this.listener.onSessionUp(this.session);
466         final Future<RpcResult<AddLspOutput>> addLspResult = this.topologyRpcs.addLsp(createAddLspInput());
467         try {
468             addLspResult.get(2, TimeUnit.SECONDS);
469             fail();
470         } catch (final Exception e) {
471             assertTrue(e instanceof TimeoutException);
472         }
473         Thread.sleep(AbstractPCEPSessionTest.RPC_TIMEOUT);
474         final RpcResult<AddLspOutput> rpcResult = addLspResult.get();
475         assertNotNull(rpcResult);
476         assertEquals(rpcResult.getResult().getFailure(), FailureType.Unsent);
477     }
478
479     @Override
480     protected Open getLocalPref() {
481         return new OpenBuilder(super.getLocalPref()).setTlvs(new TlvsBuilder().addAugmentation(Tlvs1.class,
482             new Tlvs1Builder().setStateful(new StatefulBuilder()
483             .addAugmentation(Stateful1.class, new Stateful1Builder().setInitiation(Boolean.TRUE).build())
484             .addAugmentation(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.
485                 optimizations.rev150714.Stateful1.class, new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.
486                 ns.yang.controller.pcep.sync.optimizations.rev150714.Stateful1Builder()
487                 .setTriggeredInitialSync(Boolean.TRUE).build())
488             .build()).build()).build()).build();
489     }
490
491     @Override
492     protected Open getRemotePref() {
493         return getLocalPref();
494     }
495
496     private AddLspInput createAddLspInput() {
497         final ArgumentsBuilder argsBuilder = new ArgumentsBuilder();
498         final Ipv4CaseBuilder ipv4Builder = new Ipv4CaseBuilder();
499         ipv4Builder.setIpv4(new Ipv4Builder().setSourceIpv4Address(new Ipv4Address(this.testAddress))
500             .setDestinationIpv4Address(new Ipv4Address(this.testAddress)).build());
501         argsBuilder.setEndpointsObj(new EndpointsObjBuilder().setAddressFamily(ipv4Builder.build()).build());
502         argsBuilder.setEro(createEroWithIpPrefixes(Lists.newArrayList(this.eroIpPrefix)));
503         argsBuilder.addAugmentation(Arguments2.class, new Arguments2Builder().setLsp(new LspBuilder()
504             .setDelegate(true).setAdministrative(true).build()).build());
505         return new AddLspInputBuilder().setName(this.TUNNEL_NAME).setArguments(argsBuilder.build())
506             .setNetworkTopologyRef(new NetworkTopologyRef(TOPO_IID)).setNode(this.nodeId).build();
507     }
508 }