59827b7cff3f708a5daa455142e81bf96a8decfe
[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.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.OutboundQueue;
23 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
24 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
25 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
26 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
27 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
28 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
29 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
30 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestOutput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestOutputBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SalRoleService;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleInput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleInputBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleOutput;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import org.opendaylight.yangtools.yang.common.RpcResult;
48 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
49 import org.opendaylight.yangtools.yang.common.Uint32;
50 import org.opendaylight.yangtools.yang.common.Uint64;
51 import org.opendaylight.yangtools.yang.common.Uint8;
52
53 public class SalRoleServiceImplTest {
54
55     @Mock
56     private RequestContextStack mockRequestContextStack;
57
58     @Mock
59     private DeviceContext mockDeviceContext;
60
61     @Mock
62     private FeaturesReply mockFeaturesReply;
63
64     @Mock
65     private ConnectionContext mockConnectionContext;
66
67     @Mock
68     private MessageSpy mockMessageSpy;
69
70     @Mock
71     private RequestContext<RoleRequestOutput> mockRequestContext;
72
73     @Mock
74     private DeviceState mockDeviceState;
75
76     @Mock
77     private DeviceInfo mockDeviceInfo;
78
79     @Mock
80     private GetFeaturesOutput mockFeaturesOutput;
81
82     @Mock
83     private OutboundQueue mockOutboundQueue;
84
85     private final NodeId testNodeId = new NodeId(Uri.getDefaultInstance("openflow:1"));
86
87     private static long testXid = 100L;
88
89     private static final String ROLEREQUESTFAILED = org.opendaylight.yang.gen.v1.urn
90             .opendaylight.openflow.common.types.rev130731.ErrorType.ROLEREQUESTFAILED.name();
91
92     private static final String ROLES_UNSUPPORTED = "Device reported error type " + ROLEREQUESTFAILED + " code UNSUP";
93
94     private NodeRef nodeRef;
95
96     @Before
97     public void setup() {
98         MockitoAnnotations.initMocks(this);
99         Mockito.when(mockDeviceInfo.getNodeId()).thenReturn(testNodeId);
100         Mockito.when(mockDeviceInfo.getDatapathId()).thenReturn(Uint64.valueOf(10));
101         Uint8 testVersion = Uint8.valueOf(4);
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(Uint32.valueOf(testXid)));
112         Mockito.when(mockConnectionContext.getOutboundQueueProvider()).thenReturn(mockOutboundQueue);
113         Mockito.when(mockDeviceContext.getPrimaryConnectionContext().getConnectionState())
114                 .thenReturn(ConnectionContext.CONNECTION_STATE.WORKING);
115
116         NodeKey key = new NodeKey(testNodeId);
117         InstanceIdentifier<Node> path = InstanceIdentifier.builder(Nodes.class)
118                 .child(Node.class, key)
119                 .build();
120         nodeRef = new NodeRef(path);
121
122     }
123
124     @Test
125     public void testSetRole() throws Exception {
126         RoleRequestOutput roleRequestOutput = new RoleRequestOutputBuilder()
127                 .setXid(testXid).setGenerationId(Uint64.ONE).build();
128         ListenableFuture<RpcResult<RoleRequestOutput>> futureOutput =
129                 RpcResultBuilder.<RoleRequestOutput>success().withResult(roleRequestOutput).buildFuture();
130
131         Mockito.when(mockRequestContext.getFuture()).thenReturn(futureOutput);
132
133
134         SalRoleService salRoleService = new SalRoleServiceImpl(mockRequestContextStack, mockDeviceContext);
135
136         SetRoleInput setRoleInput = new SetRoleInputBuilder()
137                 .setControllerRole(OfpRole.BECOMESLAVE)
138                 .setNode(nodeRef)
139                 .build();
140
141         Future<RpcResult<SetRoleOutput>> future = salRoleService.setRole(setRoleInput);
142
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());
146
147         SetRoleOutput setRoleOutput = roleOutputRpcResult.getResult();
148         assertNotNull(setRoleOutput);
149         assertEquals(Uint64.valueOf(testXid), setRoleOutput.getTransactionId().getValue());
150
151     }
152
153     @Test
154     public void testDuplicateRoles() throws Exception {
155         // set role to slave
156
157         RoleRequestOutput roleRequestOutput = new RoleRequestOutputBuilder()
158                 .setXid(testXid).setGenerationId(Uint64.ONE).build();
159         ListenableFuture<RpcResult<RoleRequestOutput>> futureOutput =
160                 RpcResultBuilder.<RoleRequestOutput>success().withResult(roleRequestOutput).buildFuture();
161
162         Mockito.when(mockRequestContext.getFuture()).thenReturn(futureOutput);
163
164         SalRoleService salRoleService = new SalRoleServiceImpl(mockRequestContextStack, mockDeviceContext);
165
166         SetRoleInput setRoleInput = new SetRoleInputBuilder()
167                 .setControllerRole(OfpRole.BECOMESLAVE)
168                 .setNode(nodeRef)
169                 .build();
170
171         Future<RpcResult<SetRoleOutput>> future = salRoleService.setRole(setRoleInput);
172
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());
176
177         SetRoleOutput setRoleOutput = roleOutputRpcResult.getResult();
178         assertNotNull(setRoleOutput);
179         assertEquals(Uint64.valueOf(testXid), setRoleOutput.getTransactionId().getValue());
180
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());
186
187     }
188 }