4bf473ac6c4d62b678750935a73f13660861660e
[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
94     CompositeObjectRegistration<ModelDrivenSwitch> registration;
95
96
97     @Before
98     public void setUp() {
99         OFSessionUtil.getSessionManager().setRpcPool(rpcPool);
100         Mockito.when(conductor.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_0)
101                 .thenReturn(OFConstants.OFP_VERSION_1_3);
102         context = new SessionContextOFImpl();
103         context.setPrimaryConductor(conductor);
104         Mockito.when(features.getDatapathId()).thenReturn(BigInteger.valueOf(1));
105         Mockito.when(features.getVersion()).thenReturn((short) 1);
106         context.setFeatures(features);
107         context.setNotificationEnqueuer(notificationEnqueuer);
108
109         OfEntityManager entManager = new OfEntityManager(entityOwnershipService);
110         mdSwitchOF13 = new ModelDrivenSwitchImpl(null, null, context);
111         registration = new CompositeObjectRegistration<>(mdSwitchOF13, Collections.<Registration>emptyList());
112         context.setProviderRegistration(registration);
113
114         UpdateFlowOutputBuilder updateFlowOutput = new UpdateFlowOutputBuilder();
115         RpcResult<UpdateFlowOutput> result = RpcResultBuilder.success(updateFlowOutput.build()).build();
116
117         Mockito.when(
118                 messageDispatchService.flowMod(Matchers.any(FlowModInput.class),
119                         Matchers.any(SwitchConnectionDistinguisher.class))).thenReturn(Futures.immediateFuture(result));
120
121         salRegistrationManager = new SalRegistrationManager();
122         salRegistrationManager.setPublishService(notificationProviderService);
123         salRegistrationManager.setDataService(dataBroker);
124         salRegistrationManager.setRpcProviderRegistry(rpcProviderRegistry);
125         salRegistrationManager.setOfEntityManager(entManager);
126
127         salRegistrationManager.init();
128
129     }
130
131     /**
132      * free sesion manager
133      */
134     @After
135     public void tearDown() {
136         OFSessionUtil.releaseSessionManager();
137     }
138
139     /**
140      * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SalRegistrationManager#identifierFromDatapathId(java.math.BigInteger)}
141      */
142     @Test
143     public void testIdentifierFromDatapathId() {
144         InstanceIdentifier<Node> node = SalRegistrationManager.identifierFromDatapathId(dataPathId);
145         assertNotNull(node);
146         assertEquals("NodeKey [_id=Uri [_value=openflow:1]]", ((KeyedInstanceIdentifier<?, ?>) node).getKey().toString());
147     }
148
149     /**
150      * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SalRegistrationManager#nodeKeyFromDatapathId(java.math.BigInteger)}
151      */
152     @Test
153     public void testNodeKeyFromDatapathId() {
154         NodeKey nodeKey = SalRegistrationManager.nodeKeyFromDatapathId(dataPathId);
155         assertNotNull(nodeKey);
156         assertEquals("openflow:1", nodeKey.getId().getValue());
157     }
158
159     /**
160      * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SalRegistrationManager#nodeIdFromDatapathId(java.math.BigInteger)}
161      */
162     @Test
163     public void testNodeIdFromDatapathId() {
164         NodeId nodeId = SalRegistrationManager.nodeIdFromDatapathId(dataPathId);
165         assertNotNull(nodeId);
166         assertEquals("openflow:1", nodeId.getValue());
167     }
168
169     /**
170      * Test for {@link SalRegistrationManager#getSessionManager()}
171      */
172     @Test
173     public void testGetSessionManager() {
174         assertNotNull(salRegistrationManager.getPublishService());
175     }
176
177
178     /**
179      * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SalRegistrationManager#onSessionRemoved(org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext)}
180      */
181     @Test
182     public void testOnSessionRemoved() {
183 //        assertNotNull(context.getProviderRegistration());
184 //        salRegistrationManager.onSessionAdded(null,context);
185 //        salRegistrationManager.onSessionRemoved(context);
186 //        assertNull(context.getProviderRegistration());
187     }
188
189     /**
190      * 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)}
191      */
192     public void testOnAdded() {
193         SwitchSessionKeyOF switchSessionKeyOF = new SwitchSessionKeyOF();
194         salRegistrationManager.onSessionAdded(switchSessionKeyOF, context);
195     }
196 }
197