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;
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.ConnectionAdapter;
24 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
25 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
26 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
27 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
28 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
29 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
30 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
31 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
32 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestOutput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestOutputBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SalRoleService;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleInput;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleInputBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleOutput;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
49 import org.opendaylight.yangtools.yang.common.RpcResult;
50 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
53 * Created by kramesha on 8/27/15.
55 public class SalRoleServiceImplTest {
58 private RequestContextStack mockRequestContextStack;
61 private DeviceContext mockDeviceContext;
64 private ConnectionAdapter mockConnectionAdapter;
67 private FeaturesReply mockFeaturesReply;
70 private ConnectionContext mockConnectionContext;
73 private MessageSpy mockMessageSpy;
76 private RequestContext<RoleRequestOutput> mockRequestContext;
79 private DeviceState mockDeviceState;
82 private DeviceInfo mockDeviceInfo;
85 private GetFeaturesOutput mockFeaturesOutput;
88 private OutboundQueue mockOutboundQueue;
90 private NodeId testNodeId = new NodeId(Uri.getDefaultInstance("openflow:1"));
92 private static short testVersion = 4;
94 private static long testXid = 100L;
96 private NodeRef nodeRef;
100 MockitoAnnotations.initMocks(this);
101 Mockito.when(mockDeviceInfo.getNodeId()).thenReturn(testNodeId);
102 Mockito.when(mockDeviceInfo.getDatapathId()).thenReturn(BigInteger.TEN);
103 Mockito.when(mockFeaturesOutput.getVersion()).thenReturn(testVersion);
104 Mockito.when(mockDeviceContext.getDeviceState()).thenReturn(mockDeviceState);
105 Mockito.when(mockDeviceContext.getDeviceInfo()).thenReturn(mockDeviceInfo);
106 Mockito.when(mockDeviceContext.getPrimaryConnectionContext()).thenReturn(mockConnectionContext);
107 Mockito.when(mockConnectionContext.getFeatures()).thenReturn(mockFeaturesReply);
108 Mockito.when(mockConnectionContext.getNodeId()).thenReturn(testNodeId);
109 Mockito.when(mockFeaturesReply.getVersion()).thenReturn(testVersion);
110 Mockito.when(mockDeviceContext.getMessageSpy()).thenReturn(mockMessageSpy);
111 Mockito.when(mockRequestContextStack.<RoleRequestOutput>createRequestContext()).thenReturn(mockRequestContext);
112 Mockito.when(mockRequestContext.getXid()).thenReturn(new Xid(testXid));
113 Mockito.when(mockConnectionContext.getOutboundQueueProvider()).thenReturn(mockOutboundQueue);
114 Mockito.when(mockDeviceContext.getPrimaryConnectionContext().getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.WORKING);
116 NodeKey key = new NodeKey(testNodeId);
117 InstanceIdentifier<Node> path = InstanceIdentifier.<Nodes>builder(Nodes.class)
118 .<Node, NodeKey>child(Node.class, key)
120 nodeRef = new NodeRef(path);
125 public void testSetRole() throws Exception {
126 RoleRequestOutput roleRequestOutput = (new RoleRequestOutputBuilder())
127 .setXid(testXid).setGenerationId(BigInteger.valueOf(1)).build();
128 ListenableFuture<RpcResult<RoleRequestOutput>> futureOutput =
129 RpcResultBuilder.<RoleRequestOutput>success().withResult(roleRequestOutput).buildFuture();
131 Mockito.when(mockRequestContext.getFuture()).thenReturn(futureOutput);
134 SalRoleService salRoleService = new SalRoleServiceImpl(mockRequestContextStack, mockDeviceContext);
136 SetRoleInput setRoleInput = new SetRoleInputBuilder()
137 .setControllerRole(OfpRole.BECOMESLAVE)
141 Future<RpcResult<SetRoleOutput>> future = salRoleService.setRole(setRoleInput);
143 RpcResult<SetRoleOutput> roleOutputRpcResult = future.get(5, TimeUnit.SECONDS);
144 assertNotNull("RpcResult from future cannot be null.", roleOutputRpcResult);
145 assertTrue("RpcResult from future is not successful.", roleOutputRpcResult.isSuccessful());
147 SetRoleOutput setRoleOutput = roleOutputRpcResult.getResult();
148 assertNotNull(setRoleOutput);
149 assertEquals(BigInteger.valueOf(testXid), setRoleOutput.getTransactionId().getValue());
154 public void testDuplicateRoles() throws Exception {
157 RoleRequestOutput roleRequestOutput = (new RoleRequestOutputBuilder())
158 .setXid(testXid).setGenerationId(BigInteger.valueOf(1)).build();
159 ListenableFuture<RpcResult<RoleRequestOutput>> futureOutput =
160 RpcResultBuilder.<RoleRequestOutput>success().withResult(roleRequestOutput).buildFuture();
162 Mockito.when(mockRequestContext.getFuture()).thenReturn(futureOutput);
164 SalRoleService salRoleService = new SalRoleServiceImpl(mockRequestContextStack, mockDeviceContext);
166 SetRoleInput setRoleInput = new SetRoleInputBuilder()
167 .setControllerRole(OfpRole.BECOMESLAVE)
171 Future<RpcResult<SetRoleOutput>> future = salRoleService.setRole(setRoleInput);
173 RpcResult<SetRoleOutput> roleOutputRpcResult = future.get(5, TimeUnit.SECONDS);
174 assertNotNull("RpcResult from future cannot be null.", roleOutputRpcResult);
175 assertTrue("RpcResult from future is not successful.", roleOutputRpcResult.isSuccessful());
177 SetRoleOutput setRoleOutput = roleOutputRpcResult.getResult();
178 assertNotNull(setRoleOutput);
179 assertEquals(BigInteger.valueOf(testXid), setRoleOutput.getTransactionId().getValue());
181 // make another role change with the same role - slave
182 Future<RpcResult<SetRoleOutput>> future2 = salRoleService.setRole(setRoleInput);
183 RpcResult<SetRoleOutput> roleOutputRpcResult2 = future2.get(5, TimeUnit.SECONDS);
184 assertNotNull("RpcResult from future cannot be null.", roleOutputRpcResult2);
185 assertTrue("RpcResult from future for duplicate role is not successful.", roleOutputRpcResult2.isSuccessful());