Refactor reconciliation wiring
[openflowplugin.git] / applications / forwardingrules-manager / src / test / java / org / opendaylight / openflowplugin / applications / frm / impl / 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 org.opendaylight.openflowplugin.applications.frm.impl;
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
40 @RunWith(MockitoJUnitRunner.class)
41 public class GroupListenerTest extends AbstractFRMTest {
42     private static final NodeId NODE_ID = new NodeId("testnode:1");
43     private static final NodeKey NODE_KEY = new NodeKey(NODE_ID);
44
45     @Before
46     public void setUp() {
47         setUpForwardingRulesManager();
48         setDeviceMastership(NODE_ID);
49     }
50
51     @Test
52     public void addTwoGroupsTest() {
53         addFlowCapableNode(NODE_KEY);
54
55         GroupKey groupKey = new GroupKey(new GroupId(Uint32.valueOf(255)));
56         InstanceIdentifier<Group> groupII = InstanceIdentifier.create(Nodes.class).child(Node.class, NODE_KEY)
57                 .augmentation(FlowCapableNode.class).child(Group.class, groupKey);
58         Group group = new GroupBuilder().withKey(groupKey).setGroupName("Group1").build();
59
60         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
61         writeTx.put(LogicalDatastoreType.CONFIGURATION, groupII, group);
62         assertCommit(writeTx.commit());
63         final SalGroupServiceMock salGroupService =
64                 (SalGroupServiceMock) getForwardingRulesManager().getSalGroupService();
65         await().atMost(10, SECONDS).until(() -> salGroupService.getAddGroupCalls().size() == 1);
66         List<AddGroupInput> addGroupCalls = salGroupService.getAddGroupCalls();
67         assertEquals(1, addGroupCalls.size());
68         assertEquals("DOM-0", addGroupCalls.get(0).getTransactionUri().getValue());
69
70         groupKey = new GroupKey(new GroupId(Uint32.valueOf(256)));
71         groupII = InstanceIdentifier.create(Nodes.class).child(Node.class, NODE_KEY)
72                 .augmentation(FlowCapableNode.class).child(Group.class, groupKey);
73         group = new GroupBuilder().withKey(groupKey).setGroupName("Group1").build();
74         writeTx = getDataBroker().newWriteOnlyTransaction();
75         writeTx.put(LogicalDatastoreType.CONFIGURATION, groupII, group);
76         assertCommit(writeTx.commit());
77         await().atMost(10, SECONDS).until(() -> salGroupService.getAddGroupCalls().size() == 2);
78         addGroupCalls = salGroupService.getAddGroupCalls();
79         assertEquals(2, addGroupCalls.size());
80         assertEquals("DOM-1", addGroupCalls.get(1).getTransactionUri().getValue());
81     }
82
83     @Test
84     public void updateGroupTest() {
85         addFlowCapableNode(NODE_KEY);
86
87         GroupKey groupKey = new GroupKey(new GroupId(Uint32.valueOf(255)));
88         InstanceIdentifier<Group> groupII = InstanceIdentifier.create(Nodes.class).child(Node.class, NODE_KEY)
89                 .augmentation(FlowCapableNode.class).child(Group.class, groupKey);
90         Group group = new GroupBuilder().withKey(groupKey).setGroupName("Group1").build();
91
92         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
93         writeTx.put(LogicalDatastoreType.CONFIGURATION, groupII, group);
94         assertCommit(writeTx.commit());
95         final SalGroupServiceMock salGroupService =
96                 (SalGroupServiceMock) getForwardingRulesManager().getSalGroupService();
97         await().atMost(10, SECONDS).until(() -> salGroupService.getAddGroupCalls().size() == 1);
98         List<AddGroupInput> addGroupCalls = salGroupService.getAddGroupCalls();
99         assertEquals(1, addGroupCalls.size());
100         assertEquals("DOM-0", addGroupCalls.get(0).getTransactionUri().getValue());
101
102         group = new GroupBuilder().withKey(groupKey).setGroupName("Group2").build();
103         writeTx = getDataBroker().newWriteOnlyTransaction();
104         writeTx.put(LogicalDatastoreType.CONFIGURATION, groupII, group);
105         assertCommit(writeTx.commit());
106         await().atMost(10, SECONDS).until(() -> salGroupService.getUpdateGroupCalls().size() == 1);
107         List<UpdateGroupInput> updateGroupCalls = salGroupService.getUpdateGroupCalls();
108         assertEquals(1, updateGroupCalls.size());
109         assertEquals("DOM-1", updateGroupCalls.get(0).getTransactionUri().getValue());
110     }
111
112     @Test
113     public void removeGroupTest() {
114         addFlowCapableNode(NODE_KEY);
115
116         GroupKey groupKey = new GroupKey(new GroupId(Uint32.valueOf(255)));
117         InstanceIdentifier<Group> groupII = InstanceIdentifier.create(Nodes.class).child(Node.class, NODE_KEY)
118                 .augmentation(FlowCapableNode.class).child(Group.class, groupKey);
119         Group group = new GroupBuilder().withKey(groupKey).setGroupName("Group1").build();
120
121         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
122         writeTx.put(LogicalDatastoreType.CONFIGURATION, groupII, group);
123         assertCommit(writeTx.commit());
124         SalGroupServiceMock salGroupService = (SalGroupServiceMock) getForwardingRulesManager().getSalGroupService();
125         await().atMost(10, SECONDS).until(() -> salGroupService.getAddGroupCalls().size() == 1);
126         List<AddGroupInput> addGroupCalls = salGroupService.getAddGroupCalls();
127         assertEquals(1, addGroupCalls.size());
128         assertEquals("DOM-0", addGroupCalls.get(0).getTransactionUri().getValue());
129
130         writeTx = getDataBroker().newWriteOnlyTransaction();
131         writeTx.delete(LogicalDatastoreType.CONFIGURATION, groupII);
132         assertCommit(writeTx.commit());
133         await().atMost(10, SECONDS).until(() -> salGroupService.getRemoveGroupCalls().size() == 1);
134         List<RemoveGroupInput> removeGroupCalls = salGroupService.getRemoveGroupCalls();
135         assertEquals(1, removeGroupCalls.size());
136         assertEquals("DOM-1", removeGroupCalls.get(0).getTransactionUri().getValue());
137     }
138
139     @Test
140     public void staleGroupCreationTest() {
141         addFlowCapableNode(NODE_KEY);
142
143         StaleGroupKey groupKey = new StaleGroupKey(new GroupId(Uint32.valueOf(255)));
144         InstanceIdentifier<StaleGroup> groupII = InstanceIdentifier.create(Nodes.class).child(Node.class, NODE_KEY)
145                 .augmentation(FlowCapableNode.class).child(StaleGroup.class, groupKey);
146         StaleGroup group = new StaleGroupBuilder().withKey(groupKey).setGroupName("Stale_Group1").build();
147
148         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
149         writeTx.put(LogicalDatastoreType.CONFIGURATION, groupII, group);
150         assertCommit(writeTx.commit());
151     }
152
153     @After
154     public void tearDown() throws Exception {
155         getForwardingRulesManager().close();
156     }
157 }