a1d01c385d319e90ea5a035385ab7b6bb02c2292
[bgpcep.git] / pcep / topology / topology-provider / src / test / java / org / opendaylight / bgpcep / pcep / topology / provider / TopologyProgrammingTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.bgpcep.pcep.topology.provider;
9
10 import static org.mockito.ArgumentMatchers.any;
11 import static org.mockito.Mockito.doAnswer;
12 import static org.mockito.Mockito.doNothing;
13 import static org.mockito.Mockito.doReturn;
14
15 import com.google.common.util.concurrent.ListenableFuture;
16 import java.util.concurrent.Executor;
17 import org.junit.Assert;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.mockito.Mock;
21 import org.mockito.Mockito;
22 import org.opendaylight.bgpcep.pcep.topology.provider.TopologyProgrammingTest.MockedTopologySessionListenerFactory;
23 import org.opendaylight.bgpcep.programming.spi.Instruction;
24 import org.opendaylight.bgpcep.programming.spi.InstructionScheduler;
25 import org.opendaylight.protocol.pcep.PCEPSession;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.Tlvs1;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.Tlvs1Builder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev171025.stateful.capability.tlv.StatefulBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.Open;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.InstructionStatus;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.SubmitInstructionInput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev150720.instruction.status.changed.Details;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.programming.rev171025.SubmitAddLspInputBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.programming.rev171025.SubmitEnsureLspOperationalInputBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.programming.rev171025.SubmitRemoveLspInputBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.programming.rev171025.SubmitTriggerSyncInputBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.programming.rev171025.SubmitUpdateLspInputBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.AddLspArgs;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.AddLspOutput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.EnsureLspOperationalInput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.EnsureLspOperationalOutput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.RemoveLspArgs;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.RemoveLspOutput;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.TriggerSyncArgs;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.TriggerSyncOutput;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.UpdateLspArgs;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.UpdateLspOutput;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev171025.ensure.lsp.operational.args.ArgumentsBuilder;
50 import org.opendaylight.yangtools.yang.common.RpcResult;
51
52 public class TopologyProgrammingTest extends AbstractPCEPSessionTest<MockedTopologySessionListenerFactory> {
53
54     private static final String NAME = "test-tunnel";
55     private static Stateful07TopologySessionListener listener;
56
57     @Mock
58     private InstructionScheduler scheduler;
59     @Mock
60     private ListenableFuture<Instruction> instructionFuture;
61     @Mock
62     private Instruction instruction;
63
64     private AddLspArgs addLspArgs;
65     private UpdateLspArgs updateLspArgs;
66     private RemoveLspArgs removeLspArgs;
67     private TriggerSyncArgs triggerSyncArgs;
68     private EnsureLspOperationalInput ensureLspInput;
69
70     @Mock
71     private ListenableFuture<RpcResult<AddLspOutput>> futureAddLspOutput;
72     @Mock
73     private ListenableFuture<RpcResult<UpdateLspOutput>> futureUpdateLspOutput;
74     @Mock
75     private ListenableFuture<RpcResult<RemoveLspOutput>> futureRemoveLspOutput;
76     @Mock
77     private ListenableFuture<RpcResult<TriggerSyncOutput>> futureTriggerSyncOutput;
78     @Mock
79     private ListenableFuture<RpcResult<EnsureLspOperationalOutput>> futureEnsureLspOutput;
80
81     private TopologyProgramming topologyProgramming;
82
83     @Override
84     @Before
85     public void setUp() throws Exception {
86         super.setUp();
87         doReturn(true).when(this.instruction).checkedExecutionStart();
88         doNothing().when(this.instruction).executionCompleted(InstructionStatus.Failed, null);
89         doAnswer(invocation -> {
90             final Runnable callback = (Runnable) invocation.getArguments()[0];
91             callback.run();
92             return null;
93         }).when(this.instructionFuture).addListener(any(Runnable.class), any(Executor.class));
94         doAnswer(invocation -> {
95             final Runnable callback = (Runnable) invocation.getArguments()[0];
96             callback.run();
97             return null;
98         }).when(this.futureAddLspOutput).addListener(any(Runnable.class), any(Executor.class));
99         doAnswer(invocation -> {
100             final Runnable callback = (Runnable) invocation.getArguments()[0];
101             callback.run();
102             return null;
103         }).when(this.futureUpdateLspOutput).addListener(any(Runnable.class), any(Executor.class));
104         doAnswer(invocation -> {
105             final Runnable callback = (Runnable) invocation.getArguments()[0];
106             callback.run();
107             return null;
108         }).when(this.futureRemoveLspOutput).addListener(any(Runnable.class), any(Executor.class));
109         doAnswer(invocation -> {
110             final Runnable callback = (Runnable) invocation.getArguments()[0];
111             callback.run();
112             return null;
113         }).when(this.futureTriggerSyncOutput).addListener(any(Runnable.class), any(Executor.class));
114         doAnswer(invocation -> {
115             final Runnable callback = (Runnable) invocation.getArguments()[0];
116             callback.run();
117             return null;
118         }).when(this.futureEnsureLspOutput).addListener(any(Runnable.class), any(Executor.class));
119         doAnswer(invocation -> {
120             TopologyProgrammingTest.this.addLspArgs = (AddLspArgs) invocation.getArguments()[0];
121             return TopologyProgrammingTest.this.futureAddLspOutput;
122         }).when(listener).addLsp(any());
123         doAnswer(invocation -> {
124             TopologyProgrammingTest.this.updateLspArgs = (UpdateLspArgs) invocation.getArguments()[0];
125             return TopologyProgrammingTest.this.futureUpdateLspOutput;
126         }).when(listener).updateLsp(any());
127         doAnswer(invocation -> {
128             TopologyProgrammingTest.this.removeLspArgs = (RemoveLspArgs) invocation.getArguments()[0];
129             return TopologyProgrammingTest.this.futureRemoveLspOutput;
130         }).when(listener).removeLsp(any());
131         doAnswer(invocation -> {
132             TopologyProgrammingTest.this.triggerSyncArgs = (TriggerSyncArgs) invocation.getArguments()[0];
133             return TopologyProgrammingTest.this.futureTriggerSyncOutput;
134         }).when(listener).triggerSync(any());
135         doAnswer(invocation -> {
136             TopologyProgrammingTest.this.ensureLspInput = (EnsureLspOperationalInput) invocation.getArguments()[0];
137             return TopologyProgrammingTest.this.futureEnsureLspOutput;
138         }).when(listener).ensureLspOperational(any(EnsureLspOperationalInput.class));
139         doNothing().when(listener).close();
140         doReturn(this.instruction).when(this.instructionFuture).get();
141         doReturn(true).when(this.instructionFuture).isDone();
142         doNothing().when(this.instruction).executionCompleted(any(InstructionStatus.class), any(Details.class));
143         doReturn(this.instructionFuture).when(this.scheduler).scheduleInstruction(any(SubmitInstructionInput.class));
144         this.topologyProgramming = new TopologyProgramming(this.scheduler, this.manager);
145         final PCEPSession session = getPCEPSession(getLocalPref(), getRemotePref());
146         listener.onSessionUp(session);
147     }
148
149     @Test
150     public void testSubmitAddLsp() {
151         final SubmitAddLspInputBuilder inputBuilder = new SubmitAddLspInputBuilder();
152         inputBuilder.setName(NAME);
153         inputBuilder.setNode(this.nodeId);
154         inputBuilder.setArguments(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep
155                 .rev171025.add.lsp.args.ArgumentsBuilder().build());
156         this.topologyProgramming.submitAddLsp(inputBuilder.build());
157         Assert.assertNotNull(this.addLspArgs);
158         Assert.assertEquals(NAME, this.addLspArgs.getName());
159         Assert.assertEquals(this.nodeId, this.addLspArgs.getNode());
160     }
161
162     @Test
163     public void testSubmitUpdateLsp() {
164         final SubmitUpdateLspInputBuilder inputBuilder = new SubmitUpdateLspInputBuilder();
165         inputBuilder.setName(NAME);
166         inputBuilder.setNode(this.nodeId);
167         this.topologyProgramming.submitUpdateLsp(inputBuilder.build());
168         Assert.assertNotNull(this.updateLspArgs);
169         Assert.assertEquals(NAME, this.updateLspArgs.getName());
170         Assert.assertEquals(this.nodeId, this.updateLspArgs.getNode());
171     }
172
173     @Test
174     public void testSubmitEnsureLsp() {
175         final SubmitEnsureLspOperationalInputBuilder inputBuilder = new SubmitEnsureLspOperationalInputBuilder();
176         inputBuilder.setName(NAME);
177         inputBuilder.setNode(this.nodeId);
178         inputBuilder.setArguments(new ArgumentsBuilder().build());
179         this.topologyProgramming.submitEnsureLspOperational(inputBuilder.build());
180         Assert.assertNotNull(this.ensureLspInput);
181         Assert.assertEquals(NAME, this.ensureLspInput.getName());
182         Assert.assertEquals(this.nodeId, this.ensureLspInput.getNode());
183     }
184
185     @Test
186     public void testSubmitRemoveLsp() {
187         final SubmitRemoveLspInputBuilder inputBuilder = new SubmitRemoveLspInputBuilder();
188         inputBuilder.setName(NAME);
189         inputBuilder.setNode(this.nodeId);
190         this.topologyProgramming.submitRemoveLsp(inputBuilder.build());
191         Assert.assertNotNull(this.removeLspArgs);
192         Assert.assertEquals(NAME, this.removeLspArgs.getName());
193         Assert.assertEquals(this.nodeId, this.removeLspArgs.getNode());
194     }
195
196     @Test
197     public void testSubmitTriggerSync() {
198         final SubmitTriggerSyncInputBuilder inputBuilder = new SubmitTriggerSyncInputBuilder();
199         inputBuilder.setName(NAME);
200         inputBuilder.setNode(this.nodeId);
201         this.topologyProgramming.submitTriggerSync(inputBuilder.build());
202         Assert.assertNotNull(this.triggerSyncArgs);
203         Assert.assertEquals(NAME, this.triggerSyncArgs.getName());
204         Assert.assertEquals(this.nodeId, this.triggerSyncArgs.getNode());
205     }
206
207     protected static final class MockedTopologySessionListenerFactory implements TopologySessionListenerFactory {
208         @Override
209         public TopologySessionListener createTopologySessionListener(final ServerSessionManager manager) {
210             listener = Mockito.spy(new Stateful07TopologySessionListener(manager));
211             return listener;
212         }
213     }
214
215     @Override
216     protected Open getLocalPref() {
217         return new OpenBuilder(super.getLocalPref()).setTlvs(new org.opendaylight.yang.gen.v1.urn.opendaylight.params
218                 .xml.ns.yang.pcep.types.rev131005.open.object.open.TlvsBuilder().addAugmentation(Tlvs1.class,
219                 new Tlvs1Builder().setStateful(new StatefulBuilder().build()).build()).build()).build();
220     }
221 }