Merge "BUG-4099: Groups pointing to ports"
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / role / RoleContextImplTest.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.role;
9
10 import static org.mockito.Mockito.verify;
11 import static org.mockito.Mockito.when;
12 import com.google.common.util.concurrent.SettableFuture;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.mockito.ArgumentMatcher;
16 import org.mockito.Matchers;
17 import org.mockito.Mock;
18 import org.mockito.MockitoAnnotations;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
21 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
22 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
23 import org.opendaylight.openflowplugin.api.OFConstants;
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.role.RoleManager;
28 import org.opendaylight.openflowplugin.impl.util.DeviceStateUtil;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SalRoleService;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleInput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleOutput;
39 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
40 import org.opendaylight.yangtools.yang.common.RpcResult;
41 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
42
43 /**
44  * Created by kramesha on 9/1/15.
45  */
46 public class RoleContextImplTest {
47
48     @Mock
49     private EntityOwnershipService entityOwnershipService;
50
51     @Mock
52     private DataBroker dataBroker;
53
54     @Mock
55     private RpcProviderRegistry rpcProviderRegistry;
56
57     @Mock
58     private DeviceContext deviceContext;
59
60     @Mock
61     private ConnectionContext connectionContext;
62
63     @Mock
64     private DeviceState deviceState;
65
66     @Mock
67     private SalRoleService salRoleService;
68
69     @Mock
70     private GetFeaturesOutput getFeaturesOutput;
71
72     @Mock
73     private FeaturesReply featuresReply;
74
75     private final NodeId nodeId = NodeId.getDefaultInstance("openflow:1");
76     private final KeyedInstanceIdentifier<Node, NodeKey> instanceIdentifier = DeviceStateUtil.createNodeInstanceIdentifier(nodeId);
77     private final Entity entity = new Entity(RoleManager.ENTITY_TYPE, nodeId.getValue());
78
79     @Before
80     public void setup() {
81         MockitoAnnotations.initMocks(this);
82         when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
83         when(deviceContext.getDeviceState()).thenReturn(deviceState);
84         when(connectionContext.getNodeId()).thenReturn(nodeId);
85         when(deviceState.getNodeInstanceIdentifier()).thenReturn(instanceIdentifier);
86         when(rpcProviderRegistry.getRpcService(SalRoleService.class)).thenReturn(salRoleService);
87         when(deviceState.getFeatures()).thenReturn(getFeaturesOutput);
88         when(getFeaturesOutput.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
89         when(deviceContext.getPrimaryConnectionContext().getFeatures()).thenReturn(featuresReply);
90         when(deviceContext.getPrimaryConnectionContext().getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.WORKING);
91     }
92
93     @Test
94     public void testOnRoleChanged() {
95         final OfpRole newRole = OfpRole.BECOMEMASTER;
96
97         final SettableFuture<RpcResult<SetRoleOutput>> future = SettableFuture.create();
98         future.set(RpcResultBuilder.<SetRoleOutput>success().build());
99         when(salRoleService.setRole(Matchers.argThat(new SetRoleInputMatcher(newRole, instanceIdentifier))))
100                 .thenReturn(future);
101
102         final RoleContextImpl roleContext = new RoleContextImpl(deviceContext, entityOwnershipService, entity);
103         roleContext.setSalRoleService(salRoleService);
104
105         roleContext.onRoleChanged(OfpRole.BECOMESLAVE, newRole);
106
107         verify(deviceState).setRole(newRole);
108     }
109
110
111     private class SetRoleInputMatcher extends ArgumentMatcher<SetRoleInput> {
112
113         private final OfpRole ofpRole;
114         private final NodeRef nodeRef;
115         public SetRoleInputMatcher(final OfpRole ofpRole, final KeyedInstanceIdentifier<Node, NodeKey> instanceIdentifier) {
116             this.ofpRole = ofpRole;
117             nodeRef = new NodeRef(instanceIdentifier);
118
119         }
120
121         @Override
122         public boolean matches(final Object o) {
123             final SetRoleInput input = (SetRoleInput) o;
124             if (input.getControllerRole() == ofpRole &&
125                     input.getNode().equals(nodeRef)) {
126                 return true;
127             }
128             return false;
129         }
130     }
131 }