Switch to MD-SAL APIs
[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.awaitility.Awaitility.await;
11 import static org.hamcrest.Matchers.equalTo;
12 import static org.junit.Assert.assertEquals;
13
14 import java.util.List;
15 import org.junit.After;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 import org.mockito.Mock;
20 import org.mockito.Mockito;
21 import org.mockito.junit.MockitoJUnitRunner;
22 import org.opendaylight.mdsal.binding.api.WriteTransaction;
23 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
24 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
25 import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeServiceManager;
26 import org.opendaylight.openflowplugin.applications.frm.impl.DeviceMastershipManager;
27 import org.opendaylight.openflowplugin.applications.frm.impl.ForwardingRulesManagerImpl;
28 import org.opendaylight.openflowplugin.applications.frm.recovery.OpenflowServiceRecoveryHandler;
29 import org.opendaylight.openflowplugin.applications.reconciliation.ReconciliationManager;
30 import org.opendaylight.serviceutils.srm.ServiceRecoveryRegistry;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupInput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupInput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupInput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.StaleGroup;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.StaleGroupBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.StaleGroupKey;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import test.mock.util.FRMTest;
48 import test.mock.util.RpcProviderRegistryMock;
49 import test.mock.util.SalGroupServiceMock;
50
51 @RunWith(MockitoJUnitRunner.class)
52 public class GroupListenerTest extends FRMTest {
53     private ForwardingRulesManagerImpl forwardingRulesManager;
54     private static final NodeId NODE_ID = new NodeId("testnode:1");
55     private static final NodeKey NODE_KEY = new NodeKey(NODE_ID);
56     RpcProviderRegistryMock rpcProviderRegistryMock = new RpcProviderRegistryMock();
57     @Mock
58     ClusterSingletonServiceProvider clusterSingletonService;
59     @Mock
60     DeviceMastershipManager deviceMastershipManager;
61     @Mock
62     private ReconciliationManager reconciliationManager;
63     @Mock
64     private OpenflowServiceRecoveryHandler openflowServiceRecoveryHandler;
65     @Mock
66     private ServiceRecoveryRegistry serviceRecoveryRegistry;
67     @Mock
68     private MastershipChangeServiceManager mastershipChangeServiceManager;
69
70     @Before
71     public void setUp() {
72         forwardingRulesManager = new ForwardingRulesManagerImpl(
73                 getDataBroker(),
74                 rpcProviderRegistryMock,
75                 rpcProviderRegistryMock,
76                 getConfig(),
77                 mastershipChangeServiceManager,
78                 clusterSingletonService,
79                 getConfigurationService(),
80                 reconciliationManager,
81                 openflowServiceRecoveryHandler,
82                 serviceRecoveryRegistry);
83
84         forwardingRulesManager.start();
85         // TODO consider tests rewrite (added because of complicated access)
86         forwardingRulesManager.setDeviceMastershipManager(deviceMastershipManager);
87         Mockito.when(deviceMastershipManager.isDeviceMastered(NODE_ID)).thenReturn(true);
88     }
89
90     @Test
91     public void addTwoGroupsTest() throws Exception {
92         addFlowCapableNode(NODE_KEY);
93
94         GroupKey groupKey = new GroupKey(new GroupId((long) 255));
95         InstanceIdentifier<Group> groupII = InstanceIdentifier.create(Nodes.class).child(Node.class, NODE_KEY)
96                 .augmentation(FlowCapableNode.class).child(Group.class, groupKey);
97         Group group = new GroupBuilder().withKey(groupKey).setGroupName("Group1").build();
98
99         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
100         writeTx.put(LogicalDatastoreType.CONFIGURATION, groupII, group);
101         assertCommit(writeTx.commit());
102         SalGroupServiceMock salGroupService = (SalGroupServiceMock) forwardingRulesManager.getSalGroupService();
103         await().until(listSize(salGroupService.getAddGroupCalls()), equalTo(1));
104         List<AddGroupInput> addGroupCalls = salGroupService.getAddGroupCalls();
105         assertEquals(1, addGroupCalls.size());
106         assertEquals("DOM-0", addGroupCalls.get(0).getTransactionUri().getValue());
107
108         groupKey = new GroupKey(new GroupId((long) 256));
109         groupII = InstanceIdentifier.create(Nodes.class).child(Node.class, NODE_KEY)
110                 .augmentation(FlowCapableNode.class).child(Group.class, groupKey);
111         group = new GroupBuilder().withKey(groupKey).setGroupName("Group1").build();
112         writeTx = getDataBroker().newWriteOnlyTransaction();
113         writeTx.put(LogicalDatastoreType.CONFIGURATION, groupII, group);
114         assertCommit(writeTx.commit());
115         salGroupService = (SalGroupServiceMock) forwardingRulesManager.getSalGroupService();
116         await().until(listSize(salGroupService.getAddGroupCalls()), equalTo(2));
117         addGroupCalls = salGroupService.getAddGroupCalls();
118         assertEquals(2, addGroupCalls.size());
119         assertEquals("DOM-1", addGroupCalls.get(1).getTransactionUri().getValue());
120     }
121
122     @Test
123     public void updateGroupTest() throws Exception {
124         addFlowCapableNode(NODE_KEY);
125
126         GroupKey groupKey = new GroupKey(new GroupId((long) 255));
127         InstanceIdentifier<Group> groupII = InstanceIdentifier.create(Nodes.class).child(Node.class, NODE_KEY)
128                 .augmentation(FlowCapableNode.class).child(Group.class, groupKey);
129         Group group = new GroupBuilder().withKey(groupKey).setGroupName("Group1").build();
130
131         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
132         writeTx.put(LogicalDatastoreType.CONFIGURATION, groupII, group);
133         assertCommit(writeTx.commit());
134         SalGroupServiceMock salGroupService = (SalGroupServiceMock) forwardingRulesManager.getSalGroupService();
135         await().until(listSize(salGroupService.getAddGroupCalls()), equalTo(1));
136         List<AddGroupInput> addGroupCalls = salGroupService.getAddGroupCalls();
137         assertEquals(1, addGroupCalls.size());
138         assertEquals("DOM-0", addGroupCalls.get(0).getTransactionUri().getValue());
139
140         group = new GroupBuilder().withKey(groupKey).setGroupName("Group2").build();
141         writeTx = getDataBroker().newWriteOnlyTransaction();
142         writeTx.put(LogicalDatastoreType.CONFIGURATION, groupII, group);
143         assertCommit(writeTx.commit());
144         salGroupService = (SalGroupServiceMock) forwardingRulesManager.getSalGroupService();
145         await().until(listSize(salGroupService.getUpdateGroupCalls()), equalTo(1));
146         List<UpdateGroupInput> updateGroupCalls = salGroupService.getUpdateGroupCalls();
147         assertEquals(1, updateGroupCalls.size());
148         assertEquals("DOM-1", updateGroupCalls.get(0).getTransactionUri().getValue());
149     }
150
151     @Test
152     public void removeGroupTest() throws Exception {
153         addFlowCapableNode(NODE_KEY);
154
155         GroupKey groupKey = new GroupKey(new GroupId((long) 255));
156         InstanceIdentifier<Group> groupII = InstanceIdentifier.create(Nodes.class).child(Node.class, NODE_KEY)
157                 .augmentation(FlowCapableNode.class).child(Group.class, groupKey);
158         Group group = new GroupBuilder().withKey(groupKey).setGroupName("Group1").build();
159
160         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
161         writeTx.put(LogicalDatastoreType.CONFIGURATION, groupII, group);
162         assertCommit(writeTx.commit());
163         SalGroupServiceMock salGroupService = (SalGroupServiceMock) forwardingRulesManager.getSalGroupService();
164         await().until(listSize(salGroupService.getAddGroupCalls()), equalTo(1));
165         List<AddGroupInput> addGroupCalls = salGroupService.getAddGroupCalls();
166         assertEquals(1, addGroupCalls.size());
167         assertEquals("DOM-0", addGroupCalls.get(0).getTransactionUri().getValue());
168
169         writeTx = getDataBroker().newWriteOnlyTransaction();
170         writeTx.delete(LogicalDatastoreType.CONFIGURATION, groupII);
171         assertCommit(writeTx.commit());
172         salGroupService = (SalGroupServiceMock) forwardingRulesManager.getSalGroupService();
173         await().until(listSize(salGroupService.getRemoveGroupCalls()), equalTo(1));
174         List<RemoveGroupInput> removeGroupCalls = salGroupService.getRemoveGroupCalls();
175         assertEquals(1, removeGroupCalls.size());
176         assertEquals("DOM-1", removeGroupCalls.get(0).getTransactionUri().getValue());
177     }
178
179     @Test
180     public void staleGroupCreationTest() throws Exception {
181         addFlowCapableNode(NODE_KEY);
182
183         StaleGroupKey groupKey = new StaleGroupKey(new GroupId((long) 255));
184         InstanceIdentifier<StaleGroup> groupII = InstanceIdentifier.create(Nodes.class).child(Node.class, NODE_KEY)
185                 .augmentation(FlowCapableNode.class).child(StaleGroup.class, groupKey);
186         StaleGroup group = new StaleGroupBuilder().withKey(groupKey).setGroupName("Stale_Group1").build();
187
188         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
189         writeTx.put(LogicalDatastoreType.CONFIGURATION, groupII, group);
190         assertCommit(writeTx.commit());
191     }
192
193     @After
194     public void tearDown() throws Exception {
195         forwardingRulesManager.close();
196     }
197 }