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.TimeUnit;
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;
44 import com.google.common.cache.Cache;
45 import com.google.common.cache.CacheBuilder;
48 * simple NPE smoke test
50 @RunWith(MockitoJUnitRunner.class)
51 public class ModelDrivenSwitchImplTest {
53 private ModelDrivenSwitchImpl mdSwitchOF10;
54 private ModelDrivenSwitchImpl mdSwitchOF13;
57 private SessionContext context;
59 private ConnectionConductor conductor;
61 private IMessageDispatchService messageDispatchService;
63 private GetFeaturesOutput features;
65 public static Cache<TransactionKey, Object> bulkTransactionCache = CacheBuilder.newBuilder()
66 .expireAfterWrite(10000, TimeUnit.MILLISECONDS).concurrencyLevel(1).build();
69 * @throws java.lang.Exception
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));
81 mdSwitchOF10 = new ModelDrivenSwitchImpl(null, null, context);
82 mdSwitchOF13 = new ModelDrivenSwitchImpl(null, null, context);
87 * {@link org.opendaylight.openflowplugin.openflow.md.core.sal.ModelDrivenSwitchImpl#addFlow(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput)}
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);
97 messageDispatchService.flowMod(Matchers.any(FlowModInput.class),
98 Matchers.any(SwitchConnectionDistinguisher.class))).thenReturn(Futures.immediateFuture(result));
100 AddFlowInputBuilder input = new AddFlowInputBuilder();
101 input.setMatch(new MatchBuilder().build());
103 mdSwitchOF10.addFlow(input.build());
104 mdSwitchOF13.addFlow(input.build());
109 * {@link org.opendaylight.openflowplugin.openflow.md.core.sal.ModelDrivenSwitchImpl#removeFlow(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput)}
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);
119 messageDispatchService.flowMod(Matchers.any(FlowModInput.class),
120 Matchers.any(SwitchConnectionDistinguisher.class))).thenReturn(Futures.immediateFuture(result));
122 RemoveFlowInputBuilder input = new RemoveFlowInputBuilder();
123 input.setMatch(new MatchBuilder().build());
125 mdSwitchOF10.removeFlow(input.build());
126 mdSwitchOF13.removeFlow(input.build());
131 * {@link org.opendaylight.openflowplugin.openflow.md.core.sal.ModelDrivenSwitchImpl#updateFlow(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput)}
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);
141 messageDispatchService.flowMod(Matchers.any(FlowModInput.class),
142 Matchers.any(SwitchConnectionDistinguisher.class))).thenReturn(Futures.immediateFuture(result));
144 UpdateFlowInputBuilder input = new UpdateFlowInputBuilder();
145 UpdatedFlowBuilder updatedFlow = new UpdatedFlowBuilder();
146 updatedFlow.setMatch(new MatchBuilder().build());
147 input.setUpdatedFlow(updatedFlow.build());
149 mdSwitchOF10.updateFlow(input.build());
150 mdSwitchOF13.updateFlow(input.build());