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