5eb229b0b07abb9def7bce4bc95cd7136a2fed0a
[bgpcep.git] / bgp / evpn / src / test / java / org / opendaylight / protocol / bgp / evpn / impl / esi / types / MacParserTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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.protocol.bgp.evpn.impl.esi.types;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.MAC;
13 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.MAC_MODEL;
14 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.VALUE_SIZE;
15 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.createContBuilder;
16 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.createValueBuilder;
17 import static org.opendaylight.protocol.bgp.evpn.impl.esi.types.EsiModelUtil.LD_NID;
18 import static org.opendaylight.protocol.bgp.evpn.impl.esi.types.EsiModelUtil.SYSTEM_MAC_NID;
19
20 import io.netty.buffer.ByteBuf;
21 import io.netty.buffer.Unpooled;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.protocol.util.ByteArray;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.Uint24;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.esi.Esi;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.esi.esi.ArbitraryCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.esi.esi.MacAutoGeneratedCase;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.esi.esi.MacAutoGeneratedCaseBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.esi.esi.mac.auto.generated._case.MacAutoGenerated;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.esi.esi.mac.auto.generated._case.MacAutoGeneratedBuilder;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
34
35 public final class MacParserTest {
36     private static final byte[] VALUE = {(byte) 0xf2, (byte) 0x0c, (byte) 0xdd, (byte) 0x80, (byte) 0x9f, (byte) 0xf7, (byte) 0x02,
37         (byte) 0x02, (byte) 0x00};
38     private static final byte[] RESULT = {(byte) 0x03, (byte) 0xf2, (byte) 0x0c, (byte) 0xdd, (byte) 0x80, (byte) 0x9f, (byte) 0xf7, (byte) 0x02,
39         (byte) 0x02, (byte) 0x00};
40     private static final long UINT24_LD_MODEL = 131584L;
41     private static final Uint24 UINT24_LD = new Uint24(UINT24_LD_MODEL);
42     private MacParser parser;
43
44     @Before
45     public void setUp() {
46         this.parser = new MacParser();
47     }
48
49     @Test
50     public void parserTest() {
51         final ByteBuf buff = Unpooled.buffer(VALUE_SIZE);
52
53         final MacAutoGeneratedCase macAuto = new MacAutoGeneratedCaseBuilder().setMacAutoGenerated(new MacAutoGeneratedBuilder()
54             .setLocalDiscriminator(UINT24_LD).setSystemMacAddress(MAC).build()).build();
55         this.parser.serializeEsi(macAuto, buff);
56         assertArrayEquals(RESULT, ByteArray.getAllBytes(buff));
57
58         final Esi acResult = this.parser.parseEsi(Unpooled.wrappedBuffer(VALUE));
59         assertEquals(macAuto, acResult);
60
61         final ContainerNode cont = createContBuilder(new NodeIdentifier(MacAutoGenerated.QNAME)).addChild(createValueBuilder(MAC_MODEL, SYSTEM_MAC_NID).build())
62             .addChild(createValueBuilder(UINT24_LD_MODEL, LD_NID).build()).build();
63         final Esi acmResult = this.parser.serializeEsi(cont);
64         assertEquals(macAuto, acmResult);
65     }
66
67     @Test(expected = IllegalArgumentException.class)
68     public void wrongCaseTest() {
69         this.parser.serializeEsi(new ArbitraryCaseBuilder().build(), null);
70     }
71 }