2 * Copyright (c) 2015 Intel and others. All rights reserved.
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
8 package org.opendaylight.groupbasedpolicy.neutron.gbp.util;
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;
16 import java.util.Iterator;
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;
48 public class NeutronGbpIidFactoryTest {
51 public void endpointByPortIidTest() {
52 UniqueId portId = mock(UniqueId.class);
53 InstanceIdentifier<EndpointByPort> id = NeutronGbpIidFactory.endpointByPortIid(portId);
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());
64 public void portByEndpointIidTest() {
65 L2BridgeDomainId l2BdId = mock(L2BridgeDomainId.class);
66 MacAddress mac = mock(MacAddress.class);
67 InstanceIdentifier<PortByEndpoint> id = NeutronGbpIidFactory.portByEndpointIid(l2BdId, mac);
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());
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);
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)
94 assertEquals(address, id.firstKeyOf(PortByBaseEndpoint.class).getAddress());
95 assertEquals(MacAddressType.class, id.firstKeyOf(PortByBaseEndpoint.class)
100 public void baseEndpointByPortTest() {
101 UniqueId portId = new UniqueId("00000000-1111-2222-3333-44444555566667777");
103 InstanceIdentifier<BaseEndpointByPort> id = NeutronGbpIidFactory.baseEndpointByPortIid(portId);
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());
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);
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)
128 assertEquals(ipAddress, id.firstKeyOf(ExternalGatewayAsL3Endpoint.class)
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);
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());
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());