Guard lifecycle of contexts
[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.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14
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;
53
54 /**
55  * Created by kramesha on 8/27/15.
56  */
57 public class SalRoleServiceImplTest {
58
59     @Mock
60     private RequestContextStack mockRequestContextStack;
61
62     @Mock
63     private DeviceContext mockDeviceContext;
64
65     @Mock
66     private ConnectionAdapter mockConnectionAdapter;
67
68     @Mock
69     private FeaturesReply mockFeaturesReply;
70
71     @Mock
72     private ConnectionContext mockConnectionContext;
73
74     @Mock
75     private MessageSpy mockMessageSpy;
76
77     @Mock
78     private RequestContext<RoleRequestOutput> mockRequestContext;
79
80     @Mock
81     private DeviceState mockDeviceState;
82
83     @Mock
84     private DeviceInfo mockDeviceInfo;
85
86     @Mock
87     private GetFeaturesOutput mockFeaturesOutput;
88
89     @Mock
90     private OutboundQueue mockOutboundQueue;
91
92     private NodeId testNodeId = new NodeId(Uri.getDefaultInstance("openflow:1"));
93
94     private static short testVersion = 4;
95
96     private static long testXid = 100L;
97
98     private static final String ROLEREQUESTFAILED =
99             org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ErrorType.ROLEREQUESTFAILED.name();
100
101     private static final String ROLES_UNSUPPORTED = "Device reported error type "+ ROLEREQUESTFAILED +" code UNSUP";
102
103     private NodeRef nodeRef;
104
105     @Before
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);
122
123         NodeKey key = new NodeKey(testNodeId);
124         InstanceIdentifier<Node> path = InstanceIdentifier.<Nodes>builder(Nodes.class)
125                 .<Node, NodeKey>child(Node.class, key)
126                 .build();
127         nodeRef = new NodeRef(path);
128
129     }
130
131     @Test
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();
137
138         Mockito.when(mockRequestContext.getFuture()).thenReturn(futureOutput);
139
140
141         SalRoleService salRoleService = new SalRoleServiceImpl(mockRequestContextStack, mockDeviceContext);
142
143         SetRoleInput setRoleInput = new SetRoleInputBuilder()
144                 .setControllerRole(OfpRole.BECOMESLAVE)
145                 .setNode(nodeRef)
146                 .build();
147
148         Future<RpcResult<SetRoleOutput>> future = salRoleService.setRole(setRoleInput);
149
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());
153
154         SetRoleOutput setRoleOutput = roleOutputRpcResult.getResult();
155         assertNotNull(setRoleOutput);
156         assertEquals(BigInteger.valueOf(testXid), setRoleOutput.getTransactionId().getValue());
157
158     }
159
160     @Test
161     public void testSetRoleUnsupported() throws Exception {
162         ListenableFuture<RpcResult<RoleRequestOutput>> futureOutput =
163                 RpcResultBuilder.<RoleRequestOutput>failed()
164                         .withError(ErrorType.APPLICATION, ROLES_UNSUPPORTED)
165                         .buildFuture();
166
167         Mockito.when(mockRequestContext.getFuture()).thenReturn(futureOutput);
168
169         SalRoleService salRoleService = new SalRoleServiceImpl(mockRequestContextStack, mockDeviceContext);
170
171         SetRoleInput setRoleInput = new SetRoleInputBuilder()
172                 .setControllerRole(OfpRole.BECOMESLAVE)
173                 .setNode(nodeRef)
174                 .build();
175
176         Future<RpcResult<SetRoleOutput>> future = salRoleService.setRole(setRoleInput);
177
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());
183     }
184
185     @Test
186     public void testDuplicateRoles() throws Exception {
187         // set role to slave
188
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();
193
194         Mockito.when(mockRequestContext.getFuture()).thenReturn(futureOutput);
195
196         SalRoleService salRoleService = new SalRoleServiceImpl(mockRequestContextStack, mockDeviceContext);
197
198         SetRoleInput setRoleInput = new SetRoleInputBuilder()
199                 .setControllerRole(OfpRole.BECOMESLAVE)
200                 .setNode(nodeRef)
201                 .build();
202
203         Future<RpcResult<SetRoleOutput>> future = salRoleService.setRole(setRoleInput);
204
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());
208
209         SetRoleOutput setRoleOutput = roleOutputRpcResult.getResult();
210         assertNotNull(setRoleOutput);
211         assertEquals(BigInteger.valueOf(testXid), setRoleOutput.getTransactionId().getValue());
212
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());
218
219     }
220 }