2 * Copyright (c) 2015 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
9 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow;
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Matchers.anyListOf;
13 import static org.mockito.Mockito.doNothing;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.never;
17 import static org.mockito.Mockito.spy;
18 import static org.mockito.Mockito.verify;
19 import static org.mockito.Mockito.when;
21 import java.util.Collections;
22 import java.util.HashSet;
23 import java.util.List;
25 import com.google.common.base.Optional;
26 import com.google.common.util.concurrent.CheckedFuture;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
30 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
31 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
32 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
33 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
34 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
35 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
36 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.EndpointManager;
37 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfContext;
38 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfWriter;
39 import org.opendaylight.groupbasedpolicy.resolver.EgKey;
40 import org.opendaylight.groupbasedpolicy.resolver.IndexedTenant;
41 import org.opendaylight.groupbasedpolicy.resolver.PolicyInfo;
42 import org.opendaylight.groupbasedpolicy.resolver.PolicyResolver;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.Buckets;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.Bucket;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContext;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.EndpointGroup;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
57 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
59 public class GroupTableTest {
61 private GroupTable groupTable;
63 private OfContext ofContext;
65 private DataBroker dataBroker;
66 private ReadOnlyTransaction readOnlyTransaction;
67 private WriteTransaction writeTransaction;
68 private ReadWriteTransaction readWriteTransaction;
70 private CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> checkedFutureFCNRead;
71 private CheckedFuture<Void, TransactionCommitFailedException> checkedFutureWrite;
72 private Optional<Node> optionalNode;
73 private Optional<FlowCapableNode> optionalFlowCapableNode;
76 private FlowCapableNode flowCapableNode;
78 private List<Group> groups;
79 private Buckets buckets;
80 private Bucket bucket;
81 private NodeId nodeId;
82 private PolicyInfo policyInfo;
83 private OfWriter ofWriter;
84 private GroupId groupId;
85 private Bucket bucketOther;
86 private EndpointManager endpointManager;
87 private Endpoint localEp;
89 private OfOverlayContext ofc;
90 private NodeConnectorId nodeConnectorId;
92 @SuppressWarnings("unchecked")
94 public void initialisation() throws Exception {
95 ofContext = mock(OfContext.class);
96 groupTable = spy(new GroupTable(ofContext));
98 dataBroker = mock(DataBroker.class);
99 when(ofContext.getDataBroker()).thenReturn(dataBroker);
101 node = mock(Node.class);
102 checkedFutureFCNRead = mock(CheckedFuture.class);
103 optionalFlowCapableNode = mock(Optional.class);
104 flowCapableNode = mock(FlowCapableNode.class);
106 when(checkedFutureFCNRead.get()).thenReturn(optionalFlowCapableNode);
108 when(optionalFlowCapableNode.isPresent()).thenReturn(true);
109 when(optionalFlowCapableNode.get()).thenReturn(flowCapableNode);
112 readOnlyTransaction = mock(ReadOnlyTransaction.class);
113 when(dataBroker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction);
114 when(readOnlyTransaction.read(any(LogicalDatastoreType.class),
115 any(InstanceIdentifier.class))).thenReturn(checkedFutureFCNRead);
117 writeTransaction = mock(WriteTransaction.class);
118 when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
119 checkedFutureWrite = mock(CheckedFuture.class);
120 when(writeTransaction.submit()).thenReturn(checkedFutureWrite);
122 readWriteTransaction = mock(ReadWriteTransaction.class);
123 when(dataBroker.newReadWriteTransaction()).thenReturn(readWriteTransaction);
125 group = mock(Group.class);
126 groups = Collections.singletonList(group);
127 when(flowCapableNode.getGroup()).thenReturn(groups);
129 buckets = mock(Buckets.class);
130 when(group.getBuckets()).thenReturn(buckets);
131 bucket = mock(Bucket.class);
132 when(bucket.getAction()).thenReturn(Collections.singletonList(mock(Action.class)));
133 List<Bucket> bucketList = Collections.singletonList(bucket);
134 when(buckets.getBucket()).thenReturn(bucketList);
136 bucketOther = mock(Bucket.class);
137 when(bucketOther.getAction()).thenReturn(Collections.singletonList(mock(Action.class)));
139 groupId = mock(GroupId.class);
141 nodeId = mock(NodeId.class);
142 policyInfo = mock(PolicyInfo.class);
143 ofWriter = mock(OfWriter.class);
145 endpointManager = mock(EndpointManager.class);
146 when(ofContext.getEndpointManager()).thenReturn(endpointManager);
147 localEp = mock(Endpoint.class);
148 when(endpointManager.getEndpointsForNode(nodeId)).thenReturn(Collections.singletonList(
150 PolicyResolver policyResolver = mock(PolicyResolver.class);
151 when(ofContext.getPolicyResolver()).thenReturn(policyResolver);
152 IndexedTenant indexedTenant = mock(IndexedTenant.class);
153 when(policyResolver.getTenant(any(TenantId.class))).thenReturn(indexedTenant);
154 EndpointGroup epg = mock(EndpointGroup.class);
155 when(indexedTenant.getEndpointGroup(any(EndpointGroupId.class))).thenReturn(epg);
156 egKey = mock(EgKey.class);
157 when(endpointManager.getGroupsForNode(any(NodeId.class))).thenReturn(
158 new HashSet<>(Collections.singletonList(egKey)));
159 ofc = mock(OfOverlayContext.class);
160 when(localEp.getAugmentation(OfOverlayContext.class)).thenReturn(ofc);
161 nodeConnectorId = mock(NodeConnectorId.class);
162 when(ofc.getNodeConnectorId()).thenReturn(nodeConnectorId);
166 public void updateTest() throws Exception {
167 doNothing().when(groupTable).sync(nodeId, policyInfo, ofWriter);
169 groupTable.update(nodeId, policyInfo, ofWriter);
170 verify(groupTable).sync(any(NodeId.class), any(PolicyInfo.class), any(OfWriter.class));
174 public void updateTestNoFCN() throws Exception {
175 doReturn(null).when(groupTable).getFCNodeFromDatastore(any(NodeId.class));
177 groupTable.update(nodeId, policyInfo, ofWriter);
178 verify(groupTable, never()).sync(any(NodeId.class), any(PolicyInfo.class), any(OfWriter.class));
182 public void syncTestNoGroup() throws Exception {
183 when(ofWriter.groupExists(any(NodeId.class), any(Long.class))).thenReturn(false);
184 when(endpointManager.getGroupsForNode(any(NodeId.class))).thenReturn(
185 Collections.<EgKey>emptySet());
187 groupTable.update(nodeId, policyInfo, ofWriter);
188 verify(ofWriter).writeGroup(any(NodeId.class), any(GroupId.class));
192 public void syncTestGroupExists() throws Exception {
193 when(ofWriter.groupExists(any(NodeId.class), any(Long.class))).thenReturn(true);
194 when(endpointManager.getGroupsForNode(any(NodeId.class))).thenReturn(
195 Collections.<EgKey>emptySet());
197 groupTable.update(nodeId, policyInfo, ofWriter);
198 verify(ofWriter, never()).writeGroup(any(NodeId.class), any(GroupId.class));