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