Introducing new API in neutron-mapper
[groupbasedpolicy.git] / neutron-mapper / src / test / java / org / opendaylight / groupbasedpolicy / neutron / gbp / util / NeutronGbpIidFactoryTest.java
1 package org.opendaylight.groupbasedpolicy.neutron.gbp.util;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertNotNull;
6 import static org.junit.Assert.assertTrue;
7 import static org.mockito.Mockito.mock;
8
9 import java.util.Iterator;
10
11 import org.junit.Test;
12 import org.opendaylight.groupbasedpolicy.neutron.mapper.util.MappingUtils;
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.IpPrefix;
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.ContextId;
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.L3ContextId;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.UniqueId;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.forwarding.l2_l3.rev160427.IpPrefixType;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.forwarding.l2_l3.rev160427.MacAddressType;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.Mappings;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.mappings.GbpByNeutronMappings;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.mappings.NeutronByGbpMappings;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.mappings.gbp.by.neutron.mappings.BaseEndpointsByPorts;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.mappings.gbp.by.neutron.mappings.EndpointsByPorts;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.mappings.gbp.by.neutron.mappings.base.endpoints.by.ports.BaseEndpointByPort;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.mappings.gbp.by.neutron.mappings.base.endpoints.by.ports.BaseEndpointByPortKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.mappings.gbp.by.neutron.mappings.endpoints.by.ports.EndpointByPort;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.mappings.neutron.by.gbp.mappings.ExternalGatewaysAsEndpoints;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.mappings.neutron.by.gbp.mappings.ExternalGatewaysAsL3Endpoints;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.mappings.neutron.by.gbp.mappings.PortsByBaseEndpoints;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.mappings.neutron.by.gbp.mappings.PortsByEndpoints;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.mappings.neutron.by.gbp.mappings.external.gateways.as.endpoints.ExternalGatewayAsEndpoint;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.mappings.neutron.by.gbp.mappings.external.gateways.as.l3.endpoints.ExternalGatewayAsL3Endpoint;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.mappings.neutron.by.gbp.mappings.ports.by.base.endpoints.PortByBaseEndpoint;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.mappings.neutron.by.gbp.mappings.ports.by.base.endpoints.PortByBaseEndpointKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.mappings.neutron.by.gbp.mappings.ports.by.endpoints.PortByEndpoint;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
41
42 public class NeutronGbpIidFactoryTest {
43
44     @Test
45     public void endpointByPortIidTest() {
46         UniqueId portId = mock(UniqueId.class);
47         InstanceIdentifier<EndpointByPort> id = NeutronGbpIidFactory.endpointByPortIid(portId);
48         assertNotNull(id);
49         Class<?>[] expectedTypes = {Mappings.class, GbpByNeutronMappings.class, EndpointsByPorts.class,
50                 EndpointByPort.class};
51         assertPathArgumentTypes(id.getPathArguments(), expectedTypes);
52         assertEquals(EndpointByPort.class, id.getTargetType());
53         assertFalse(id.isWildcarded());
54         assertEquals(portId, id.firstKeyOf(EndpointByPort.class).getPortId());
55     }
56
57     @Test
58     public void portByEndpointIidTest() {
59         L2BridgeDomainId l2BdId = mock(L2BridgeDomainId.class);
60         MacAddress mac = mock(MacAddress.class);
61         InstanceIdentifier<PortByEndpoint> id = NeutronGbpIidFactory.portByEndpointIid(l2BdId, mac);
62         assertNotNull(id);
63         Class<?>[] expectedTypes = {Mappings.class, NeutronByGbpMappings.class, PortsByEndpoints.class,
64                 PortByEndpoint.class};
65         assertPathArgumentTypes(id.getPathArguments(), expectedTypes);
66         assertEquals(PortByEndpoint.class, id.getTargetType());
67         assertFalse(id.isWildcarded());
68         assertEquals(l2BdId, id.firstKeyOf(PortByEndpoint.class).getL2Context());
69         assertEquals(mac, id.firstKeyOf(PortByEndpoint.class).getMacAddress());
70     }
71
72     @Test
73     public void portByBaseEndpointIidTest() {
74         String address = "00:00:00:00:00:00:01";
75         ContextId ctxId = new ContextId("00000000-1111-2222-3333-44444555566667777");
76         PortByBaseEndpointKey key = new PortByBaseEndpointKey(address, MacAddressType.class, ctxId, MappingUtils.L2_BRDIGE_DOMAIN);
77         InstanceIdentifier<PortByBaseEndpoint> id = NeutronGbpIidFactory.portByBaseEndpointIid(key);
78         assertNotNull(id);
79         Class<?>[] expectedTypes = {Mappings.class, NeutronByGbpMappings.class, PortsByBaseEndpoints.class,
80                 PortByBaseEndpoint.class};
81         assertPathArgumentTypes(id.getPathArguments(), expectedTypes);
82         assertEquals(PortByBaseEndpoint.class, id.getTargetType());
83         assertFalse(id.isWildcarded());
84         assertEquals(ctxId, id.firstKeyOf(PortByBaseEndpoint.class).getContextId());
85         assertEquals(MappingUtils.L2_BRDIGE_DOMAIN, id.firstKeyOf(PortByBaseEndpoint.class)
86                 .getContextType());
87         assertEquals(address, id.firstKeyOf(PortByBaseEndpoint.class).getAddress());
88         assertEquals(MacAddressType.class, id.firstKeyOf(PortByBaseEndpoint.class)
89                 .getAddressType());
90     }
91
92     @Test
93     public void baseEndpointByPortTest() {
94         UniqueId portId = new UniqueId("00000000-1111-2222-3333-44444555566667777");
95
96         InstanceIdentifier<BaseEndpointByPort> id = NeutronGbpIidFactory.baseEndpointByPortIid(portId);
97         assertNotNull(id);
98         Class<?>[] expectedTypes = {Mappings.class, GbpByNeutronMappings.class, BaseEndpointsByPorts.class,
99                 BaseEndpointByPort.class};
100         assertPathArgumentTypes(id.getPathArguments(), expectedTypes);
101         assertEquals(BaseEndpointByPort.class, id.getTargetType());
102         assertFalse(id.isWildcarded());
103         assertEquals(portId, id.firstKeyOf(BaseEndpointByPort.class).getPortId());
104     }
105
106     @Test
107     public void externalGatewayAsL3EndpointTest() {
108         L3ContextId l3Context = mock(L3ContextId.class);
109         IpAddress ipAddress = mock(IpAddress.class);
110         InstanceIdentifier<ExternalGatewayAsL3Endpoint> id = NeutronGbpIidFactory.externalGatewayAsL3Endpoint(
111                 l3Context, ipAddress);
112         assertNotNull(id);
113         Class<?>[] expectedTypes = {Mappings.class, NeutronByGbpMappings.class, ExternalGatewaysAsL3Endpoints.class,
114                 ExternalGatewayAsL3Endpoint.class};
115         assertPathArgumentTypes(id.getPathArguments(), expectedTypes);
116         assertEquals(ExternalGatewayAsL3Endpoint.class, id.getTargetType());
117         assertFalse(id.isWildcarded());
118         assertEquals(l3Context, id.firstKeyOf(ExternalGatewayAsL3Endpoint.class)
119             .getL3Context());
120         assertEquals(ipAddress, id.firstKeyOf(ExternalGatewayAsL3Endpoint.class)
121             .getIpAddress());
122     }
123
124     @Test
125     public void externalGatewayAsEndpointTest() {
126         String ipAddress = "192.168.50.20/32";
127         IpPrefix ipPrefix = new IpPrefix(ipAddress.toCharArray());
128         ContextId ctxId = new ContextId("00000000-1111-2222-3333-44444555566667777");
129         InstanceIdentifier<ExternalGatewayAsEndpoint> id = NeutronGbpIidFactory.externalGatewayAsEndpoint(
130                 ctxId, ipPrefix, MappingUtils.L3_CONTEXT);
131         assertNotNull(id);
132         Class<?>[] expectedTypes = {Mappings.class, NeutronByGbpMappings.class, ExternalGatewaysAsEndpoints.class,
133                 ExternalGatewayAsEndpoint.class};
134         assertPathArgumentTypes(id.getPathArguments(), expectedTypes);
135         assertEquals(ExternalGatewayAsEndpoint.class, id.getTargetType());
136         assertFalse(id.isWildcarded());
137         assertEquals(ctxId, id.firstKeyOf(ExternalGatewayAsEndpoint.class)
138             .getContextId());
139         assertEquals(MappingUtils.L3_CONTEXT, id.firstKeyOf(ExternalGatewayAsEndpoint.class)
140                 .getContextType());
141         assertEquals(ipAddress, id.firstKeyOf(ExternalGatewayAsEndpoint.class)
142             .getAddress());
143         assertEquals(IpPrefixType.class, id.firstKeyOf(ExternalGatewayAsEndpoint.class)
144                 .getAddressType());
145     }
146
147     private static void assertPathArgumentTypes(Iterable<PathArgument> pathArguments, Class<?>[] expectedTypes) {
148         assertNotNull(pathArguments);
149         Iterator<PathArgument> it = pathArguments.iterator();
150         for (int i = 0; i < expectedTypes.length; ++i) {
151             assertTrue("Next path argument expected.", it.hasNext());
152             assertEquals("Unexpected path argument type.", expectedTypes[i], it.next().getType());
153         }
154     }
155
156 }