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 org.awaitility.Awaitility.await;
11 import static org.hamcrest.Matchers.equalTo;
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.Mock;
20 import org.mockito.Mockito;
21 import org.mockito.runners.MockitoJUnitRunner;
22 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
23 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
24 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
25 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
26 import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeServiceManager;
27 import org.opendaylight.openflowplugin.applications.frm.impl.DeviceMastershipManager;
28 import org.opendaylight.openflowplugin.applications.frm.impl.ForwardingRulesManagerImpl;
29 import org.opendaylight.openflowplugin.applications.frm.recovery.OpenflowServiceRecoveryHandler;
30 import org.opendaylight.openflowplugin.applications.reconciliation.ReconciliationManager;
31 import org.opendaylight.serviceutils.srm.ServiceRecoveryRegistry;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupInput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupInput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupInput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.StaleGroup;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.StaleGroupBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.StaleGroupKey;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import test.mock.util.FRMTest;
49 import test.mock.util.RpcProviderRegistryMock;
50 import test.mock.util.SalGroupServiceMock;
52 @RunWith(MockitoJUnitRunner.class)
53 public class GroupListenerTest extends FRMTest {
54 private ForwardingRulesManagerImpl forwardingRulesManager;
55 private static final NodeId NODE_ID = new NodeId("testnode:1");
56 private static final NodeKey NODE_KEY = new NodeKey(NODE_ID);
57 RpcProviderRegistry rpcProviderRegistryMock = new RpcProviderRegistryMock();
59 ClusterSingletonServiceProvider clusterSingletonService;
61 DeviceMastershipManager deviceMastershipManager;
63 private ReconciliationManager reconciliationManager;
65 private OpenflowServiceRecoveryHandler openflowServiceRecoveryHandler;
67 private ServiceRecoveryRegistry serviceRecoveryRegistry;
69 private MastershipChangeServiceManager mastershipChangeServiceManager;
73 forwardingRulesManager = new ForwardingRulesManagerImpl(
75 rpcProviderRegistryMock,
77 mastershipChangeServiceManager,
78 clusterSingletonService,
79 getConfigurationService(),
80 reconciliationManager,
81 openflowServiceRecoveryHandler,
82 serviceRecoveryRegistry);
84 forwardingRulesManager.start();
85 // TODO consider tests rewrite (added because of complicated access)
86 forwardingRulesManager.setDeviceMastershipManager(deviceMastershipManager);
87 Mockito.when(deviceMastershipManager.isDeviceMastered(NODE_ID)).thenReturn(true);
91 public void addTwoGroupsTest() throws Exception {
92 addFlowCapableNode(NODE_KEY);
94 GroupKey groupKey = new GroupKey(new GroupId((long) 255));
95 InstanceIdentifier<Group> groupII = InstanceIdentifier.create(Nodes.class).child(Node.class, NODE_KEY)
96 .augmentation(FlowCapableNode.class).child(Group.class, groupKey);
97 Group group = new GroupBuilder().withKey(groupKey).setGroupName("Group1").build();
99 WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
100 writeTx.put(LogicalDatastoreType.CONFIGURATION, groupII, group);
101 assertCommit(writeTx.submit());
102 SalGroupServiceMock salGroupService = (SalGroupServiceMock) forwardingRulesManager.getSalGroupService();
103 await().until(listSize(salGroupService.getAddGroupCalls()), equalTo(1));
104 List<AddGroupInput> addGroupCalls = salGroupService.getAddGroupCalls();
105 assertEquals(1, addGroupCalls.size());
106 assertEquals("DOM-0", addGroupCalls.get(0).getTransactionUri().getValue());
108 groupKey = new GroupKey(new GroupId((long) 256));
109 groupII = InstanceIdentifier.create(Nodes.class).child(Node.class, NODE_KEY)
110 .augmentation(FlowCapableNode.class).child(Group.class, groupKey);
111 group = new GroupBuilder().withKey(groupKey).setGroupName("Group1").build();
112 writeTx = getDataBroker().newWriteOnlyTransaction();
113 writeTx.put(LogicalDatastoreType.CONFIGURATION, groupII, group);
114 assertCommit(writeTx.submit());
115 salGroupService = (SalGroupServiceMock) forwardingRulesManager.getSalGroupService();
116 await().until(listSize(salGroupService.getAddGroupCalls()), equalTo(2));
117 addGroupCalls = salGroupService.getAddGroupCalls();
118 assertEquals(2, addGroupCalls.size());
119 assertEquals("DOM-1", addGroupCalls.get(1).getTransactionUri().getValue());
123 public void updateGroupTest() throws Exception {
124 addFlowCapableNode(NODE_KEY);
126 GroupKey groupKey = new GroupKey(new GroupId((long) 255));
127 InstanceIdentifier<Group> groupII = InstanceIdentifier.create(Nodes.class).child(Node.class, NODE_KEY)
128 .augmentation(FlowCapableNode.class).child(Group.class, groupKey);
129 Group group = new GroupBuilder().withKey(groupKey).setGroupName("Group1").build();
131 WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
132 writeTx.put(LogicalDatastoreType.CONFIGURATION, groupII, group);
133 assertCommit(writeTx.submit());
134 SalGroupServiceMock salGroupService = (SalGroupServiceMock) forwardingRulesManager.getSalGroupService();
135 await().until(listSize(salGroupService.getAddGroupCalls()), equalTo(1));
136 List<AddGroupInput> addGroupCalls = salGroupService.getAddGroupCalls();
137 assertEquals(1, addGroupCalls.size());
138 assertEquals("DOM-0", addGroupCalls.get(0).getTransactionUri().getValue());
140 group = new GroupBuilder().withKey(groupKey).setGroupName("Group2").build();
141 writeTx = getDataBroker().newWriteOnlyTransaction();
142 writeTx.put(LogicalDatastoreType.CONFIGURATION, groupII, group);
143 assertCommit(writeTx.submit());
144 salGroupService = (SalGroupServiceMock) forwardingRulesManager.getSalGroupService();
145 await().until(listSize(salGroupService.getUpdateGroupCalls()), equalTo(1));
146 List<UpdateGroupInput> updateGroupCalls = salGroupService.getUpdateGroupCalls();
147 assertEquals(1, updateGroupCalls.size());
148 assertEquals("DOM-1", updateGroupCalls.get(0).getTransactionUri().getValue());
152 public void removeGroupTest() throws Exception {
153 addFlowCapableNode(NODE_KEY);
155 GroupKey groupKey = new GroupKey(new GroupId((long) 255));
156 InstanceIdentifier<Group> groupII = InstanceIdentifier.create(Nodes.class).child(Node.class, NODE_KEY)
157 .augmentation(FlowCapableNode.class).child(Group.class, groupKey);
158 Group group = new GroupBuilder().withKey(groupKey).setGroupName("Group1").build();
160 WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
161 writeTx.put(LogicalDatastoreType.CONFIGURATION, groupII, group);
162 assertCommit(writeTx.submit());
163 SalGroupServiceMock salGroupService = (SalGroupServiceMock) forwardingRulesManager.getSalGroupService();
164 await().until(listSize(salGroupService.getAddGroupCalls()), equalTo(1));
165 List<AddGroupInput> addGroupCalls = salGroupService.getAddGroupCalls();
166 assertEquals(1, addGroupCalls.size());
167 assertEquals("DOM-0", addGroupCalls.get(0).getTransactionUri().getValue());
169 writeTx = getDataBroker().newWriteOnlyTransaction();
170 writeTx.delete(LogicalDatastoreType.CONFIGURATION, groupII);
171 assertCommit(writeTx.submit());
172 salGroupService = (SalGroupServiceMock) forwardingRulesManager.getSalGroupService();
173 await().until(listSize(salGroupService.getRemoveGroupCalls()), equalTo(1));
174 List<RemoveGroupInput> removeGroupCalls = salGroupService.getRemoveGroupCalls();
175 assertEquals(1, removeGroupCalls.size());
176 assertEquals("DOM-1", removeGroupCalls.get(0).getTransactionUri().getValue());
180 public void staleGroupCreationTest() throws Exception {
181 addFlowCapableNode(NODE_KEY);
183 StaleGroupKey groupKey = new StaleGroupKey(new GroupId((long) 255));
184 InstanceIdentifier<StaleGroup> groupII = InstanceIdentifier.create(Nodes.class).child(Node.class, NODE_KEY)
185 .augmentation(FlowCapableNode.class).child(StaleGroup.class, groupKey);
186 StaleGroup group = new StaleGroupBuilder().withKey(groupKey).setGroupName("Stale_Group1").build();
188 WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
189 writeTx.put(LogicalDatastoreType.CONFIGURATION, groupII, group);
190 assertCommit(writeTx.submit());
194 public void tearDown() throws Exception {
195 forwardingRulesManager.close();