Bug-4866 - [Clustering]: Switch state resync is not
[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 org.junit.Test;
11 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
12 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
15 import org.opendaylight.openflowplugin.applications.frm.impl.ForwardingRulesManagerImpl;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupInput;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupInput;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupInput;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.*;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27
28 import test.mock.util.EntityOwnershipServiceMock;
29 import test.mock.util.FRMTest;
30 import test.mock.util.RpcProviderRegistryMock;
31 import test.mock.util.SalGroupServiceMock;
32
33 import java.util.List;
34
35 import static org.junit.Assert.assertEquals;
36
37 public class GroupListenerTest extends FRMTest {
38     RpcProviderRegistry rpcProviderRegistryMock = new RpcProviderRegistryMock();
39     EntityOwnershipService eos = new EntityOwnershipServiceMock();
40
41     NodeKey s1Key = new NodeKey(new NodeId("S1"));
42
43     @Test
44     public void addTwoGroupsTest() throws Exception {
45         ForwardingRulesManagerImpl forwardingRulesManager = new ForwardingRulesManagerImpl(getDataBroker(), rpcProviderRegistryMock,
46                 getConfig(), eos);
47         forwardingRulesManager.start();
48
49         addFlowCapableNode(s1Key);
50
51         GroupKey groupKey = new GroupKey(new GroupId((long) 255));
52         InstanceIdentifier<Group> groupII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
53                 .augmentation(FlowCapableNode.class).child(Group.class, groupKey);
54         Group group = new GroupBuilder().setKey(groupKey).setGroupName("Group1").build();
55
56         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
57         writeTx.put(LogicalDatastoreType.CONFIGURATION, groupII, group);
58         assertCommit(writeTx.submit());
59         SalGroupServiceMock salGroupService = (SalGroupServiceMock) forwardingRulesManager.getSalGroupService();
60         List<AddGroupInput> addGroupCalls = salGroupService.getAddGroupCalls();
61         assertEquals(1, addGroupCalls.size());
62         assertEquals("DOM-0", addGroupCalls.get(0).getTransactionUri().getValue());
63
64         groupKey = new GroupKey(new GroupId((long) 256));
65         groupII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
66                 .augmentation(FlowCapableNode.class).child(Group.class, groupKey);
67         group = new GroupBuilder().setKey(groupKey).setGroupName("Group1").build();
68         writeTx = getDataBroker().newWriteOnlyTransaction();
69         writeTx.put(LogicalDatastoreType.CONFIGURATION, groupII, group);
70         assertCommit(writeTx.submit());
71         salGroupService = (SalGroupServiceMock) forwardingRulesManager.getSalGroupService();
72         addGroupCalls = salGroupService.getAddGroupCalls();
73         assertEquals(2, addGroupCalls.size());
74         assertEquals("DOM-1", addGroupCalls.get(1).getTransactionUri().getValue());
75
76         forwardingRulesManager.close();
77     }
78
79     @Test
80     public void updateGroupTest() throws Exception {
81         ForwardingRulesManagerImpl forwardingRulesManager = new ForwardingRulesManagerImpl(
82                 getDataBroker(),
83                 rpcProviderRegistryMock,
84                 getConfig(), eos);
85         forwardingRulesManager.start();
86
87         addFlowCapableNode(s1Key);
88
89         GroupKey groupKey = new GroupKey(new GroupId((long) 255));
90         InstanceIdentifier<Group> groupII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
91                 .augmentation(FlowCapableNode.class).child(Group.class, groupKey);
92         Group group = new GroupBuilder().setKey(groupKey).setGroupName("Group1").build();
93
94         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
95         writeTx.put(LogicalDatastoreType.CONFIGURATION, groupII, group);
96         assertCommit(writeTx.submit());
97         SalGroupServiceMock salGroupService = (SalGroupServiceMock) forwardingRulesManager.getSalGroupService();
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().setKey(groupKey).setGroupName("Group2").build();
103         writeTx = getDataBroker().newWriteOnlyTransaction();
104         writeTx.put(LogicalDatastoreType.CONFIGURATION, groupII, group);
105         assertCommit(writeTx.submit());
106         salGroupService = (SalGroupServiceMock) forwardingRulesManager.getSalGroupService();
107         List<UpdateGroupInput> updateGroupCalls = salGroupService.getUpdateGroupCalls();
108         assertEquals(1, updateGroupCalls.size());
109         assertEquals("DOM-1", updateGroupCalls.get(0).getTransactionUri().getValue());
110
111         forwardingRulesManager.close();
112     }
113
114     @Test
115     public void removeGroupTest() throws Exception {
116         ForwardingRulesManagerImpl forwardingRulesManager = new ForwardingRulesManagerImpl(
117                 getDataBroker(),
118                 rpcProviderRegistryMock,
119                 getConfig(), eos);
120         forwardingRulesManager.start();
121
122         addFlowCapableNode(s1Key);
123
124         GroupKey groupKey = new GroupKey(new GroupId((long) 255));
125         InstanceIdentifier<Group> groupII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
126                 .augmentation(FlowCapableNode.class).child(Group.class, groupKey);
127         Group group = new GroupBuilder().setKey(groupKey).setGroupName("Group1").build();
128
129         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
130         writeTx.put(LogicalDatastoreType.CONFIGURATION, groupII, group);
131         assertCommit(writeTx.submit());
132         SalGroupServiceMock salGroupService = (SalGroupServiceMock) forwardingRulesManager.getSalGroupService();
133         List<AddGroupInput> addGroupCalls = salGroupService.getAddGroupCalls();
134         assertEquals(1, addGroupCalls.size());
135         assertEquals("DOM-0", addGroupCalls.get(0).getTransactionUri().getValue());
136
137         writeTx = getDataBroker().newWriteOnlyTransaction();
138         writeTx.delete(LogicalDatastoreType.CONFIGURATION, groupII);
139         assertCommit(writeTx.submit());
140         salGroupService = (SalGroupServiceMock) forwardingRulesManager.getSalGroupService();
141         List<RemoveGroupInput> removeGroupCalls = salGroupService.getRemoveGroupCalls();
142         assertEquals(1, removeGroupCalls.size());
143         assertEquals("DOM-1", removeGroupCalls.get(0).getTransactionUri().getValue());
144
145         forwardingRulesManager.close();
146     }
147
148     @Test
149     public void staleGroupCreationTest() throws Exception {
150         addFlowCapableNode(s1Key);
151
152         StaleGroupKey groupKey = new StaleGroupKey(new GroupId((long) 255));
153         InstanceIdentifier<StaleGroup> groupII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
154                 .augmentation(FlowCapableNode.class).child(StaleGroup.class, groupKey);
155         StaleGroup group = new StaleGroupBuilder().setKey(groupKey).setGroupName("Stale_Group1").build();
156
157         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
158         writeTx.put(LogicalDatastoreType.CONFIGURATION, groupII, group);
159         assertCommit(writeTx.submit());
160     }
161 }