2 * Copyright (c) 2015 Cisco Systems, Inc. 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;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.math.BigInteger;
15 import java.util.concurrent.Future;
16 import java.util.concurrent.TimeUnit;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.mockito.Mock;
20 import org.mockito.Mockito;
21 import org.mockito.MockitoAnnotations;
22 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
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.rev100924.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;
52 * Created by kramesha on 8/27/15.
54 public class SalRoleServiceImplTest {
57 private RequestContextStack mockRequestContextStack;
60 private DeviceContext mockDeviceContext;
63 private ConnectionAdapter mockConnectionAdapter;
66 private FeaturesReply mockFeaturesReply;
69 private ConnectionContext mockConnectionContext;
72 private MessageSpy mockMessageSpy;
75 private RequestContext<RoleRequestOutput> mockRequestContext;
78 private DeviceState mockDeviceState;
81 private DeviceInfo mockDeviceInfo;
84 private GetFeaturesOutput mockFeaturesOutput;
87 private OutboundQueue mockOutboundQueue;
89 private NodeId testNodeId = new NodeId(Uri.getDefaultInstance("openflow:1"));
91 private static short testVersion = 4;
93 private static long testXid = 100L;
95 private NodeRef nodeRef;
99 MockitoAnnotations.initMocks(this);
100 Mockito.when(mockDeviceInfo.getNodeId()).thenReturn(testNodeId);
101 Mockito.when(mockDeviceInfo.getDatapathId()).thenReturn(BigInteger.TEN);
102 Mockito.when(mockFeaturesOutput.getVersion()).thenReturn(testVersion);
103 Mockito.when(mockDeviceContext.getDeviceState()).thenReturn(mockDeviceState);
104 Mockito.when(mockDeviceContext.getDeviceInfo()).thenReturn(mockDeviceInfo);
105 Mockito.when(mockDeviceContext.getPrimaryConnectionContext()).thenReturn(mockConnectionContext);
106 Mockito.when(mockConnectionContext.getFeatures()).thenReturn(mockFeaturesReply);
107 Mockito.when(mockConnectionContext.getNodeId()).thenReturn(testNodeId);
108 Mockito.when(mockFeaturesReply.getVersion()).thenReturn(testVersion);
109 Mockito.when(mockDeviceContext.getMessageSpy()).thenReturn(mockMessageSpy);
110 Mockito.when(mockRequestContextStack.<RoleRequestOutput>createRequestContext()).thenReturn(mockRequestContext);
111 Mockito.when(mockRequestContext.getXid()).thenReturn(new Xid(testXid));
112 Mockito.when(mockConnectionContext.getOutboundQueueProvider()).thenReturn(mockOutboundQueue);
113 Mockito.when(mockDeviceContext.getPrimaryConnectionContext().getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.WORKING);
115 NodeKey key = new NodeKey(testNodeId);
116 InstanceIdentifier<Node> path = InstanceIdentifier.<Nodes>builder(Nodes.class)
117 .<Node, NodeKey>child(Node.class, key)
119 nodeRef = new NodeRef(path);
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();
130 Mockito.when(mockRequestContext.getFuture()).thenReturn(futureOutput);
133 SalRoleService salRoleService = new SalRoleServiceImpl(mockRequestContextStack, mockDeviceContext);
135 SetRoleInput setRoleInput = new SetRoleInputBuilder()
136 .setControllerRole(OfpRole.BECOMESLAVE)
140 Future<RpcResult<SetRoleOutput>> future = salRoleService.setRole(setRoleInput);
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());
146 SetRoleOutput setRoleOutput = roleOutputRpcResult.getResult();
147 assertNotNull(setRoleOutput);
148 assertEquals(BigInteger.valueOf(testXid), setRoleOutput.getTransactionId().getValue());
153 public void testDuplicateRoles() throws Exception {
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();
161 Mockito.when(mockRequestContext.getFuture()).thenReturn(futureOutput);
163 SalRoleService salRoleService = new SalRoleServiceImpl(mockRequestContextStack, mockDeviceContext);
165 SetRoleInput setRoleInput = new SetRoleInputBuilder()
166 .setControllerRole(OfpRole.BECOMESLAVE)
170 Future<RpcResult<SetRoleOutput>> future = salRoleService.setRole(setRoleInput);
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());
176 SetRoleOutput setRoleOutput = roleOutputRpcResult.getResult();
177 assertNotNull(setRoleOutput);
178 assertEquals(BigInteger.valueOf(testXid), setRoleOutput.getTransactionId().getValue());
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());