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