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