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