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.OfOverlayContext;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.EndpointGroup;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.overlay.rev150105.TunnelTypeVxlan;
43 public class SourceMapperTest {
45 private SourceMapper mapper;
47 private OfContext ctx;
48 private short tableId;
49 private NodeId nodeId;
50 private PolicyInfo policyInfo;
51 private OfWriter ofWriter;
52 private Endpoint endpoint;
53 private EndpointManager endpointManager;
54 private IndexedTenant tenant;
55 private TenantId tenantId;
56 private PolicyManager policyManager;
57 private OfOverlayContext ofOverlayContext;
58 private NodeConnectorId nodeConnectorId;
59 private EndpointGroupId endpointGroupIdSingle;
60 private EndpointGroupId endpointGroupIdList;
61 private EndpointGroup endpointGroup;
62 private SwitchManager switchManager;
65 public void initialisation() {
66 ctx = mock(OfContext.class);
68 nodeId = mock(NodeId.class);
69 ofWriter = mock(OfWriter.class);
71 mapper = new SourceMapper(ctx, tableId);
73 endpointManager = mock(EndpointManager.class);
74 when(ctx.getEndpointManager()).thenReturn(endpointManager);
75 endpoint = mock(Endpoint.class);
76 List<Endpoint> endpointsForNode = Arrays.asList(endpoint);
77 when(endpointManager.getEndpointsForNode(nodeId)).thenReturn(endpointsForNode);
79 ofOverlayContext = mock(OfOverlayContext.class);
80 when(endpoint.getAugmentation(OfOverlayContext.class)).thenReturn(ofOverlayContext);
81 nodeConnectorId = mock(NodeConnectorId.class);
82 when(ofOverlayContext.getNodeConnectorId()).thenReturn(nodeConnectorId);
84 tenantId = mock(TenantId.class);
85 when(endpoint.getTenant()).thenReturn(tenantId);
86 tenant = mock(IndexedTenant.class);
87 when(ctx.getTenant(tenantId)).thenReturn(tenant);
88 policyManager = mock(PolicyManager.class);
89 when(ctx.getPolicyManager()).thenReturn(policyManager);
90 policyInfo = mock(PolicyInfo.class);
91 when(ctx.getCurrentPolicy()).thenReturn(policyInfo);
93 endpointGroup = mock(EndpointGroup.class);
94 when(tenant.getEndpointGroup(any(EndpointGroupId.class))).thenReturn(endpointGroup);
96 Set<NodeId> nodeIdPeers = new HashSet<NodeId>(Arrays.asList(nodeId));
97 when(endpointManager.getNodesForGroup(any(EgKey.class))).thenReturn(nodeIdPeers);
99 switchManager = mock(SwitchManager.class);
100 when(ctx.getSwitchManager()).thenReturn(switchManager);
101 when(switchManager.getTunnelPort(nodeId, TunnelTypeVxlan.class)).thenReturn(nodeConnectorId);
105 public void constructorTest() {
106 Assert.assertEquals(tableId, mapper.getTableId());
110 public void syncTestEndpointGroup() throws Exception {
111 endpointGroupIdSingle = mock(EndpointGroupId.class);
112 when(endpoint.getEndpointGroup()).thenReturn(endpointGroupIdSingle);
113 when(endpoint.getEndpointGroups()).thenReturn(null);
115 mapper.sync(nodeId, ofWriter);
116 verify(ofWriter, times(4)).writeFlow(any(NodeId.class), any(Short.class), any(Flow.class));
120 public void syncTestEndpointGroups() throws Exception {
121 endpointGroupIdList = mock(EndpointGroupId.class);
122 List<EndpointGroupId> endpointGroups = Arrays.asList(endpointGroupIdList);
123 when(endpoint.getEndpointGroups()).thenReturn(endpointGroups);
125 mapper.sync(nodeId, ofWriter);
126 verify(ofWriter, times(4)).writeFlow(any(NodeId.class), any(Short.class), any(Flow.class));
130 public void syncTestEndpointGroupPeers() throws Exception {
131 endpointGroupIdSingle = mock(EndpointGroupId.class);
132 when(endpoint.getEndpointGroup()).thenReturn(endpointGroupIdSingle);
133 when(endpoint.getEndpointGroups()).thenReturn(null);
135 mapper.sync(nodeId, ofWriter);
136 verify(ofWriter, times(4)).writeFlow(any(NodeId.class), any(Short.class), any(Flow.class));
140 public void syncTestEndpointGroupTunPortNull() throws Exception {
141 endpointGroupIdSingle = mock(EndpointGroupId.class);
142 when(endpoint.getEndpointGroup()).thenReturn(endpointGroupIdSingle);
143 when(endpoint.getEndpointGroups()).thenReturn(null);
144 when(switchManager.getTunnelPort(nodeId, TunnelTypeVxlan.class)).thenReturn(null);
146 mapper.sync(nodeId, ofWriter);
147 verify(ofWriter, times(2)).writeFlow(any(NodeId.class), any(Short.class), any(Flow.class));
151 public void syncTestTenantNull() throws Exception {
152 when(ctx.getTenant(tenantId)).thenReturn(null);
154 mapper.sync(nodeId, ofWriter);
155 verify(ofWriter, times(1)).writeFlow(any(NodeId.class), any(Short.class), any(Flow.class));