Bug 5592 - NodeId+DatapathId in Services (remove dependency on DeviceContext)
[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.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.rev100924.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
50 /**
51  * Created by kramesha on 8/27/15.
52  */
53 public class SalRoleServiceImplTest {
54
55     @Mock
56     private RequestContextStack mockRequestContextStack;
57
58     @Mock
59     private DeviceContext mockDeviceContext;
60
61     @Mock
62     private ConnectionAdapter mockConnectionAdapter;
63
64     @Mock
65     private FeaturesReply mockFeaturesReply;
66
67     @Mock
68     private ConnectionContext mockConnectionContext;
69
70     @Mock
71     private MessageSpy mockMessageSpy;
72
73     @Mock
74     private RequestContext<RoleRequestOutput> mockRequestContext;
75
76     @Mock
77     private DeviceState mockDeviceState;
78
79     @Mock
80     private GetFeaturesOutput mockFeaturesOutput;
81
82     @Mock
83     private OutboundQueue mockOutboundQueue;
84
85     private NodeId testNodeId = new NodeId(Uri.getDefaultInstance("openflow:1"));
86
87     private static short testVersion = 4;
88
89     private static long testXid = 100L;
90
91     private NodeRef nodeRef;
92
93     @Before
94     public void setup() {
95         MockitoAnnotations.initMocks(this);
96         Mockito.when(mockDeviceState.getNodeId()).thenReturn(testNodeId);
97         Mockito.when(mockDeviceState.getFeatures()).thenReturn(mockFeaturesOutput);
98         Mockito.when(mockFeaturesOutput.getVersion()).thenReturn(testVersion);
99         Mockito.when(mockDeviceContext.getDeviceState()).thenReturn(mockDeviceState);
100         Mockito.when(mockDeviceContext.getPrimaryConnectionContext()).thenReturn(mockConnectionContext);
101         Mockito.when(mockConnectionContext.getFeatures()).thenReturn(mockFeaturesReply);
102         Mockito.when(mockConnectionContext.getNodeId()).thenReturn(testNodeId);
103         Mockito.when(mockFeaturesReply.getVersion()).thenReturn(testVersion);
104         Mockito.when(mockDeviceContext.getMessageSpy()).thenReturn(mockMessageSpy);
105         Mockito.when(mockRequestContextStack.<RoleRequestOutput>createRequestContext()).thenReturn(mockRequestContext);
106         Mockito.when(mockRequestContext.getXid()).thenReturn(new Xid(testXid));
107         Mockito.when(mockConnectionContext.getOutboundQueueProvider()).thenReturn(mockOutboundQueue);
108         Mockito.when(mockDeviceContext.getPrimaryConnectionContext().getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.WORKING);
109
110         NodeKey key = new NodeKey(testNodeId);
111         InstanceIdentifier<Node> path = InstanceIdentifier.<Nodes>builder(Nodes.class)
112                 .<Node, NodeKey>child(Node.class, key)
113                 .build();
114         nodeRef = new NodeRef(path);
115
116     }
117
118     @Test
119     public void testSetRole() throws Exception {
120         RoleRequestOutput roleRequestOutput = (new RoleRequestOutputBuilder())
121                 .setXid(testXid).setGenerationId(BigInteger.valueOf(1)).build();
122         ListenableFuture<RpcResult<RoleRequestOutput>> futureOutput =
123                 RpcResultBuilder.<RoleRequestOutput>success().withResult(roleRequestOutput).buildFuture();
124
125         Mockito.when(mockRequestContext.getFuture()).thenReturn(futureOutput);
126
127
128         SalRoleService salRoleService = new SalRoleServiceImpl(mockRequestContextStack, mockDeviceContext);
129
130         SetRoleInput setRoleInput = new SetRoleInputBuilder()
131                 .setControllerRole(OfpRole.BECOMESLAVE)
132                 .setNode(nodeRef)
133                 .build();
134
135         Future<RpcResult<SetRoleOutput>> future = salRoleService.setRole(setRoleInput);
136
137         RpcResult<SetRoleOutput> roleOutputRpcResult = future.get(5, TimeUnit.SECONDS);
138         assertNotNull("RpcResult from future cannot be null.", roleOutputRpcResult);
139         assertTrue("RpcResult from future is not successful.", roleOutputRpcResult.isSuccessful());
140
141         SetRoleOutput setRoleOutput = roleOutputRpcResult.getResult();
142         assertNotNull(setRoleOutput);
143         assertEquals(BigInteger.valueOf(testXid), setRoleOutput.getTransactionId().getValue());
144
145     }
146
147     @Test
148     public void testDuplicateRoles() throws Exception {
149         // set role to slave
150
151         RoleRequestOutput roleRequestOutput = (new RoleRequestOutputBuilder())
152                 .setXid(testXid).setGenerationId(BigInteger.valueOf(1)).build();
153         ListenableFuture<RpcResult<RoleRequestOutput>> futureOutput =
154                 RpcResultBuilder.<RoleRequestOutput>success().withResult(roleRequestOutput).buildFuture();
155
156         Mockito.when(mockRequestContext.getFuture()).thenReturn(futureOutput);
157
158         SalRoleService salRoleService = new SalRoleServiceImpl(mockRequestContextStack, mockDeviceContext);
159
160         SetRoleInput setRoleInput = new SetRoleInputBuilder()
161                 .setControllerRole(OfpRole.BECOMESLAVE)
162                 .setNode(nodeRef)
163                 .build();
164
165         Future<RpcResult<SetRoleOutput>> future = salRoleService.setRole(setRoleInput);
166
167         RpcResult<SetRoleOutput> roleOutputRpcResult = future.get(5, TimeUnit.SECONDS);
168         assertNotNull("RpcResult from future cannot be null.", roleOutputRpcResult);
169         assertTrue("RpcResult from future is not successful.", roleOutputRpcResult.isSuccessful());
170
171         SetRoleOutput setRoleOutput = roleOutputRpcResult.getResult();
172         assertNotNull(setRoleOutput);
173         assertEquals(BigInteger.valueOf(testXid), setRoleOutput.getTransactionId().getValue());
174
175         // make another role change with the same role - slave
176         Future<RpcResult<SetRoleOutput>> future2 = salRoleService.setRole(setRoleInput);
177         RpcResult<SetRoleOutput> roleOutputRpcResult2 = future2.get(5, TimeUnit.SECONDS);
178         assertNotNull("RpcResult from future cannot be null.", roleOutputRpcResult2);
179         assertTrue("RpcResult from future for duplicate role is not successful.", roleOutputRpcResult2.isSuccessful());
180
181     }
182 }