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.api.OFConstants;
32 import org.opendaylight.openflowplugin.openflow.md.core.ConnectionConductor;
33 import org.opendaylight.openflowplugin.api.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.UpdateFlowOutput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutputBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
44 import org.opendaylight.yangtools.concepts.CompositeObjectRegistration;
45 import org.opendaylight.yangtools.concepts.ListenerRegistration;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
48 import org.opendaylight.yangtools.yang.binding.Notification;
49 import org.opendaylight.yangtools.yang.common.RpcError;
50 import org.opendaylight.yangtools.yang.common.RpcResult;
53 * Created by Martin Bobak mbobak@cisco.com on 8/26/14.
55 @RunWith(MockitoJUnitRunner.class)
56 public class SalRegistrationManagerTest {
59 private static final BigInteger dataPathId = BigInteger.ONE;
61 private SalRegistrationManager salRegistrationManager;
63 private SessionContext context;
65 private ConnectionConductor conductor;
67 private IMessageDispatchService messageDispatchService;
69 private GetFeaturesOutput features;
71 private BindingAwareBroker.ProviderContext providerContext;
73 private ModelDrivenSwitch mdSwitchOF13;
75 CompositeObjectRegistration<ModelDrivenSwitch> registration;
81 Mockito.when(context.getPrimaryConductor()).thenReturn(conductor);
82 Mockito.when(context.getMessageDispatchService()).thenReturn(messageDispatchService);
83 Mockito.when(conductor.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_0)
84 .thenReturn(OFConstants.OFP_VERSION_1_3);
85 Mockito.when(context.getFeatures()).thenReturn(features);
87 mdSwitchOF13 = new ModelDrivenSwitchImpl(null, null, context);
88 registration = new CompositeObjectRegistration<>(mdSwitchOF13, Collections.EMPTY_LIST);
90 Mockito.when(context.getProviderRegistration()).thenReturn(registration);
91 Mockito.when(features.getDatapathId()).thenReturn(BigInteger.valueOf(1));
92 Mockito.when(features.getVersion()).thenReturn((short) 1);
94 Set<RpcError> errorSet = Collections.emptySet();
95 UpdateFlowOutputBuilder updateFlowOutput = new UpdateFlowOutputBuilder();
96 RpcResult<UpdateFlowOutput> result = Rpcs.getRpcResult(true, updateFlowOutput.build(), errorSet);
99 messageDispatchService.flowMod(Matchers.any(FlowModInput.class),
100 Matchers.any(SwitchConnectionDistinguisher.class))).thenReturn(Futures.immediateFuture(result));
102 salRegistrationManager = new SalRegistrationManager();
103 salRegistrationManager.onSessionInitiated(providerContext);
104 salRegistrationManager.setPublishService(new MockNotificationProviderService());
108 * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SalRegistrationManager#identifierFromDatapathId(java.math.BigInteger)}
111 public void testIdentifierFromDatapathId() {
112 InstanceIdentifier<Node> node = salRegistrationManager.identifierFromDatapathId(dataPathId);
114 assertEquals("NodeKey [_id=Uri [_value=openflow:1]]", ((KeyedInstanceIdentifier) node).getKey().toString());
118 * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SalRegistrationManager#nodeKeyFromDatapathId(java.math.BigInteger)}
121 public void testNodeKeyFromDatapathId() {
122 NodeKey nodeKey = salRegistrationManager.nodeKeyFromDatapathId(dataPathId);
123 assertNotNull(nodeKey);
124 assertEquals("openflow:1", nodeKey.getId().getValue());
128 * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SalRegistrationManager#nodeIdFromDatapathId(java.math.BigInteger)}
131 public void testNodeIdFromDatapathId() {
132 NodeId nodeId = salRegistrationManager.nodeIdFromDatapathId(dataPathId);
133 assertNotNull(nodeId);
134 assertEquals("openflow:1", nodeId.getValue());
138 * Test for {@link SalRegistrationManager#getSessionManager()}
141 public void testGetSessionManager() {
142 assertNotNull(salRegistrationManager.getPublishService());
147 * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SalRegistrationManager#onSessionRemoved(org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext)}
150 public void testOnSessionRemoved() {
151 salRegistrationManager.onSessionRemoved(context);
155 * 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)}
157 public void testOnAdded() {
158 SwitchSessionKeyOF switchSessionKeyOF = new SwitchSessionKeyOF();
159 salRegistrationManager.onSessionAdded(switchSessionKeyOF, context);
163 private class MockNotificationProviderService implements NotificationProviderService {
166 public void publish(Notification notification) {
171 public void publish(Notification notification, ExecutorService executorService) {
176 public ListenerRegistration<NotificationInterestListener> registerInterestListener(NotificationInterestListener notificationInterestListener) {
181 public <T extends Notification> ListenerRegistration<NotificationListener<T>> registerNotificationListener(Class<T> tClass, NotificationListener<T> tNotificationListener) {
186 public ListenerRegistration<org.opendaylight.yangtools.yang.binding.NotificationListener> registerNotificationListener(org.opendaylight.yangtools.yang.binding.NotificationListener notificationListener) {