Bug 5617: OfOverlay Refactoring - Destination mapper
[groupbasedpolicy.git] / renderers / ofoverlay / src / test / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / mapper / destination / DestinationMapperUtilsTest.java
1 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.mapper.destination;
2
3 import org.junit.Before;
4 import org.junit.Test;
5 import org.mockito.Mockito;
6 import org.opendaylight.groupbasedpolicy.dto.EpKey;
7 import org.opendaylight.groupbasedpolicy.dto.IndexedTenant;
8 import org.opendaylight.groupbasedpolicy.dto.PolicyInfo;
9 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfContext;
10 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.endpoint.EndpointManager;
11 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.OrdinalFactory;
12 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.mapper.MapperUtilsTest;
13 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2BridgeDomainId;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2ContextId;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L3ContextId;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.NetworkDomainId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubnetId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoint.l3.prefix.fields.EndpointL3Gateways;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoint.l3.prefix.fields.EndpointL3GatewaysBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3Builder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3Prefix;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3PrefixBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.Tenant;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.TenantBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L2BridgeDomainBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L3Context;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L3ContextBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.Subnet;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.SubnetBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.EndpointGroupBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
39
40 import java.util.ArrayList;
41 import java.util.Collection;
42 import java.util.Collections;
43 import java.util.HashSet;
44 import java.util.List;
45 import java.util.Set;
46
47 import static junit.framework.Assert.assertEquals;
48 import static junit.framework.TestCase.assertNotNull;
49 import static org.junit.Assert.assertNull;
50 import static org.junit.Assert.assertTrue;
51 import static org.mockito.Mockito.*;
52
53 public class DestinationMapperUtilsTest extends MapperUtilsTest {
54
55     private DestinationMapperUtils utils;
56
57     @Before
58     public void init() {
59         endpointManager = mock(EndpointManager.class);
60         policyInfo = mock(PolicyInfo.class);
61         ctx = mock(OfContext.class);
62         utils = new DestinationMapperUtils(ctx);
63     }
64
65     @Test
66     public void getSubnets() {
67         EndpointBuilder endpointBuilder = new EndpointBuilder();
68         HashSet<Subnet> emptyArray = utils.getSubnets(endpointBuilder.build().getTenant());
69         assertTrue(emptyArray.equals(Collections.emptySet()));
70
71         List<Subnet> subnets = getSubnetList();
72         Tenant tenant = buildTenant().build();
73         endpointBuilder.setTenant(tenant.getId());
74         when(ctx.getTenant(any(TenantId.class))).thenReturn(new IndexedTenant(tenant));
75
76         HashSet<Subnet> result = utils.getSubnets(endpointBuilder.build().getTenant());
77         List<Subnet> resultAsList = new ArrayList<>(result);
78         assertTrue(subnets.containsAll(resultAsList));
79     }
80
81     @Test
82     public void getL3ContextForSubnet_nullTenant() {
83         SubnetBuilder subnetBuilder = new SubnetBuilder();
84         subnetBuilder.setId(new SubnetId(SUBNET_2));
85         L3Context l3Context = utils.getL3ContextForSubnet(null, subnetBuilder.build());
86         assertNull(l3Context);
87     }
88
89     @Test
90     public void getL3ContextForSubnet_nullResult() {
91         SubnetBuilder subnetBuilder = new SubnetBuilder();
92         subnetBuilder.setId(new SubnetId("otherSubnet"));
93         L3Context l3Context = utils.getL3ContextForSubnet(getTestIndexedTenant(), subnetBuilder.build());
94         assertNull(l3Context);
95     }
96
97     @Test
98     public void getL3ContextForSubnet_l3Context() {
99         SubnetBuilder subnetBuilder = new SubnetBuilder();
100         subnetBuilder.setId(new SubnetId(L3C_ID));
101
102         // expected result
103         L3ContextBuilder expectedL3Context = new L3ContextBuilder();
104         expectedL3Context.setId(new L3ContextId(L3C_ID));
105
106         L3Context l3Context = utils.getL3ContextForSubnet(getTestIndexedTenant(), subnetBuilder.build());
107         assertEquals(l3Context, expectedL3Context.build());
108     }
109
110     @Test
111     public void getEpNetworkContainment_getNull() {
112         EndpointBuilder endpointBuilder = buildEndpoint(IPV4_0, MAC_1, CONNECTOR_1);
113         endpointBuilder.setNetworkContainment(null);
114
115         NetworkDomainId result = utils.getEPNetworkContainment(endpointBuilder.build(), null);
116         assertNull(result);
117     }
118
119     @Test
120     public void getEpNetworkContainment_getDomainIdFromEpg() {
121         EndpointBuilder endpointBuilder = buildEndpoint(IPV4_0, MAC_1, CONNECTOR_1);
122         endpointBuilder.setNetworkContainment(null);
123         EndpointGroupBuilder endpointGroupBuilder = new EndpointGroupBuilder();
124         endpointGroupBuilder.setId(ENDPOINT_GROUP_0);
125         endpointGroupBuilder.setNetworkDomain(new NetworkDomainId(NET_DOMAIN_ID));
126         endpointBuilder.setEndpointGroup(endpointGroupBuilder.build().getId());
127
128         NetworkDomainId result = utils.getEPNetworkContainment(endpointBuilder.build(), getTestIndexedTenant());
129         assertEquals(result, new SubnetId(SUBNET_0));
130     }
131
132     @Test
133     public void getEpNetworkContainment_getDomainIdFromEndpoint() {
134         String domainId = "domainId";
135         EndpointBuilder endpointBuilder = buildEndpoint(IPV4_0, MAC_1, CONNECTOR_1);
136         endpointBuilder.setNetworkContainment(new NetworkDomainId(domainId));
137
138         NetworkDomainId result = utils.getEPNetworkContainment(endpointBuilder.build(), getTestIndexedTenant());
139         assertEquals(result, new NetworkDomainId(domainId));
140     }
141
142     @Test
143     public void getLocalSubnets() {
144         Collection<Endpoint> endpoints = new ArrayList<>();
145         EndpointBuilder epWithoutSubnet = buildEndpoint(IPV4_0, MAC_1, CONNECTOR_1);
146         epWithoutSubnet.setTenant(null);
147         EndpointBuilder epWithSubnet = buildEndpoint(IPV4_0, MAC_0, CONNECTOR_1);
148         TenantBuilder tenantBuilder = new TenantBuilder(buildTenant().build());
149         Tenant tenant = tenantBuilder.build();
150         epWithSubnet.setTenant(tenant.getId());
151         epWithSubnet.setNetworkContainment(new NetworkDomainId(SUBNET_0));
152         epWithSubnet.setTenant(tenant.getId());
153         epWithSubnet.setNetworkContainment(new NetworkDomainId(SUBNET_1));
154         endpoints.add(epWithoutSubnet.build());
155         endpoints.add(epWithSubnet.build());
156
157         when(ctx.getEndpointManager()).thenReturn(endpointManager);
158         when(ctx.getTenant(null)).thenReturn(null);
159         when(ctx.getTenant(tenant.getId())).thenReturn(new IndexedTenant(tenant));
160         when(endpointManager.getEndpointsForNode(NODE_ID)).thenReturn(endpoints);
161
162         List<Subnet> subnets = utils.getLocalSubnets(NODE_ID);
163         verify(endpointManager, times(1)).getEndpointsForNode(any(NodeId.class));
164         assertTrue(subnets.size() == 1);
165     }
166
167     @Test
168     public void getL2EpOfSubnetGateway_nullCase() {
169         Endpoint result = utils.getL2EpOfSubnetGateway(buildTenant().getId(), null);
170         assertNull(result);
171     }
172
173     @Test
174     public void getL2EpOfSubnetGateway_nullL3Prefix() {
175         SubnetBuilder subnetBuilder = new SubnetBuilder();
176         subnetBuilder.setId(new SubnetId(SUBNET_0));
177         subnetBuilder.setVirtualRouterIp(new IpAddress(new Ipv4Address(IPV4_0)));
178
179         when(ctx.getEndpointManager()).thenReturn(endpointManager);
180         when(endpointManager.getEndpointsL3PrefixForTenant(buildTenant().getId())).thenReturn(null);
181
182         Endpoint result = utils.getL2EpOfSubnetGateway(buildTenant().getId(), subnetBuilder.build());
183         assertNull(result);
184     }
185
186     @Test
187     public void getL2EpOfSubnetGateway_emptyL3Prefix() {
188         SubnetBuilder subnetBuilder = new SubnetBuilder();
189         subnetBuilder.setId(new SubnetId(SUBNET_2));
190         subnetBuilder.setVirtualRouterIp(new IpAddress(new Ipv4Address(IPV4_0)));
191
192         when(ctx.getEndpointManager()).thenReturn(endpointManager);
193         when(endpointManager.getEndpointsL3PrefixForTenant(buildTenant().getId()))
194                 .thenReturn(new ArrayList<EndpointL3Prefix>());
195
196         Endpoint result = utils.getL2EpOfSubnetGateway(buildTenant().getId(), subnetBuilder.build());
197         assertNull(result);
198     }
199
200     @Test
201     public void getL2EpOfSubnetGateway() {
202         SubnetBuilder subnetBuilder = new SubnetBuilder();
203         subnetBuilder.setId(new SubnetId(SUBNET_1));
204         subnetBuilder.setVirtualRouterIp(new IpAddress(new Ipv4Address(IPV4_0)));
205
206         // L3 Prefix
207         Collection<EndpointL3Prefix> l3PrefixCollection = new ArrayList<>();
208         EndpointL3PrefixBuilder endpointL3PrefixBuilder = new EndpointL3PrefixBuilder();
209         List<EndpointL3Gateways> endpointL3GatewaysList = new ArrayList<>();
210         EndpointL3GatewaysBuilder endpointL3GatewaysBuilder = new EndpointL3GatewaysBuilder();
211         endpointL3GatewaysList.add(endpointL3GatewaysBuilder.build());
212         endpointL3PrefixBuilder.setEndpointL3Gateways(endpointL3GatewaysList);
213         endpointL3GatewaysBuilder.setL3Context(new L3ContextBuilder().setId(new L3ContextId("l3cId")).build().getId());
214         l3PrefixCollection.add(endpointL3PrefixBuilder.build());
215
216         // L3 Endpoint
217         EndpointL3Builder endpointL3Builder = new EndpointL3Builder();
218         endpointL3Builder.setL2Context(new L2BridgeDomainBuilder().setId(new L2BridgeDomainId("bdId")).build().getId());
219         endpointL3Builder.setMacAddress(new MacAddress(MAC_0));
220
221         // Endpoint
222         EndpointBuilder endpointBuilder = buildEndpoint(IPV4_0, MAC_0, CONNECTOR_0);
223
224         when(ctx.getEndpointManager()).thenReturn(endpointManager);
225         when(endpointManager.getEndpointsL3PrefixForTenant(buildTenant().getId())).thenReturn(l3PrefixCollection);
226         when(endpointManager.getL3Endpoint(any(L3ContextId.class), any(IpAddress.class),
227                 any(TenantId.class))).thenReturn(endpointL3Builder.build());
228         when(endpointManager.getEndpoint(any(EpKey.class))).thenReturn(endpointBuilder.build());
229
230         Endpoint result = utils.getL2EpOfSubnetGateway(buildTenant().getId(), subnetBuilder.build());
231         verify(endpointManager, times(1)).getEndpointsL3PrefixForTenant(any(TenantId.class));
232         verify(endpointManager, times(1)).getL3Endpoint(any(L3ContextId.class), any(IpAddress.class),
233                 Mockito.any(TenantId.class));
234         verify(endpointManager, times(1)).getEndpoint(any(EpKey.class));
235         assertNotNull(result);
236     }
237
238     @Test
239     public void routerPortMac_noL3ContextId() {
240         L3ContextBuilder contextBuilder = new L3ContextBuilder();
241         MacAddress result = utils.routerPortMac(contextBuilder.build(), new IpAddress(new Ipv4Address(IPV4_0)),
242                 buildTenant().getId());
243         assertEquals(result, DestinationMapper.ROUTER_MAC);
244     }
245
246     @Test
247     public void routerPortMac_nullEp() {
248         L3ContextBuilder contextBuilder = new L3ContextBuilder();
249         contextBuilder.setId(new L3ContextId("l3id"));
250         L3Context context = contextBuilder.build();
251
252         when(ctx.getEndpointManager()).thenReturn(endpointManager);
253         when(endpointManager.getL3Endpoint(any(L3ContextId.class), any(IpAddress.class), any(TenantId.class)))
254                 .thenReturn(null);
255
256         MacAddress result = utils.routerPortMac(context, new IpAddress(new Ipv4Address(IPV4_0)),
257                 buildTenant().getId());
258
259         verify(endpointManager, times(1)).getL3Endpoint(any(L3ContextId.class), any(IpAddress.class),
260                 any(TenantId.class));
261         assertEquals(result, DestinationMapper.ROUTER_MAC);
262     }
263
264     @Test
265     public void routerPortMac() {
266         L3ContextBuilder contextBuilder = new L3ContextBuilder();
267         contextBuilder.setId(new L3ContextId("l3id"));
268         L3Context context = contextBuilder.build();
269         EndpointL3Builder endpointL3Builder = buildL3Endpoint(IPV4_0, IPV4_1, MAC_0, L2);
270
271         when(ctx.getEndpointManager()).thenReturn(endpointManager);
272         when(endpointManager.getL3Endpoint(any(L3ContextId.class), any(IpAddress.class), any(TenantId.class)))
273                 .thenReturn(endpointL3Builder.build());
274
275         MacAddress result = utils.routerPortMac(context, new IpAddress(new Ipv4Address(IPV4_0)),
276                 buildTenant().getId());
277
278         verify(endpointManager, times(1)).getL3Endpoint(any(L3ContextId.class), any(IpAddress.class),
279                 any(TenantId.class));
280         assertEquals(result, new MacAddress(MAC_0));
281     }
282
283     @Test
284     public void getAllEndpointGroups() {
285         EndpointBuilder endpointBuilder = buildEndpoint(IPV4_0, MAC_0, CONNECTOR_0);
286         endpointBuilder.setEndpointGroup(ENDPOINT_GROUP_0);
287         List<EndpointGroupId> endpointGroups = new ArrayList<>();
288         EndpointGroupBuilder endpointGroupBuilder = new EndpointGroupBuilder();
289         endpointGroupBuilder.setId(ENDPOINT_GROUP_1);
290         endpointGroups.add(endpointGroupBuilder.build().getId());
291         endpointBuilder.setEndpointGroups(endpointGroups);
292
293         Set<EndpointGroupId> result = utils.getAllEndpointGroups(endpointBuilder.build());
294         assertTrue(result.size() == 2);
295     }
296
297     @Test
298     public void getEndpointOrdinals_exceptionCaught() {
299         EndpointBuilder endpointBuilder = buildEndpoint(IPV4_0, MAC_0, CONNECTOR_0);
300         endpointBuilder.setEndpointGroup(ENDPOINT_GROUP_0);
301
302         when(ctx.getTenant(Mockito.any(TenantId.class))).thenReturn(getTestIndexedTenant());
303
304         OrdinalFactory.EndpointFwdCtxOrdinals ordinals = utils.getEndpointOrdinals(endpointBuilder.build());
305         assertNull(ordinals);
306     }
307
308     @Test
309     public void getEndpointOrdinals() {
310         EndpointBuilder endpointBuilder = buildEndpoint(IPV4_0, MAC_0, CONNECTOR_0);
311         endpointBuilder.setEndpointGroup(ENDPOINT_GROUP_0);
312
313         when(ctx.getTenant(Mockito.any(TenantId.class))).thenReturn(getTestIndexedTenant());
314         when(ctx.getEndpointManager()).thenReturn(endpointManager);
315         when(ctx.getCurrentPolicy()).thenReturn(policyInfo);
316
317         OrdinalFactory.EndpointFwdCtxOrdinals ordinals = utils.getEndpointOrdinals(endpointBuilder.build());
318         assertEquals(ordinals.getEp().toString(), new EpKey(new L2ContextId(L2BD_ID), new MacAddress(MAC_0)).toString());
319         assertEquals(ordinals.getNetworkContainment(), NET_DOMAIN_ID);
320     }
321
322     @Test
323     public void getIndexedTenant() {
324         TenantBuilder tenantBuilder = buildTenant();
325         tenantBuilder.setId(TENANT_ID);
326         when(ctx.getTenant(TENANT_ID)).thenReturn(new IndexedTenant(tenantBuilder.build()));
327         IndexedTenant result = utils.getIndexedTenant(TENANT_ID);
328         assertTrue(result.getTenant().getId().equals(TENANT_ID));
329     }
330 }