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;
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;
42 * simple NPE smoke test
44 @RunWith(MockitoJUnitRunner.class)
45 public class ModelDrivenSwitchImplTest {
47 private ModelDrivenSwitchImpl mdSwitchOF10;
48 private ModelDrivenSwitchImpl mdSwitchOF13;
51 private SessionContext context;
53 private ConnectionConductor conductor;
55 private IMessageDispatchService messageDispatchService;
58 * @throws java.lang.Exception
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);
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)}.
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));
85 AddFlowInputBuilder input = new AddFlowInputBuilder();
86 input.setMatch(new MatchBuilder().build());
88 mdSwitchOF10.addFlow(input.build());
89 mdSwitchOF13.addFlow(input.build());
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)}.
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));
105 RemoveFlowInputBuilder input = new RemoveFlowInputBuilder();
106 input.setMatch(new MatchBuilder().build());
108 mdSwitchOF10.removeFlow(input.build());
109 mdSwitchOF13.removeFlow(input.build());
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)}.
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));
125 UpdateFlowInputBuilder input = new UpdateFlowInputBuilder();
126 UpdatedFlowBuilder updatedFlow = new UpdatedFlowBuilder();
127 updatedFlow.setMatch(new MatchBuilder().build());
128 input.setUpdatedFlow(updatedFlow.build());
130 mdSwitchOF10.updateFlow(input.build());
131 mdSwitchOF13.updateFlow(input.build());