Bug 5617: OfOverlay Refactoring - Destination mapper
[groupbasedpolicy.git] / renderers / ofoverlay / src / test / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / mapper / destination / DestinationMapperTest.java
1 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.mapper.destination;
2
3
4 import org.junit.Before;
5 import org.junit.Test;
6 import org.mockito.InOrder;
7 import org.opendaylight.groupbasedpolicy.dto.EgKey;
8 import org.opendaylight.groupbasedpolicy.dto.EpKey;
9 import org.opendaylight.groupbasedpolicy.dto.IndexedTenant;
10 import org.opendaylight.groupbasedpolicy.dto.PolicyInfo;
11 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfContext;
12 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfWriter;
13 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.PolicyManager;
14 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.endpoint.EndpointManager;
15 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.OrdinalFactory;
16 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.mapper.MapperUtilsTest;
17 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.node.SwitchManager;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ContextId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L3ContextId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.NetworkDomainId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubnetId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoint.fields.L3Address;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoint.l3.prefix.fields.EndpointL3Gateways;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoint.l3.prefix.fields.EndpointL3GatewaysBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3Prefix;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3PrefixBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContext;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContextBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.Tenant;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.TenantBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.ForwardingContextBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.PolicyBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L3Context;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L3ContextBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.Subnet;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.SubnetBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.ExternalImplicitGroup;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.ExternalImplicitGroupBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.overlay.rev150105.TunnelTypeVxlan;
49
50 import java.util.ArrayList;
51 import java.util.Collection;
52 import java.util.HashSet;
53 import java.util.List;
54 import java.util.Set;
55
56 import static junit.framework.TestCase.assertTrue;
57 import static org.mockito.Mockito.*;
58
59 public class DestinationMapperTest extends MapperUtilsTest {
60
61     private DestinationMapper destinationMapper;
62
63     @Before
64     public void init() {
65         tableId = 3;
66         endpointManager = mock(EndpointManager.class);
67         policyManager = mock(PolicyManager.class);
68         switchManager = mock(SwitchManager.class);
69         policyInfo = mock(PolicyInfo.class);
70         ctx = mock(OfContext.class);
71         ofWriter = mock(OfWriter.class);
72         destinationMapper = new DestinationMapper(ctx, tableId);
73     }
74
75     @Test
76     public void getTableId() {
77         assertTrue(destinationMapper.getTableId() == tableId);
78     }
79
80     @Test
81     public void syncFlows() throws Exception {
82         DestinationMapperFlows flows = mock(DestinationMapperFlows.class);
83         EndpointBuilder endpointBuilder = buildEndpoint(IPV4_1, MAC_1, CONNECTOR_1);
84
85         when(ctx.getTenant(any(TenantId.class))).thenReturn(getTestIndexedTenant());
86         when(ctx.getEndpointManager()).thenReturn(endpointManager);
87         when(ctx.getPolicyManager()).thenReturn(policyManager);
88         when(ctx.getCurrentPolicy()).thenReturn(policyInfo);
89
90         destinationMapper.syncFlows(flows, endpointBuilder.build(), null, ofWriter);
91
92         // Verify usage
93         verify(ctx, times(2)).getTenant(any(TenantId.class));
94         verify(ctx, times(5)).getEndpointManager();
95         verify(ctx, times(2)).getPolicyManager();
96         verify(ctx, times(2)).getCurrentPolicy();
97
98         //Verify order
99         InOrder order = inOrder(flows);
100         order.verify(flows, times(1)).dropFlow(anyInt(), anyLong(), eq(ofWriter));
101         order.verify(flows, times(1)).createBroadcastFlow(anyInt(), any(OrdinalFactory.EndpointFwdCtxOrdinals.class),
102                 any(MacAddress.class), eq(ofWriter));
103     }
104
105     @Test
106     public void syncExternalEndpointFlows_L2Flow() {
107         DestinationMapperFlows flows = mock(DestinationMapperFlows.class);
108         // External ports
109         Set<NodeConnectorId> externalPorts = new HashSet<>();
110         externalPorts.add(CONNECTOR_0);
111         externalPorts.add(CONNECTOR_1);
112         // Endpoint
113         EndpointBuilder endpointBuilder = buildEndpoint(IPV4_1, MAC_1, CONNECTOR_1);
114         // Peer endpoint
115         EndpointBuilder peerEndpointBuilder = buildEndpoint(IPV4_2, MAC_0, CONNECTOR_0);
116         // External Implicit groups
117         List<ExternalImplicitGroup> externalImplicitGroups = new ArrayList<>();
118         ExternalImplicitGroupBuilder externalImplicitGroupBuilder = new ExternalImplicitGroupBuilder();
119         externalImplicitGroupBuilder.setId(ENDPOINT_GROUP_0);
120         externalImplicitGroups.add(externalImplicitGroupBuilder.build());
121         TenantBuilder peerTenantBuilder = buildTenant();
122         peerTenantBuilder.setPolicy(new PolicyBuilder()
123                 .setEndpointGroup(getEndpointGroups())
124                 .setSubjectFeatureInstances(getSubjectFeatureInstances())
125                 .setExternalImplicitGroup(externalImplicitGroups)
126                 .build());
127         Endpoint peerEndpoint = peerEndpointBuilder.build();
128         Collection<Endpoint> peerEndpoints = new ArrayList<>();
129         peerEndpoints.add(peerEndpoint);
130
131         when(ctx.getTenant(any(TenantId.class))).thenReturn(new IndexedTenant(peerTenantBuilder.build()));
132         when(ctx.getEndpointManager()).thenReturn(endpointManager);
133         when(ctx.getPolicyManager()).thenReturn(policyManager);
134         when(ctx.getSwitchManager()).thenReturn(switchManager);
135         when(ctx.getCurrentPolicy()).thenReturn(policyInfo);
136         when(endpointManager.getEndpointsForGroup(any(EgKey.class))).thenReturn(peerEndpoints);
137         when(endpointManager.getEndpointsForNode(any(NodeId.class))).thenReturn(peerEndpoints);
138         when(switchManager.getExternalPorts(any(NodeId.class))).thenReturn(externalPorts);
139
140         destinationMapper.syncEndpointFlows(flows, NODE_ID, endpointBuilder.build(), ofWriter);
141
142         // Verify usage
143         verify(ctx, times(11)).getTenant(any(TenantId.class));
144         verify(ctx, times(6)).getEndpointManager();
145         verify(ctx, times(1)).getPolicyManager();
146         verify(ctx, times(4)).getCurrentPolicy();
147         verify(flows, times(1)).createExternalL2Flow(anyShort(), anyInt(), any(Endpoint.class),
148                 anySetOf(NodeConnectorId.class), eq(ofWriter));
149     }
150
151     @Test
152     public void syncExternalEndpointFlows_L3Flow() {
153         DestinationMapperFlows flows = mock(DestinationMapperFlows.class);
154         // L3 Endpoint prefix
155         Collection<EndpointL3Prefix> endpointL3PrefixCollection = new HashSet<>();
156         List<EndpointL3Gateways> endpointL3GatewaysList = new ArrayList<>();
157         EndpointL3PrefixBuilder endpointL3PrefixBuilder = new EndpointL3PrefixBuilder();
158         EndpointL3GatewaysBuilder endpointL3GatewaysBuilder = new EndpointL3GatewaysBuilder();
159         endpointL3GatewaysList.add(endpointL3GatewaysBuilder.build());
160         endpointL3PrefixBuilder.setEndpointL3Gateways(endpointL3GatewaysList);
161         endpointL3PrefixCollection.add(endpointL3PrefixBuilder.build());
162         // External ports
163         Set<NodeConnectorId> externalPorts = new HashSet<>();
164         externalPorts.add(new NodeConnectorId(CONNECTOR_0));
165         externalPorts.add(new NodeConnectorId(CONNECTOR_1));
166         // Endpoint
167         EndpointBuilder endpointBuilder = buildEndpoint(IPV4_1, MAC_1, CONNECTOR_1);
168         endpointBuilder.setL3Address(getL3AddressList(IPV4_1, L3C_ID));
169         endpointBuilder.setNetworkContainment(new NetworkDomainId(SUBNET_0));
170         Endpoint endpoint = endpointBuilder.build();
171         // Peer
172         EndpointBuilder peerEndpointBuilder = buildEndpoint(IPV4_2, MAC_0, CONNECTOR_0);
173         peerEndpointBuilder.setNetworkContainment(new NetworkDomainId(SUBNET_0));
174         // External implicit groups
175         List<Subnet> subnets = new ArrayList<>();
176         SubnetBuilder subnetBuilder = new SubnetBuilder();
177         subnetBuilder.setId(new SubnetId(SUBNET_0));
178         subnetBuilder.setParent(new ContextId(L3C_ID));
179         subnetBuilder.setVirtualRouterIp(new IpAddress(new Ipv4Address(IPV4_2)));
180         subnets.add(subnetBuilder.build());
181         List<L3Context> l3Contexts = new ArrayList<>();
182         L3ContextBuilder l3ContextBuilder = new L3ContextBuilder();
183         l3ContextBuilder.setId(L3C_ID);
184         l3Contexts.add(l3ContextBuilder.build());
185         List<ExternalImplicitGroup> externalImplicitGroups = new ArrayList<>();
186         ExternalImplicitGroupBuilder externalImplicitGroupBuilder = new ExternalImplicitGroupBuilder();
187         externalImplicitGroupBuilder.setId(ENDPOINT_GROUP_0);
188         externalImplicitGroups.add(externalImplicitGroupBuilder.build());
189         ForwardingContextBuilder forwardingContextBuilder = new ForwardingContextBuilder();
190         forwardingContextBuilder.setSubnet(subnets);
191         forwardingContextBuilder.setL3Context(l3Contexts);
192         TenantBuilder peerTenantBuilder = buildTenant();
193         peerTenantBuilder.setForwardingContext(forwardingContextBuilder.build());
194         peerTenantBuilder.setPolicy(new PolicyBuilder()
195                 .setSubjectFeatureInstances(getSubjectFeatureInstances())
196                 .setEndpointGroup(getEndpointGroups())
197                 .setExternalImplicitGroup(externalImplicitGroups)
198                 .build());
199         Tenant peerTenant = peerTenantBuilder.build();
200         peerEndpointBuilder.setNetworkContainment(new NetworkDomainId(SUBNET_1));
201         Endpoint peerEndpoint = peerEndpointBuilder.build();
202         Collection<Endpoint> peerEndpoints = new ArrayList<>();
203         peerEndpoints.add(peerEndpoint);
204
205         when(ctx.getTenant(any(TenantId.class))).thenReturn(new IndexedTenant(peerTenant));
206         when(ctx.getEndpointManager()).thenReturn(endpointManager);
207         when(ctx.getPolicyManager()).thenReturn(policyManager);
208         when(ctx.getSwitchManager()).thenReturn(switchManager);
209         when(ctx.getCurrentPolicy()).thenReturn(policyInfo);
210         when(endpointManager.getEndpointsForGroup(any(EgKey.class))).thenReturn(peerEndpoints);
211         when(endpointManager.getEndpointsForNode(any(NodeId.class))).thenReturn(peerEndpoints);
212         when(endpointManager.getEndpointsL3PrefixForTenant(any(TenantId.class))).thenReturn(endpointL3PrefixCollection);
213         when(endpointManager.getL3Endpoint(any(L3ContextId.class), any(IpAddress.class), any(TenantId.class)))
214                 .thenReturn(buildL3Endpoint(IPV4_1, IPV4_2, MAC_0, L2BD_ID.getValue()).build());
215         when(endpointManager.getEndpoint(any(EpKey.class))).thenReturn(endpoint);
216         when(switchManager.getExternalPorts(any(NodeId.class))).thenReturn(externalPorts);
217
218         destinationMapper.syncEndpointFlows(flows, NODE_ID, endpoint, ofWriter);
219
220         // Verify usage
221         verify(ctx, times(13)).getTenant(any(TenantId.class));
222         verify(ctx, times(9)).getEndpointManager();
223         verify(ctx, times(1)).getPolicyManager();
224         verify(ctx, times(4)).getCurrentPolicy();
225         verify(flows, times(1)).createExternalL3RoutedFlow(anyShort(), anyInt(), any(Endpoint.class), any(Endpoint.class),
226                 any(L3Address.class), anySetOf(NodeConnectorId.class), eq(ofWriter));
227     }
228
229     @Test
230     public void syncLocalEndpointFlows() {
231         DestinationMapperFlows flows = mock(DestinationMapperFlows.class);
232         // Endpoint
233         EndpointBuilder endpointBuilder = buildEndpoint(IPV4_1, MAC_1, CONNECTOR_1);
234         endpointBuilder.setNetworkContainment(SUBNET_0);
235         // Peer
236         EndpointBuilder peerEndpointBuilder = buildEndpoint(IPV4_2, MAC_0, CONNECTOR_0);
237         // Subnets, l3Context and forwarding context
238         List<Subnet> subnets = new ArrayList<>();
239         SubnetBuilder subnetBuilder = new SubnetBuilder();
240         subnetBuilder.setId(new SubnetId(SUBNET_0));
241         subnetBuilder.setParent(new ContextId(L3C_ID));
242         subnetBuilder.setVirtualRouterIp(new IpAddress(new Ipv4Address(IPV4_2)));
243         subnets.add(subnetBuilder.build());
244         List<L3Context> l3Contexts = new ArrayList<>();
245         L3ContextBuilder l3ContextBuilder = new L3ContextBuilder();
246         l3ContextBuilder.setId(L3C_ID);
247         l3Contexts.add(l3ContextBuilder.build());
248         ForwardingContextBuilder forwardingContextBuilder = new ForwardingContextBuilder();
249         forwardingContextBuilder.setSubnet(subnets);
250         forwardingContextBuilder.setL3Context(l3Contexts);
251         TenantBuilder peerTenantBuilder = new TenantBuilder(getTestIndexedTenant().getTenant());
252         peerTenantBuilder.setForwardingContext(forwardingContextBuilder.build());
253         Tenant peerTenant = peerTenantBuilder.build();
254         peerEndpointBuilder.setNetworkContainment(new NetworkDomainId(SUBNET_0));
255         Endpoint peerEndpoint = peerEndpointBuilder.build();
256         Collection<Endpoint> peerEndpoints = new ArrayList<>();
257         peerEndpoints.add(peerEndpoint);
258
259         when(ctx.getTenant(any(TenantId.class))).thenReturn(new IndexedTenant(peerTenant));
260         when(ctx.getEndpointManager()).thenReturn(endpointManager);
261         when(ctx.getPolicyManager()).thenReturn(policyManager);
262         when(ctx.getCurrentPolicy()).thenReturn(policyInfo);
263         when(endpointManager.getEndpointsForGroup(any(EgKey.class))).thenReturn(peerEndpoints);
264         when(endpointManager.getEndpointsForNode(any(NodeId.class))).thenReturn(peerEndpoints);
265
266         destinationMapper.syncFlows(flows, endpointBuilder.build(), NODE_ID, ofWriter);
267
268         // Verify usage
269         verify(ctx, times(18)).getTenant(any(TenantId.class));
270         verify(ctx, times(9)).getEndpointManager();
271         verify(ctx, times(2)).getPolicyManager();
272         verify(ctx, times(4)).getCurrentPolicy();
273
274         // Verify order
275         InOrder order = inOrder(flows);
276         order.verify(flows, times(1)).createLocalL2Flow(anyShort(), anyInt(), any(Endpoint.class), eq(ofWriter));
277         order.verify(flows, times(1)).createLocalL3RoutedFlow(anyShort(), anyInt(), any(Endpoint.class),
278                 any(L3Address.class), any(Subnet.class), any(Subnet.class), eq(ofWriter));
279     }
280
281     @Test
282     public void syncRemoteEndpointFlows() {
283         DestinationMapperFlows flows = mock(DestinationMapperFlows.class);
284         // Endpoint
285         EndpointBuilder endpointBuilder = buildEndpoint(IPV4_1, MAC_1, CONNECTOR_1);
286         // Peer
287         EndpointBuilder peerEndpointBuilder = buildEndpoint(IPV4_2, MAC_0, CONNECTOR_0);
288         // Subnets, l3Context and forwarding context
289         List<Subnet> subnets = new ArrayList<>();
290         SubnetBuilder subnetBuilder = new SubnetBuilder();
291         subnetBuilder.setId(new SubnetId(SUBNET_0));
292         subnetBuilder.setParent(new ContextId(L3C_ID));
293         subnetBuilder.setVirtualRouterIp(new IpAddress(new Ipv4Address(IPV4_2)));
294         subnets.add(subnetBuilder.build());
295         List<L3Context> l3Contexts = new ArrayList<>();
296         L3ContextBuilder l3ContextBuilder = new L3ContextBuilder();
297         l3ContextBuilder.setId(L3C_ID);
298         l3Contexts.add(l3ContextBuilder.build());
299         ForwardingContextBuilder forwardingContextBuilder = new ForwardingContextBuilder();
300         forwardingContextBuilder.setSubnet(subnets);
301         forwardingContextBuilder.setL3Context(l3Contexts);
302         TenantBuilder peerTenantBuilder = buildTenant();
303         peerTenantBuilder.setForwardingContext(forwardingContextBuilder.build());
304         Tenant peerTenant = peerTenantBuilder.build();
305         // Augmentation
306         OfOverlayContextBuilder ofOverlayContextBuilder = new OfOverlayContextBuilder();
307         ofOverlayContextBuilder.setNodeConnectorId(new NodeConnectorId(CONNECTOR_0));
308         ofOverlayContextBuilder.setNodeId(new NodeId("remoteNodeID"));
309         peerEndpointBuilder.setNetworkContainment(new NetworkDomainId(SUBNET_0));
310         peerEndpointBuilder.addAugmentation(OfOverlayContext.class, ofOverlayContextBuilder.build());
311         Endpoint peerEndpoint = peerEndpointBuilder.build();
312         Collection<Endpoint> peerEndpoints = new ArrayList<>();
313         peerEndpoints.add(peerEndpoint);
314
315         when(ctx.getTenant(any(TenantId.class))).thenReturn(new IndexedTenant(peerTenant));
316         when(ctx.getEndpointManager()).thenReturn(endpointManager);
317         when(ctx.getPolicyManager()).thenReturn(policyManager);
318         when(ctx.getSwitchManager()).thenReturn(switchManager);
319         when(ctx.getCurrentPolicy()).thenReturn(policyInfo);
320         when(endpointManager.getEndpointsForGroup(any(EgKey.class))).thenReturn(peerEndpoints);
321         when(endpointManager.getEndpointsForNode(any(NodeId.class))).thenReturn(peerEndpoints);
322         when(switchManager.getTunnelIP(any(NodeId.class), eq(TunnelTypeVxlan.class)))
323                 .thenReturn(new IpAddress(new Ipv4Address(IPV4_2)));
324         when(switchManager.getTunnelPort(any(NodeId.class), eq(TunnelTypeVxlan.class)))
325                 .thenReturn(new NodeConnectorId(CONNECTOR_2));
326
327         destinationMapper.syncEndpointFlows(flows, NODE_ID, endpointBuilder.build(), ofWriter);
328
329         // Verify usage
330         verify(ctx, times(13)).getTenant(any(TenantId.class));
331         verify(ctx, times(5)).getEndpointManager();
332         verify(ctx, times(1)).getPolicyManager();
333         verify(ctx, times(3)).getCurrentPolicy();
334
335         // Verify order
336         InOrder order = inOrder(flows);
337         order.verify(flows, times(1)).createRemoteL2Flow(anyShort(), anyInt(), any(Endpoint.class), any(Endpoint.class),
338                 any(IpAddress.class), any(NodeConnectorId.class), eq(ofWriter));
339         order.verify(flows, times(1)).createRemoteL3RoutedFlow(anyShort(), anyInt(), any(Endpoint.class),
340                 any(L3Address.class), any(Subnet.class), any(IpAddress.class), any(NodeConnectorId.class),
341                 any(Subnet.class), eq(ofWriter));
342     }
343
344     @Test
345     public void syncArpFlow() throws Exception {
346         DestinationMapperFlows flows = mock(DestinationMapperFlows.class);
347         // Subnets, l3Context and forwarding context
348         List<L3Context> l3Contexts = new ArrayList<>();
349         L3ContextBuilder l3ContextBuilder = new L3ContextBuilder();
350         l3ContextBuilder.setId(L3C_ID);
351         l3Contexts.add(l3ContextBuilder.build());
352         HashSet<Subnet> subnets = new HashSet<>();
353         SubnetBuilder subnetBuilder = new SubnetBuilder();
354         subnetBuilder.setId(new SubnetId(SUBNET_0));
355         subnetBuilder.setParent(new ContextId(L3C_ID));
356         subnetBuilder.setVirtualRouterIp(new IpAddress(new Ipv4Address(IPV4_2)));
357         subnets.add(subnetBuilder.build());
358         ForwardingContextBuilder forwardingContextBuilder = new ForwardingContextBuilder();
359         forwardingContextBuilder.setSubnet(new ArrayList<>(subnets));
360         forwardingContextBuilder.setL3Context(l3Contexts);
361         TenantBuilder tenantBuilder = buildTenant();
362         tenantBuilder.setForwardingContext(forwardingContextBuilder.build());
363         Tenant tenant = tenantBuilder.build();
364         destinationMapper.subnetsByTenant.put(tenant.getId(), subnets);
365
366         when(ctx.getTenant(any(TenantId.class))).thenReturn(new IndexedTenant(tenant));
367
368         destinationMapper.syncArpFlow(flows, tenant.getId(), ofWriter);
369
370         verify(ctx, times(1)).getTenant(any(TenantId.class));
371         verify(flows, times(1)).createRouterArpFlow(anyInt(), any(IndexedTenant.class), any(Subnet.class), eq(ofWriter));
372     }
373
374     @Test
375     public void syncL3PrefixFlow() {
376         DestinationMapperFlows flows = mock(DestinationMapperFlows.class);
377         // Subnets, l3Context and forwarding context
378         List<L3Context> l3Contexts = new ArrayList<>();
379         L3ContextBuilder l3ContextBuilder = new L3ContextBuilder();
380         l3ContextBuilder.setId(L3C_ID);
381         l3Contexts.add(l3ContextBuilder.build());
382         HashSet<Subnet> subnets = new HashSet<>();
383         SubnetBuilder subnetBuilder = new SubnetBuilder();
384         subnetBuilder.setId(new SubnetId(SUBNET_0));
385         subnetBuilder.setParent(new ContextId(L3C_ID));
386         subnetBuilder.setVirtualRouterIp(new IpAddress(IPV4_2));
387         subnets.add(subnetBuilder.build());
388         ForwardingContextBuilder forwardingContextBuilder = new ForwardingContextBuilder();
389         forwardingContextBuilder.setL3Context(l3Contexts);
390         forwardingContextBuilder.setSubnet(new ArrayList<>(subnets));
391         TenantBuilder tenantBuilder = buildTenant();
392         tenantBuilder.setForwardingContext(forwardingContextBuilder.build());
393         Tenant tenant = tenantBuilder.build();
394         Collection<Endpoint> endpoints = new ArrayList<>();
395         EndpointBuilder endpointBuilder = buildEndpoint(IPV4_1, MAC_1, CONNECTOR_1);
396         endpointBuilder.setNetworkContainment(new NetworkDomainId(SUBNET_0));
397         Endpoint endpoint = endpointBuilder.build();
398         endpoints.add(endpoint);
399
400         ArrayList<EndpointL3Prefix> l3Prefixes = new ArrayList<>();
401         EndpointL3PrefixBuilder prefixBuilder = new EndpointL3PrefixBuilder();
402         List<EndpointL3Gateways> endpointL3GatewaysList = new ArrayList<>();
403         EndpointL3GatewaysBuilder endpointL3GatewaysBuilder = new EndpointL3GatewaysBuilder();
404         endpointL3GatewaysBuilder.setIpAddress(new IpAddress(new Ipv4Address(IPV4_1)));
405         endpointL3GatewaysBuilder.setL3Context(L3C_ID);
406         endpointL3GatewaysList.add(endpointL3GatewaysBuilder.build());
407         prefixBuilder.setEndpointL3Gateways(endpointL3GatewaysList);
408         l3Prefixes.add(prefixBuilder.build());
409
410         EndpointL3 endpointL3 = buildL3Endpoint(IPV4_0, IPV4_2, MAC_2, L2).build();
411
412         when(ctx.getTenant(any(TenantId.class))).thenReturn(new IndexedTenant(tenant));
413         when(ctx.getEndpointManager()).thenReturn(endpointManager);
414         when(ctx.getSwitchManager()).thenReturn(switchManager);
415         when(ctx.getPolicyManager()).thenReturn(policyManager);
416         when(ctx.getCurrentPolicy()).thenReturn(policyInfo);
417         when(endpointManager.getEndpointsForNode(any(NodeId.class))).thenReturn(endpoints);
418         when(endpointManager.getL3Endpoint(any(L3ContextId.class), any(IpAddress.class), any(TenantId.class)))
419                 .thenReturn(endpointL3);
420         when(endpointManager.getL2EndpointFromL3(any(EndpointL3.class))).thenReturn(endpoint);
421
422         destinationMapper.syncL3PrefixFlow(flows, l3Prefixes, null, NODE_ID, ofWriter);
423
424         verify(ctx, times(5)).getTenant(any(TenantId.class));
425         verify(ctx, times(4)).getEndpointManager();
426         verify(ctx, times(1)).getSwitchManager();
427         verify(ctx, times(1)).getPolicyManager();
428         verify(ctx, times(1)).getCurrentPolicy();
429         verify(flows, times(1)).createL3PrefixFlow(anyShort(), anyInt(), any(Endpoint.class), any(EndpointL3Prefix.class),
430                 any(IndexedTenant.class), any(Subnet.class), anySetOf(NodeConnectorId.class), eq(ofWriter));
431     }
432
433 }