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