BUG-1254: fix concurrent add/remove session test
[openflowplugin.git] / openflowplugin / src / test / java / org / opendaylight / openflowplugin / openflow / md / core / sal / SalRegistrationManagerTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowplugin.openflow.md.core.sal;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListeningExecutorService;
16
17 import java.math.BigInteger;
18 import java.util.Collections;
19 import java.util.Set;
20 import java.util.concurrent.ExecutorService;
21
22 import org.junit.After;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.Matchers;
27 import org.mockito.Mock;
28 import org.mockito.Mockito;
29 import org.mockito.runners.MockitoJUnitRunner;
30 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
31 import org.opendaylight.controller.sal.binding.api.NotificationListener;
32 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
33 import org.opendaylight.controller.sal.common.util.Rpcs;
34 import org.opendaylight.openflowplugin.api.openflow.md.ModelDrivenSwitch;
35 import org.opendaylight.openflowplugin.api.OFConstants;
36 import org.opendaylight.openflowplugin.api.openflow.md.core.ConnectionConductor;
37 import org.opendaylight.openflowplugin.api.openflow.md.core.NotificationEnqueuer;
38 import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
39 import org.opendaylight.openflowplugin.api.openflow.md.core.session.IMessageDispatchService;
40 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
41 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext;
42 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SwitchSessionKeyOF;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutputBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
50 import org.opendaylight.yangtools.concepts.CompositeObjectRegistration;
51 import org.opendaylight.yangtools.concepts.ListenerRegistration;
52 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
53 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
54 import org.opendaylight.yangtools.yang.binding.Notification;
55 import org.opendaylight.yangtools.yang.common.RpcError;
56 import org.opendaylight.yangtools.yang.common.RpcResult;
57
58 /**
59  * Created by Martin Bobak mbobak@cisco.com on 8/26/14.
60  */
61 @RunWith(MockitoJUnitRunner.class)
62 public class SalRegistrationManagerTest {
63
64
65     private static final BigInteger dataPathId = BigInteger.ONE;
66
67     private SalRegistrationManager salRegistrationManager;
68     @Mock
69     private SessionContext context;
70     @Mock
71     private ConnectionConductor conductor;
72     @Mock
73     private IMessageDispatchService messageDispatchService;
74     @Mock
75     private GetFeaturesOutput features;
76     @Mock
77     private BindingAwareBroker.ProviderContext providerContext;
78     @Mock
79     private NotificationEnqueuer notificationEnqueuer;
80     @Mock
81     private ListeningExecutorService rpcPool;
82     @Mock
83     private NotificationProviderService notificationProviderService;
84
85     private ModelDrivenSwitch mdSwitchOF13;
86
87     CompositeObjectRegistration<ModelDrivenSwitch> registration;
88
89
90     @Before
91     public void setUp() {
92         OFSessionUtil.getSessionManager().setRpcPool(rpcPool);
93
94         Mockito.when(context.getPrimaryConductor()).thenReturn(conductor);
95         Mockito.when(context.getMessageDispatchService()).thenReturn(messageDispatchService);
96         Mockito.when(conductor.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_0)
97                 .thenReturn(OFConstants.OFP_VERSION_1_3);
98         Mockito.when(context.getFeatures()).thenReturn(features);
99
100         mdSwitchOF13 = new ModelDrivenSwitchImpl(null, null, context);
101         registration = new CompositeObjectRegistration<>(mdSwitchOF13, Collections.EMPTY_LIST);
102
103         Mockito.when(context.getProviderRegistration()).thenReturn(registration);
104         Mockito.when(context.getNotificationEnqueuer()).thenReturn(notificationEnqueuer);
105         Mockito.when(features.getDatapathId()).thenReturn(BigInteger.valueOf(1));
106         Mockito.when(features.getVersion()).thenReturn((short) 1);
107
108         Set<RpcError> errorSet = Collections.emptySet();
109         UpdateFlowOutputBuilder updateFlowOutput = new UpdateFlowOutputBuilder();
110         RpcResult<UpdateFlowOutput> result = Rpcs.getRpcResult(true, updateFlowOutput.build(), errorSet);
111
112         Mockito.when(
113                 messageDispatchService.flowMod(Matchers.any(FlowModInput.class),
114                         Matchers.any(SwitchConnectionDistinguisher.class))).thenReturn(Futures.immediateFuture(result));
115
116         salRegistrationManager = new SalRegistrationManager();
117         salRegistrationManager.onSessionInitiated(providerContext);
118         salRegistrationManager.setPublishService(notificationProviderService);
119     }
120     
121     /**
122      * free sesion manager
123      */
124     @After
125     public void tearDown() {
126         OFSessionUtil.releaseSessionManager();
127     }
128
129     /**
130      * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SalRegistrationManager#identifierFromDatapathId(java.math.BigInteger)}
131      */
132     @Test
133     public void testIdentifierFromDatapathId() {
134         InstanceIdentifier<Node> node = salRegistrationManager.identifierFromDatapathId(dataPathId);
135         assertNotNull(node);
136         assertEquals("NodeKey [_id=Uri [_value=openflow:1]]", ((KeyedInstanceIdentifier) node).getKey().toString());
137     }
138
139     /**
140      * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SalRegistrationManager#nodeKeyFromDatapathId(java.math.BigInteger)}
141      */
142     @Test
143     public void testNodeKeyFromDatapathId() {
144         NodeKey nodeKey = salRegistrationManager.nodeKeyFromDatapathId(dataPathId);
145         assertNotNull(nodeKey);
146         assertEquals("openflow:1", nodeKey.getId().getValue());
147     }
148
149     /**
150      * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SalRegistrationManager#nodeIdFromDatapathId(java.math.BigInteger)}
151      */
152     @Test
153     public void testNodeIdFromDatapathId() {
154         NodeId nodeId = salRegistrationManager.nodeIdFromDatapathId(dataPathId);
155         assertNotNull(nodeId);
156         assertEquals("openflow:1", nodeId.getValue());
157     }
158
159     /**
160      * Test for {@link SalRegistrationManager#getSessionManager()}
161      */
162     @Test
163     public void testGetSessionManager() {
164         assertNotNull(salRegistrationManager.getPublishService());
165     }
166
167
168     /**
169      * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SalRegistrationManager#onSessionRemoved(org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext)}
170      */
171     @Test
172     public void testOnSessionRemoved() {
173         salRegistrationManager.onSessionRemoved(context);
174     }
175
176     /**
177      * 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)}
178      */
179     public void testOnAdded() {
180         SwitchSessionKeyOF switchSessionKeyOF = new SwitchSessionKeyOF();
181         salRegistrationManager.onSessionAdded(switchSessionKeyOF, context);
182     }
183 }
184