1209558252576ba06932a73c73aa5ff1df8155a5
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / services / sal / SalRoleServiceImplTest.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.impl.services.sal;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13
14 import com.google.common.util.concurrent.ListenableFuture;
15 import java.math.BigInteger;
16 import java.util.concurrent.Future;
17 import java.util.concurrent.TimeUnit;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.mockito.Mock;
21 import org.mockito.Mockito;
22 import org.mockito.MockitoAnnotations;
23 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
24 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
25 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
26 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
27 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
28 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
29 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
30 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
31 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestOutput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestOutputBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SalRoleService;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleInput;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleInputBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleOutput;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import org.opendaylight.yangtools.yang.common.RpcResult;
49 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
50
51 public class SalRoleServiceImplTest {
52
53     @Mock
54     private RequestContextStack mockRequestContextStack;
55
56     @Mock
57     private DeviceContext mockDeviceContext;
58
59     @Mock
60     private FeaturesReply mockFeaturesReply;
61
62     @Mock
63     private ConnectionContext mockConnectionContext;
64
65     @Mock
66     private MessageSpy mockMessageSpy;
67
68     @Mock
69     private RequestContext<RoleRequestOutput> mockRequestContext;
70
71     @Mock
72     private DeviceState mockDeviceState;
73
74     @Mock
75     private DeviceInfo mockDeviceInfo;
76
77     @Mock
78     private GetFeaturesOutput mockFeaturesOutput;
79
80     @Mock
81     private OutboundQueue mockOutboundQueue;
82
83     private NodeId testNodeId = new NodeId(Uri.getDefaultInstance("openflow:1"));
84
85     private static short testVersion = 4;
86
87     private static long testXid = 100L;
88
89     private static final String ROLEREQUESTFAILED = org.opendaylight.yang.gen.v1.urn
90             .opendaylight.openflow.common.types.rev130731.ErrorType.ROLEREQUESTFAILED.name();
91
92     private static final String ROLES_UNSUPPORTED = "Device reported error type " + ROLEREQUESTFAILED + " code UNSUP";
93
94     private NodeRef nodeRef;
95
96     @Before
97     public void setup() {
98         MockitoAnnotations.initMocks(this);
99         Mockito.when(mockDeviceInfo.getNodeId()).thenReturn(testNodeId);
100         Mockito.when(mockDeviceInfo.getDatapathId()).thenReturn(BigInteger.TEN);
101         Mockito.when(mockFeaturesOutput.getVersion()).thenReturn(testVersion);
102         Mockito.when(mockDeviceContext.getDeviceState()).thenReturn(mockDeviceState);
103         Mockito.when(mockDeviceContext.getDeviceInfo()).thenReturn(mockDeviceInfo);
104         Mockito.when(mockDeviceContext.getPrimaryConnectionContext()).thenReturn(mockConnectionContext);
105         Mockito.when(mockConnectionContext.getFeatures()).thenReturn(mockFeaturesReply);
106         Mockito.when(mockConnectionContext.getNodeId()).thenReturn(testNodeId);
107         Mockito.when(mockFeaturesReply.getVersion()).thenReturn(testVersion);
108         Mockito.when(mockDeviceContext.getMessageSpy()).thenReturn(mockMessageSpy);
109         Mockito.when(mockRequestContextStack.<RoleRequestOutput>createRequestContext()).thenReturn(mockRequestContext);
110         Mockito.when(mockRequestContext.getXid()).thenReturn(new Xid(testXid));
111         Mockito.when(mockConnectionContext.getOutboundQueueProvider()).thenReturn(mockOutboundQueue);
112         Mockito.when(mockDeviceContext.getPrimaryConnectionContext().getConnectionState())
113                 .thenReturn(ConnectionContext.CONNECTION_STATE.WORKING);
114
115         NodeKey key = new NodeKey(testNodeId);
116         InstanceIdentifier<Node> path = InstanceIdentifier.<Nodes>builder(Nodes.class)
117                 .<Node, NodeKey>child(Node.class, key)
118                 .build();
119         nodeRef = new NodeRef(path);
120
121     }
122
123     @Test
124     public void testSetRole() throws Exception {
125         RoleRequestOutput roleRequestOutput = (new RoleRequestOutputBuilder())
126                 .setXid(testXid).setGenerationId(BigInteger.valueOf(1)).build();
127         ListenableFuture<RpcResult<RoleRequestOutput>> futureOutput =
128                 RpcResultBuilder.<RoleRequestOutput>success().withResult(roleRequestOutput).buildFuture();
129
130         Mockito.when(mockRequestContext.getFuture()).thenReturn(futureOutput);
131
132
133         SalRoleService salRoleService = new SalRoleServiceImpl(mockRequestContextStack, mockDeviceContext);
134
135         SetRoleInput setRoleInput = new SetRoleInputBuilder()
136                 .setControllerRole(OfpRole.BECOMESLAVE)
137                 .setNode(nodeRef)
138                 .build();
139
140         Future<RpcResult<SetRoleOutput>> future = salRoleService.setRole(setRoleInput);
141
142         RpcResult<SetRoleOutput> roleOutputRpcResult = future.get(5, TimeUnit.SECONDS);
143         assertNotNull("RpcResult from future cannot be null.", roleOutputRpcResult);
144         assertTrue("RpcResult from future is not successful.", roleOutputRpcResult.isSuccessful());
145
146         SetRoleOutput setRoleOutput = roleOutputRpcResult.getResult();
147         assertNotNull(setRoleOutput);
148         assertEquals(BigInteger.valueOf(testXid), setRoleOutput.getTransactionId().getValue());
149
150     }
151
152     @Test
153     public void testDuplicateRoles() throws Exception {
154         // set role to slave
155
156         RoleRequestOutput roleRequestOutput = (new RoleRequestOutputBuilder())
157                 .setXid(testXid).setGenerationId(BigInteger.valueOf(1)).build();
158         ListenableFuture<RpcResult<RoleRequestOutput>> futureOutput =
159                 RpcResultBuilder.<RoleRequestOutput>success().withResult(roleRequestOutput).buildFuture();
160
161         Mockito.when(mockRequestContext.getFuture()).thenReturn(futureOutput);
162
163         SalRoleService salRoleService = new SalRoleServiceImpl(mockRequestContextStack, mockDeviceContext);
164
165         SetRoleInput setRoleInput = new SetRoleInputBuilder()
166                 .setControllerRole(OfpRole.BECOMESLAVE)
167                 .setNode(nodeRef)
168                 .build();
169
170         Future<RpcResult<SetRoleOutput>> future = salRoleService.setRole(setRoleInput);
171
172         RpcResult<SetRoleOutput> roleOutputRpcResult = future.get(5, TimeUnit.SECONDS);
173         assertNotNull("RpcResult from future cannot be null.", roleOutputRpcResult);
174         assertTrue("RpcResult from future is not successful.", roleOutputRpcResult.isSuccessful());
175
176         SetRoleOutput setRoleOutput = roleOutputRpcResult.getResult();
177         assertNotNull(setRoleOutput);
178         assertEquals(BigInteger.valueOf(testXid), setRoleOutput.getTransactionId().getValue());
179
180         // make another role change with the same role - slave
181         Future<RpcResult<SetRoleOutput>> future2 = salRoleService.setRole(setRoleInput);
182         RpcResult<SetRoleOutput> roleOutputRpcResult2 = future2.get(5, TimeUnit.SECONDS);
183         assertNotNull("RpcResult from future cannot be null.", roleOutputRpcResult2);
184         assertTrue("RpcResult from future for duplicate role is not successful.", roleOutputRpcResult2.isSuccessful());
185
186     }
187 }