6190e6a3e1c134b614dd9a8acdabdf3b9ba772b9
[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.endpoints.by.ports.EndpointByPort;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.mappings.neutron.by.gbp.mappings.ExternalGatewaysAsEndpoints;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.mappings.neutron.by.gbp.mappings.ExternalGatewaysAsL3Endpoints;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.mappings.neutron.by.gbp.mappings.PortsByBaseEndpoints;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.mappings.neutron.by.gbp.mappings.PortsByEndpoints;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.mappings.neutron.by.gbp.mappings.external.gateways.as.endpoints.ExternalGatewayAsEndpoint;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.mappings.neutron.by.gbp.mappings.external.gateways.as.l3.endpoints.ExternalGatewayAsL3Endpoint;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.mappings.neutron.by.gbp.mappings.ports.by.base.endpoints.PortByBaseEndpoint;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.mappings.neutron.by.gbp.mappings.ports.by.base.endpoints.PortByBaseEndpointKey;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.mappings.neutron.by.gbp.mappings.ports.by.endpoints.PortByEndpoint;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
47
48 public class NeutronGbpIidFactoryTest {
49
50     @Test
51     public void endpointByPortIidTest() {
52         UniqueId portId = mock(UniqueId.class);
53         InstanceIdentifier<EndpointByPort> id = NeutronGbpIidFactory.endpointByPortIid(portId);
54         assertNotNull(id);
55         Class<?>[] expectedTypes =
56             {Mappings.class, GbpByNeutronMappings.class, EndpointsByPorts.class, EndpointByPort.class};
57         assertPathArgumentTypes(id.getPathArguments(), expectedTypes);
58         assertEquals(EndpointByPort.class, id.getTargetType());
59         assertFalse(id.isWildcarded());
60         assertEquals(portId, id.firstKeyOf(EndpointByPort.class).getPortId());
61     }
62
63     @Test
64     public void portByEndpointIidTest() {
65         L2BridgeDomainId l2BdId = mock(L2BridgeDomainId.class);
66         MacAddress mac = mock(MacAddress.class);
67         InstanceIdentifier<PortByEndpoint> id = NeutronGbpIidFactory.portByEndpointIid(l2BdId, mac);
68         assertNotNull(id);
69         Class<?>[] expectedTypes =
70             {Mappings.class, NeutronByGbpMappings.class, PortsByEndpoints.class, PortByEndpoint.class};
71         assertPathArgumentTypes(id.getPathArguments(), expectedTypes);
72         assertEquals(PortByEndpoint.class, id.getTargetType());
73         assertFalse(id.isWildcarded());
74         assertEquals(l2BdId, id.firstKeyOf(PortByEndpoint.class).getL2Context());
75         assertEquals(mac, id.firstKeyOf(PortByEndpoint.class).getMacAddress());
76     }
77
78     @Test
79     public void portByBaseEndpointIidTest() {
80         String address = "00:00:00:00:00:00:01";
81         ContextId ctxId = new ContextId("00000000-1111-2222-3333-44444555566667777");
82         PortByBaseEndpointKey key =
83             new PortByBaseEndpointKey(address, MacAddressType.class, ctxId, MappingUtils.L2_BRDIGE_DOMAIN);
84         InstanceIdentifier<PortByBaseEndpoint> id = NeutronGbpIidFactory.portByBaseEndpointIid(key);
85         assertNotNull(id);
86         Class<?>[] expectedTypes =
87             {Mappings.class, NeutronByGbpMappings.class, PortsByBaseEndpoints.class, 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 =
106             {Mappings.class, GbpByNeutronMappings.class, BaseEndpointsByPorts.class, 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 =
121             {Mappings.class, NeutronByGbpMappings.class, ExternalGatewaysAsL3Endpoints.class,
122                 ExternalGatewayAsL3Endpoint.class};
123         assertPathArgumentTypes(id.getPathArguments(), expectedTypes);
124         assertEquals(ExternalGatewayAsL3Endpoint.class, id.getTargetType());
125         assertFalse(id.isWildcarded());
126         assertEquals(l3Context, id.firstKeyOf(ExternalGatewayAsL3Endpoint.class)
127             .getL3Context());
128         assertEquals(ipAddress, id.firstKeyOf(ExternalGatewayAsL3Endpoint.class)
129             .getIpAddress());
130     }
131
132     @Test
133     public void externalGatewayAsEndpointTest() {
134         String ipAddress = "192.168.50.20/32";
135         IpPrefix ipPrefix = new IpPrefix(ipAddress.toCharArray());
136         ContextId ctxId = new ContextId("00000000-1111-2222-3333-44444555566667777");
137         InstanceIdentifier<ExternalGatewayAsEndpoint> id =
138             NeutronGbpIidFactory.externalGatewayAsEndpoint(ctxId, ipPrefix, MappingUtils.L3_CONTEXT);
139         assertNotNull(id);
140         Class<?>[] expectedTypes =
141             {Mappings.class, NeutronByGbpMappings.class, ExternalGatewaysAsEndpoints.class,
142                 ExternalGatewayAsEndpoint.class};
143         assertPathArgumentTypes(id.getPathArguments(), expectedTypes);
144         assertEquals(ExternalGatewayAsEndpoint.class, id.getTargetType());
145         assertFalse(id.isWildcarded());
146         assertEquals(ctxId, id.firstKeyOf(ExternalGatewayAsEndpoint.class).getContextId());
147         assertEquals(MappingUtils.L3_CONTEXT, id.firstKeyOf(ExternalGatewayAsEndpoint.class).getContextType());
148         assertEquals(ipAddress, id.firstKeyOf(ExternalGatewayAsEndpoint.class).getAddress());
149         assertEquals(IpPrefixType.class, id.firstKeyOf(ExternalGatewayAsEndpoint.class).getAddressType());
150     }
151
152     private static void assertPathArgumentTypes(Iterable<PathArgument> pathArguments, Class<?>[] expectedTypes) {
153         assertNotNull(pathArguments);
154         Iterator<PathArgument> it = pathArguments.iterator();
155         for (int i = 0; i < expectedTypes.length; ++i) {
156             assertTrue("Next path argument expected.", it.hasNext());
157             assertEquals("Unexpected path argument type.", expectedTypes[i], it.next().getType());
158         }
159     }
160
161 }