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