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.renderer.ofoverlay.EndpointManager;
26 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfWriter;
27 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfContext;
28 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.PolicyManager;
29 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.node.SwitchManager;
30 import org.opendaylight.groupbasedpolicy.resolver.EgKey;
31 import org.opendaylight.groupbasedpolicy.resolver.IndexedTenant;
32 import org.opendaylight.groupbasedpolicy.resolver.PolicyInfo;
33 import org.opendaylight.groupbasedpolicy.resolver.PolicyResolver;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.EndpointLocation.LocationType;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContext;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.EndpointGroup;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.overlay.rev150105.TunnelTypeVxlan;
45 public class SourceMapperTest {
47 private SourceMapper mapper;
49 private OfContext ctx;
50 private short tableId;
51 private NodeId nodeId;
52 private PolicyInfo policyInfo;
53 private OfWriter ofWriter;
54 private Endpoint endpoint;
55 private EndpointManager endpointManager;
56 private IndexedTenant tenant;
57 private PolicyResolver policyResolver;
58 private TenantId tenantId;
59 private PolicyManager policyManager;
60 private OfOverlayContext ofOverlayContext;
61 private NodeConnectorId nodeConnectorId;
62 private EndpointGroupId endpointGroupIdSingle;
63 private EndpointGroupId endpointGroupIdList;
64 private EndpointGroup endpointGroup;
65 private SwitchManager switchManager;
68 public void initialisation() {
69 ctx = mock(OfContext.class);
71 nodeId = mock(NodeId.class);
72 policyInfo = mock(PolicyInfo.class);
73 ofWriter = mock(OfWriter.class);
75 mapper = new SourceMapper(ctx, tableId);
77 endpointManager = mock(EndpointManager.class);
78 when(ctx.getEndpointManager()).thenReturn(endpointManager);
79 endpoint = mock(Endpoint.class);
80 List<Endpoint> endpointsForNode = Arrays.asList(endpoint);
81 when(endpointManager.getEndpointsForNode(nodeId)).thenReturn(endpointsForNode);
83 ofOverlayContext = mock(OfOverlayContext.class);
84 when(endpoint.getAugmentation(OfOverlayContext.class)).thenReturn(ofOverlayContext);
85 nodeConnectorId = mock(NodeConnectorId.class);
86 when(ofOverlayContext.getNodeConnectorId()).thenReturn(nodeConnectorId);
87 when(ofOverlayContext.getLocationType()).thenReturn(LocationType.Internal);
89 tenantId = mock(TenantId.class);
90 when(endpoint.getTenant()).thenReturn(tenantId);
91 policyResolver = mock(PolicyResolver.class);
92 when(ctx.getPolicyResolver()).thenReturn(policyResolver);
93 tenant = mock(IndexedTenant.class);
94 when(policyResolver.getTenant(tenantId)).thenReturn(tenant);
95 policyManager = mock(PolicyManager.class);
96 when(ctx.getPolicyManager()).thenReturn(policyManager);
98 endpointGroup = mock(EndpointGroup.class);
99 when(tenant.getEndpointGroup(any(EndpointGroupId.class))).thenReturn(endpointGroup);
101 Set<NodeId> nodeIdPeers = new HashSet<NodeId>(Arrays.asList(nodeId));
102 when(endpointManager.getNodesForGroup(any(EgKey.class))).thenReturn(nodeIdPeers);
104 switchManager = mock(SwitchManager.class);
105 when(ctx.getSwitchManager()).thenReturn(switchManager);
106 when(switchManager.getTunnelPort(nodeId, TunnelTypeVxlan.class)).thenReturn(nodeConnectorId);
110 public void constructorTest() {
111 Assert.assertEquals(tableId, mapper.getTableId());
115 public void syncTestEndpointGroup() throws Exception {
116 endpointGroupIdSingle = mock(EndpointGroupId.class);
117 when(endpoint.getEndpointGroup()).thenReturn(endpointGroupIdSingle);
118 when(endpoint.getEndpointGroups()).thenReturn(null);
120 mapper.sync(nodeId, policyInfo, ofWriter);
121 verify(ofWriter, times(4)).writeFlow(any(NodeId.class), any(Short.class), any(Flow.class));
125 public void syncTestEndpointGroups() throws Exception {
126 endpointGroupIdList = mock(EndpointGroupId.class);
127 List<EndpointGroupId> endpointGroups = Arrays.asList(endpointGroupIdList);
128 when(endpoint.getEndpointGroups()).thenReturn(endpointGroups);
130 mapper.sync(nodeId, policyInfo, ofWriter);
131 verify(ofWriter, times(4)).writeFlow(any(NodeId.class), any(Short.class), any(Flow.class));
135 public void syncTestEndpointGroupPeers() throws Exception {
136 endpointGroupIdSingle = mock(EndpointGroupId.class);
137 when(endpoint.getEndpointGroup()).thenReturn(endpointGroupIdSingle);
138 when(endpoint.getEndpointGroups()).thenReturn(null);
140 mapper.sync(nodeId, policyInfo, ofWriter);
141 verify(ofWriter, times(4)).writeFlow(any(NodeId.class), any(Short.class), any(Flow.class));
145 public void syncTestEndpointGroupTunPortNull() throws Exception {
146 endpointGroupIdSingle = mock(EndpointGroupId.class);
147 when(endpoint.getEndpointGroup()).thenReturn(endpointGroupIdSingle);
148 when(endpoint.getEndpointGroups()).thenReturn(null);
149 when(switchManager.getTunnelPort(nodeId, TunnelTypeVxlan.class)).thenReturn(null);
151 mapper.sync(nodeId, policyInfo, ofWriter);
152 verify(ofWriter, times(2)).writeFlow(any(NodeId.class), any(Short.class), any(Flow.class));
156 public void syncTestTenantNull() throws Exception {
157 when(policyResolver.getTenant(tenantId)).thenReturn(null);
159 mapper.sync(nodeId, policyInfo, ofWriter);
160 verify(ofWriter, times(1)).writeFlow(any(NodeId.class), any(Short.class), any(Flow.class));