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;
14 import com.google.common.util.concurrent.Futures;
15 import java.math.BigInteger;
16 import java.util.Collections;
18 import java.util.concurrent.ExecutorService;
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.sal.binding.api.BindingAwareBroker;
27 import org.opendaylight.controller.sal.binding.api.NotificationListener;
28 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
29 import org.opendaylight.controller.sal.common.util.Rpcs;
30 import org.opendaylight.openflowplugin.openflow.md.ModelDrivenSwitch;
31 import org.opendaylight.openflowplugin.openflow.md.OFConstants;
32 import org.opendaylight.openflowplugin.openflow.md.core.ConnectionConductor;
33 import org.opendaylight.openflowplugin.openflow.md.core.SwitchConnectionDistinguisher;
34 import org.opendaylight.openflowplugin.openflow.md.core.session.IMessageDispatchService;
35 import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext;
36 import org.opendaylight.openflowplugin.openflow.md.core.session.SwitchSessionKeyOF;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutputBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
45 import org.opendaylight.yangtools.concepts.CompositeObjectRegistration;
46 import org.opendaylight.yangtools.concepts.ListenerRegistration;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
49 import org.opendaylight.yangtools.yang.binding.Notification;
50 import org.opendaylight.yangtools.yang.common.RpcError;
51 import org.opendaylight.yangtools.yang.common.RpcResult;
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 SessionContext context;
66 private ConnectionConductor conductor;
68 private IMessageDispatchService messageDispatchService;
70 private GetFeaturesOutput features;
72 private BindingAwareBroker.ProviderContext providerContext;
74 private ModelDrivenSwitch mdSwitchOF13;
76 CompositeObjectRegistration<ModelDrivenSwitch> registration;
82 Mockito.when(context.getPrimaryConductor()).thenReturn(conductor);
83 Mockito.when(context.getMessageDispatchService()).thenReturn(messageDispatchService);
84 Mockito.when(conductor.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_0)
85 .thenReturn(OFConstants.OFP_VERSION_1_3);
86 Mockito.when(context.getFeatures()).thenReturn(features);
88 mdSwitchOF13 = new ModelDrivenSwitchImpl(null, null, context);
89 registration = new CompositeObjectRegistration<>(mdSwitchOF13, Collections.EMPTY_LIST);
91 Mockito.when(context.getProviderRegistration()).thenReturn(registration);
92 Mockito.when(features.getDatapathId()).thenReturn(BigInteger.valueOf(1));
93 Mockito.when(features.getVersion()).thenReturn((short) 1);
95 Set<RpcError> errorSet = Collections.emptySet();
96 UpdateFlowOutputBuilder updateFlowOutput = new UpdateFlowOutputBuilder();
97 RpcResult<UpdateFlowOutput> result = Rpcs.getRpcResult(true, updateFlowOutput.build(), errorSet);
100 messageDispatchService.flowMod(Matchers.any(FlowModInput.class),
101 Matchers.any(SwitchConnectionDistinguisher.class))).thenReturn(Futures.immediateFuture(result));
103 salRegistrationManager = new SalRegistrationManager();
104 salRegistrationManager.onSessionInitiated(providerContext);
105 salRegistrationManager.setPublishService(new MockNotificationProviderService());
109 * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SalRegistrationManager#identifierFromDatapathId(java.math.BigInteger)}
112 public void testIdentifierFromDatapathId() {
113 InstanceIdentifier<Node> node = salRegistrationManager.identifierFromDatapathId(dataPathId);
115 assertEquals("NodeKey [_id=Uri [_value=openflow:1]]", ((KeyedInstanceIdentifier) node).getKey().toString());
119 * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SalRegistrationManager#nodeKeyFromDatapathId(java.math.BigInteger)}
122 public void testNodeKeyFromDatapathId() {
123 NodeKey nodeKey = salRegistrationManager.nodeKeyFromDatapathId(dataPathId);
124 assertNotNull(nodeKey);
125 assertEquals("openflow:1", nodeKey.getId().getValue());
129 * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SalRegistrationManager#nodeIdFromDatapathId(java.math.BigInteger)}
132 public void testNodeIdFromDatapathId() {
133 NodeId nodeId = salRegistrationManager.nodeIdFromDatapathId(dataPathId);
134 assertNotNull(nodeId);
135 assertEquals("openflow:1", nodeId.getValue());
139 * Test for {@link SalRegistrationManager#getSessionManager()}
142 public void testGetSessionManager() {
143 assertNotNull(salRegistrationManager.getPublishService());
148 * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SalRegistrationManager#onSessionRemoved(org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext)}
151 public void testOnSessionRemoved() {
152 salRegistrationManager.onSessionRemoved(context);
156 * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SalRegistrationManager#onSessionAdded(org.opendaylight.openflowplugin.openflow.md.core.session.SwitchSessionKeyOF, org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext)}
158 public void testOnAdded() {
159 SwitchSessionKeyOF switchSessionKeyOF = new SwitchSessionKeyOF();
160 salRegistrationManager.onSessionAdded(switchSessionKeyOF, context);
164 private class MockNotificationProviderService implements NotificationProviderService {
167 public void publish(Notification notification) {
172 public void publish(Notification notification, ExecutorService executorService) {
177 public ListenerRegistration<NotificationInterestListener> registerInterestListener(NotificationInterestListener notificationInterestListener) {
182 public <T extends Notification> ListenerRegistration<NotificationListener<T>> registerNotificationListener(Class<T> tClass, NotificationListener<T> tNotificationListener) {
187 public ListenerRegistration<org.opendaylight.yangtools.yang.binding.NotificationListener> registerNotificationListener(org.opendaylight.yangtools.yang.binding.NotificationListener notificationListener) {