2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.openflow.md.core.sal;
11 import java.math.BigInteger;
12 import java.util.Collections;
14 import java.util.concurrent.Executors;
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;
43 import com.google.common.util.concurrent.Futures;
44 import com.google.common.util.concurrent.MoreExecutors;
47 * simple NPE smoke test
49 @RunWith(MockitoJUnitRunner.class)
50 public class ModelDrivenSwitchImplTest {
52 private ModelDrivenSwitchImpl mdSwitchOF10;
53 private ModelDrivenSwitchImpl mdSwitchOF13;
56 private SessionContext context;
58 private ConnectionConductor conductor;
60 private IMessageDispatchService messageDispatchService;
62 private GetFeaturesOutput features;
65 * @throws java.lang.Exception
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));
76 OFSessionUtil.getSessionManager().setRpcPool(MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10)));
78 mdSwitchOF10 = new ModelDrivenSwitchImpl(null, null, context);
79 mdSwitchOF13 = new ModelDrivenSwitchImpl(null, null, context);
84 * {@link org.opendaylight.openflowplugin.openflow.md.core.sal.ModelDrivenSwitchImpl#addFlow(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput)}
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);
94 messageDispatchService.flowMod(Matchers.any(FlowModInput.class),
95 Matchers.any(SwitchConnectionDistinguisher.class))).thenReturn(Futures.immediateFuture(result));
97 AddFlowInputBuilder input = new AddFlowInputBuilder();
98 input.setMatch(new MatchBuilder().build());
100 mdSwitchOF10.addFlow(input.build());
101 mdSwitchOF13.addFlow(input.build());
106 * {@link org.opendaylight.openflowplugin.openflow.md.core.sal.ModelDrivenSwitchImpl#removeFlow(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput)}
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);
116 messageDispatchService.flowMod(Matchers.any(FlowModInput.class),
117 Matchers.any(SwitchConnectionDistinguisher.class))).thenReturn(Futures.immediateFuture(result));
119 RemoveFlowInputBuilder input = new RemoveFlowInputBuilder();
120 input.setMatch(new MatchBuilder().build());
122 mdSwitchOF10.removeFlow(input.build());
123 mdSwitchOF13.removeFlow(input.build());
128 * {@link org.opendaylight.openflowplugin.openflow.md.core.sal.ModelDrivenSwitchImpl#updateFlow(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput)}
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);
138 messageDispatchService.flowMod(Matchers.any(FlowModInput.class),
139 Matchers.any(SwitchConnectionDistinguisher.class))).thenReturn(Futures.immediateFuture(result));
141 UpdateFlowInputBuilder input = new UpdateFlowInputBuilder();
142 UpdatedFlowBuilder updatedFlow = new UpdatedFlowBuilder();
143 updatedFlow.setMatch(new MatchBuilder().build());
144 input.setUpdatedFlow(updatedFlow.build());
146 mdSwitchOF10.updateFlow(input.build());
147 mdSwitchOF13.updateFlow(input.build());