Implementing DHCP proxy command for VPP
[groupbasedpolicy.git] / renderers / ios-xe / src / test / java / org / opendaylight / groupbasedpolicy / renderer / ios_xe_provider / impl / util / RendererPolicyUtilTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.util;
10
11 import com.google.common.collect.Lists;
12 import com.google.common.collect.Ordering;
13 import java.util.ArrayList;
14 import java.util.Collections;
15 import java.util.List;
16 import org.junit.Assert;
17 import org.junit.Test;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.AddressEndpointKey;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.address.endpoints.AddressEndpointBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ConditionName;
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.EndpointGroupId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.forwarding.l2_l3.rev170511.IpPrefixType;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.forwarding.l2_l3.rev170511.L2BridgeDomain;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.forwarding.l2_l3.rev170511.L3Context;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.forwarding.l2_l3.rev170511.MacAddressType;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.forwarding.rev160427.AddressType;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.forwarding.rev160427.ContextType;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.policy.configuration.endpoints.AddressEndpointWithLocation;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.policy.configuration.endpoints.AddressEndpointWithLocationBuilder;
31
32 /**
33  * Test for {@link RendererPolicyUtil}.
34  */
35 public class RendererPolicyUtilTest {
36
37     @Test
38     public void testLookupEndpoint() throws Exception {
39         final AddressEndpointKey key1 = new AddressEndpointBuilder()
40                 .setAddress("address1")
41                 .setAddressType(IpPrefixType.class)
42                 .setContextId(new ContextId("context1"))
43                 .setContextType(L3Context.class)
44                 .build();
45
46         final AddressEndpointWithLocation addressEPWithLocation1 =
47                 createAddressEPWithLocation("address1", IpPrefixType.class, "context1", L3Context.class);
48         final AddressEndpointWithLocation addressEPWithLocation2 =
49                 createAddressEPWithLocation("address2", IpPrefixType.class, "context1", L3Context.class);
50         final AddressEndpointWithLocation addressEPWithLocation3 =
51                 createAddressEPWithLocation("address1", MacAddressType.class, "context1", L3Context.class);
52         final AddressEndpointWithLocation addressEPWithLocation4 =
53                 createAddressEPWithLocation("address1", IpPrefixType.class, "context2", L3Context.class);
54         final AddressEndpointWithLocation addressEPWithLocation5 =
55                 createAddressEPWithLocation("address1", IpPrefixType.class, "context1", L2BridgeDomain.class);
56
57         final List<AddressEndpointWithLocation> endpoints = Lists.newArrayList(
58                 addressEPWithLocation2, addressEPWithLocation3,
59                 addressEPWithLocation4, addressEPWithLocation5,
60                 addressEPWithLocation1
61                 );
62
63         final AddressEndpointWithLocation actualEndpoint = RendererPolicyUtil.lookupEndpoint(key1, endpoints);
64         Assert.assertSame(addressEPWithLocation1, actualEndpoint);
65     }
66
67     @Test
68     public void testCreateEndpointGroupIdOrdering() throws Exception {
69         final Ordering<EndpointGroupId> endpointGroupIdOrdering = RendererPolicyUtil.createEndpointGroupIdOrdering();
70         final String epg1 = "epg1";
71         final ArrayList<EndpointGroupId> list = Lists.newArrayList(
72                 new EndpointGroupId("epg3"), new EndpointGroupId(epg1), new EndpointGroupId("epg2"));
73
74         Collections.sort(list, endpointGroupIdOrdering);
75         Assert.assertEquals(epg1, list.get(0).getValue());
76
77         Collections.sort(list, endpointGroupIdOrdering.reversed());
78         Assert.assertEquals(epg1, list.get(2).getValue());
79     }
80
81     @Test
82     public void testCreateConditionNameOrdering() throws Exception {
83         final Ordering<ConditionName> conditionNameOrdering = RendererPolicyUtil.createConditionNameOrdering();
84         final String name1 = "name1";
85         final ArrayList<ConditionName> list = Lists.newArrayList(
86                 new ConditionName("name3"), new ConditionName(name1), new ConditionName("name2"));
87
88         Collections.sort(list, conditionNameOrdering);
89         Assert.assertEquals(name1, list.get(0).getValue());
90
91         Collections.sort(list, conditionNameOrdering.reversed());
92         Assert.assertEquals(name1, list.get(2).getValue());
93     }
94
95     private AddressEndpointWithLocation createAddressEPWithLocation(final String address,
96                                                                     final Class<? extends AddressType> addressType,
97                                                                     final String context,
98                                                                     final Class<? extends ContextType> contextType) {
99         return new AddressEndpointWithLocationBuilder()
100                 .setAddress(address)
101                 .setAddressType(addressType)
102                 .setContextId(new ContextId(context))
103                 .setContextType(contextType)
104                 .build();
105     }
106 }