75080e0b63f96c4f30771a63b2fa8760de8b618f
[groupbasedpolicy.git] / renderers / ofoverlay / src / test / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / flow / GroupTableTest.java
1 /*
2  * Copyright (c) 2015 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
9 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow;
10
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Mockito.doNothing;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.never;
16 import static org.mockito.Mockito.spy;
17 import static org.mockito.Mockito.verify;
18 import static org.mockito.Mockito.when;
19
20 import java.util.Collections;
21 import java.util.HashSet;
22 import java.util.List;
23
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
27 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
28 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
29 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
30 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
31 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
32 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
33 import org.opendaylight.groupbasedpolicy.dto.EgKey;
34 import org.opendaylight.groupbasedpolicy.dto.IndexedTenant;
35 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfContext;
36 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfWriter;
37 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.endpoint.EndpointManager;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupTypes;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.Buckets;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.Bucket;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContext;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.EndpointGroup;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
53 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
54
55 import com.google.common.base.Optional;
56 import com.google.common.util.concurrent.CheckedFuture;
57
58 public class GroupTableTest {
59
60     private GroupTable groupTable;
61
62     private OfContext ofContext;
63
64     private DataBroker dataBroker;
65     private ReadOnlyTransaction readOnlyTransaction;
66     private WriteTransaction writeTransaction;
67     private ReadWriteTransaction readWriteTransaction;
68
69     private CheckedFuture<Optional<FlowCapableNode>, ReadFailedException> checkedFutureFCNRead;
70     private CheckedFuture<Void, TransactionCommitFailedException> checkedFutureWrite;
71     private Optional<FlowCapableNode> optionalFlowCapableNode;
72
73     private FlowCapableNode flowCapableNode;
74     private Group group;
75     private List<Group> groups;
76     private Buckets buckets;
77     private Bucket bucket;
78     private NodeId nodeId;
79     private OfWriter ofWriter;
80     private Bucket bucketOther;
81     private EndpointManager endpointManager;
82     private Endpoint localEp;
83     private EgKey egKey;
84     private OfOverlayContext ofc;
85     private NodeConnectorId nodeConnectorId;
86
87     @SuppressWarnings("unchecked")
88     @Before
89     public void initialisation() throws Exception {
90         ofContext = mock(OfContext.class);
91         groupTable = spy(new GroupTable(ofContext));
92
93         dataBroker = mock(DataBroker.class);
94         when(ofContext.getDataBroker()).thenReturn(dataBroker);
95
96         checkedFutureFCNRead =  mock(CheckedFuture.class);
97         optionalFlowCapableNode = mock(Optional.class);
98         flowCapableNode = mock(FlowCapableNode.class);
99
100         when(checkedFutureFCNRead.get()).thenReturn(optionalFlowCapableNode);
101
102         when(optionalFlowCapableNode.isPresent()).thenReturn(true);
103         when(optionalFlowCapableNode.get()).thenReturn(flowCapableNode);
104
105
106         readOnlyTransaction = mock(ReadOnlyTransaction.class);
107         when(dataBroker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction);
108         when(readOnlyTransaction.read(any(LogicalDatastoreType.class),
109                 any(InstanceIdentifier.class))).thenReturn(checkedFutureFCNRead);
110
111         writeTransaction = mock(WriteTransaction.class);
112         when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
113         checkedFutureWrite = mock(CheckedFuture.class);
114         when(writeTransaction.submit()).thenReturn(checkedFutureWrite);
115
116         readWriteTransaction = mock(ReadWriteTransaction.class);
117         when(dataBroker.newReadWriteTransaction()).thenReturn(readWriteTransaction);
118
119         group = mock(Group.class);
120         groups = Collections.singletonList(group);
121         when(flowCapableNode.getGroup()).thenReturn(groups);
122
123         buckets = mock(Buckets.class);
124         when(group.getBuckets()).thenReturn(buckets);
125         bucket = mock(Bucket.class);
126         when(bucket.getAction()).thenReturn(Collections.singletonList(mock(Action.class)));
127         List<Bucket> bucketList = Collections.singletonList(bucket);
128         when(buckets.getBucket()).thenReturn(bucketList);
129
130         bucketOther = mock(Bucket.class);
131         when(bucketOther.getAction()).thenReturn(Collections.singletonList(mock(Action.class)));
132
133
134         nodeId = mock(NodeId.class);
135         ofWriter = mock(OfWriter.class);
136
137         endpointManager = mock(EndpointManager.class);
138         when(ofContext.getEndpointManager()).thenReturn(endpointManager);
139         localEp = mock(Endpoint.class);
140         when(endpointManager.getEndpointsForNode(nodeId)).thenReturn(Collections.singletonList(
141                 localEp));
142         IndexedTenant indexedTenant = mock(IndexedTenant.class);
143         when(ofContext.getTenant(any(TenantId.class))).thenReturn(indexedTenant);
144         EndpointGroup epg = mock(EndpointGroup.class);
145         when(indexedTenant.getEndpointGroup(any(EndpointGroupId.class))).thenReturn(epg);
146         egKey = mock(EgKey.class);
147         when(endpointManager.getGroupsForNode(any(NodeId.class))).thenReturn(
148                 new HashSet<>(Collections.singletonList(egKey)));
149         ofc = mock(OfOverlayContext.class);
150         when(localEp.getAugmentation(OfOverlayContext.class)).thenReturn(ofc);
151         nodeConnectorId = mock(NodeConnectorId.class);
152         when(ofc.getNodeConnectorId()).thenReturn(nodeConnectorId);
153     }
154
155     @Test
156     public void updateTest() throws Exception {
157         doNothing().when(groupTable).sync(nodeId, ofWriter);
158
159         groupTable.sync(nodeId, ofWriter);
160         verify(groupTable).sync(any(NodeId.class), any(OfWriter.class));
161     }
162
163     @Test
164     public void updateTestNoFCN() throws Exception {
165         doReturn(null).when(groupTable).getFCNodeFromDatastore(any(NodeId.class));
166
167         groupTable.sync(nodeId, ofWriter);
168         verify(ofWriter, never()).writeBucket(any(NodeId.class), any(GroupId.class), any(Bucket.class));;
169         verify(ofWriter, never()).writeFlow(any(NodeId.class), any(Short.class), any(Flow.class));
170         verify(ofWriter, never()).writeGroup(any(NodeId.class), any(GroupId.class), any(GroupTypes.class),
171                 any(String.class), any(String.class), any(Boolean.class));
172     }
173
174     @Test
175     public void syncTestNoGroup() throws Exception {
176         when(ofWriter.groupExists(any(NodeId.class), any(Long.class))).thenReturn(false);
177         when(endpointManager.getGroupsForNode(any(NodeId.class))).thenReturn(
178                 Collections.<EgKey>emptySet());
179
180         groupTable.sync(nodeId, ofWriter);
181         verify(ofWriter).writeGroup(any(NodeId.class), any(GroupId.class));
182     }
183
184     @Test
185     public void syncTestGroupExists() throws Exception {
186         when(ofWriter.groupExists(any(NodeId.class), any(Long.class))).thenReturn(true);
187         when(endpointManager.getGroupsForNode(any(NodeId.class))).thenReturn(
188                 Collections.<EgKey>emptySet());
189
190         groupTable.sync(nodeId, ofWriter);
191         verify(ofWriter, never()).writeGroup(any(NodeId.class), any(GroupId.class));
192     }
193 }