922fe2c0d585e903384c2f901be13b82906dbc24
[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 java.util.concurrent.TimeUnit.SECONDS;
11 import static org.awaitility.Awaitility.await;
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.junit.MockitoJUnitRunner;
20 import org.opendaylight.mdsal.binding.api.WriteTransaction;
21 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupInput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupInput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.StaleGroup;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.StaleGroupBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.StaleGroupKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.opendaylight.yangtools.yang.common.Uint32;
39 import test.mock.util.AbstractFRMTest;
40 import test.mock.util.SalGroupServiceMock;
41
42 @RunWith(MockitoJUnitRunner.class)
43 public class GroupListenerTest extends AbstractFRMTest {
44     private static final NodeId NODE_ID = new NodeId("testnode:1");
45     private static final NodeKey NODE_KEY = new NodeKey(NODE_ID);
46
47     @Before
48     public void setUp() {
49         setUpForwardingRulesManager();
50         setDeviceMastership(NODE_ID);
51     }
52
53     @Test
54     public void addTwoGroupsTest() {
55         addFlowCapableNode(NODE_KEY);
56
57         GroupKey groupKey = new GroupKey(new GroupId(Uint32.valueOf(255)));
58         InstanceIdentifier<Group> groupII = InstanceIdentifier.create(Nodes.class).child(Node.class, NODE_KEY)
59                 .augmentation(FlowCapableNode.class).child(Group.class, groupKey);
60         Group group = new GroupBuilder().withKey(groupKey).setGroupName("Group1").build();
61
62         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
63         writeTx.put(LogicalDatastoreType.CONFIGURATION, groupII, group);
64         assertCommit(writeTx.commit());
65         final SalGroupServiceMock salGroupService =
66                 (SalGroupServiceMock) getForwardingRulesManager().getSalGroupService();
67         await().atMost(10, SECONDS).until(() -> salGroupService.getAddGroupCalls().size() == 1);
68         List<AddGroupInput> addGroupCalls = salGroupService.getAddGroupCalls();
69         assertEquals(1, addGroupCalls.size());
70         assertEquals("DOM-0", addGroupCalls.get(0).getTransactionUri().getValue());
71
72         groupKey = new GroupKey(new GroupId(Uint32.valueOf(256)));
73         groupII = InstanceIdentifier.create(Nodes.class).child(Node.class, NODE_KEY)
74                 .augmentation(FlowCapableNode.class).child(Group.class, groupKey);
75         group = new GroupBuilder().withKey(groupKey).setGroupName("Group1").build();
76         writeTx = getDataBroker().newWriteOnlyTransaction();
77         writeTx.put(LogicalDatastoreType.CONFIGURATION, groupII, group);
78         assertCommit(writeTx.commit());
79         await().atMost(10, SECONDS).until(() -> salGroupService.getAddGroupCalls().size() == 2);
80         addGroupCalls = salGroupService.getAddGroupCalls();
81         assertEquals(2, addGroupCalls.size());
82         assertEquals("DOM-1", addGroupCalls.get(1).getTransactionUri().getValue());
83     }
84
85     @Test
86     public void updateGroupTest() {
87         addFlowCapableNode(NODE_KEY);
88
89         GroupKey groupKey = new GroupKey(new GroupId(Uint32.valueOf(255)));
90         InstanceIdentifier<Group> groupII = InstanceIdentifier.create(Nodes.class).child(Node.class, NODE_KEY)
91                 .augmentation(FlowCapableNode.class).child(Group.class, groupKey);
92         Group group = new GroupBuilder().withKey(groupKey).setGroupName("Group1").build();
93
94         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
95         writeTx.put(LogicalDatastoreType.CONFIGURATION, groupII, group);
96         assertCommit(writeTx.commit());
97         final SalGroupServiceMock salGroupService =
98                 (SalGroupServiceMock) getForwardingRulesManager().getSalGroupService();
99         await().atMost(10, SECONDS).until(() -> salGroupService.getAddGroupCalls().size() == 1);
100         List<AddGroupInput> addGroupCalls = salGroupService.getAddGroupCalls();
101         assertEquals(1, addGroupCalls.size());
102         assertEquals("DOM-0", addGroupCalls.get(0).getTransactionUri().getValue());
103
104         group = new GroupBuilder().withKey(groupKey).setGroupName("Group2").build();
105         writeTx = getDataBroker().newWriteOnlyTransaction();
106         writeTx.put(LogicalDatastoreType.CONFIGURATION, groupII, group);
107         assertCommit(writeTx.commit());
108         await().atMost(10, SECONDS).until(() -> salGroupService.getUpdateGroupCalls().size() == 1);
109         List<UpdateGroupInput> updateGroupCalls = salGroupService.getUpdateGroupCalls();
110         assertEquals(1, updateGroupCalls.size());
111         assertEquals("DOM-1", updateGroupCalls.get(0).getTransactionUri().getValue());
112     }
113
114     @Test
115     public void removeGroupTest() {
116         addFlowCapableNode(NODE_KEY);
117
118         GroupKey groupKey = new GroupKey(new GroupId(Uint32.valueOf(255)));
119         InstanceIdentifier<Group> groupII = InstanceIdentifier.create(Nodes.class).child(Node.class, NODE_KEY)
120                 .augmentation(FlowCapableNode.class).child(Group.class, groupKey);
121         Group group = new GroupBuilder().withKey(groupKey).setGroupName("Group1").build();
122
123         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
124         writeTx.put(LogicalDatastoreType.CONFIGURATION, groupII, group);
125         assertCommit(writeTx.commit());
126         SalGroupServiceMock salGroupService = (SalGroupServiceMock) getForwardingRulesManager().getSalGroupService();
127         await().atMost(10, SECONDS).until(() -> salGroupService.getAddGroupCalls().size() == 1);
128         List<AddGroupInput> addGroupCalls = salGroupService.getAddGroupCalls();
129         assertEquals(1, addGroupCalls.size());
130         assertEquals("DOM-0", addGroupCalls.get(0).getTransactionUri().getValue());
131
132         writeTx = getDataBroker().newWriteOnlyTransaction();
133         writeTx.delete(LogicalDatastoreType.CONFIGURATION, groupII);
134         assertCommit(writeTx.commit());
135         await().atMost(10, SECONDS).until(() -> salGroupService.getRemoveGroupCalls().size() == 1);
136         List<RemoveGroupInput> removeGroupCalls = salGroupService.getRemoveGroupCalls();
137         assertEquals(1, removeGroupCalls.size());
138         assertEquals("DOM-1", removeGroupCalls.get(0).getTransactionUri().getValue());
139     }
140
141     @Test
142     public void staleGroupCreationTest() {
143         addFlowCapableNode(NODE_KEY);
144
145         StaleGroupKey groupKey = new StaleGroupKey(new GroupId(Uint32.valueOf(255)));
146         InstanceIdentifier<StaleGroup> groupII = InstanceIdentifier.create(Nodes.class).child(Node.class, NODE_KEY)
147                 .augmentation(FlowCapableNode.class).child(StaleGroup.class, groupKey);
148         StaleGroup group = new StaleGroupBuilder().withKey(groupKey).setGroupName("Stale_Group1").build();
149
150         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
151         writeTx.put(LogicalDatastoreType.CONFIGURATION, groupII, group);
152         assertCommit(writeTx.commit());
153     }
154
155     @After
156     public void tearDown() throws Exception {
157         getForwardingRulesManager().close();
158     }
159 }