Bug 1764 - moved Session related interfaces to openflowplugin-api
[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
16 import java.math.BigInteger;
17 import java.util.Collections;
18 import java.util.Set;
19 import java.util.concurrent.ExecutorService;
20
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.NotificationListener;
30 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
31 import org.opendaylight.controller.sal.common.util.Rpcs;
32 import org.opendaylight.openflowplugin.api.openflow.md.ModelDrivenSwitch;
33 import org.opendaylight.openflowplugin.api.OFConstants;
34 import org.opendaylight.openflowplugin.api.openflow.md.core.ConnectionConductor;
35 import org.opendaylight.openflowplugin.api.openflow.md.core.NotificationEnqueuer;
36 import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
37 import org.opendaylight.openflowplugin.api.openflow.md.core.session.IMessageDispatchService;
38 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext;
39 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SwitchSessionKeyOF;
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.concepts.ListenerRegistration;
49 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
50 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
51 import org.opendaylight.yangtools.yang.binding.Notification;
52 import org.opendaylight.yangtools.yang.common.RpcError;
53 import org.opendaylight.yangtools.yang.common.RpcResult;
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     @Mock
66     private SessionContext 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
78     private ModelDrivenSwitch mdSwitchOF13;
79
80     CompositeObjectRegistration<ModelDrivenSwitch> registration;
81
82     @Before
83     public void setUp() {
84
85         Mockito.when(context.getPrimaryConductor()).thenReturn(conductor);
86         Mockito.when(context.getMessageDispatchService()).thenReturn(messageDispatchService);
87         Mockito.when(conductor.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_0)
88                 .thenReturn(OFConstants.OFP_VERSION_1_3);
89         Mockito.when(context.getFeatures()).thenReturn(features);
90
91         mdSwitchOF13 = new ModelDrivenSwitchImpl(null, null, context);
92         registration = new CompositeObjectRegistration<>(mdSwitchOF13, Collections.EMPTY_LIST);
93
94         Mockito.when(context.getProviderRegistration()).thenReturn(registration);
95         Mockito.when(context.getNotificationEnqueuer()).thenReturn(notificationEnqueuer);
96         Mockito.when(features.getDatapathId()).thenReturn(BigInteger.valueOf(1));
97         Mockito.when(features.getVersion()).thenReturn((short) 1);
98
99         Set<RpcError> errorSet = Collections.emptySet();
100         UpdateFlowOutputBuilder updateFlowOutput = new UpdateFlowOutputBuilder();
101         RpcResult<UpdateFlowOutput> result = Rpcs.getRpcResult(true, updateFlowOutput.build(), errorSet);
102
103         Mockito.when(
104                 messageDispatchService.flowMod(Matchers.any(FlowModInput.class),
105                         Matchers.any(SwitchConnectionDistinguisher.class))).thenReturn(Futures.immediateFuture(result));
106
107         salRegistrationManager = new SalRegistrationManager();
108         salRegistrationManager.onSessionInitiated(providerContext);
109         salRegistrationManager.setPublishService(new MockNotificationProviderService());
110     }
111
112     /**
113      * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SalRegistrationManager#identifierFromDatapathId(java.math.BigInteger)}
114      */
115     @Test
116     public void testIdentifierFromDatapathId() {
117         InstanceIdentifier<Node> node = salRegistrationManager.identifierFromDatapathId(dataPathId);
118         assertNotNull(node);
119         assertEquals("NodeKey [_id=Uri [_value=openflow:1]]", ((KeyedInstanceIdentifier) node).getKey().toString());
120     }
121
122     /**
123      * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SalRegistrationManager#nodeKeyFromDatapathId(java.math.BigInteger)}
124      */
125     @Test
126     public void testNodeKeyFromDatapathId() {
127         NodeKey nodeKey = salRegistrationManager.nodeKeyFromDatapathId(dataPathId);
128         assertNotNull(nodeKey);
129         assertEquals("openflow:1", nodeKey.getId().getValue());
130     }
131
132     /**
133      * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SalRegistrationManager#nodeIdFromDatapathId(java.math.BigInteger)}
134      */
135     @Test
136     public void testNodeIdFromDatapathId() {
137         NodeId nodeId = salRegistrationManager.nodeIdFromDatapathId(dataPathId);
138         assertNotNull(nodeId);
139         assertEquals("openflow:1", nodeId.getValue());
140     }
141
142     /**
143      * Test for {@link SalRegistrationManager#getSessionManager()}
144      */
145     @Test
146     public void testGetSessionManager() {
147         assertNotNull(salRegistrationManager.getPublishService());
148     }
149
150
151     /**
152      * Test for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SalRegistrationManager#onSessionRemoved(org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext)}
153      */
154     @Test
155     public void testOnSessionRemoved() {
156         salRegistrationManager.onSessionRemoved(context);
157     }
158
159     /**
160      * 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)}
161      */
162     public void testOnAdded() {
163         SwitchSessionKeyOF switchSessionKeyOF = new SwitchSessionKeyOF();
164         salRegistrationManager.onSessionAdded(switchSessionKeyOF, context);
165     }
166
167
168     private class MockNotificationProviderService implements NotificationProviderService {
169
170         @Override
171         public void publish(Notification notification) {
172
173         }
174
175         @Override
176         public void publish(Notification notification, ExecutorService executorService) {
177
178         }
179
180         @Override
181         public ListenerRegistration<NotificationInterestListener> registerInterestListener(NotificationInterestListener notificationInterestListener) {
182             return null;
183         }
184
185         @Override
186         public <T extends Notification> ListenerRegistration<NotificationListener<T>> registerNotificationListener(Class<T> tClass, NotificationListener<T> tNotificationListener) {
187             return null;
188         }
189
190         @Override
191         public ListenerRegistration<org.opendaylight.yangtools.yang.binding.NotificationListener> registerNotificationListener(org.opendaylight.yangtools.yang.binding.NotificationListener notificationListener) {
192             return null;
193         }
194     }
195
196 }
197