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;
52 import org.opendaylight.openflowplugin.openflow.md.core.role.OfEntityManager;
53 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
56 * Created by Martin Bobak mbobak@cisco.com on 8/26/14.
58 @RunWith(MockitoJUnitRunner.class)
59 public class SalRegistrationManagerTest {
62 private static final BigInteger dataPathId = BigInteger.ONE;
64 private SalRegistrationManager salRegistrationManager;
66 private SessionContextOFImpl context;
68 private ConnectionConductor conductor;
70 private IMessageDispatchService messageDispatchService;
72 private GetFeaturesOutput features;
74 private BindingAwareBroker.ProviderContext providerContext;
76 private NotificationEnqueuer notificationEnqueuer;
78 private ListeningExecutorService rpcPool;
80 private NotificationProviderService notificationProviderService;
82 private RpcProviderRegistry rpcProviderRegistry;
84 private DataBroker dataBroker;
86 private EntityOwnershipService entityOwnershipService;
89 private ModelDrivenSwitchImpl ofSwitch;
91 private ModelDrivenSwitch mdSwitchOF13;
94 CompositeObjectRegistration<ModelDrivenSwitch> registration;
99 OFSessionUtil.getSessionManager().setRpcPool(rpcPool);
100 Mockito.when(conductor.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_0)
101 .thenReturn(OFConstants.OFP_VERSION_1_3);
102 context = new SessionContextOFImpl();
103 context.setPrimaryConductor(conductor);
104 Mockito.when(features.getDatapathId()).thenReturn(BigInteger.valueOf(1));
105 Mockito.when(features.getVersion()).thenReturn((short) 1);
106 context.setFeatures(features);
107 context.setNotificationEnqueuer(notificationEnqueuer);
109 OfEntityManager entManager = new OfEntityManager(entityOwnershipService);
110 mdSwitchOF13 = new ModelDrivenSwitchImpl(null, null, context);
111 registration = new CompositeObjectRegistration<>(mdSwitchOF13, Collections.<Registration>emptyList());
112 context.setProviderRegistration(registration);
114 UpdateFlowOutputBuilder updateFlowOutput = new UpdateFlowOutputBuilder();
115 RpcResult<UpdateFlowOutput> result = RpcResultBuilder.success(updateFlowOutput.build()).build();
118 messageDispatchService.flowMod(Matchers.any(FlowModInput.class),
119 Matchers.any(SwitchConnectionDistinguisher.class))).thenReturn(Futures.immediateFuture(result));
121 salRegistrationManager = new SalRegistrationManager();
122 salRegistrationManager.setPublishService(notificationProviderService);
123 salRegistrationManager.setDataService(dataBroker);
124 salRegistrationManager.setRpcProviderRegistry(rpcProviderRegistry);
125 salRegistrationManager.setOfEntityManager(entManager);
127 salRegistrationManager.init();
132 * free sesion manager
135 public void tearDown() {
136 OFSessionUtil.releaseSessionManager();
140 * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SalRegistrationManager#identifierFromDatapathId(java.math.BigInteger)}
143 public void testIdentifierFromDatapathId() {
144 InstanceIdentifier<Node> node = SalRegistrationManager.identifierFromDatapathId(dataPathId);
146 assertEquals("NodeKey [_id=Uri [_value=openflow:1]]", ((KeyedInstanceIdentifier<?, ?>) node).getKey().toString());
150 * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SalRegistrationManager#nodeKeyFromDatapathId(java.math.BigInteger)}
153 public void testNodeKeyFromDatapathId() {
154 NodeKey nodeKey = SalRegistrationManager.nodeKeyFromDatapathId(dataPathId);
155 assertNotNull(nodeKey);
156 assertEquals("openflow:1", nodeKey.getId().getValue());
160 * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SalRegistrationManager#nodeIdFromDatapathId(java.math.BigInteger)}
163 public void testNodeIdFromDatapathId() {
164 NodeId nodeId = SalRegistrationManager.nodeIdFromDatapathId(dataPathId);
165 assertNotNull(nodeId);
166 assertEquals("openflow:1", nodeId.getValue());
170 * Test for {@link SalRegistrationManager#getSessionManager()}
173 public void testGetSessionManager() {
174 assertNotNull(salRegistrationManager.getPublishService());
179 * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SalRegistrationManager#onSessionRemoved(org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext)}
182 public void testOnSessionRemoved() {
183 // assertNotNull(context.getProviderRegistration());
184 // salRegistrationManager.onSessionAdded(null,context);
185 // salRegistrationManager.onSessionRemoved(context);
186 // assertNull(context.getProviderRegistration());
190 * 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)}
192 public void testOnAdded() {
193 SwitchSessionKeyOF switchSessionKeyOF = new SwitchSessionKeyOF();
194 salRegistrationManager.onSessionAdded(switchSessionKeyOF, context);