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;
16 import java.math.BigInteger;
17 import java.util.Collections;
19 import java.util.concurrent.ExecutorService;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.mockito.Matchers;
25 import org.mockito.Mock;
26 import org.mockito.Mockito;
27 import org.mockito.runners.MockitoJUnitRunner;
28 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
29 import org.opendaylight.controller.sal.binding.api.NotificationListener;
30 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
31 import org.opendaylight.controller.sal.common.util.Rpcs;
32 import org.opendaylight.openflowplugin.api.openflow.md.ModelDrivenSwitch;
33 import org.opendaylight.openflowplugin.api.OFConstants;
34 import org.opendaylight.openflowplugin.api.openflow.md.core.ConnectionConductor;
35 import org.opendaylight.openflowplugin.api.openflow.md.core.NotificationEnqueuer;
36 import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
37 import org.opendaylight.openflowplugin.api.openflow.md.core.session.IMessageDispatchService;
38 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext;
39 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SwitchSessionKeyOF;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutputBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
47 import org.opendaylight.yangtools.concepts.CompositeObjectRegistration;
48 import org.opendaylight.yangtools.concepts.ListenerRegistration;
49 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
50 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
51 import org.opendaylight.yangtools.yang.binding.Notification;
52 import org.opendaylight.yangtools.yang.common.RpcError;
53 import org.opendaylight.yangtools.yang.common.RpcResult;
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 SessionContext context;
68 private ConnectionConductor conductor;
70 private IMessageDispatchService messageDispatchService;
72 private GetFeaturesOutput features;
74 private BindingAwareBroker.ProviderContext providerContext;
76 private NotificationEnqueuer notificationEnqueuer;
78 private ModelDrivenSwitch mdSwitchOF13;
80 CompositeObjectRegistration<ModelDrivenSwitch> registration;
85 Mockito.when(context.getPrimaryConductor()).thenReturn(conductor);
86 Mockito.when(context.getMessageDispatchService()).thenReturn(messageDispatchService);
87 Mockito.when(conductor.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_0)
88 .thenReturn(OFConstants.OFP_VERSION_1_3);
89 Mockito.when(context.getFeatures()).thenReturn(features);
91 mdSwitchOF13 = new ModelDrivenSwitchImpl(null, null, context);
92 registration = new CompositeObjectRegistration<>(mdSwitchOF13, Collections.EMPTY_LIST);
94 Mockito.when(context.getProviderRegistration()).thenReturn(registration);
95 Mockito.when(context.getNotificationEnqueuer()).thenReturn(notificationEnqueuer);
96 Mockito.when(features.getDatapathId()).thenReturn(BigInteger.valueOf(1));
97 Mockito.when(features.getVersion()).thenReturn((short) 1);
99 Set<RpcError> errorSet = Collections.emptySet();
100 UpdateFlowOutputBuilder updateFlowOutput = new UpdateFlowOutputBuilder();
101 RpcResult<UpdateFlowOutput> result = Rpcs.getRpcResult(true, updateFlowOutput.build(), errorSet);
104 messageDispatchService.flowMod(Matchers.any(FlowModInput.class),
105 Matchers.any(SwitchConnectionDistinguisher.class))).thenReturn(Futures.immediateFuture(result));
107 salRegistrationManager = new SalRegistrationManager();
108 salRegistrationManager.onSessionInitiated(providerContext);
109 salRegistrationManager.setPublishService(new MockNotificationProviderService());
113 * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SalRegistrationManager#identifierFromDatapathId(java.math.BigInteger)}
116 public void testIdentifierFromDatapathId() {
117 InstanceIdentifier<Node> node = salRegistrationManager.identifierFromDatapathId(dataPathId);
119 assertEquals("NodeKey [_id=Uri [_value=openflow:1]]", ((KeyedInstanceIdentifier) node).getKey().toString());
123 * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SalRegistrationManager#nodeKeyFromDatapathId(java.math.BigInteger)}
126 public void testNodeKeyFromDatapathId() {
127 NodeKey nodeKey = salRegistrationManager.nodeKeyFromDatapathId(dataPathId);
128 assertNotNull(nodeKey);
129 assertEquals("openflow:1", nodeKey.getId().getValue());
133 * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SalRegistrationManager#nodeIdFromDatapathId(java.math.BigInteger)}
136 public void testNodeIdFromDatapathId() {
137 NodeId nodeId = salRegistrationManager.nodeIdFromDatapathId(dataPathId);
138 assertNotNull(nodeId);
139 assertEquals("openflow:1", nodeId.getValue());
143 * Test for {@link SalRegistrationManager#getSessionManager()}
146 public void testGetSessionManager() {
147 assertNotNull(salRegistrationManager.getPublishService());
152 * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SalRegistrationManager#onSessionRemoved(org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext)}
155 public void testOnSessionRemoved() {
156 salRegistrationManager.onSessionRemoved(context);
160 * 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)}
162 public void testOnAdded() {
163 SwitchSessionKeyOF switchSessionKeyOF = new SwitchSessionKeyOF();
164 salRegistrationManager.onSessionAdded(switchSessionKeyOF, context);
168 private class MockNotificationProviderService implements NotificationProviderService {
171 public void publish(Notification notification) {
176 public void publish(Notification notification, ExecutorService executorService) {
181 public ListenerRegistration<NotificationInterestListener> registerInterestListener(NotificationInterestListener notificationInterestListener) {
186 public <T extends Notification> ListenerRegistration<NotificationListener<T>> registerNotificationListener(Class<T> tClass, NotificationListener<T> tNotificationListener) {
191 public ListenerRegistration<org.opendaylight.yangtools.yang.binding.NotificationListener> registerNotificationListener(org.opendaylight.yangtools.yang.binding.NotificationListener notificationListener) {