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