RoleManager,SalRoleService Impl and Test
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / services / SalRoleServiceImplTest.java
1 /**
2  * Copyright (c) 2015 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.impl.services;
9
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.RequestContext;
27 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
28 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
29 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestOutput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestOutputBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SalRoleService;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleInput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleInputBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleOutput;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 import org.opendaylight.yangtools.yang.common.RpcResult;
46 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
47
48 /**
49  * Created by kramesha on 8/27/15.
50  */
51 public class SalRoleServiceImplTest {
52
53     @Mock
54     private RequestContextStack mockRequestContextStack;
55
56     @Mock
57     private DeviceContext mockDeviceContext;
58
59     @Mock
60     private ConnectionAdapter mockConnectionAdapter;
61
62     @Mock
63     private FeaturesReply mockFeaturesReply;
64
65     @Mock
66     private ConnectionContext mockConnectionContext;
67
68     @Mock
69     private MessageSpy mockMessageSpy;
70
71     @Mock
72     private RequestContext<RoleRequestOutput> mockRequestContext;
73
74     @Mock
75     private OutboundQueue mockOutboundQueue;
76
77     private NodeId testNodeId = new NodeId(Uri.getDefaultInstance("openflow:1"));
78
79     private static short testVersion = 4;
80
81     private static long testXid = 100L;
82
83     private NodeRef nodeRef;
84
85     @Before
86     public void setup() {
87         MockitoAnnotations.initMocks(this);
88         Mockito.when(mockDeviceContext.getPrimaryConnectionContext()).thenReturn(mockConnectionContext);
89         Mockito.when(mockConnectionContext.getFeatures()).thenReturn(mockFeaturesReply);
90         Mockito.when(mockConnectionContext.getNodeId()).thenReturn(testNodeId);
91         Mockito.when(mockFeaturesReply.getVersion()).thenReturn(testVersion);
92         Mockito.when(mockDeviceContext.getMessageSpy()).thenReturn(mockMessageSpy);
93         Mockito.when(mockRequestContextStack.<RoleRequestOutput>createRequestContext()).thenReturn(mockRequestContext);
94         Mockito.when(mockRequestContext.getXid()).thenReturn(new Xid(testXid));
95         Mockito.when(mockConnectionContext.getOutboundQueueProvider()).thenReturn(mockOutboundQueue);
96         Mockito.when(mockDeviceContext.getPrimaryConnectionContext().getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.WORKING);
97
98         NodeKey key = new NodeKey(testNodeId);
99         InstanceIdentifier<Node> path = InstanceIdentifier.<Nodes>builder(Nodes.class)
100                 .<Node, NodeKey>child(Node.class, key)
101                 .build();
102         nodeRef = new NodeRef(path);
103
104     }
105
106     @Test
107     public void testSetRole() throws Exception {
108         RoleRequestOutput roleRequestOutput = (new RoleRequestOutputBuilder())
109                 .setXid(testXid).setGenerationId(BigInteger.valueOf(1)).build();
110         ListenableFuture<RpcResult<RoleRequestOutput>> futureOutput =
111                 RpcResultBuilder.<RoleRequestOutput>success().withResult(roleRequestOutput).buildFuture();
112
113         Mockito.when(mockRequestContext.getFuture()).thenReturn(futureOutput);
114
115
116         SalRoleService salRoleService = new SalRoleServiceImpl(mockRequestContextStack, mockDeviceContext);
117
118         SetRoleInput setRoleInput = new SetRoleInputBuilder()
119                 .setControllerRole(OfpRole.BECOMESLAVE)
120                 .setNode(nodeRef)
121                 .build();
122
123         Future<RpcResult<SetRoleOutput>> future = salRoleService.setRole(setRoleInput);
124
125         RpcResult<SetRoleOutput> roleOutputRpcResult = future.get(5, TimeUnit.SECONDS);
126         assertNotNull("RpcResult from future cannot be null.", roleOutputRpcResult);
127         assertTrue("RpcResult from future is not successful.", roleOutputRpcResult.isSuccessful());
128
129         SetRoleOutput setRoleOutput = roleOutputRpcResult.getResult();
130         assertNotNull(setRoleOutput);
131         assertEquals(BigInteger.valueOf(testXid), setRoleOutput.getTransactionId().getValue());
132
133     }
134
135     @Test
136     public void testDuplicateRoles() throws Exception {
137         // set role to slave
138
139         RoleRequestOutput roleRequestOutput = (new RoleRequestOutputBuilder())
140                 .setXid(testXid).setGenerationId(BigInteger.valueOf(1)).build();
141         ListenableFuture<RpcResult<RoleRequestOutput>> futureOutput =
142                 RpcResultBuilder.<RoleRequestOutput>success().withResult(roleRequestOutput).buildFuture();
143
144         Mockito.when(mockRequestContext.getFuture()).thenReturn(futureOutput);
145
146         SalRoleService salRoleService = new SalRoleServiceImpl(mockRequestContextStack, mockDeviceContext);
147
148         SetRoleInput setRoleInput = new SetRoleInputBuilder()
149                 .setControllerRole(OfpRole.BECOMESLAVE)
150                 .setNode(nodeRef)
151                 .build();
152
153         Future<RpcResult<SetRoleOutput>> future = salRoleService.setRole(setRoleInput);
154
155         RpcResult<SetRoleOutput> roleOutputRpcResult = future.get(5, TimeUnit.SECONDS);
156         assertNotNull("RpcResult from future cannot be null.", roleOutputRpcResult);
157         assertTrue("RpcResult from future is not successful.", roleOutputRpcResult.isSuccessful());
158
159         SetRoleOutput setRoleOutput = roleOutputRpcResult.getResult();
160         assertNotNull(setRoleOutput);
161         assertEquals(BigInteger.valueOf(testXid), setRoleOutput.getTransactionId().getValue());
162
163         // make another role change with the same role - slave
164         Future<RpcResult<SetRoleOutput>> future2 = salRoleService.setRole(setRoleInput);
165         RpcResult<SetRoleOutput> roleOutputRpcResult2 = future2.get(5, TimeUnit.SECONDS);
166         assertNotNull("RpcResult from future cannot be null.", roleOutputRpcResult2);
167         assertTrue("RpcResult from future for duplicate role is not successful.", roleOutputRpcResult2.isSuccessful());
168
169     }
170 }