b8016ac8fd5e8a5fff205b59e9521be1b888c183
[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.rev130715.IpAddress;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.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(SUBNET_0);
101         subnetBuilder.setParent(L2FD_ID);
102
103         // expected result
104         L3ContextBuilder expectedL3Context = new L3ContextBuilder();
105         expectedL3Context.setId(new L3ContextId(L3C_ID));
106
107         L3Context l3Context = utils.getL3ContextForSubnet(getTestIndexedTenant(), subnetBuilder.build());
108         assertEquals(l3Context, expectedL3Context.build());
109     }
110
111     @Test
112     public void getEpNetworkContainment_getNull() {
113         EndpointBuilder endpointBuilder = buildEndpoint(IPV4_0, MAC_1, CONNECTOR_1);
114         endpointBuilder.setNetworkContainment(null);
115
116         NetworkDomainId result = utils.getEPNetworkContainment(endpointBuilder.build(), null);
117         assertNull(result);
118     }
119
120     @Test
121     public void getEpNetworkContainment_getDomainIdFromEpg() {
122         EndpointBuilder endpointBuilder = buildEndpoint(IPV4_0, MAC_1, CONNECTOR_1);
123         endpointBuilder.setNetworkContainment(null);
124         EndpointGroupBuilder endpointGroupBuilder = new EndpointGroupBuilder();
125         endpointGroupBuilder.setId(ENDPOINT_GROUP_0);
126         endpointGroupBuilder.setNetworkDomain(new NetworkDomainId(NET_DOMAIN_ID));
127         endpointBuilder.setEndpointGroup(endpointGroupBuilder.build().getId());
128
129         NetworkDomainId result = utils.getEPNetworkContainment(endpointBuilder.build(), getTestIndexedTenant());
130         assertEquals(result, new SubnetId(SUBNET_0));
131     }
132
133     @Test
134     public void getEpNetworkContainment_getDomainIdFromEndpoint() {
135         String domainId = "domainId";
136         EndpointBuilder endpointBuilder = buildEndpoint(IPV4_0, MAC_1, CONNECTOR_1);
137         endpointBuilder.setNetworkContainment(new NetworkDomainId(domainId));
138
139         NetworkDomainId result = utils.getEPNetworkContainment(endpointBuilder.build(), getTestIndexedTenant());
140         assertEquals(result, new NetworkDomainId(domainId));
141     }
142
143     @Test
144     public void getLocalSubnets() {
145         Collection<Endpoint> endpoints = new ArrayList<>();
146         EndpointBuilder epWithoutSubnet = buildEndpoint(IPV4_0, MAC_1, CONNECTOR_1);
147         epWithoutSubnet.setTenant(null);
148         EndpointBuilder epWithSubnet = buildEndpoint(IPV4_0, MAC_0, CONNECTOR_1);
149         TenantBuilder tenantBuilder = new TenantBuilder(buildTenant().build());
150         Tenant tenant = tenantBuilder.build();
151         epWithSubnet.setTenant(tenant.getId());
152         epWithSubnet.setTenant(tenant.getId());
153         endpoints.add(epWithoutSubnet.build());
154         endpoints.add(epWithSubnet.build());
155
156         when(ctx.getEndpointManager()).thenReturn(endpointManager);
157         when(ctx.getTenant(null)).thenReturn(null);
158         when(ctx.getTenant(tenant.getId())).thenReturn(new IndexedTenant(tenant));
159         when(endpointManager.getEndpointsForNode(NODE_ID)).thenReturn(endpoints);
160
161         List<Subnet> subnets = utils.getLocalSubnets(NODE_ID);
162         verify(endpointManager, times(1)).getEndpointsForNode(any(NodeId.class));
163         assertTrue(subnets.size() == 1);
164     }
165
166     @Test
167     public void getL2EpOfSubnetGateway_nullCase() {
168         Endpoint result = utils.getL2EpOfSubnetGateway(buildTenant().getId(), null);
169         assertNull(result);
170     }
171
172     @Test
173     public void getL2EpOfSubnetGateway_nullL3Prefix() {
174         SubnetBuilder subnetBuilder = new SubnetBuilder();
175         subnetBuilder.setId(new SubnetId(SUBNET_0));
176         subnetBuilder.setVirtualRouterIp(new IpAddress(new Ipv4Address(IPV4_0)));
177
178         when(ctx.getEndpointManager()).thenReturn(endpointManager);
179         when(endpointManager.getEndpointsL3PrefixForTenant(buildTenant().getId())).thenReturn(null);
180
181         Endpoint result = utils.getL2EpOfSubnetGateway(buildTenant().getId(), subnetBuilder.build());
182         assertNull(result);
183     }
184
185     @Test
186     public void getL2EpOfSubnetGateway_emptyL3Prefix() {
187         SubnetBuilder subnetBuilder = new SubnetBuilder();
188         subnetBuilder.setId(new SubnetId(SUBNET_2));
189         subnetBuilder.setVirtualRouterIp(new IpAddress(new Ipv4Address(IPV4_0)));
190
191         when(ctx.getEndpointManager()).thenReturn(endpointManager);
192         when(endpointManager.getEndpointsL3PrefixForTenant(buildTenant().getId()))
193                 .thenReturn(new ArrayList<EndpointL3Prefix>());
194
195         Endpoint result = utils.getL2EpOfSubnetGateway(buildTenant().getId(), subnetBuilder.build());
196         assertNull(result);
197     }
198
199     @Test
200     public void getL2EpOfSubnetGateway() {
201         SubnetBuilder subnetBuilder = new SubnetBuilder();
202         subnetBuilder.setId(new SubnetId(SUBNET_1));
203         subnetBuilder.setVirtualRouterIp(new IpAddress(new Ipv4Address(IPV4_0)));
204
205         // L3 Prefix
206         Collection<EndpointL3Prefix> l3PrefixCollection = new ArrayList<>();
207         EndpointL3PrefixBuilder endpointL3PrefixBuilder = new EndpointL3PrefixBuilder();
208         List<EndpointL3Gateways> endpointL3GatewaysList = new ArrayList<>();
209         EndpointL3GatewaysBuilder endpointL3GatewaysBuilder = new EndpointL3GatewaysBuilder();
210         endpointL3GatewaysList.add(endpointL3GatewaysBuilder.build());
211         endpointL3PrefixBuilder.setEndpointL3Gateways(endpointL3GatewaysList);
212         endpointL3GatewaysBuilder.setL3Context(new L3ContextBuilder().setId(new L3ContextId("l3cId")).build().getId());
213         l3PrefixCollection.add(endpointL3PrefixBuilder.build());
214
215         // L3 Endpoint
216         EndpointL3Builder endpointL3Builder = new EndpointL3Builder();
217         endpointL3Builder.setL2Context(new L2BridgeDomainBuilder().setId(new L2BridgeDomainId("bdId")).build().getId());
218         endpointL3Builder.setMacAddress(new MacAddress(MAC_0));
219
220         // Endpoint
221         EndpointBuilder endpointBuilder = buildEndpoint(IPV4_0, MAC_0, CONNECTOR_0);
222
223         when(ctx.getEndpointManager()).thenReturn(endpointManager);
224         when(endpointManager.getEndpointsL3PrefixForTenant(buildTenant().getId())).thenReturn(l3PrefixCollection);
225         when(endpointManager.getL3Endpoint(any(L3ContextId.class), any(IpAddress.class),
226                 any(TenantId.class))).thenReturn(endpointL3Builder.build());
227         when(endpointManager.getEndpoint(any(EpKey.class))).thenReturn(endpointBuilder.build());
228
229         Endpoint result = utils.getL2EpOfSubnetGateway(buildTenant().getId(), subnetBuilder.build());
230         verify(endpointManager, times(1)).getEndpointsL3PrefixForTenant(any(TenantId.class));
231         verify(endpointManager, times(1)).getL3Endpoint(any(L3ContextId.class), any(IpAddress.class),
232                 Mockito.any(TenantId.class));
233         verify(endpointManager, times(1)).getEndpoint(any(EpKey.class));
234         assertNotNull(result);
235     }
236
237     @Test
238     public void routerPortMac_noL3ContextId() {
239         L3ContextBuilder contextBuilder = new L3ContextBuilder();
240         MacAddress result = utils.routerPortMac(contextBuilder.build(), new IpAddress(new Ipv4Address(IPV4_0)),
241                 buildTenant().getId());
242         assertEquals(result, DestinationMapper.ROUTER_MAC);
243     }
244
245     @Test
246     public void routerPortMac_nullEp() {
247         L3ContextBuilder contextBuilder = new L3ContextBuilder();
248         contextBuilder.setId(new L3ContextId("l3id"));
249         L3Context context = contextBuilder.build();
250
251         when(ctx.getEndpointManager()).thenReturn(endpointManager);
252         when(endpointManager.getL3Endpoint(any(L3ContextId.class), any(IpAddress.class), any(TenantId.class)))
253                 .thenReturn(null);
254
255         MacAddress result = utils.routerPortMac(context, new IpAddress(new Ipv4Address(IPV4_0)),
256                 buildTenant().getId());
257
258         verify(endpointManager, times(1)).getL3Endpoint(any(L3ContextId.class), any(IpAddress.class),
259                 any(TenantId.class));
260         assertEquals(result, DestinationMapper.ROUTER_MAC);
261     }
262
263     @Test
264     public void routerPortMac() {
265         L3ContextBuilder contextBuilder = new L3ContextBuilder();
266         contextBuilder.setId(new L3ContextId("l3id"));
267         L3Context context = contextBuilder.build();
268         EndpointL3Builder endpointL3Builder = buildL3Endpoint(IPV4_0, IPV4_1, MAC_0, L2);
269
270         when(ctx.getEndpointManager()).thenReturn(endpointManager);
271         when(endpointManager.getL3Endpoint(any(L3ContextId.class), any(IpAddress.class), any(TenantId.class)))
272                 .thenReturn(endpointL3Builder.build());
273
274         MacAddress result = utils.routerPortMac(context, new IpAddress(new Ipv4Address(IPV4_0)),
275                 buildTenant().getId());
276
277         verify(endpointManager, times(1)).getL3Endpoint(any(L3ContextId.class), any(IpAddress.class),
278                 any(TenantId.class));
279         assertEquals(result, new MacAddress(MAC_0));
280     }
281
282     @Test
283     public void getAllEndpointGroups() {
284         EndpointBuilder endpointBuilder = buildEndpoint(IPV4_0, MAC_0, CONNECTOR_0);
285         endpointBuilder.setEndpointGroup(ENDPOINT_GROUP_0);
286         List<EndpointGroupId> endpointGroups = new ArrayList<>();
287         EndpointGroupBuilder endpointGroupBuilder = new EndpointGroupBuilder();
288         endpointGroupBuilder.setId(ENDPOINT_GROUP_1);
289         endpointGroups.add(endpointGroupBuilder.build().getId());
290         endpointBuilder.setEndpointGroups(endpointGroups);
291
292         Set<EndpointGroupId> result = utils.getAllEndpointGroups(endpointBuilder.build());
293         assertTrue(result.size() == 2);
294     }
295
296     @Test
297     public void getEndpointOrdinals_exceptionCaught() {
298         EndpointBuilder endpointBuilder = buildEndpoint(IPV4_0, MAC_0, CONNECTOR_0);
299         endpointBuilder.setEndpointGroup(ENDPOINT_GROUP_0);
300
301         when(ctx.getTenant(Mockito.any(TenantId.class))).thenReturn(getTestIndexedTenant());
302
303         OrdinalFactory.EndpointFwdCtxOrdinals ordinals = utils.getEndpointOrdinals(endpointBuilder.build());
304         assertNull(ordinals);
305     }
306
307     @Test
308     public void getEndpointOrdinals() {
309         EndpointBuilder endpointBuilder = buildEndpoint(IPV4_0, MAC_0, CONNECTOR_0);
310         endpointBuilder.setEndpointGroup(ENDPOINT_GROUP_0);
311
312         when(ctx.getTenant(Mockito.any(TenantId.class))).thenReturn(getTestIndexedTenant());
313         when(ctx.getEndpointManager()).thenReturn(endpointManager);
314         when(ctx.getCurrentPolicy()).thenReturn(policyInfo);
315
316         OrdinalFactory.EndpointFwdCtxOrdinals ordinals = utils.getEndpointOrdinals(endpointBuilder.build());
317         assertEquals(ordinals.getEp().toString(), new EpKey(new L2ContextId(L2BD_ID), new MacAddress(MAC_0)).toString());
318         assertEquals(ordinals.getNetworkContainment(), SUBNET_0);
319     }
320
321     @Test
322     public void getIndexedTenant() {
323         TenantBuilder tenantBuilder = buildTenant();
324         tenantBuilder.setId(TENANT_ID);
325         when(ctx.getTenant(TENANT_ID)).thenReturn(new IndexedTenant(tenantBuilder.build()));
326         IndexedTenant result = utils.getIndexedTenant(TENANT_ID);
327         assertTrue(result.getTenant().getId().equals(TENANT_ID));
328     }
329 }