Enforce findbug and checkstyle under PCEP
[bgpcep.git] / pcep / topology / 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.opendaylight.protocol.pcep.pcc.mock.spi.MsgBuilderUtil.createLspTlvs;
18 import static org.opendaylight.protocol.util.CheckUtil.checkEquals;
19 import static org.opendaylight.protocol.util.CheckUtil.checkNotPresentOperational;
20 import static org.opendaylight.protocol.util.CheckUtil.readDataOperational;
21
22 import com.google.common.base.Optional;
23 import com.google.common.collect.Lists;
24 import java.net.UnknownHostException;
25 import java.util.Collections;
26 import java.util.List;
27 import java.util.concurrent.ExecutionException;
28 import java.util.concurrent.Future;
29 import java.util.concurrent.TimeUnit;
30 import java.util.concurrent.TimeoutException;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
34 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
35 import org.opendaylight.protocol.pcep.PCEPCloseTermination;
36 import org.opendaylight.protocol.pcep.TerminationReason;
37 import org.opendaylight.protocol.pcep.impl.PCEPSessionImpl;
38 import org.opendaylight.protocol.pcep.pcc.mock.spi.MsgBuilderUtil;
39 import org.opendaylight.protocol.pcep.spi.AbstractMessageParser;
40 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
41 import org.opendaylight.protocol.util.CheckUtil;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.topology.rev140113.NetworkTopologyRef;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev171025.Pcinitiate;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev171025.Stateful1;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev171025.Stateful1Builder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.initiated.rev171025.pcinitiate.message.pcinitiate.message.Requests;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.Arguments1;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.Arguments1Builder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.Arguments2;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.Arguments2Builder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.Arguments3;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.Arguments3Builder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.OperationalStatus;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.Pcrpt;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.PcrptBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.Pcupd;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.PlspId;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.SymbolicPathName;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.Tlvs1;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.Tlvs1Builder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.lsp.identifiers.tlv.LspIdentifiersBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.lsp.object.LspBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.lsp.object.lsp.Tlvs;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.pcrpt.message.PcrptMessageBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.pcrpt.message.pcrpt.message.Reports;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.pcrpt.message.pcrpt.message.ReportsBuilder;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.pcrpt.message.pcrpt.message.reports.PathBuilder;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.pcupd.message.pcupd.message.Updates;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.stateful.capability.tlv.StatefulBuilder;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.symbolic.path.name.tlv.SymbolicPathNameBuilder;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Close;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.stateful.stats.rev171113.StatefulCapabilitiesStatsAug;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.stateful.stats.rev171113.StatefulMessagesStatsAug;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.stats.rev171113.PcepSessionState;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.stats.rev171113.pcep.session.state.LocalPref;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.stats.rev171113.pcep.session.state.PeerPref;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.stats.rev171113.reply.time.grouping.ReplyTime;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv4CaseBuilder;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.ipv4._case.Ipv4Builder;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.object.EndpointsObjBuilder;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.explicit.route.object.EroBuilder;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.Open;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.open.TlvsBuilder;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.ErrorObject;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.LspId;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.AddLspInput;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.AddLspInputBuilder;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.AddLspOutput;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.EnsureLspOperationalInput;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.EnsureLspOperationalInputBuilder;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.FailureType;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.OperationResult;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.RemoveLspInput;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.RemoveLspInputBuilder;
98 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.UpdateLspInput;
99 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.UpdateLspInputBuilder;
100 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.UpdateLspOutput;
101 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.add.lsp.args.ArgumentsBuilder;
102 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.pcep.client.attributes.path.computation.client.ReportedLsp;
103 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.pcep.client.attributes.path.computation.client.reported.lsp.Path;
104 import org.opendaylight.yangtools.yang.common.RpcResult;
105
106 public class Stateful07TopologySessionListenerTest
107         extends AbstractPCEPSessionTest<Stateful07TopologySessionListenerFactory> {
108
109     private final String tunnelName = "pcc_" + this.testAddress + "_tunnel_0";
110
111     private Stateful07TopologySessionListener listener;
112
113     private PCEPSessionImpl session;
114
115     @Override
116     @Before
117     public void setUp() throws Exception {
118         super.setUp();
119         this.listener = (Stateful07TopologySessionListener) getSessionListener();
120         this.session = getPCEPSession(getLocalPref(), getRemotePref());
121     }
122
123     @Test
124     public void testStateful07TopologySessionListener() throws Exception {
125         this.listener.onSessionUp(this.session);
126         final PcepSessionState listenerState = this.listener.listenerState;
127         assertEquals(this.testAddress, listenerState.getPeerPref().getIpAddress());
128         final LocalPref state = this.listener.listenerState.getLocalPref();
129         assertNotNull(state);
130         assertEquals(DEAD_TIMER, state.getDeadtimer().shortValue());
131         assertEquals(KEEP_ALIVE, state.getKeepalive().shortValue());
132         assertEquals(0, state.getSessionId().intValue());
133         assertEquals(this.testAddress, state.getIpAddress());
134
135         final PeerPref peerState = listenerState.getPeerPref();
136
137         assertEquals(DEAD_TIMER, peerState.getDeadtimer().shortValue());
138         assertEquals(KEEP_ALIVE, peerState.getKeepalive().shortValue());
139         assertEquals(0, peerState.getSessionId().intValue());
140         assertEquals(this.testAddress, peerState.getIpAddress());
141
142         // add-lsp
143         this.topologyRpcs.addLsp(createAddLspInput());
144         assertEquals(1, this.receivedMsgs.size());
145         assertTrue(this.receivedMsgs.get(0) instanceof Pcinitiate);
146         final Pcinitiate pcinitiate = (Pcinitiate) this.receivedMsgs.get(0);
147         final Requests req = pcinitiate.getPcinitiateMessage().getRequests().get(0);
148         final long srpId = req.getSrp().getOperationId().getValue();
149         final Tlvs tlvs = createLspTlvs(req.getLsp().getPlspId().getValue(), true,
150                 this.testAddress, this.testAddress, this.testAddress, Optional.absent());
151         final Pcrpt pcRpt = MsgBuilderUtil.createPcRtpMessage(new LspBuilder(req.getLsp())
152                         .setTlvs(tlvs).setPlspId(new PlspId(1L)).setSync(false).setRemove(false)
153                         .setOperational(OperationalStatus.Active).build(), Optional.of(MsgBuilderUtil.createSrp(srpId)),
154                 MsgBuilderUtil.createPath(req.getEro().getSubobject()));
155         final Pcrpt esm = MsgBuilderUtil.createPcRtpMessage(new LspBuilder().setSync(false).build(),
156                 Optional.of(MsgBuilderUtil.createSrp(0L)), null);
157         this.listener.onMessage(this.session, esm);
158         readDataOperational(getDataBroker(), this.pathComputationClientIId, pcc -> {
159             assertEquals(this.testAddress, pcc.getIpAddress().getIpv4Address().getValue());
160             // reported lsp so far empty, has not received response (PcRpt) yet
161             assertTrue(pcc.getReportedLsp().isEmpty());
162             return pcc;
163         });
164
165         this.listener.onMessage(this.session, pcRpt);
166         // check created lsp
167         readDataOperational(getDataBroker(), this.pathComputationClientIId, pcc -> {
168             assertEquals(1, pcc.getReportedLsp().size());
169             final ReportedLsp reportedLsp = pcc.getReportedLsp().get(0);
170             assertEquals(this.tunnelName, reportedLsp.getName());
171             assertEquals(1, reportedLsp.getPath().size());
172             final Path path = reportedLsp.getPath().get(0);
173             assertEquals(1, path.getEro().getSubobject().size());
174             assertEquals(this.eroIpPrefix, getLastEroIpPrefix(path.getEro()));
175             return pcc;
176         });
177
178         // check stats
179         checkEquals(() -> assertEquals(1, listenerState.getDelegatedLspsCount().intValue()));
180         checkEquals(() -> assertTrue(this.listener.isSessionSynchronized()));
181         checkEquals(() -> assertTrue(listenerState.getMessages()
182                 .getAugmentation(StatefulMessagesStatsAug.class).getLastReceivedRptMsgTimestamp() > 0));
183         checkEquals(() -> assertEquals(2, listenerState.getMessages()
184                 .getAugmentation(StatefulMessagesStatsAug.class).getReceivedRptMsgCount().intValue()));
185         checkEquals(() -> assertEquals(1, listenerState.getMessages()
186                 .getAugmentation(StatefulMessagesStatsAug.class).getSentInitMsgCount().intValue()));
187         checkEquals(() -> assertEquals(0, listenerState.getMessages()
188                 .getAugmentation(StatefulMessagesStatsAug.class).getSentUpdMsgCount().intValue()));
189
190         // update-lsp
191         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.update.lsp.args
192                 .ArgumentsBuilder updArgsBuilder = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang
193                 .topology.pcep.rev171025.update.lsp.args.ArgumentsBuilder();
194         updArgsBuilder.setEro(createEroWithIpPrefixes(Lists.newArrayList(this.eroIpPrefix, this.dstIpPrefix)));
195         updArgsBuilder.addAugmentation(Arguments3.class, new Arguments3Builder().setLsp(new LspBuilder()
196                 .setDelegate(true).setAdministrative(true).build()).build());
197         final UpdateLspInput update = new UpdateLspInputBuilder().setArguments(updArgsBuilder.build())
198                 .setName(this.tunnelName).setNetworkTopologyRef(new NetworkTopologyRef(TOPO_IID))
199                 .setNode(this.nodeId).build();
200         this.topologyRpcs.updateLsp(update);
201         assertEquals(2, this.receivedMsgs.size());
202         assertTrue(this.receivedMsgs.get(1) instanceof Pcupd);
203         final Pcupd updateMsg = (Pcupd) this.receivedMsgs.get(1);
204         final Updates upd = updateMsg.getPcupdMessage().getUpdates().get(0);
205         final long srpId2 = upd.getSrp().getOperationId().getValue();
206         final Tlvs tlvs2 = createLspTlvs(upd.getLsp().getPlspId().getValue(), false,
207                 this.newDestinationAddress, this.testAddress, this.testAddress, Optional.absent());
208         final Pcrpt pcRpt2 = MsgBuilderUtil.createPcRtpMessage(new LspBuilder(upd.getLsp()).setTlvs(tlvs2)
209                         .setSync(true).setRemove(false).setOperational(OperationalStatus.Active).build(),
210                 Optional.of(MsgBuilderUtil.createSrp(srpId2)), MsgBuilderUtil.createPath(upd.getPath()
211                         .getEro().getSubobject()));
212         this.listener.onMessage(this.session, pcRpt2);
213
214         //check updated lsp
215         readDataOperational(getDataBroker(), this.pathComputationClientIId, pcc -> {
216             assertEquals(1, pcc.getReportedLsp().size());
217             final ReportedLsp reportedLsp = pcc.getReportedLsp().get(0);
218             assertEquals(this.tunnelName, reportedLsp.getName());
219             assertEquals(1, reportedLsp.getPath().size());
220             final Path path = reportedLsp.getPath().get(0);
221             assertEquals(2, path.getEro().getSubobject().size());
222             assertEquals(this.dstIpPrefix, getLastEroIpPrefix(path.getEro()));
223             assertEquals(1, listenerState.getDelegatedLspsCount().intValue());
224             assertTrue(this.listener.isSessionSynchronized());
225             final StatefulMessagesStatsAug statefulstate = listenerState.getMessages()
226                     .getAugmentation(StatefulMessagesStatsAug.class);
227             assertTrue(statefulstate.getLastReceivedRptMsgTimestamp() > 0);
228             assertEquals(3, statefulstate.getReceivedRptMsgCount().intValue());
229             assertEquals(1, statefulstate.getSentInitMsgCount().intValue());
230             assertEquals(1, statefulstate.getSentUpdMsgCount().intValue());
231             final ReplyTime replyTime = listenerState.getMessages().getReplyTime();
232             assertTrue(replyTime.getAverageTime() > 0);
233             assertTrue(replyTime.getMaxTime() > 0);
234             final StatefulCapabilitiesStatsAug statefulCapabilities = listenerState
235                     .getPeerCapabilities().getAugmentation(StatefulCapabilitiesStatsAug.class);
236             assertFalse(statefulCapabilities.isActive());
237             assertTrue(statefulCapabilities.isInstantiation());
238             assertTrue(statefulCapabilities.isStateful());
239             return pcc;
240         });
241
242         // ensure-operational
243         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.ensure.lsp
244                 .operational.args.ArgumentsBuilder ensureArgs = new org.opendaylight.yang.gen.v1.urn.opendaylight.params
245                 .xml.ns.yang.topology.pcep.rev171025.ensure.lsp.operational.args.ArgumentsBuilder();
246         ensureArgs.addAugmentation(Arguments1.class, new Arguments1Builder().setOperational(OperationalStatus.Active)
247                 .build());
248         final EnsureLspOperationalInput ensure = new EnsureLspOperationalInputBuilder().setArguments(ensureArgs.build())
249                 .setName(this.tunnelName).setNetworkTopologyRef(new NetworkTopologyRef(TOPO_IID))
250                 .setNode(this.nodeId).build();
251         final OperationResult result = this.topologyRpcs.ensureLspOperational(ensure).get().getResult();
252         //check result
253         assertNull(result.getFailure());
254
255         // remove-lsp
256         final RemoveLspInput remove = new RemoveLspInputBuilder().setName(this.tunnelName)
257                 .setNetworkTopologyRef(new NetworkTopologyRef(TOPO_IID)).setNode(this.nodeId).build();
258         this.topologyRpcs.removeLsp(remove);
259         assertEquals(3, this.receivedMsgs.size());
260         assertTrue(this.receivedMsgs.get(2) instanceof Pcinitiate);
261         final Pcinitiate pcinitiate2 = (Pcinitiate) this.receivedMsgs.get(2);
262         final Requests req2 = pcinitiate2.getPcinitiateMessage().getRequests().get(0);
263         final long srpId3 = req2.getSrp().getOperationId().getValue();
264         final Tlvs tlvs3 = createLspTlvs(req2.getLsp().getPlspId().getValue(), false,
265                 this.testAddress, this.testAddress, this.testAddress, Optional.absent());
266         final Pcrpt pcRpt3 = MsgBuilderUtil.createPcRtpMessage(new LspBuilder(req2.getLsp()).setTlvs(tlvs3)
267                         .setRemove(true).setSync(true).setOperational(OperationalStatus.Down).build(),
268                 Optional.of(MsgBuilderUtil.createSrp(srpId3)), MsgBuilderUtil.createPath(Collections.emptyList()));
269         this.listener.onMessage(this.session, pcRpt3);
270
271         // check if lsp was removed
272         readDataOperational(getDataBroker(), this.pathComputationClientIId, pcc -> {
273             assertEquals(0, pcc.getReportedLsp().size());
274             return pcc;
275         });
276         // check stats
277         checkEquals(() -> assertEquals(0, listenerState.getDelegatedLspsCount().intValue()));
278         checkEquals(() -> assertTrue(this.listener.isSessionSynchronized()));
279         checkEquals(() -> assertTrue(listenerState.getMessages()
280                 .getAugmentation(StatefulMessagesStatsAug.class).getLastReceivedRptMsgTimestamp() > 0));
281         checkEquals(() -> assertEquals(4, listenerState.getMessages()
282                 .getAugmentation(StatefulMessagesStatsAug.class).getReceivedRptMsgCount().intValue()));
283         checkEquals(() -> assertEquals(2, listenerState.getMessages()
284                 .getAugmentation(StatefulMessagesStatsAug.class).getSentInitMsgCount().intValue()));
285         checkEquals(() -> assertEquals(1, listenerState.getMessages()
286                 .getAugmentation(StatefulMessagesStatsAug.class).getSentUpdMsgCount().intValue()));
287     }
288
289     @Test
290     public void testOnUnhandledErrorMessage() {
291         final Message errorMsg = AbstractMessageParser.createErrorMsg(PCEPErrors.NON_ZERO_PLSPID, Optional.absent());
292         this.listener.onSessionUp(this.session);
293         assertTrue(this.listener.onMessage(Optional.<AbstractTopologySessionListener.MessageContext>absent().orNull(),
294                 errorMsg));
295     }
296
297     @Test
298     public void testOnErrorMessage() throws InterruptedException, ExecutionException {
299         final Message errorMsg = MsgBuilderUtil.createErrorMsg(PCEPErrors.NON_ZERO_PLSPID, 1L);
300         this.listener.onSessionUp(this.session);
301         final Future<RpcResult<AddLspOutput>> futureOutput = this.topologyRpcs.addLsp(createAddLspInput());
302         this.listener.onMessage(this.session, errorMsg);
303
304         final AddLspOutput output = futureOutput.get().getResult();
305         assertEquals(FailureType.Failed, output.getFailure());
306         assertEquals(1, output.getError().size());
307         final ErrorObject err = output.getError().get(0).getErrorObject();
308         assertEquals(PCEPErrors.NON_ZERO_PLSPID.getErrorType(), err.getType().shortValue());
309         assertEquals(PCEPErrors.NON_ZERO_PLSPID.getErrorValue(), err.getValue().shortValue());
310     }
311
312     @Test
313     public void testOnSessionDown() throws InterruptedException, ExecutionException {
314         this.listener.onSessionUp(this.session);
315         // send request
316         final Future<RpcResult<AddLspOutput>> futureOutput = this.topologyRpcs.addLsp(createAddLspInput());
317         assertFalse(this.session.isClosed());
318         this.listener.onSessionDown(this.session, new IllegalArgumentException());
319         assertTrue(this.session.isClosed());
320         final AddLspOutput output = futureOutput.get().getResult();
321         // deal with unsent request after session down
322         assertEquals(FailureType.Unsent, output.getFailure());
323     }
324
325     /**
326      * All the pcep session registration should be closed when the session manager is closed.
327      */
328     @Test
329     public void testOnServerSessionManagerDown() throws InterruptedException, ExecutionException,
330             TransactionCommitFailedException {
331         this.listener.onSessionUp(this.session);
332         // the session should not be closed when session manager is up
333         assertFalse(this.session.isClosed());
334         // send request
335         final Future<RpcResult<AddLspOutput>> futureOutput = this.topologyRpcs.addLsp(createAddLspInput());
336         stopSessionManager();
337         final AddLspOutput output = futureOutput.get().getResult();
338         // deal with unsent request after session down
339         assertEquals(FailureType.Unsent, output.getFailure());
340         // verify the session is closed after server session manager is closed
341         assertTrue(this.session.isClosed());
342     }
343
344     /**
345      * Verify the PCEP session should not be up when server session manager is down,
346      * otherwise it would be a problem when the session is up while it's not registered with session manager.
347      */
348     @Test
349     public void testOnServerSessionManagerUnstarted() throws InterruptedException, ExecutionException,
350             TransactionCommitFailedException, ReadFailedException {
351         stopSessionManager();
352         assertFalse(this.session.isClosed());
353         this.listener.onSessionUp(this.session);
354         // verify the session was NOT added to topology
355         checkNotPresentOperational(getDataBroker(), TOPO_IID);
356         // verify the session is closed due to server session manager is closed
357         assertTrue(this.session.isClosed());
358         // send request
359         final Future<RpcResult<AddLspOutput>> futureOutput = this.topologyRpcs.addLsp(createAddLspInput());
360         final AddLspOutput output = futureOutput.get().getResult();
361         // deal with unsent request after session down
362         assertEquals(FailureType.Unsent, output.getFailure());
363     }
364
365     @Test
366     public void testOnServerSessionManagerRestartAndSessionRecovery() throws Exception {
367         // close server session manager first
368         stopSessionManager();
369         assertFalse(this.session.isClosed());
370         this.listener.onSessionUp(this.session);
371         // verify the session was NOT added to topology
372         checkNotPresentOperational(getDataBroker(), TOPO_IID);
373         // verify the session is closed due to server session manager is closed
374         assertTrue(this.session.isClosed());
375         // send request
376         final Future<RpcResult<AddLspOutput>> futureOutput = this.topologyRpcs.addLsp(createAddLspInput());
377         final AddLspOutput output = futureOutput.get().getResult();
378         // deal with unsent request after session down
379         assertEquals(FailureType.Unsent, output.getFailure());
380         // PCC client is not there
381         checkNotPresentOperational(getDataBroker(), this.pathComputationClientIId);
382
383         // reset received message queue
384         this.receivedMsgs.clear();
385         // now we restart the session manager
386         startSessionManager();
387         // try to start the session again
388         // notice since the session was terminated before, it is not usable anymore.
389         // we need to get a new session instance. the new session will have the same local / remote preference
390         this.session = getPCEPSession(getLocalPref(), getRemotePref());
391         assertFalse(this.session.isClosed());
392         this.listener.onSessionUp(this.session);
393         assertFalse(this.session.isClosed());
394
395         // create node
396         this.topologyRpcs.addLsp(createAddLspInput());
397         final Pcinitiate pcinitiate = (Pcinitiate) this.receivedMsgs.get(0);
398         final Requests req = pcinitiate.getPcinitiateMessage().getRequests().get(0);
399         final long srpId = req.getSrp().getOperationId().getValue();
400         final Tlvs tlvs = createLspTlvs(req.getLsp().getPlspId().getValue(), true,
401                 this.testAddress, this.testAddress, this.testAddress, Optional.absent());
402         final Pcrpt pcRpt = MsgBuilderUtil.createPcRtpMessage(new LspBuilder(req.getLsp()).setTlvs(tlvs).setSync(true)
403                         .setRemove(false).setOperational(OperationalStatus.Active).build(),
404                 Optional.of(MsgBuilderUtil.createSrp(srpId)), MsgBuilderUtil.createPath(req.getEro().getSubobject()));
405         this.listener.onMessage(this.session, pcRpt);
406         readDataOperational(getDataBroker(), TOPO_IID, topology -> {
407             assertEquals(1, topology.getNode().size());
408             return topology;
409         });
410     }
411
412     /**
413      * When a session is somehow duplicated in controller, the controller should drop existing session.
414      */
415     @Test
416     public void testDuplicatedSession() throws ReadFailedException {
417         this.listener.onSessionUp(this.session);
418
419         // create node
420         this.topologyRpcs.addLsp(createAddLspInput());
421         final Pcinitiate pcinitiate = (Pcinitiate) this.receivedMsgs.get(0);
422         final Requests req = pcinitiate.getPcinitiateMessage().getRequests().get(0);
423         final long srpId = req.getSrp().getOperationId().getValue();
424         final Tlvs tlvs = createLspTlvs(req.getLsp().getPlspId().getValue(), true,
425                 this.testAddress, this.testAddress, this.testAddress, Optional.absent());
426         final Pcrpt pcRpt = MsgBuilderUtil.createPcRtpMessage(new LspBuilder(req.getLsp()).setTlvs(tlvs).setSync(true)
427                         .setRemove(false).setOperational(OperationalStatus.Active).build(),
428                 Optional.of(MsgBuilderUtil.createSrp(srpId)), MsgBuilderUtil.createPath(req.getEro().getSubobject()));
429         this.listener.onMessage(this.session, pcRpt);
430         readDataOperational(getDataBroker(), TOPO_IID, topology -> {
431             assertEquals(1, topology.getNode().size());
432             return topology;
433         });
434
435         // now we do session up again
436         this.listener.onSessionUp(this.session);
437         assertTrue(this.session.isClosed());
438         // node should be removed after termination
439         checkNotPresentOperational(getDataBroker(), this.pathComputationClientIId);
440         assertFalse(this.receivedMsgs.isEmpty());
441         // the last message should be a Close message
442         assertTrue(this.receivedMsgs.get(this.receivedMsgs.size() - 1) instanceof Close);
443     }
444
445     @Test
446     public void testConflictingListeners() throws Exception {
447         this.listener.onSessionUp(this.session);
448         assertFalse(this.session.isClosed());
449         Stateful07TopologySessionListener conflictingListener =
450                 (Stateful07TopologySessionListener) getSessionListener();
451         conflictingListener.onSessionUp(this.session);
452         assertTrue(this.session.isClosed());
453     }
454
455     @Test
456     public void testOnSessionTermination() throws Exception {
457         this.listener.onSessionUp(this.session);
458         // create node
459         this.topologyRpcs.addLsp(createAddLspInput());
460         final Pcinitiate pcinitiate = (Pcinitiate) this.receivedMsgs.get(0);
461         final Requests req = pcinitiate.getPcinitiateMessage().getRequests().get(0);
462         final long srpId = req.getSrp().getOperationId().getValue();
463         final Tlvs tlvs = createLspTlvs(req.getLsp().getPlspId().getValue(), true,
464                 this.testAddress, this.testAddress, this.testAddress, Optional.absent());
465         final Pcrpt pcRpt = MsgBuilderUtil.createPcRtpMessage(new LspBuilder(req.getLsp()).setTlvs(tlvs).setSync(true)
466                         .setRemove(false).setOperational(OperationalStatus.Active).build(),
467                 Optional.of(MsgBuilderUtil.createSrp(srpId)), MsgBuilderUtil.createPath(req.getEro().getSubobject()));
468         this.listener.onMessage(this.session, pcRpt);
469         readDataOperational(getDataBroker(), TOPO_IID, topology -> {
470             assertEquals(1, topology.getNode().size());
471             return topology;
472         });
473
474         assertFalse(this.session.isClosed());
475         // node should be removed after termination
476         this.listener.onSessionTerminated(this.session, new PCEPCloseTermination(TerminationReason.UNKNOWN));
477         assertTrue(this.session.isClosed());
478         checkNotPresentOperational(getDataBroker(), this.pathComputationClientIId);
479     }
480
481     @Test
482     public void testUnknownLsp() throws Exception {
483         final List<Reports> reports = Lists.newArrayList(new ReportsBuilder().setPath(new PathBuilder()
484                 .setEro(new EroBuilder().build()).build()).setLsp(new LspBuilder().setPlspId(new PlspId(5L))
485                 .setSync(false).setRemove(false).setTlvs(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml
486                         .ns.yang.pcep.ietf.stateful.rev171025.lsp.object.lsp.TlvsBuilder().setLspIdentifiers(
487                         new LspIdentifiersBuilder().setLspId(new LspId(1L)).build()).setSymbolicPathName(
488                         new SymbolicPathNameBuilder().setPathName(new SymbolicPathName(new byte[]{22, 34}))
489                                 .build()).build()).build()).build());
490         final Pcrpt rptmsg = new PcrptBuilder().setPcrptMessage(new PcrptMessageBuilder().setReports(reports).build())
491                 .build();
492         this.listener.onSessionUp(this.session);
493         this.listener.onMessage(this.session, rptmsg);
494         readDataOperational(getDataBroker(), TOPO_IID, node -> {
495             assertFalse(node.getNode().isEmpty());
496             return node;
497         });
498     }
499
500     @Test
501     public void testUpdateUnknownLsp() throws InterruptedException, ExecutionException {
502         this.listener.onSessionUp(this.session);
503         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.update.lsp.args
504                 .ArgumentsBuilder updArgsBuilder = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang
505                 .topology.pcep.rev171025.update.lsp.args.ArgumentsBuilder();
506         updArgsBuilder.setEro(createEroWithIpPrefixes(Lists.newArrayList(this.eroIpPrefix, this.dstIpPrefix)));
507         updArgsBuilder.addAugmentation(Arguments3.class, new Arguments3Builder().setLsp(new LspBuilder()
508                 .setDelegate(true).setAdministrative(true).build()).build());
509         final UpdateLspInput update = new UpdateLspInputBuilder().setArguments(updArgsBuilder.build())
510                 .setName(this.tunnelName).setNetworkTopologyRef(new NetworkTopologyRef(TOPO_IID)).setNode(this.nodeId)
511                 .build();
512         final UpdateLspOutput result = this.topologyRpcs.updateLsp(update).get().getResult();
513         assertEquals(FailureType.Unsent, result.getFailure());
514         assertEquals(1, result.getError().size());
515         final ErrorObject errorObject = result.getError().get(0).getErrorObject();
516         assertNotNull(errorObject);
517         assertEquals(PCEPErrors.UNKNOWN_PLSP_ID, PCEPErrors.forValue(errorObject.getType(), errorObject.getValue()));
518     }
519
520     @Test
521     public void testRemoveUnknownLsp() throws InterruptedException, ExecutionException {
522         this.listener.onSessionUp(this.session);
523         final RemoveLspInput remove = new RemoveLspInputBuilder().setName(this.tunnelName).setNetworkTopologyRef(
524                 new NetworkTopologyRef(TOPO_IID)).setNode(this.nodeId).build();
525         final OperationResult result = this.topologyRpcs.removeLsp(remove).get().getResult();
526         assertEquals(FailureType.Unsent, result.getFailure());
527         assertEquals(1, result.getError().size());
528         final ErrorObject errorObject = result.getError().get(0).getErrorObject();
529         assertNotNull(errorObject);
530         assertEquals(PCEPErrors.UNKNOWN_PLSP_ID, PCEPErrors.forValue(errorObject.getType(), errorObject.getValue()));
531     }
532
533     @Test
534     public void testAddAlreadyExistingLsp() throws UnknownHostException, InterruptedException, ExecutionException {
535         this.listener.onSessionUp(this.session);
536         this.topologyRpcs.addLsp(createAddLspInput());
537         assertEquals(1, this.receivedMsgs.size());
538         assertTrue(this.receivedMsgs.get(0) instanceof Pcinitiate);
539         final Pcinitiate pcinitiate = (Pcinitiate) this.receivedMsgs.get(0);
540         final Requests req = pcinitiate.getPcinitiateMessage().getRequests().get(0);
541         final long srpId = req.getSrp().getOperationId().getValue();
542         final Tlvs tlvs = createLspTlvs(req.getLsp().getPlspId().getValue(), true,
543                 this.testAddress, this.testAddress, this.testAddress, Optional.absent());
544         final Pcrpt pcRpt = MsgBuilderUtil.createPcRtpMessage(new LspBuilder(req.getLsp()).setTlvs(tlvs)
545                 .setPlspId(new PlspId(1L))
546                 .setSync(false)
547                 .setRemove(false)
548                 .setOperational(OperationalStatus.Active)
549                 .build(), Optional.of(MsgBuilderUtil.createSrp(srpId)), MsgBuilderUtil.createPath(req.getEro()
550                 .getSubobject()));
551         this.listener.onMessage(this.session, pcRpt);
552
553         //try to add already existing LSP
554         final AddLspOutput result = this.topologyRpcs.addLsp(createAddLspInput()).get().getResult();
555         assertEquals(FailureType.Unsent, result.getFailure());
556         assertEquals(1, result.getError().size());
557         final ErrorObject errorObject = result.getError().get(0).getErrorObject();
558         assertNotNull(errorObject);
559         assertEquals(PCEPErrors.USED_SYMBOLIC_PATH_NAME, PCEPErrors.forValue(errorObject.getType(),
560                 errorObject.getValue()));
561     }
562
563     @Test
564     @SuppressWarnings("checkstyle:IllegalCatch")
565     public void testPccResponseTimeout() throws Exception {
566         this.listener.onSessionUp(this.session);
567         final Future<RpcResult<AddLspOutput>> addLspResult = this.topologyRpcs.addLsp(createAddLspInput());
568         try {
569             addLspResult.get(2, TimeUnit.SECONDS);
570             fail();
571         } catch (final Exception e) {
572             assertTrue(e instanceof TimeoutException);
573         }
574         Thread.sleep(AbstractPCEPSessionTest.RPC_TIMEOUT);
575         CheckUtil.checkEquals(() -> {
576             final RpcResult<AddLspOutput> rpcResult = addLspResult.get();
577             assertNotNull(rpcResult);
578             assertEquals(rpcResult.getResult().getFailure(), FailureType.Unsent);
579         });
580     }
581
582     @Test
583     public void testDelegatedLspsCountWithDelegation() throws Exception {
584         this.listener.onSessionUp(this.session);
585         this.topologyRpcs.addLsp(createAddLspInput());
586         assertEquals(1, this.receivedMsgs.size());
587         assertTrue(this.receivedMsgs.get(0) instanceof Pcinitiate);
588         final Pcinitiate pcinitiate = (Pcinitiate) this.receivedMsgs.get(0);
589         final Requests req = pcinitiate.getPcinitiateMessage().getRequests().get(0);
590         final long srpId = req.getSrp().getOperationId().getValue();
591         final Tlvs tlvs = createLspTlvs(req.getLsp().getPlspId().getValue(), true,
592                 this.testAddress, this.testAddress, this.testAddress, Optional.absent());
593         //delegate set to true
594         final Pcrpt pcRpt = MsgBuilderUtil.createPcRtpMessage(new LspBuilder(req.getLsp()).setTlvs(tlvs)
595                 .setPlspId(new PlspId(1L))
596                 .setSync(false)
597                 .setRemove(false)
598                 .setOperational(OperationalStatus.Active)
599                 .setDelegate(true)
600                 .build(), Optional.of(MsgBuilderUtil.createSrp(srpId)), MsgBuilderUtil.createPath(
601                 req.getEro().getSubobject()));
602         this.listener.onMessage(this.session, pcRpt);
603         checkEquals(() -> assertEquals(1, this.listener.listenerState.getDelegatedLspsCount().intValue()));
604     }
605
606     @Test
607     public void testDelegatedLspsCountWithoutDelegation() throws Exception {
608         this.listener.onSessionUp(this.session);
609         this.topologyRpcs.addLsp(createAddLspInput());
610         assertEquals(1, this.receivedMsgs.size());
611         assertTrue(this.receivedMsgs.get(0) instanceof Pcinitiate);
612         final Pcinitiate pcinitiate = (Pcinitiate) this.receivedMsgs.get(0);
613         final Requests req = pcinitiate.getPcinitiateMessage().getRequests().get(0);
614         final long srpId = req.getSrp().getOperationId().getValue();
615         final Tlvs tlvs = createLspTlvs(req.getLsp().getPlspId().getValue(), true,
616                 this.testAddress, this.testAddress, this.testAddress, Optional.absent());
617         //delegate set to false
618         final Pcrpt pcRpt = MsgBuilderUtil.createPcRtpMessage(new LspBuilder(req.getLsp()).setTlvs(tlvs)
619                         .setPlspId(
620                                 new PlspId(1L))
621                         .setSync(false)
622                         .setRemove(false)
623                         .setOperational(OperationalStatus.Active)
624                         .setDelegate(false)
625                         .build(), Optional.of(MsgBuilderUtil.createSrp(srpId)),
626                 MsgBuilderUtil.createPath(req.getEro().getSubobject()));
627         this.listener.onMessage(this.session, pcRpt);
628         checkEquals(() -> assertEquals(0, this.listener.listenerState.getDelegatedLspsCount().intValue()));
629     }
630
631     @Override
632     protected Open getLocalPref() {
633         return new OpenBuilder(super.getLocalPref()).setTlvs(new TlvsBuilder().addAugmentation(Tlvs1.class,
634                 new Tlvs1Builder().setStateful(new StatefulBuilder()
635                         .addAugmentation(Stateful1.class, new Stateful1Builder().setInitiation(Boolean.TRUE).build())
636                         .addAugmentation(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller
637                                 .pcep.sync.optimizations.rev171025.Stateful1.class, new org.opendaylight.yang.gen.v1
638                                 .urn.opendaylight.params.xml.ns.yang.controller.pcep.sync.optimizations.rev171025
639                                 .Stateful1Builder().setTriggeredInitialSync(Boolean.TRUE).build())
640                         .build()).build()).build()).build();
641     }
642
643     @Override
644     protected Open getRemotePref() {
645         return getLocalPref();
646     }
647
648     private AddLspInput createAddLspInput() {
649         final ArgumentsBuilder argsBuilder = new ArgumentsBuilder();
650         final Ipv4CaseBuilder ipv4Builder = new Ipv4CaseBuilder();
651         ipv4Builder.setIpv4(new Ipv4Builder().setSourceIpv4Address(new Ipv4Address(this.testAddress))
652                 .setDestinationIpv4Address(new Ipv4Address(this.testAddress)).build());
653         argsBuilder.setEndpointsObj(new EndpointsObjBuilder().setAddressFamily(ipv4Builder.build()).build());
654         argsBuilder.setEro(createEroWithIpPrefixes(Lists.newArrayList(this.eroIpPrefix)));
655         argsBuilder.addAugmentation(Arguments2.class, new Arguments2Builder().setLsp(new LspBuilder()
656                 .setDelegate(true).setAdministrative(true).build()).build());
657         return new AddLspInputBuilder().setName(this.tunnelName).setArguments(argsBuilder.build())
658                 .setNetworkTopologyRef(new NetworkTopologyRef(TOPO_IID)).setNode(this.nodeId).build();
659     }
660 }