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