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