OPNFLWPLUG-1032: Neon-MRI: Bump odlparent, yangtools, mdsal
[openflowplugin.git] / applications / forwardingrules-sync / src / test / java / org / opendaylight / openflowplugin / applications / frsync / impl / strategy / FlowForwarderTest.java
1 /*
2  * Copyright (c) 2016 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.openflowplugin.applications.frsync.impl.strategy;
10
11 import java.math.BigInteger;
12 import java.util.Collections;
13 import java.util.concurrent.Future;
14 import java.util.concurrent.TimeUnit;
15 import org.junit.Assert;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 import org.mockito.ArgumentCaptor;
20 import org.mockito.ArgumentMatchers;
21 import org.mockito.Captor;
22 import org.mockito.Mock;
23 import org.mockito.Mockito;
24 import org.mockito.runners.MockitoJUnitRunner;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.DropActionCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutputBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowOutput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowOutputBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutputBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlow;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlow;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
58 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
59 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
60 import org.opendaylight.yangtools.yang.common.RpcResult;
61 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
62
63 /**
64  * Test for {@link FlowForwarder}.
65  */
66 @RunWith(MockitoJUnitRunner.class)
67 public class FlowForwarderTest {
68     private final NodeKey s1Key = new NodeKey(new NodeId("S1"));
69     private final TableKey tableKey = new TableKey((short) 2);
70     private final FlowId flowId = new FlowId("test_Flow");
71     private final FlowKey flowKey = new FlowKey(flowId);
72     private final Match emptyMatch = new MatchBuilder().build();
73     private final Flow flow = new FlowBuilder()
74             .setId(flowId)
75             .setTableId((short) 2)
76             .setMatch(emptyMatch)
77             .build();
78
79     private final KeyedInstanceIdentifier<Node, NodeKey> nodePath = InstanceIdentifier.create(Nodes.class)
80             .child(Node.class, s1Key);
81     private final InstanceIdentifier<FlowCapableNode> flowCapableNodePath = nodePath
82             .augmentation(FlowCapableNode.class);
83     private final InstanceIdentifier<Table> tableII = flowCapableNodePath.child(Table.class, tableKey);
84     private final InstanceIdentifier<Flow> flowPath = tableII.child(Flow.class, flowKey);
85
86     private FlowForwarder flowForwarder;
87
88     @Mock
89     private SalFlowService salFlowService;
90     @Captor
91     private ArgumentCaptor<AddFlowInput> addFlowInputCpt;
92     @Captor
93     private ArgumentCaptor<UpdateFlowInput> updateFlowInputCpt;
94     @Captor
95     private ArgumentCaptor<RemoveFlowInput> removeFlowInputCpt;
96
97
98     @Before
99     public void setUp() throws Exception {
100         flowForwarder = new FlowForwarder(salFlowService);
101     }
102
103     @Test
104     public void addTest() throws Exception {
105         Mockito.when(salFlowService.addFlow(addFlowInputCpt.capture())).thenReturn(
106                 RpcResultBuilder.success(
107                         new AddFlowOutputBuilder()
108                                 .setTransactionId(new TransactionId(BigInteger.ONE))
109                                 .build()).buildFuture());
110
111         final Future<RpcResult<AddFlowOutput>> addResult = flowForwarder.add(flowPath, flow, flowCapableNodePath);
112
113         Mockito.verify(salFlowService).addFlow(ArgumentMatchers.<AddFlowInput>any());
114         final AddFlowInput flowInput = addFlowInputCpt.getValue();
115         Assert.assertEquals(2, flowInput.getTableId().shortValue());
116         Assert.assertEquals(emptyMatch, flowInput.getMatch());
117         Assert.assertEquals(null, flowInput.getInstructions());
118         Assert.assertEquals(nodePath, flowInput.getNode().getValue());
119         Assert.assertEquals(flowPath, flowInput.getFlowRef().getValue());
120         Assert.assertEquals(null, flowInput.isStrict());
121
122
123         final RpcResult<AddFlowOutput> addFlowOutputRpcResult = addResult.get(2, TimeUnit.SECONDS);
124         Assert.assertTrue(addFlowOutputRpcResult.isSuccessful());
125         final AddFlowOutput resultValue = addFlowOutputRpcResult.getResult();
126         Assert.assertEquals(1, resultValue.getTransactionId().getValue().intValue());
127     }
128
129     @Test
130     public void updateTest() throws Exception {
131         Mockito.when(salFlowService.updateFlow(updateFlowInputCpt.capture())).thenReturn(
132                 RpcResultBuilder.success(
133                         new UpdateFlowOutputBuilder()
134                                 .setTransactionId(new TransactionId(BigInteger.ONE))
135                                 .build()).buildFuture());
136
137         final Instructions originalInstructions = new InstructionsBuilder()
138                 .setInstruction(Collections.singletonList(new InstructionBuilder()
139                         .setInstruction(new ApplyActionsCaseBuilder()
140                                 .setApplyActions(new ApplyActionsBuilder()
141                                         .setAction(Collections.singletonList(new ActionBuilder()
142                                                 .setAction(new DropActionCaseBuilder()
143                                                         .build())
144                                                 .build())
145                                         ).build()
146                                 ).build())
147                         .build())
148                 ).build();
149
150         final Flow flowUpdated = new FlowBuilder(flow)
151                 .setInstructions(originalInstructions)
152                 .setMatch(new MatchBuilder().build())
153                 .build();
154
155         final Future<RpcResult<UpdateFlowOutput>> updateResult = flowForwarder.update(flowPath, flow,
156                 flowUpdated, flowCapableNodePath);
157
158         Mockito.verify(salFlowService).updateFlow(ArgumentMatchers.<UpdateFlowInput>any());
159         final UpdateFlowInput updateFlowInput = updateFlowInputCpt.getValue();
160         final OriginalFlow flowOrigInput = updateFlowInput.getOriginalFlow();
161         final UpdatedFlow flowInput = updateFlowInput.getUpdatedFlow();
162
163         Assert.assertEquals(nodePath, updateFlowInput.getNode().getValue());
164         Assert.assertEquals(flowPath, updateFlowInput.getFlowRef().getValue());
165
166         Assert.assertEquals(2, flowInput.getTableId().shortValue());
167         Assert.assertEquals(emptyMatch, flowInput.getMatch());
168         Assert.assertEquals(originalInstructions, flowInput.getInstructions());
169         Assert.assertEquals(true, flowInput.isStrict());
170
171         Assert.assertEquals(2, flowOrigInput.getTableId().shortValue());
172         Assert.assertEquals(emptyMatch, flowOrigInput.getMatch());
173         Assert.assertEquals(null, flowOrigInput.getInstructions());
174         Assert.assertEquals(true, flowOrigInput.isStrict());
175
176
177         final RpcResult<UpdateFlowOutput> updateFlowOutputRpcResult = updateResult.get(2, TimeUnit.SECONDS);
178         Assert.assertTrue(updateFlowOutputRpcResult.isSuccessful());
179         final UpdateFlowOutput resultValue = updateFlowOutputRpcResult.getResult();
180         Assert.assertEquals(1, resultValue.getTransactionId().getValue().intValue());
181     }
182
183     @Test
184     public void removeTest() throws Exception {
185         Mockito.when(salFlowService.removeFlow(removeFlowInputCpt.capture())).thenReturn(
186                 RpcResultBuilder.success(
187                         new RemoveFlowOutputBuilder()
188                                 .setTransactionId(new TransactionId(BigInteger.ONE))
189                                 .build()).buildFuture());
190
191         final Flow removeFlow = new FlowBuilder(flow).build();
192         final Future<RpcResult<RemoveFlowOutput>> removeResult = flowForwarder.remove(flowPath,
193                 removeFlow, flowCapableNodePath);
194
195         Mockito.verify(salFlowService).removeFlow(ArgumentMatchers.<RemoveFlowInput>any());
196         final RemoveFlowInput flowInput = removeFlowInputCpt.getValue();
197         Assert.assertEquals(2, flowInput.getTableId().shortValue());
198         Assert.assertEquals(emptyMatch, flowInput.getMatch());
199         Assert.assertEquals(null, flowInput.getInstructions());
200         Assert.assertEquals(true, flowInput.isStrict());
201
202
203         final RpcResult<RemoveFlowOutput> removeFlowOutputRpcResult = removeResult.get(2, TimeUnit.SECONDS);
204         Assert.assertTrue(removeFlowOutputRpcResult.isSuccessful());
205         final RemoveFlowOutput resultValue = removeFlowOutputRpcResult.getResult();
206         Assert.assertEquals(1, resultValue.getTransactionId().getValue().intValue());
207     }
208 }