Fix codestyle
[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 long testXid = 100L;
86
87     private static final String ROLEREQUESTFAILED = org.opendaylight.yang.gen.v1.urn
88             .opendaylight.openflow.common.types.rev130731.ErrorType.ROLEREQUESTFAILED.name();
89
90     private static final String ROLES_UNSUPPORTED = "Device reported error type " + ROLEREQUESTFAILED + " code UNSUP";
91
92     private NodeRef nodeRef;
93
94     @Before
95     public void setup() {
96         MockitoAnnotations.initMocks(this);
97         Mockito.when(mockDeviceInfo.getNodeId()).thenReturn(testNodeId);
98         Mockito.when(mockDeviceInfo.getDatapathId()).thenReturn(BigInteger.TEN);
99         short testVersion = 4;
100         Mockito.when(mockFeaturesOutput.getVersion()).thenReturn(testVersion);
101         Mockito.when(mockDeviceContext.getDeviceState()).thenReturn(mockDeviceState);
102         Mockito.when(mockDeviceContext.getDeviceInfo()).thenReturn(mockDeviceInfo);
103         Mockito.when(mockDeviceContext.getPrimaryConnectionContext()).thenReturn(mockConnectionContext);
104         Mockito.when(mockConnectionContext.getFeatures()).thenReturn(mockFeaturesReply);
105         Mockito.when(mockConnectionContext.getNodeId()).thenReturn(testNodeId);
106         Mockito.when(mockFeaturesReply.getVersion()).thenReturn(testVersion);
107         Mockito.when(mockDeviceContext.getMessageSpy()).thenReturn(mockMessageSpy);
108         Mockito.when(mockRequestContextStack.<RoleRequestOutput>createRequestContext()).thenReturn(mockRequestContext);
109         Mockito.when(mockRequestContext.getXid()).thenReturn(new Xid(testXid));
110         Mockito.when(mockConnectionContext.getOutboundQueueProvider()).thenReturn(mockOutboundQueue);
111         Mockito.when(mockDeviceContext.getPrimaryConnectionContext().getConnectionState())
112                 .thenReturn(ConnectionContext.CONNECTION_STATE.WORKING);
113
114         NodeKey key = new NodeKey(testNodeId);
115         InstanceIdentifier<Node> path = InstanceIdentifier.<Nodes>builder(Nodes.class)
116                 .<Node, NodeKey>child(Node.class, key)
117                 .build();
118         nodeRef = new NodeRef(path);
119
120     }
121
122     @Test
123     public void testSetRole() throws Exception {
124         RoleRequestOutput roleRequestOutput = (new RoleRequestOutputBuilder())
125                 .setXid(testXid).setGenerationId(BigInteger.valueOf(1)).build();
126         ListenableFuture<RpcResult<RoleRequestOutput>> futureOutput =
127                 RpcResultBuilder.<RoleRequestOutput>success().withResult(roleRequestOutput).buildFuture();
128
129         Mockito.when(mockRequestContext.getFuture()).thenReturn(futureOutput);
130
131
132         SalRoleService salRoleService = new SalRoleServiceImpl(mockRequestContextStack, mockDeviceContext);
133
134         SetRoleInput setRoleInput = new SetRoleInputBuilder()
135                 .setControllerRole(OfpRole.BECOMESLAVE)
136                 .setNode(nodeRef)
137                 .build();
138
139         Future<RpcResult<SetRoleOutput>> future = salRoleService.setRole(setRoleInput);
140
141         RpcResult<SetRoleOutput> roleOutputRpcResult = future.get(5, TimeUnit.SECONDS);
142         assertNotNull("RpcResult from future cannot be null.", roleOutputRpcResult);
143         assertTrue("RpcResult from future is not successful.", roleOutputRpcResult.isSuccessful());
144
145         SetRoleOutput setRoleOutput = roleOutputRpcResult.getResult();
146         assertNotNull(setRoleOutput);
147         assertEquals(BigInteger.valueOf(testXid), setRoleOutput.getTransactionId().getValue());
148
149     }
150
151     @Test
152     public void testDuplicateRoles() throws Exception {
153         // set role to slave
154
155         RoleRequestOutput roleRequestOutput = (new RoleRequestOutputBuilder())
156                 .setXid(testXid).setGenerationId(BigInteger.valueOf(1)).build();
157         ListenableFuture<RpcResult<RoleRequestOutput>> futureOutput =
158                 RpcResultBuilder.<RoleRequestOutput>success().withResult(roleRequestOutput).buildFuture();
159
160         Mockito.when(mockRequestContext.getFuture()).thenReturn(futureOutput);
161
162         SalRoleService salRoleService = new SalRoleServiceImpl(mockRequestContextStack, mockDeviceContext);
163
164         SetRoleInput setRoleInput = new SetRoleInputBuilder()
165                 .setControllerRole(OfpRole.BECOMESLAVE)
166                 .setNode(nodeRef)
167                 .build();
168
169         Future<RpcResult<SetRoleOutput>> future = salRoleService.setRole(setRoleInput);
170
171         RpcResult<SetRoleOutput> roleOutputRpcResult = future.get(5, TimeUnit.SECONDS);
172         assertNotNull("RpcResult from future cannot be null.", roleOutputRpcResult);
173         assertTrue("RpcResult from future is not successful.", roleOutputRpcResult.isSuccessful());
174
175         SetRoleOutput setRoleOutput = roleOutputRpcResult.getResult();
176         assertNotNull(setRoleOutput);
177         assertEquals(BigInteger.valueOf(testXid), setRoleOutput.getTransactionId().getValue());
178
179         // make another role change with the same role - slave
180         Future<RpcResult<SetRoleOutput>> future2 = salRoleService.setRole(setRoleInput);
181         RpcResult<SetRoleOutput> roleOutputRpcResult2 = future2.get(5, TimeUnit.SECONDS);
182         assertNotNull("RpcResult from future cannot be null.", roleOutputRpcResult2);
183         assertTrue("RpcResult from future for duplicate role is not successful.", roleOutputRpcResult2.isSuccessful());
184
185     }
186 }