2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
10 import static java.util.concurrent.TimeUnit.SECONDS;
11 import static org.awaitility.Awaitility.await;
12 import static org.junit.Assert.assertEquals;
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;
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);
49 setUpForwardingRulesManager();
50 setDeviceMastership(NODE_ID);
54 public void addTwoGroupsTest() {
55 addFlowCapableNode(NODE_KEY);
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();
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());
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());
86 public void updateGroupTest() {
87 addFlowCapableNode(NODE_KEY);
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();
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());
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());
115 public void removeGroupTest() {
116 addFlowCapableNode(NODE_KEY);
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();
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());
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());
142 public void staleGroupCreationTest() {
143 addFlowCapableNode(NODE_KEY);
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();
150 WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
151 writeTx.put(LogicalDatastoreType.CONFIGURATION, groupII, group);
152 assertCommit(writeTx.commit());
156 public void tearDown() throws Exception {
157 getForwardingRulesManager().close();