Bug-2827: role switch proposal
[openflowplugin.git] / openflowplugin / src / test / java / org / opendaylight / openflowplugin / openflow / md / util / RoleUtilTest.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 package org.opendaylight.openflowplugin.openflow.md.util;
9
10 import java.math.BigInteger;
11 import java.util.concurrent.ExecutionException;
12 import java.util.concurrent.Future;
13
14 import org.junit.Assert;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.junit.runner.RunWith;
18 import org.mockito.ArgumentCaptor;
19 import org.mockito.Matchers;
20 import org.mockito.Mock;
21 import org.mockito.Mockito;
22 import org.mockito.runners.MockitoJUnitRunner;
23 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
24 import org.opendaylight.openflowplugin.api.openflow.md.core.ConnectionConductor;
25 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext;
26 import org.opendaylight.openflowplugin.openflow.md.core.session.OFRoleManager;
27 import org.opendaylight.openflowplugin.openflow.md.core.session.RolePushException;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ControllerRole;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequest;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestInput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestOutput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestOutputBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.common.config.impl.rev140326.OfpRole;
35 import org.opendaylight.yangtools.yang.common.RpcResult;
36 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
37
38 import com.google.common.util.concurrent.Futures;
39
40 /**
41  * testing {@link RoleUtil}
42  */
43 @RunWith(MockitoJUnitRunner.class)
44 public class RoleUtilTest {
45     
46     @Mock
47     private SessionContext session;
48     @Mock
49     private GetFeaturesOutput features;
50     @Mock
51     private ConnectionConductor primaryConductor;
52     @Mock
53     private ConnectionAdapter connectionAdapter;
54     
55     private final BigInteger generationId = BigInteger.TEN;
56     private RoleRequestOutput roleRequestOutput;
57
58     /**
59      * prepare values
60      */
61     @Before
62     public void setUp() {
63         Mockito.when(session.getFeatures()).thenReturn(features);
64         Mockito.when(features.getVersion()).thenReturn(Short.valueOf((short) 42));
65         Mockito.when(session.getNextXid()).thenReturn(84L);
66         Mockito.when(session.getPrimaryConductor()).thenReturn(primaryConductor);
67         Mockito.when(primaryConductor.getConnectionAdapter()).thenReturn(connectionAdapter);
68         roleRequestOutput = new RoleRequestOutputBuilder()
69             .setGenerationId(generationId)
70             .setRole(ControllerRole.OFPCRROLESLAVE)
71             .setVersion((short) 42)
72             .setXid(21L)
73             .build();
74         Mockito.when(connectionAdapter.roleRequest(Matchers.any(RoleRequestInput.class)))
75             .thenReturn(Futures.immediateFuture(RpcResultBuilder.success(roleRequestOutput).build()));
76     }
77
78     /**
79      * Test method for {@link org.opendaylight.openflowplugin.openflow.md.util.RoleUtil#toOFJavaRole(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.common.config.impl.rev140326.OfpRole)}.
80      */
81     @Test
82     public void testToOFJavaRole() {
83         Assert.assertEquals(ControllerRole.OFPCRROLEMASTER, RoleUtil.toOFJavaRole(OfpRole.BECOMEMASTER));
84         Assert.assertEquals(ControllerRole.OFPCRROLESLAVE, RoleUtil.toOFJavaRole(OfpRole.BECOMESLAVE));
85         Assert.assertEquals(ControllerRole.OFPCRROLENOCHANGE, RoleUtil.toOFJavaRole(OfpRole.NOCHANGE));
86     }
87     
88     /**
89      * Test method for {@link org.opendaylight.openflowplugin.openflow.md.util.RoleUtil#toOFJavaRole(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.common.config.impl.rev140326.OfpRole)}.
90      */
91     @Test(expected = NullPointerException.class)
92     public void testToOFJavaRoleNull() {
93         RoleUtil.toOFJavaRole(null);
94     }
95
96     /**
97      * Test method for {@link org.opendaylight.openflowplugin.openflow.md.util.RoleUtil#createRoleRequestInput(org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext, org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.common.config.impl.rev140326.OfpRole, java.math.BigInteger)}.
98      */
99     @Test
100     public void testCreateRuleRequestInput() {
101         RoleRequestInput roleRequestInput = RoleUtil.createRoleRequestInput(session, OfpRole.BECOMEMASTER, generationId).build();
102         Assert.assertEquals(generationId, roleRequestInput.getGenerationId());
103         Assert.assertEquals(RoleRequestInput.class, roleRequestInput.getImplementedInterface());
104         Assert.assertEquals(ControllerRole.OFPCRROLEMASTER, roleRequestInput.getRole());
105         Assert.assertEquals(42, roleRequestInput.getVersion().intValue());
106         Assert.assertEquals(84L, roleRequestInput.getXid().longValue());
107     }
108
109     /**
110      * Test method for {@link org.opendaylight.openflowplugin.openflow.md.util.RoleUtil#sendRoleChangeRequest(org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext, org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.common.config.impl.rev140326.OfpRole, java.math.BigInteger)}.
111      * @throws Exception 
112      */
113     @Test
114     public void testSendRoleChangeRequest() throws Exception {
115         Future<RpcResult<RoleRequestOutput>> roleRequestOutputFx = RoleUtil.sendRoleChangeRequest(session, OfpRole.BECOMEMASTER, generationId);
116         Assert.assertNotNull(roleRequestOutputFx);
117         
118         ArgumentCaptor<RoleRequestInput> roleRequestCaptor = ArgumentCaptor.forClass(RoleRequestInput.class);
119         Mockito.verify(connectionAdapter).roleRequest(roleRequestCaptor.capture());
120      
121         RoleRequest roleRequestInput = roleRequestCaptor.getValue();
122         Assert.assertEquals(generationId, roleRequestInput.getGenerationId());
123         Assert.assertEquals(RoleRequestInput.class, roleRequestInput.getImplementedInterface());
124         Assert.assertEquals(ControllerRole.OFPCRROLEMASTER, roleRequestInput.getRole());
125         Assert.assertEquals(42, roleRequestInput.getVersion().intValue());
126         Assert.assertEquals(84L, roleRequestInput.getXid().longValue());
127     }
128
129     /**
130      * Test method for {@link org.opendaylight.openflowplugin.openflow.md.util.RoleUtil#readGenerationIdFromDevice(org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext)}.
131      * @throws Exception 
132      */
133     @Test
134     public void testReadGenerationIdFromDevice() throws Exception {
135         BigInteger generationIdFromDevice = RoleUtil.readGenerationIdFromDevice(session).get();
136         Assert.assertEquals(generationId, generationIdFromDevice);
137     }
138     
139     /**
140      * Test method for {@link org.opendaylight.openflowplugin.openflow.md.util.RoleUtil#getNextGenerationId(java.math.BigInteger)}.
141      */
142     @Test
143     public void testGetNextGenerationId() {
144         BigInteger[] src = new BigInteger[] {
145                 BigInteger.ZERO,
146                 BigInteger.ONE,
147                 OFRoleManager.MAX_GENERATION_ID.subtract(BigInteger.ONE),
148                 OFRoleManager.MAX_GENERATION_ID
149         };
150         
151         BigInteger[] out = new BigInteger[] {
152                 BigInteger.ONE,
153                 BigInteger.valueOf(2L),
154                 OFRoleManager.MAX_GENERATION_ID,
155                 BigInteger.ZERO
156         };
157         
158         for (int i = 0; i < src.length; i++) {
159             BigInteger nextGenerationId = RoleUtil.getNextGenerationId(src[i]);
160             Assert.assertEquals(out[i], nextGenerationId);
161         }
162     }
163     
164     /**
165      * Test method for {@link org.opendaylight.openflowplugin.openflow.md.util.RoleUtil#makeCheckedRuleRequestFxResult(com.google.common.util.concurrent.ListenableFuture)}.
166      * @throws Exception 
167      */
168     @Test
169     public void testMakeCheckedRuleRequestFxResult() throws Exception {
170         String message = "me sooo naughty!";
171         try {
172             RoleUtil.makeCheckedRuleRequestFxResult(Futures.<Boolean>immediateFailedFuture(new Exception(message))).checkedGet();
173         } catch (Exception e) {
174             Assert.assertEquals(RolePushException.class, e.getClass());
175             Assert.assertEquals(ExecutionException.class, e.getCause().getClass());
176             Assert.assertEquals(Exception.class, e.getCause().getCause().getClass());
177             Assert.assertNull(e.getCause().getCause().getCause());
178             Assert.assertEquals(message, e.getCause().getCause().getMessage());
179         }
180         
181         try {
182             RoleUtil.makeCheckedRuleRequestFxResult(Futures.<Boolean>immediateFailedFuture(new RolePushException(message))).checkedGet();
183         } catch (Exception e) {
184             Assert.assertEquals(RolePushException.class, e.getClass());
185             Assert.assertNull(e.getCause());
186             Assert.assertEquals(message, e.getMessage());
187         }
188         
189     }
190 }