7c990531d340a4bddfa1effa77d693a2b0d77938
[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 org.junit.Assert.assertEquals;
11
12 import java.util.List;
13 import org.junit.After;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.junit.runner.RunWith;
17 import org.mockito.Mock;
18 import org.mockito.Mockito;
19 import org.mockito.runners.MockitoJUnitRunner;
20 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
23 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
24 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
25 import org.opendaylight.openflowplugin.applications.frm.impl.DeviceMastershipManager;
26 import org.opendaylight.openflowplugin.applications.frm.impl.ForwardingRulesManagerImpl;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.StaleGroup;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.StaleGroupBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.StaleGroupKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import test.mock.util.FRMTest;
44 import test.mock.util.RpcProviderRegistryMock;
45 import test.mock.util.SalGroupServiceMock;
46
47 @RunWith(MockitoJUnitRunner.class)
48 public class GroupListenerTest extends FRMTest {
49     private ForwardingRulesManagerImpl forwardingRulesManager;
50     private final static NodeId NODE_ID = new NodeId("testnode:1");
51     private final static NodeKey s1Key = new NodeKey(NODE_ID);
52     RpcProviderRegistry rpcProviderRegistryMock = new RpcProviderRegistryMock();
53     @Mock
54     ClusterSingletonServiceProvider clusterSingletonService;
55     @Mock
56     DeviceMastershipManager deviceMastershipManager;
57     @Mock
58     private NotificationProviderService notificationService;
59
60     @Before
61     public void setUp() {
62         forwardingRulesManager = new ForwardingRulesManagerImpl(
63                 getDataBroker(),
64                 rpcProviderRegistryMock,
65                 getConfig(),
66                 clusterSingletonService,
67                 notificationService, false, false, 5);
68         forwardingRulesManager.start();
69         // TODO consider tests rewrite (added because of complicated access)
70         forwardingRulesManager.setDeviceMastershipManager(deviceMastershipManager);
71         Mockito.when(deviceMastershipManager.isDeviceMastered(NODE_ID)).thenReturn(true);
72     }
73
74     @Test
75     public void addTwoGroupsTest() throws Exception {
76         addFlowCapableNode(s1Key);
77
78         GroupKey groupKey = new GroupKey(new GroupId((long) 255));
79         InstanceIdentifier<Group> groupII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
80                 .augmentation(FlowCapableNode.class).child(Group.class, groupKey);
81         Group group = new GroupBuilder().setKey(groupKey).setGroupName("Group1").build();
82
83         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
84         writeTx.put(LogicalDatastoreType.CONFIGURATION, groupII, group);
85         assertCommit(writeTx.submit());
86         SalGroupServiceMock salGroupService = (SalGroupServiceMock) forwardingRulesManager.getSalGroupService();
87         List<AddGroupInput> addGroupCalls = salGroupService.getAddGroupCalls();
88         assertEquals(1, addGroupCalls.size());
89         assertEquals("DOM-0", addGroupCalls.get(0).getTransactionUri().getValue());
90
91         groupKey = new GroupKey(new GroupId((long) 256));
92         groupII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
93                 .augmentation(FlowCapableNode.class).child(Group.class, groupKey);
94         group = new GroupBuilder().setKey(groupKey).setGroupName("Group1").build();
95         writeTx = getDataBroker().newWriteOnlyTransaction();
96         writeTx.put(LogicalDatastoreType.CONFIGURATION, groupII, group);
97         assertCommit(writeTx.submit());
98         salGroupService = (SalGroupServiceMock) forwardingRulesManager.getSalGroupService();
99         addGroupCalls = salGroupService.getAddGroupCalls();
100         assertEquals(2, addGroupCalls.size());
101         assertEquals("DOM-1", addGroupCalls.get(1).getTransactionUri().getValue());
102     }
103
104     @Test
105     public void updateGroupTest() throws Exception {
106         addFlowCapableNode(s1Key);
107
108         GroupKey groupKey = new GroupKey(new GroupId((long) 255));
109         InstanceIdentifier<Group> groupII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
110                 .augmentation(FlowCapableNode.class).child(Group.class, groupKey);
111         Group group = new GroupBuilder().setKey(groupKey).setGroupName("Group1").build();
112
113         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
114         writeTx.put(LogicalDatastoreType.CONFIGURATION, groupII, group);
115         assertCommit(writeTx.submit());
116         SalGroupServiceMock salGroupService = (SalGroupServiceMock) forwardingRulesManager.getSalGroupService();
117         List<AddGroupInput> addGroupCalls = salGroupService.getAddGroupCalls();
118         assertEquals(1, addGroupCalls.size());
119         assertEquals("DOM-0", addGroupCalls.get(0).getTransactionUri().getValue());
120
121         group = new GroupBuilder().setKey(groupKey).setGroupName("Group2").build();
122         writeTx = getDataBroker().newWriteOnlyTransaction();
123         writeTx.put(LogicalDatastoreType.CONFIGURATION, groupII, group);
124         assertCommit(writeTx.submit());
125         salGroupService = (SalGroupServiceMock) forwardingRulesManager.getSalGroupService();
126         List<UpdateGroupInput> updateGroupCalls = salGroupService.getUpdateGroupCalls();
127         assertEquals(1, updateGroupCalls.size());
128         assertEquals("DOM-1", updateGroupCalls.get(0).getTransactionUri().getValue());
129     }
130
131     @Test
132     public void removeGroupTest() throws Exception {
133         addFlowCapableNode(s1Key);
134
135         GroupKey groupKey = new GroupKey(new GroupId((long) 255));
136         InstanceIdentifier<Group> groupII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
137                 .augmentation(FlowCapableNode.class).child(Group.class, groupKey);
138         Group group = new GroupBuilder().setKey(groupKey).setGroupName("Group1").build();
139
140         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
141         writeTx.put(LogicalDatastoreType.CONFIGURATION, groupII, group);
142         assertCommit(writeTx.submit());
143         SalGroupServiceMock salGroupService = (SalGroupServiceMock) forwardingRulesManager.getSalGroupService();
144         List<AddGroupInput> addGroupCalls = salGroupService.getAddGroupCalls();
145         assertEquals(1, addGroupCalls.size());
146         assertEquals("DOM-0", addGroupCalls.get(0).getTransactionUri().getValue());
147
148         writeTx = getDataBroker().newWriteOnlyTransaction();
149         writeTx.delete(LogicalDatastoreType.CONFIGURATION, groupII);
150         assertCommit(writeTx.submit());
151         salGroupService = (SalGroupServiceMock) forwardingRulesManager.getSalGroupService();
152         List<RemoveGroupInput> removeGroupCalls = salGroupService.getRemoveGroupCalls();
153         assertEquals(1, removeGroupCalls.size());
154         assertEquals("DOM-1", removeGroupCalls.get(0).getTransactionUri().getValue());
155     }
156
157     @Test
158     public void staleGroupCreationTest() throws Exception {
159         addFlowCapableNode(s1Key);
160
161         StaleGroupKey groupKey = new StaleGroupKey(new GroupId((long) 255));
162         InstanceIdentifier<StaleGroup> groupII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
163                 .augmentation(FlowCapableNode.class).child(StaleGroup.class, groupKey);
164         StaleGroup group = new StaleGroupBuilder().setKey(groupKey).setGroupName("Stale_Group1").build();
165
166         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
167         writeTx.put(LogicalDatastoreType.CONFIGURATION, groupII, group);
168         assertCommit(writeTx.submit());
169     }
170
171     @After
172     public void tearDown() throws Exception {
173         forwardingRulesManager.close();
174     }
175
176 }