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