2 * Copyright (c) 2016 Cisco Systems, Inc. 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.protocol.bgp.evpn.impl.esi.types;
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.LD;
13 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.VALUE_SIZE;
14 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.createContBuilder;
15 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.createValueBuilder;
16 import static org.opendaylight.protocol.bgp.evpn.impl.esi.types.EsiModelUtil.LD_NID;
17 import static org.opendaylight.protocol.bgp.evpn.impl.esi.types.EsiModelUtil.RD_NID;
19 import io.netty.buffer.ByteBuf;
20 import io.netty.buffer.Unpooled;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.opendaylight.protocol.util.ByteArray;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.esi.Esi;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.esi.esi.ArbitraryCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.esi.esi.RouterIdGeneratedCase;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.esi.esi.RouterIdGeneratedCaseBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.esi.esi.router.id.generated._case.RouterIdGenerated;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.esi.esi.router.id.generated._case.RouterIdGeneratedBuilder;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
34 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
35 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
37 public class RouterIdParserTest {
38 public static final byte[] RESULT = {(byte) 0x04, (byte) 0x2A, (byte) 0x2A, (byte) 0x2A, (byte) 0x2A, (byte) 0x02,
39 (byte) 0x02, (byte) 0x02, (byte) 0x02, (byte) 0x00};
40 private static final byte[] VALUE = {(byte) 0x2A, (byte) 0x2A, (byte) 0x2A, (byte) 0x2A, (byte) 0x02, (byte) 0x02,
41 (byte) 0x02, (byte) 0x02, (byte) 0x00};
42 private static final Ipv4AddressNoZone ROUTE_ID = new Ipv4AddressNoZone("42.42.42.42");
43 public static final RouterIdGeneratedCase ROUTE_ID_CASE = new RouterIdGeneratedCaseBuilder()
44 .setRouterIdGenerated(new RouterIdGeneratedBuilder()
45 .setLocalDiscriminator(LD).setRouterId(ROUTE_ID).build()).build();
46 private static final String ROUTE_ID_MODEL = "42.42.42.42";
47 private RouterIdParser parser;
51 this.parser = new RouterIdParser();
55 public void parserTest() {
56 final ByteBuf buff = Unpooled.buffer(VALUE_SIZE);
58 this.parser.serializeEsi(ROUTE_ID_CASE, buff);
59 assertArrayEquals(RESULT, ByteArray.getAllBytes(buff));
61 final Esi acResult = this.parser.parseEsi(Unpooled.wrappedBuffer(VALUE));
62 assertEquals(ROUTE_ID_CASE, acResult);
64 final Esi acmResult = this.parser.serializeEsi(RouterIdParserTest.createRouteContainer());
65 assertEquals(ROUTE_ID_CASE, acmResult);
68 public static ChoiceNode createRouterIdCase() {
69 final DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> nextHop = Builders.choiceBuilder();
70 nextHop.withNodeIdentifier(new NodeIdentifier(RouterIdGeneratedCase.QNAME));
71 return nextHop.addChild(createRouteContainer()).build();
74 private static ContainerNode createRouteContainer() {
75 return createContBuilder(new NodeIdentifier(RouterIdGenerated.QNAME))
76 .addChild(createValueBuilder(ROUTE_ID_MODEL, RD_NID).build())
77 .addChild(createValueBuilder(LD, LD_NID).build())
81 @Test(expected = IllegalArgumentException.class)
82 public void wrongCaseTest() {
83 this.parser.serializeEsi(new ArbitraryCaseBuilder().build(), null);