1 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.mapper.source;
3 import org.junit.Before;
5 import org.mockito.InOrder;
6 import org.mockito.Mockito;
7 import org.opendaylight.groupbasedpolicy.dto.EgKey;
8 import org.opendaylight.groupbasedpolicy.dto.PolicyInfo;
9 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfContext;
10 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfWriter;
11 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.PolicyManager;
12 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.endpoint.EndpointManager;
13 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.OrdinalFactory;
14 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.mapper.MapperUtilsTest;
15 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.node.SwitchManager;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.overlay.rev150105.TunnelTypeVxlan;
25 import java.util.ArrayList;
26 import java.util.HashSet;
27 import java.util.List;
30 import static org.mockito.Mockito.*;
33 public class SourceMapperTest extends MapperUtilsTest {
38 ctx = mock(OfContext.class);
39 endpointManager = mock(EndpointManager.class);
40 switchManager = mock(SwitchManager.class);
41 policyInfo = mock(PolicyInfo.class);
42 policyManager = mock(PolicyManager.class);
43 ofWriter = mock(OfWriter.class);
47 public void syncFlows_tunnelPortTest() {
48 Endpoint endpoint = buildEndpoint(IPV4_0, MAC_1, CONNECTOR_0).build();
49 EndpointBuilder endpointBuilder = new EndpointBuilder(endpoint);
50 endpointBuilder.setEndpointGroup(ENDPOINT_GROUP_1);
52 // List of other endpoints (one entry is good enough)
53 HashSet<Endpoint> otherEndpoints = new HashSet<>();
54 Endpoint otherEndpoint = buildEndpoint(IPV4_1, MAC_1, CONNECTOR_1).build();
55 EndpointBuilder otherEndpointBuilder = new EndpointBuilder(otherEndpoint).setEndpointGroup(ENDPOINT_GROUP_0);
56 List<EndpointGroupId> endpointGroupIds = new ArrayList<>();
57 endpointGroupIds.add(ENDPOINT_GROUP_1);
58 endpointGroupIds.add(ENDPOINT_GROUP_2);
59 otherEndpointBuilder.setEndpointGroups(endpointGroupIds);
60 otherEndpoints.add(otherEndpointBuilder.build());
63 Set<NodeId> nodeIds = new HashSet<>();
65 nodeIds.add(new NodeId("someNodeId"));
67 SourceMapper sourceMapper = new SourceMapper(ctx, tableId);
69 when(ctx.getPolicyManager()).thenReturn(policyManager);
70 when(ctx.getSwitchManager()).thenReturn(switchManager);
71 when(ctx.getEndpointManager()).thenReturn(endpointManager);
72 when(ctx.getCurrentPolicy()).thenReturn(policyInfo);
73 when(ctx.getTenant(Mockito.any(TenantId.class))).thenReturn(getTestIndexedTenant());
74 when(policyManager.getTABLEID_DESTINATION_MAPPER()).thenReturn((short) 3);
75 when(switchManager.getTunnelPort(NODE_ID, TunnelTypeVxlan.class)).thenReturn(new NodeConnectorId(CONNECTOR_1));
76 when(endpointManager.getEndpointsForGroup(Mockito.any(EgKey.class))).thenReturn(otherEndpoints);
78 SourceMapperFlows flows = mock(SourceMapperFlows.class);
80 sourceMapper.syncFlows(flows, endpointBuilder.build(), NODE_ID, ofWriter);
82 // Verify method usage
83 verify(ctx, times(3)).getEndpointManager();
84 verify(ctx, times(7)).getTenant(Mockito.any(TenantId.class));
85 verify(ctx.getPolicyManager(), times(1)).getTABLEID_DESTINATION_MAPPER();
86 verify(ctx.getSwitchManager(), times(1)).getTunnelPort(NODE_ID, TunnelTypeVxlan.class);
87 verify(ctx.getEndpointManager(), times(1)).getEndpointsForGroup(Mockito.any(EgKey.class));
90 InOrder order = inOrder(ctx, flows);
91 order.verify(flows, atLeastOnce()).dropFlow(Mockito.anyInt(), Mockito.anyLong(), eq(ofWriter));
92 order.verify(ctx, times(1)).getPolicyManager();
93 order.verify(ctx, times(1)).getSwitchManager();
94 order.verify(ctx, times(1)).getCurrentPolicy();
95 order.verify(ctx, times(1)).getEndpointManager();
96 order.verify(flows, atLeastOnce()).createTunnelFlow(Mockito.anyShort(), Mockito.anyInt(),
97 Mockito.any(NodeConnectorId.class), Mockito.any(OrdinalFactory.EndpointFwdCtxOrdinals.class),
99 order.verify(flows, atLeastOnce()).createBroadcastFlow(Mockito.anyShort(), Mockito.anyInt(),
100 Mockito.any(NodeConnectorId.class), Mockito.any(OrdinalFactory.EndpointFwdCtxOrdinals.class),
105 public void syncFlows_endpointGroupsOnly() {
106 Endpoint endpoint = buildEndpoint(IPV4_1, MAC_1, CONNECTOR_1).build();
107 EndpointBuilder endpointBuilder = new EndpointBuilder(endpoint);
108 endpointBuilder.setEndpointGroup(ENDPOINT_GROUP_0);
109 List<EndpointGroupId> endpointGroupIds = new ArrayList<>();
110 endpointGroupIds.add(ENDPOINT_GROUP_1);
111 endpointGroupIds.add(ENDPOINT_GROUP_2);
112 endpointBuilder.setEndpointGroups(endpointGroupIds);
114 SourceMapper sourceMapper = new SourceMapper(ctx, tableId);
116 when(ctx.getPolicyManager()).thenReturn(policyManager);
117 when(ctx.getSwitchManager()).thenReturn(switchManager);
118 when(ctx.getEndpointManager()).thenReturn(endpointManager);
119 when(ctx.getCurrentPolicy()).thenReturn(policyInfo);
120 when(ctx.getTenant(Mockito.any(TenantId.class))).thenReturn(getTestIndexedTenant());
121 when(policyManager.getTABLEID_DESTINATION_MAPPER()).thenReturn((short) 3);
123 SourceMapperFlows flows = mock(SourceMapperFlows.class);
125 sourceMapper.syncFlows(flows, endpointBuilder.build(), NODE_ID, ofWriter);
127 // Verify OfContext method usage
128 verify(ctx, times(4)).getTenant(Mockito.any(TenantId.class));
129 verify(ctx.getPolicyManager(), times(1)).getTABLEID_DESTINATION_MAPPER();
130 verify(ctx.getSwitchManager(), times(1)).getTunnelPort(NODE_ID, TunnelTypeVxlan.class);
133 InOrder order = inOrder(ctx, flows);
134 order.verify(flows, atLeastOnce()).dropFlow(Mockito.anyInt(), Mockito.anyLong(), eq(ofWriter));
135 order.verify(ctx, times(1)).getPolicyManager();
136 order.verify(ctx, times(1)).getSwitchManager();
137 order.verify(ctx, times(3)).getTenant(Mockito.any(TenantId.class));
138 order.verify(ctx, times(1)).getEndpointManager();
139 order.verify(ctx, times(1)).getCurrentPolicy();
140 order.verify(flows, atLeastOnce()).synchronizeEp(Mockito.anyShort(), Mockito.anyInt(),
141 Mockito.any(OrdinalFactory.EndpointFwdCtxOrdinals.class), Mockito.any(MacAddress.class),
142 Mockito.any(NodeConnectorId.class), eq(ofWriter));