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;
15 import java.util.concurrent.TimeUnit;
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;
46 import com.google.common.cache.Cache;
47 import com.google.common.cache.CacheBuilder;
50 * simple NPE smoke test
52 @RunWith(MockitoJUnitRunner.class)
53 public class ModelDrivenSwitchImplTest {
55 private ModelDrivenSwitchImpl mdSwitchOF10;
56 private ModelDrivenSwitchImpl mdSwitchOF13;
59 private SessionContext context;
61 private ConnectionConductor conductor;
63 private IMessageDispatchService messageDispatchService;
65 private GetFeaturesOutput features;
67 public static Cache<TransactionKey, Object> bulkTransactionCache = CacheBuilder.newBuilder()
68 .expireAfterWrite(10000, TimeUnit.MILLISECONDS).concurrencyLevel(1).build();
71 * @throws java.lang.Exception
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));
83 OFSessionUtil.getSessionManager().setRpcPool(Executors.newFixedThreadPool(10));
85 mdSwitchOF10 = new ModelDrivenSwitchImpl(null, null, context);
86 mdSwitchOF13 = new ModelDrivenSwitchImpl(null, null, context);
91 * {@link org.opendaylight.openflowplugin.openflow.md.core.sal.ModelDrivenSwitchImpl#addFlow(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput)}
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);
101 messageDispatchService.flowMod(Matchers.any(FlowModInput.class),
102 Matchers.any(SwitchConnectionDistinguisher.class))).thenReturn(Futures.immediateFuture(result));
104 AddFlowInputBuilder input = new AddFlowInputBuilder();
105 input.setMatch(new MatchBuilder().build());
107 mdSwitchOF10.addFlow(input.build());
108 mdSwitchOF13.addFlow(input.build());
113 * {@link org.opendaylight.openflowplugin.openflow.md.core.sal.ModelDrivenSwitchImpl#removeFlow(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput)}
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);
123 messageDispatchService.flowMod(Matchers.any(FlowModInput.class),
124 Matchers.any(SwitchConnectionDistinguisher.class))).thenReturn(Futures.immediateFuture(result));
126 RemoveFlowInputBuilder input = new RemoveFlowInputBuilder();
127 input.setMatch(new MatchBuilder().build());
129 mdSwitchOF10.removeFlow(input.build());
130 mdSwitchOF13.removeFlow(input.build());
135 * {@link org.opendaylight.openflowplugin.openflow.md.core.sal.ModelDrivenSwitchImpl#updateFlow(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput)}
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);
145 messageDispatchService.flowMod(Matchers.any(FlowModInput.class),
146 Matchers.any(SwitchConnectionDistinguisher.class))).thenReturn(Futures.immediateFuture(result));
148 UpdateFlowInputBuilder input = new UpdateFlowInputBuilder();
149 UpdatedFlowBuilder updatedFlow = new UpdatedFlowBuilder();
150 updatedFlow.setMatch(new MatchBuilder().build());
151 input.setUpdatedFlow(updatedFlow.build());
153 mdSwitchOF10.updateFlow(input.build());
154 mdSwitchOF13.updateFlow(input.build());