367f0a0917be5f2df0da62a7364dbb962cec5e77
[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 import org.opendaylight.openflowplugin.openflow.md.core.role.OfEntityManager;
53 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
54
55 /**
56  * Created by Martin Bobak mbobak@cisco.com on 8/26/14.
57  */
58 @RunWith(MockitoJUnitRunner.class)
59 public class SalRegistrationManagerTest {
60
61
62     private static final BigInteger dataPathId = BigInteger.ONE;
63
64     private SalRegistrationManager salRegistrationManager;
65
66     private SessionContextOFImpl context;
67     @Mock
68     private ConnectionConductor conductor;
69     @Mock
70     private IMessageDispatchService messageDispatchService;
71     @Mock
72     private GetFeaturesOutput features;
73     @Mock
74     private BindingAwareBroker.ProviderContext providerContext;
75     @Mock
76     private NotificationEnqueuer notificationEnqueuer;
77     @Mock
78     private ListeningExecutorService rpcPool;
79     @Mock
80     private NotificationProviderService notificationProviderService;
81     @Mock
82     private RpcProviderRegistry rpcProviderRegistry;
83     @Mock
84     private DataBroker dataBroker;
85     @Mock
86     private EntityOwnershipService entityOwnershipService;
87
88     @Mock
89     private ModelDrivenSwitchImpl ofSwitch;
90
91     private ModelDrivenSwitch mdSwitchOF13;
92
93     CompositeObjectRegistration<ModelDrivenSwitch> registration;
94
95
96     @Before
97     public void setUp() {
98         OFSessionUtil.getSessionManager().setRpcPool(rpcPool);
99         Mockito.when(conductor.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_0)
100                 .thenReturn(OFConstants.OFP_VERSION_1_3);
101         context = new SessionContextOFImpl();
102         context.setPrimaryConductor(conductor);
103         Mockito.when(features.getDatapathId()).thenReturn(BigInteger.valueOf(1));
104         Mockito.when(features.getVersion()).thenReturn((short) 1);
105         context.setFeatures(features);
106         context.setNotificationEnqueuer(notificationEnqueuer);
107
108         OfEntityManager entManager = new OfEntityManager(entityOwnershipService,getConfig());
109         mdSwitchOF13 = new ModelDrivenSwitchImpl(null, null, context);
110         registration = new CompositeObjectRegistration<>(mdSwitchOF13, Collections.<Registration>emptyList());
111         context.setProviderRegistration(registration);
112
113         UpdateFlowOutputBuilder updateFlowOutput = new UpdateFlowOutputBuilder();
114         RpcResult<UpdateFlowOutput> result = RpcResultBuilder.success(updateFlowOutput.build()).build();
115
116         Mockito.when(
117                 messageDispatchService.flowMod(Matchers.any(FlowModInput.class),
118                         Matchers.any(SwitchConnectionDistinguisher.class))).thenReturn(Futures.immediateFuture(result));
119
120         salRegistrationManager = new SalRegistrationManager();
121         salRegistrationManager.setPublishService(notificationProviderService);
122         salRegistrationManager.setDataService(dataBroker);
123         salRegistrationManager.setRpcProviderRegistry(rpcProviderRegistry);
124         salRegistrationManager.setOfEntityManager(entManager);
125
126         salRegistrationManager.init();
127
128     }
129
130     /**
131      * free sesion manager
132      */
133     @After
134     public void tearDown() {
135         OFSessionUtil.releaseSessionManager();
136     }
137
138     /**
139      * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SalRegistrationManager#identifierFromDatapathId(java.math.BigInteger)}
140      */
141     @Test
142     public void testIdentifierFromDatapathId() {
143         InstanceIdentifier<Node> node = SalRegistrationManager.identifierFromDatapathId(dataPathId);
144         assertNotNull(node);
145         assertEquals("NodeKey [_id=Uri [_value=openflow:1]]", ((KeyedInstanceIdentifier<?, ?>) node).getKey().toString());
146     }
147
148     /**
149      * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SalRegistrationManager#nodeKeyFromDatapathId(java.math.BigInteger)}
150      */
151     @Test
152     public void testNodeKeyFromDatapathId() {
153         NodeKey nodeKey = SalRegistrationManager.nodeKeyFromDatapathId(dataPathId);
154         assertNotNull(nodeKey);
155         assertEquals("openflow:1", nodeKey.getId().getValue());
156     }
157
158     /**
159      * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SalRegistrationManager#nodeIdFromDatapathId(java.math.BigInteger)}
160      */
161     @Test
162     public void testNodeIdFromDatapathId() {
163         NodeId nodeId = SalRegistrationManager.nodeIdFromDatapathId(dataPathId);
164         assertNotNull(nodeId);
165         assertEquals("openflow:1", nodeId.getValue());
166     }
167
168     /**
169      * Test for {@link SalRegistrationManager#getSessionManager()}
170      */
171     @Test
172     public void testGetSessionManager() {
173         assertNotNull(salRegistrationManager.getPublishService());
174     }
175
176
177     /**
178      * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SalRegistrationManager#onSessionRemoved(org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext)}
179      */
180     @Test
181     public void testOnSessionRemoved() {
182 //        assertNotNull(context.getProviderRegistration());
183 //        salRegistrationManager.onSessionAdded(null,context);
184 //        salRegistrationManager.onSessionRemoved(context);
185 //        assertNull(context.getProviderRegistration());
186     }
187
188     /**
189      * 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)}
190      */
191     public void testOnAdded() {
192         SwitchSessionKeyOF switchSessionKeyOF = new SwitchSessionKeyOF();
193         salRegistrationManager.onSessionAdded(switchSessionKeyOF, context);
194     }
195
196     public OpenflowPluginConfig getConfig(){
197         OpenflowPluginConfig.OpenflowPluginConfigBuilder cfgBuilder =
198                 new OpenflowPluginConfig.OpenflowPluginConfigBuilder();
199         cfgBuilder.setSkipTableFeatures(true);
200         return cfgBuilder.build();
201
202     }
203 }
204