Bug 6110: Fixed bugs in statistics manager due to race condition.
[openflowplugin.git] / applications / forwardingrules-manager / src / test / java / test / mock / GroupListenerTest.java
1 /**
2  * Copyright (c) 2014 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 test.mock;
9
10 import static org.junit.Assert.assertEquals;
11
12 import java.util.List;
13 import org.junit.After;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.junit.runner.RunWith;
17 import org.mockito.Mock;
18 import org.mockito.Mockito;
19 import org.mockito.runners.MockitoJUnitRunner;
20 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
23 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
24 import org.opendaylight.openflowplugin.applications.frm.impl.DeviceMastershipManager;
25 import org.opendaylight.openflowplugin.applications.frm.impl.ForwardingRulesManagerImpl;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.StaleGroup;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.StaleGroupBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.StaleGroupKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import test.mock.util.FRMTest;
43 import test.mock.util.RpcProviderRegistryMock;
44 import test.mock.util.SalGroupServiceMock;
45
46 @RunWith(MockitoJUnitRunner.class)
47 public class GroupListenerTest extends FRMTest {
48     private ForwardingRulesManagerImpl forwardingRulesManager;
49     private final static NodeId NODE_ID = new NodeId("testnode:1");
50     private final static NodeKey s1Key = new NodeKey(NODE_ID);
51     RpcProviderRegistry rpcProviderRegistryMock = new RpcProviderRegistryMock();
52     @Mock
53     ClusterSingletonServiceProvider clusterSingletonService;
54     @Mock
55     DeviceMastershipManager deviceMastershipManager;
56
57     @Before
58     public void setUp() {
59         forwardingRulesManager = new ForwardingRulesManagerImpl(
60                 getDataBroker(),
61                 rpcProviderRegistryMock,
62                 getConfig(),
63                 clusterSingletonService);
64         forwardingRulesManager.start();
65         // TODO consider tests rewrite (added because of complicated access)
66         forwardingRulesManager.setDeviceMastershipManager(deviceMastershipManager);
67         Mockito.when(deviceMastershipManager.isDeviceMastered(NODE_ID)).thenReturn(true);
68     }
69
70     @Test
71     public void addTwoGroupsTest() throws Exception {
72         addFlowCapableNode(s1Key);
73
74         GroupKey groupKey = new GroupKey(new GroupId((long) 255));
75         InstanceIdentifier<Group> groupII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
76                 .augmentation(FlowCapableNode.class).child(Group.class, groupKey);
77         Group group = new GroupBuilder().setKey(groupKey).setGroupName("Group1").build();
78
79         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
80         writeTx.put(LogicalDatastoreType.CONFIGURATION, groupII, group);
81         assertCommit(writeTx.submit());
82         SalGroupServiceMock salGroupService = (SalGroupServiceMock) forwardingRulesManager.getSalGroupService();
83         List<AddGroupInput> addGroupCalls = salGroupService.getAddGroupCalls();
84         assertEquals(1, addGroupCalls.size());
85         assertEquals("DOM-0", addGroupCalls.get(0).getTransactionUri().getValue());
86
87         groupKey = new GroupKey(new GroupId((long) 256));
88         groupII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
89                 .augmentation(FlowCapableNode.class).child(Group.class, groupKey);
90         group = new GroupBuilder().setKey(groupKey).setGroupName("Group1").build();
91         writeTx = getDataBroker().newWriteOnlyTransaction();
92         writeTx.put(LogicalDatastoreType.CONFIGURATION, groupII, group);
93         assertCommit(writeTx.submit());
94         salGroupService = (SalGroupServiceMock) forwardingRulesManager.getSalGroupService();
95         addGroupCalls = salGroupService.getAddGroupCalls();
96         assertEquals(2, addGroupCalls.size());
97         assertEquals("DOM-1", addGroupCalls.get(1).getTransactionUri().getValue());
98     }
99
100     @Test
101     public void updateGroupTest() throws Exception {
102         addFlowCapableNode(s1Key);
103
104         GroupKey groupKey = new GroupKey(new GroupId((long) 255));
105         InstanceIdentifier<Group> groupII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
106                 .augmentation(FlowCapableNode.class).child(Group.class, groupKey);
107         Group group = new GroupBuilder().setKey(groupKey).setGroupName("Group1").build();
108
109         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
110         writeTx.put(LogicalDatastoreType.CONFIGURATION, groupII, group);
111         assertCommit(writeTx.submit());
112         SalGroupServiceMock salGroupService = (SalGroupServiceMock) forwardingRulesManager.getSalGroupService();
113         List<AddGroupInput> addGroupCalls = salGroupService.getAddGroupCalls();
114         assertEquals(1, addGroupCalls.size());
115         assertEquals("DOM-0", addGroupCalls.get(0).getTransactionUri().getValue());
116
117         group = new GroupBuilder().setKey(groupKey).setGroupName("Group2").build();
118         writeTx = getDataBroker().newWriteOnlyTransaction();
119         writeTx.put(LogicalDatastoreType.CONFIGURATION, groupII, group);
120         assertCommit(writeTx.submit());
121         salGroupService = (SalGroupServiceMock) forwardingRulesManager.getSalGroupService();
122         List<UpdateGroupInput> updateGroupCalls = salGroupService.getUpdateGroupCalls();
123         assertEquals(1, updateGroupCalls.size());
124         assertEquals("DOM-1", updateGroupCalls.get(0).getTransactionUri().getValue());
125     }
126
127     @Test
128     public void removeGroupTest() throws Exception {
129         addFlowCapableNode(s1Key);
130
131         GroupKey groupKey = new GroupKey(new GroupId((long) 255));
132         InstanceIdentifier<Group> groupII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
133                 .augmentation(FlowCapableNode.class).child(Group.class, groupKey);
134         Group group = new GroupBuilder().setKey(groupKey).setGroupName("Group1").build();
135
136         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
137         writeTx.put(LogicalDatastoreType.CONFIGURATION, groupII, group);
138         assertCommit(writeTx.submit());
139         SalGroupServiceMock salGroupService = (SalGroupServiceMock) forwardingRulesManager.getSalGroupService();
140         List<AddGroupInput> addGroupCalls = salGroupService.getAddGroupCalls();
141         assertEquals(1, addGroupCalls.size());
142         assertEquals("DOM-0", addGroupCalls.get(0).getTransactionUri().getValue());
143
144         writeTx = getDataBroker().newWriteOnlyTransaction();
145         writeTx.delete(LogicalDatastoreType.CONFIGURATION, groupII);
146         assertCommit(writeTx.submit());
147         salGroupService = (SalGroupServiceMock) forwardingRulesManager.getSalGroupService();
148         List<RemoveGroupInput> removeGroupCalls = salGroupService.getRemoveGroupCalls();
149         assertEquals(1, removeGroupCalls.size());
150         assertEquals("DOM-1", removeGroupCalls.get(0).getTransactionUri().getValue());
151     }
152
153     @Test
154     public void staleGroupCreationTest() throws Exception {
155         addFlowCapableNode(s1Key);
156
157         StaleGroupKey groupKey = new StaleGroupKey(new GroupId((long) 255));
158         InstanceIdentifier<StaleGroup> groupII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
159                 .augmentation(FlowCapableNode.class).child(StaleGroup.class, groupKey);
160         StaleGroup group = new StaleGroupBuilder().setKey(groupKey).setGroupName("Stale_Group1").build();
161
162         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
163         writeTx.put(LogicalDatastoreType.CONFIGURATION, groupII, group);
164         assertCommit(writeTx.submit());
165     }
166
167     @After
168     public void tearDown() throws Exception {
169         forwardingRulesManager.close();
170     }
171
172 }