2 * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.impl.services.sal;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import java.math.BigInteger;
17 import java.util.concurrent.Future;
18 import java.util.concurrent.TimeUnit;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.mockito.Mock;
22 import org.mockito.Mockito;
23 import org.mockito.MockitoAnnotations;
24 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
25 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
26 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
27 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
28 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
29 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
30 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
31 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
32 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
33 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestOutput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestOutputBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SalRoleService;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleInput;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleInputBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleOutput;
49 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
50 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
51 import org.opendaylight.yangtools.yang.common.RpcResult;
52 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
55 * Created by kramesha on 8/27/15.
57 public class SalRoleServiceImplTest {
60 private RequestContextStack mockRequestContextStack;
63 private DeviceContext mockDeviceContext;
66 private ConnectionAdapter mockConnectionAdapter;
69 private FeaturesReply mockFeaturesReply;
72 private ConnectionContext mockConnectionContext;
75 private MessageSpy mockMessageSpy;
78 private RequestContext<RoleRequestOutput> mockRequestContext;
81 private DeviceState mockDeviceState;
84 private DeviceInfo mockDeviceInfo;
87 private GetFeaturesOutput mockFeaturesOutput;
90 private OutboundQueue mockOutboundQueue;
92 private NodeId testNodeId = new NodeId(Uri.getDefaultInstance("openflow:1"));
94 private static short testVersion = 4;
96 private static long testXid = 100L;
98 private static final String ROLEREQUESTFAILED =
99 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ErrorType.ROLEREQUESTFAILED.name();
101 private static final String ROLES_UNSUPPORTED = "Device reported error type "+ ROLEREQUESTFAILED +" code UNSUP";
103 private NodeRef nodeRef;
106 public void setup() {
107 MockitoAnnotations.initMocks(this);
108 Mockito.when(mockDeviceInfo.getNodeId()).thenReturn(testNodeId);
109 Mockito.when(mockDeviceInfo.getDatapathId()).thenReturn(BigInteger.TEN);
110 Mockito.when(mockFeaturesOutput.getVersion()).thenReturn(testVersion);
111 Mockito.when(mockDeviceContext.getDeviceState()).thenReturn(mockDeviceState);
112 Mockito.when(mockDeviceContext.getDeviceInfo()).thenReturn(mockDeviceInfo);
113 Mockito.when(mockDeviceContext.getPrimaryConnectionContext()).thenReturn(mockConnectionContext);
114 Mockito.when(mockConnectionContext.getFeatures()).thenReturn(mockFeaturesReply);
115 Mockito.when(mockConnectionContext.getNodeId()).thenReturn(testNodeId);
116 Mockito.when(mockFeaturesReply.getVersion()).thenReturn(testVersion);
117 Mockito.when(mockDeviceContext.getMessageSpy()).thenReturn(mockMessageSpy);
118 Mockito.when(mockRequestContextStack.<RoleRequestOutput>createRequestContext()).thenReturn(mockRequestContext);
119 Mockito.when(mockRequestContext.getXid()).thenReturn(new Xid(testXid));
120 Mockito.when(mockConnectionContext.getOutboundQueueProvider()).thenReturn(mockOutboundQueue);
121 Mockito.when(mockDeviceContext.getPrimaryConnectionContext().getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.WORKING);
123 NodeKey key = new NodeKey(testNodeId);
124 InstanceIdentifier<Node> path = InstanceIdentifier.<Nodes>builder(Nodes.class)
125 .<Node, NodeKey>child(Node.class, key)
127 nodeRef = new NodeRef(path);
132 public void testSetRole() throws Exception {
133 RoleRequestOutput roleRequestOutput = (new RoleRequestOutputBuilder())
134 .setXid(testXid).setGenerationId(BigInteger.valueOf(1)).build();
135 ListenableFuture<RpcResult<RoleRequestOutput>> futureOutput =
136 RpcResultBuilder.<RoleRequestOutput>success().withResult(roleRequestOutput).buildFuture();
138 Mockito.when(mockRequestContext.getFuture()).thenReturn(futureOutput);
141 SalRoleService salRoleService = new SalRoleServiceImpl(mockRequestContextStack, mockDeviceContext);
143 SetRoleInput setRoleInput = new SetRoleInputBuilder()
144 .setControllerRole(OfpRole.BECOMESLAVE)
148 Future<RpcResult<SetRoleOutput>> future = salRoleService.setRole(setRoleInput);
150 RpcResult<SetRoleOutput> roleOutputRpcResult = future.get(5, TimeUnit.SECONDS);
151 assertNotNull("RpcResult from future cannot be null.", roleOutputRpcResult);
152 assertTrue("RpcResult from future is not successful.", roleOutputRpcResult.isSuccessful());
154 SetRoleOutput setRoleOutput = roleOutputRpcResult.getResult();
155 assertNotNull(setRoleOutput);
156 assertEquals(BigInteger.valueOf(testXid), setRoleOutput.getTransactionId().getValue());
161 public void testSetRoleUnsupported() throws Exception {
162 ListenableFuture<RpcResult<RoleRequestOutput>> futureOutput =
163 RpcResultBuilder.<RoleRequestOutput>failed()
164 .withError(ErrorType.APPLICATION, ROLES_UNSUPPORTED)
167 Mockito.when(mockRequestContext.getFuture()).thenReturn(futureOutput);
169 SalRoleService salRoleService = new SalRoleServiceImpl(mockRequestContextStack, mockDeviceContext);
171 SetRoleInput setRoleInput = new SetRoleInputBuilder()
172 .setControllerRole(OfpRole.BECOMESLAVE)
176 Future<RpcResult<SetRoleOutput>> future = salRoleService.setRole(setRoleInput);
178 RpcResult<SetRoleOutput> roleOutputRpcResult = future.get(5, TimeUnit.SECONDS);
179 assertNotNull("RpcResult from future cannot be null.", roleOutputRpcResult);
180 assertFalse("RpcResult from future is successful.", roleOutputRpcResult.isSuccessful());
181 assertEquals(ROLES_UNSUPPORTED, roleOutputRpcResult
182 .getErrors().iterator().next().getMessage());
186 public void testDuplicateRoles() throws Exception {
189 RoleRequestOutput roleRequestOutput = (new RoleRequestOutputBuilder())
190 .setXid(testXid).setGenerationId(BigInteger.valueOf(1)).build();
191 ListenableFuture<RpcResult<RoleRequestOutput>> futureOutput =
192 RpcResultBuilder.<RoleRequestOutput>success().withResult(roleRequestOutput).buildFuture();
194 Mockito.when(mockRequestContext.getFuture()).thenReturn(futureOutput);
196 SalRoleService salRoleService = new SalRoleServiceImpl(mockRequestContextStack, mockDeviceContext);
198 SetRoleInput setRoleInput = new SetRoleInputBuilder()
199 .setControllerRole(OfpRole.BECOMESLAVE)
203 Future<RpcResult<SetRoleOutput>> future = salRoleService.setRole(setRoleInput);
205 RpcResult<SetRoleOutput> roleOutputRpcResult = future.get(5, TimeUnit.SECONDS);
206 assertNotNull("RpcResult from future cannot be null.", roleOutputRpcResult);
207 assertTrue("RpcResult from future is not successful.", roleOutputRpcResult.isSuccessful());
209 SetRoleOutput setRoleOutput = roleOutputRpcResult.getResult();
210 assertNotNull(setRoleOutput);
211 assertEquals(BigInteger.valueOf(testXid), setRoleOutput.getTransactionId().getValue());
213 // make another role change with the same role - slave
214 Future<RpcResult<SetRoleOutput>> future2 = salRoleService.setRole(setRoleInput);
215 RpcResult<SetRoleOutput> roleOutputRpcResult2 = future2.get(5, TimeUnit.SECONDS);
216 assertNotNull("RpcResult from future cannot be null.", roleOutputRpcResult2);
217 assertTrue("RpcResult from future for duplicate role is not successful.", roleOutputRpcResult2.isSuccessful());