Bug 4957 TxChainManager lifecycle startup cleaning
[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 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListeningExecutorService;
16 import java.math.BigInteger;
17 import java.util.Collections;
18 import org.junit.After;
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.md.sal.binding.api.DataBroker;
27 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
28 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
29 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
30 import org.opendaylight.openflowplugin.api.OFConstants;
31 import org.opendaylight.openflowplugin.api.openflow.md.ModelDrivenSwitch;
32 import org.opendaylight.openflowplugin.api.openflow.md.core.ConnectionConductor;
33 import org.opendaylight.openflowplugin.api.openflow.md.core.NotificationEnqueuer;
34 import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
35 import org.opendaylight.openflowplugin.api.openflow.md.core.session.IMessageDispatchService;
36 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SwitchSessionKeyOF;
37 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
38 import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContextOFImpl;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutputBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
46 import org.opendaylight.yangtools.concepts.CompositeObjectRegistration;
47 import org.opendaylight.yangtools.concepts.Registration;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
49 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
50 import org.opendaylight.yangtools.yang.common.RpcResult;
51 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
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     @Mock
80     private RpcProviderRegistry rpcProviderRegistry;
81     @Mock
82     private DataBroker dataBroker;
83
84     private ModelDrivenSwitch mdSwitchOF13;
85
86     CompositeObjectRegistration<ModelDrivenSwitch> registration;
87
88
89     @Before
90     public void setUp() {
91         OFSessionUtil.getSessionManager().setRpcPool(rpcPool);
92         Mockito.when(conductor.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_0)
93                 .thenReturn(OFConstants.OFP_VERSION_1_3);
94         context = new SessionContextOFImpl();
95         context.setPrimaryConductor(conductor);
96         Mockito.when(features.getDatapathId()).thenReturn(BigInteger.valueOf(1));
97         Mockito.when(features.getVersion()).thenReturn((short) 1);
98         context.setFeatures(features);
99         context.setNotificationEnqueuer(notificationEnqueuer);
100
101         mdSwitchOF13 = new ModelDrivenSwitchImpl(null, null, context);
102         registration = new CompositeObjectRegistration<>(mdSwitchOF13, Collections.<Registration>emptyList());
103         context.setProviderRegistration(registration);
104
105         UpdateFlowOutputBuilder updateFlowOutput = new UpdateFlowOutputBuilder();
106         RpcResult<UpdateFlowOutput> result = RpcResultBuilder.success(updateFlowOutput.build()).build();
107
108         Mockito.when(
109                 messageDispatchService.flowMod(Matchers.any(FlowModInput.class),
110                         Matchers.any(SwitchConnectionDistinguisher.class))).thenReturn(Futures.immediateFuture(result));
111
112         salRegistrationManager = new SalRegistrationManager();
113         salRegistrationManager.setPublishService(notificationProviderService);
114         salRegistrationManager.setDataService(dataBroker);
115         salRegistrationManager.setRpcProviderRegistry(rpcProviderRegistry);
116
117         salRegistrationManager.init();
118
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         assertNotNull(context.getProviderRegistration());
174         salRegistrationManager.onSessionRemoved(context);
175         assertNull(context.getProviderRegistration());
176     }
177
178     /**
179      * 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)}
180      */
181     public void testOnAdded() {
182         SwitchSessionKeyOF switchSessionKeyOF = new SwitchSessionKeyOF();
183         salRegistrationManager.onSessionAdded(switchSessionKeyOF, context);
184     }
185 }
186