2 * Copyright (c) 2014 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 static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListeningExecutorService;
16 import java.math.BigInteger;
17 import java.util.Collections;
18 import org.junit.After;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.mockito.Matchers;
23 import org.mockito.Mock;
24 import org.mockito.Mockito;
25 import org.mockito.runners.MockitoJUnitRunner;
26 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
27 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
28 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
29 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
30 import org.opendaylight.openflowplugin.api.OFConstants;
31 import org.opendaylight.openflowplugin.api.openflow.md.ModelDrivenSwitch;
32 import org.opendaylight.openflowplugin.api.openflow.md.core.ConnectionConductor;
33 import org.opendaylight.openflowplugin.api.openflow.md.core.NotificationEnqueuer;
34 import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
35 import org.opendaylight.openflowplugin.api.openflow.md.core.session.IMessageDispatchService;
36 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SwitchSessionKeyOF;
37 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
38 import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContextOFImpl;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutputBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
46 import org.opendaylight.yangtools.concepts.CompositeObjectRegistration;
47 import org.opendaylight.yangtools.concepts.Registration;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
49 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
50 import org.opendaylight.yangtools.yang.common.RpcResult;
51 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
54 * Created by Martin Bobak mbobak@cisco.com on 8/26/14.
56 @RunWith(MockitoJUnitRunner.class)
57 public class SalRegistrationManagerTest {
60 private static final BigInteger dataPathId = BigInteger.ONE;
62 private SalRegistrationManager salRegistrationManager;
64 private SessionContextOFImpl context;
66 private ConnectionConductor conductor;
68 private IMessageDispatchService messageDispatchService;
70 private GetFeaturesOutput features;
72 private BindingAwareBroker.ProviderContext providerContext;
74 private NotificationEnqueuer notificationEnqueuer;
76 private ListeningExecutorService rpcPool;
78 private NotificationProviderService notificationProviderService;
80 private RpcProviderRegistry rpcProviderRegistry;
82 private DataBroker dataBroker;
84 private ModelDrivenSwitch mdSwitchOF13;
86 CompositeObjectRegistration<ModelDrivenSwitch> registration;
91 OFSessionUtil.getSessionManager().setRpcPool(rpcPool);
92 Mockito.when(conductor.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_0)
93 .thenReturn(OFConstants.OFP_VERSION_1_3);
94 context = new SessionContextOFImpl();
95 context.setPrimaryConductor(conductor);
96 Mockito.when(features.getDatapathId()).thenReturn(BigInteger.valueOf(1));
97 Mockito.when(features.getVersion()).thenReturn((short) 1);
98 context.setFeatures(features);
99 context.setNotificationEnqueuer(notificationEnqueuer);
101 mdSwitchOF13 = new ModelDrivenSwitchImpl(null, null, context);
102 registration = new CompositeObjectRegistration<>(mdSwitchOF13, Collections.<Registration>emptyList());
103 context.setProviderRegistration(registration);
105 UpdateFlowOutputBuilder updateFlowOutput = new UpdateFlowOutputBuilder();
106 RpcResult<UpdateFlowOutput> result = RpcResultBuilder.success(updateFlowOutput.build()).build();
109 messageDispatchService.flowMod(Matchers.any(FlowModInput.class),
110 Matchers.any(SwitchConnectionDistinguisher.class))).thenReturn(Futures.immediateFuture(result));
112 salRegistrationManager = new SalRegistrationManager();
113 salRegistrationManager.setPublishService(notificationProviderService);
114 salRegistrationManager.setDataService(dataBroker);
115 salRegistrationManager.setRpcProviderRegistry(rpcProviderRegistry);
117 salRegistrationManager.init();
122 * free sesion manager
125 public void tearDown() {
126 OFSessionUtil.releaseSessionManager();
130 * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SalRegistrationManager#identifierFromDatapathId(java.math.BigInteger)}
133 public void testIdentifierFromDatapathId() {
134 InstanceIdentifier<Node> node = SalRegistrationManager.identifierFromDatapathId(dataPathId);
136 assertEquals("NodeKey [_id=Uri [_value=openflow:1]]", ((KeyedInstanceIdentifier<?, ?>) node).getKey().toString());
140 * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SalRegistrationManager#nodeKeyFromDatapathId(java.math.BigInteger)}
143 public void testNodeKeyFromDatapathId() {
144 NodeKey nodeKey = SalRegistrationManager.nodeKeyFromDatapathId(dataPathId);
145 assertNotNull(nodeKey);
146 assertEquals("openflow:1", nodeKey.getId().getValue());
150 * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SalRegistrationManager#nodeIdFromDatapathId(java.math.BigInteger)}
153 public void testNodeIdFromDatapathId() {
154 NodeId nodeId = SalRegistrationManager.nodeIdFromDatapathId(dataPathId);
155 assertNotNull(nodeId);
156 assertEquals("openflow:1", nodeId.getValue());
160 * Test for {@link SalRegistrationManager#getSessionManager()}
163 public void testGetSessionManager() {
164 assertNotNull(salRegistrationManager.getPublishService());
169 * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SalRegistrationManager#onSessionRemoved(org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext)}
172 public void testOnSessionRemoved() {
173 assertNotNull(context.getProviderRegistration());
174 salRegistrationManager.onSessionRemoved(context);
175 assertNull(context.getProviderRegistration());
179 * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SalRegistrationManager#onSessionAdded(org.opendaylight.openflowplugin.api.openflow.md.core.session.SwitchSessionKeyOF, org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext)}
181 public void testOnAdded() {
182 SwitchSessionKeyOF switchSessionKeyOF = new SwitchSessionKeyOF();
183 salRegistrationManager.onSessionAdded(switchSessionKeyOF, context);