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.Mockito.mock;
13 import static org.mockito.Mockito.times;
14 import static org.mockito.Mockito.verify;
15 import static org.mockito.Mockito.when;
17 import java.util.Arrays;
18 import java.util.HashSet;
19 import java.util.List;
22 import org.junit.Assert;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.opendaylight.groupbasedpolicy.dto.EgKey;
26 import org.opendaylight.groupbasedpolicy.dto.IndexedTenant;
27 import org.opendaylight.groupbasedpolicy.dto.PolicyInfo;
28 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfContext;
29 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfWriter;
30 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.PolicyManager;
31 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.endpoint.EndpointManager;
32 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.node.SwitchManager;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.EndpointLocation.LocationType;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContext;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.EndpointGroup;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.overlay.rev150105.TunnelTypeVxlan;
44 public class SourceMapperTest {
46 private SourceMapper mapper;
48 private OfContext ctx;
49 private short tableId;
50 private NodeId nodeId;
51 private PolicyInfo policyInfo;
52 private OfWriter ofWriter;
53 private Endpoint endpoint;
54 private EndpointManager endpointManager;
55 private IndexedTenant tenant;
56 private TenantId tenantId;
57 private PolicyManager policyManager;
58 private OfOverlayContext ofOverlayContext;
59 private NodeConnectorId nodeConnectorId;
60 private EndpointGroupId endpointGroupIdSingle;
61 private EndpointGroupId endpointGroupIdList;
62 private EndpointGroup endpointGroup;
63 private SwitchManager switchManager;
66 public void initialisation() {
67 ctx = mock(OfContext.class);
69 nodeId = mock(NodeId.class);
70 ofWriter = mock(OfWriter.class);
72 mapper = new SourceMapper(ctx, tableId);
74 endpointManager = mock(EndpointManager.class);
75 when(ctx.getEndpointManager()).thenReturn(endpointManager);
76 endpoint = mock(Endpoint.class);
77 List<Endpoint> endpointsForNode = Arrays.asList(endpoint);
78 when(endpointManager.getEndpointsForNode(nodeId)).thenReturn(endpointsForNode);
80 ofOverlayContext = mock(OfOverlayContext.class);
81 when(endpoint.getAugmentation(OfOverlayContext.class)).thenReturn(ofOverlayContext);
82 nodeConnectorId = mock(NodeConnectorId.class);
83 when(ofOverlayContext.getNodeConnectorId()).thenReturn(nodeConnectorId);
84 when(ofOverlayContext.getLocationType()).thenReturn(LocationType.Internal);
86 tenantId = mock(TenantId.class);
87 when(endpoint.getTenant()).thenReturn(tenantId);
88 tenant = mock(IndexedTenant.class);
89 when(ctx.getTenant(tenantId)).thenReturn(tenant);
90 policyManager = mock(PolicyManager.class);
91 when(ctx.getPolicyManager()).thenReturn(policyManager);
92 policyInfo = mock(PolicyInfo.class);
93 when(ctx.getCurrentPolicy()).thenReturn(policyInfo);
95 endpointGroup = mock(EndpointGroup.class);
96 when(tenant.getEndpointGroup(any(EndpointGroupId.class))).thenReturn(endpointGroup);
98 Set<NodeId> nodeIdPeers = new HashSet<NodeId>(Arrays.asList(nodeId));
99 when(endpointManager.getNodesForGroup(any(EgKey.class))).thenReturn(nodeIdPeers);
101 switchManager = mock(SwitchManager.class);
102 when(ctx.getSwitchManager()).thenReturn(switchManager);
103 when(switchManager.getTunnelPort(nodeId, TunnelTypeVxlan.class)).thenReturn(nodeConnectorId);
107 public void constructorTest() {
108 Assert.assertEquals(tableId, mapper.getTableId());
112 public void syncTestEndpointGroup() throws Exception {
113 endpointGroupIdSingle = mock(EndpointGroupId.class);
114 when(endpoint.getEndpointGroup()).thenReturn(endpointGroupIdSingle);
115 when(endpoint.getEndpointGroups()).thenReturn(null);
117 mapper.sync(nodeId, ofWriter);
118 verify(ofWriter, times(4)).writeFlow(any(NodeId.class), any(Short.class), any(Flow.class));
122 public void syncTestEndpointGroups() throws Exception {
123 endpointGroupIdList = mock(EndpointGroupId.class);
124 List<EndpointGroupId> endpointGroups = Arrays.asList(endpointGroupIdList);
125 when(endpoint.getEndpointGroups()).thenReturn(endpointGroups);
127 mapper.sync(nodeId, ofWriter);
128 verify(ofWriter, times(4)).writeFlow(any(NodeId.class), any(Short.class), any(Flow.class));
132 public void syncTestEndpointGroupPeers() throws Exception {
133 endpointGroupIdSingle = mock(EndpointGroupId.class);
134 when(endpoint.getEndpointGroup()).thenReturn(endpointGroupIdSingle);
135 when(endpoint.getEndpointGroups()).thenReturn(null);
137 mapper.sync(nodeId, ofWriter);
138 verify(ofWriter, times(4)).writeFlow(any(NodeId.class), any(Short.class), any(Flow.class));
142 public void syncTestEndpointGroupTunPortNull() throws Exception {
143 endpointGroupIdSingle = mock(EndpointGroupId.class);
144 when(endpoint.getEndpointGroup()).thenReturn(endpointGroupIdSingle);
145 when(endpoint.getEndpointGroups()).thenReturn(null);
146 when(switchManager.getTunnelPort(nodeId, TunnelTypeVxlan.class)).thenReturn(null);
148 mapper.sync(nodeId, ofWriter);
149 verify(ofWriter, times(2)).writeFlow(any(NodeId.class), any(Short.class), any(Flow.class));
153 public void syncTestTenantNull() throws Exception {
154 when(ctx.getTenant(tenantId)).thenReturn(null);
156 mapper.sync(nodeId, ofWriter);
157 verify(ofWriter, times(1)).writeFlow(any(NodeId.class), any(Short.class), any(Flow.class));