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