BUG-956 deadlock by rpc invocation - phase2
[openflowplugin.git] / openflowplugin / src / test / java / org / opendaylight / openflowplugin / openflow / md / core / sal / ModelDrivenSwitchImplTest.java
1 /**
2  * Copyright (c) 2013 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.openflow.md.core.sal;
10
11 import java.math.BigInteger;
12 import java.util.Collections;
13 import java.util.Set;
14 import java.util.concurrent.Executors;
15
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 import org.mockito.Matchers;
20 import org.mockito.Mock;
21 import org.mockito.Mockito;
22 import org.mockito.runners.MockitoJUnitRunner;
23 import org.opendaylight.controller.sal.common.util.Rpcs;
24 import org.opendaylight.openflowplugin.openflow.md.OFConstants;
25 import org.opendaylight.openflowplugin.openflow.md.core.ConnectionConductor;
26 import org.opendaylight.openflowplugin.openflow.md.core.SwitchConnectionDistinguisher;
27 import org.opendaylight.openflowplugin.openflow.md.core.session.IMessageDispatchService;
28 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
29 import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInputBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInputBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutputBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlowBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev131103.TransactionId;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
40 import org.opendaylight.yangtools.yang.common.RpcError;
41 import org.opendaylight.yangtools.yang.common.RpcResult;
42
43 import com.google.common.util.concurrent.Futures;
44 import com.google.common.util.concurrent.MoreExecutors;
45
46 /**
47  * simple NPE smoke test
48  */
49 @RunWith(MockitoJUnitRunner.class)
50 public class ModelDrivenSwitchImplTest {
51
52     private ModelDrivenSwitchImpl mdSwitchOF10;
53     private ModelDrivenSwitchImpl mdSwitchOF13;
54
55     @Mock
56     private SessionContext context;
57     @Mock
58     private ConnectionConductor conductor;
59     @Mock
60     private IMessageDispatchService messageDispatchService;
61     @Mock
62     private GetFeaturesOutput features;
63
64     /**
65      * @throws java.lang.Exception
66      */
67     @Before
68     public void setUp() throws Exception {
69         Mockito.when(context.getPrimaryConductor()).thenReturn(conductor);
70         Mockito.when(context.getMessageDispatchService()).thenReturn(messageDispatchService);
71         Mockito.when(conductor.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_0)
72                 .thenReturn(OFConstants.OFP_VERSION_1_3);
73         Mockito.when(context.getFeatures()).thenReturn(features);
74         Mockito.when(features.getDatapathId()).thenReturn(BigInteger.valueOf(1));
75         
76         OFSessionUtil.getSessionManager().setRpcPool(MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10)));
77
78         mdSwitchOF10 = new ModelDrivenSwitchImpl(null, null, context);
79         mdSwitchOF13 = new ModelDrivenSwitchImpl(null, null, context);
80     }
81
82     /**
83      * Test method for
84      * {@link org.opendaylight.openflowplugin.openflow.md.core.sal.ModelDrivenSwitchImpl#addFlow(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput)}
85      * .
86      */
87     @Test
88     public void testAddFlow() {
89         UpdateFlowOutputBuilder updateFlowOutput = new UpdateFlowOutputBuilder();
90         updateFlowOutput.setTransactionId(new TransactionId(new BigInteger("42")));
91         Set<RpcError> errorSet = Collections.emptySet();
92         RpcResult<UpdateFlowOutput> result = Rpcs.getRpcResult(true, updateFlowOutput.build(), errorSet);
93         Mockito.when(
94                 messageDispatchService.flowMod(Matchers.any(FlowModInput.class),
95                         Matchers.any(SwitchConnectionDistinguisher.class))).thenReturn(Futures.immediateFuture(result));
96
97         AddFlowInputBuilder input = new AddFlowInputBuilder();
98         input.setMatch(new MatchBuilder().build());
99
100         mdSwitchOF10.addFlow(input.build());
101         mdSwitchOF13.addFlow(input.build());
102     }
103
104     /**
105      * Test method for
106      * {@link org.opendaylight.openflowplugin.openflow.md.core.sal.ModelDrivenSwitchImpl#removeFlow(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput)}
107      * .
108      */
109     @Test
110     public void testRemoveFlow() {
111         UpdateFlowOutputBuilder updateFlowOutput = new UpdateFlowOutputBuilder();
112         updateFlowOutput.setTransactionId(new TransactionId(new BigInteger("42")));
113         Set<RpcError> errorSet = Collections.emptySet();
114         RpcResult<UpdateFlowOutput> result = Rpcs.getRpcResult(true, updateFlowOutput.build(), errorSet);
115         Mockito.when(
116                 messageDispatchService.flowMod(Matchers.any(FlowModInput.class),
117                         Matchers.any(SwitchConnectionDistinguisher.class))).thenReturn(Futures.immediateFuture(result));
118
119         RemoveFlowInputBuilder input = new RemoveFlowInputBuilder();
120         input.setMatch(new MatchBuilder().build());
121
122         mdSwitchOF10.removeFlow(input.build());
123         mdSwitchOF13.removeFlow(input.build());
124     }
125
126     /**
127      * Test method for
128      * {@link org.opendaylight.openflowplugin.openflow.md.core.sal.ModelDrivenSwitchImpl#updateFlow(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput)}
129      * .
130      */
131     @Test
132     public void testUpdateFlow() {
133         UpdateFlowOutputBuilder updateFlowOutput = new UpdateFlowOutputBuilder();
134         updateFlowOutput.setTransactionId(new TransactionId(new BigInteger("42")));
135         Set<RpcError> errorSet = Collections.emptySet();
136         RpcResult<UpdateFlowOutput> result = Rpcs.getRpcResult(true, updateFlowOutput.build(), errorSet);
137         Mockito.when(
138                 messageDispatchService.flowMod(Matchers.any(FlowModInput.class),
139                         Matchers.any(SwitchConnectionDistinguisher.class))).thenReturn(Futures.immediateFuture(result));
140
141         UpdateFlowInputBuilder input = new UpdateFlowInputBuilder();
142         UpdatedFlowBuilder updatedFlow = new UpdatedFlowBuilder();
143         updatedFlow.setMatch(new MatchBuilder().build());
144         input.setUpdatedFlow(updatedFlow.build());
145
146         mdSwitchOF10.updateFlow(input.build());
147         mdSwitchOF13.updateFlow(input.build());
148     }
149 }