cf7517ad0791defc10989f1c08953a698c4e3eb4
[bgpcep.git] / bgp / evpn / src / test / java / org / opendaylight / protocol / bgp / evpn / impl / esi / types / LanParserTest.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.BRIDGE_MAC_NID;
18 import static org.opendaylight.protocol.bgp.evpn.impl.esi.types.EsiModelUtil.RBP_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.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.LanAutoGeneratedCase;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.esi.esi.LanAutoGeneratedCaseBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.esi.esi.lan.auto.generated._case.LanAutoGenerated;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.esi.esi.lan.auto.generated._case.LanAutoGeneratedBuilder;
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;
36
37 public class LanParserTest {
38     private static final Integer PRIORITY = 514;
39     public static final LanAutoGeneratedCase LAN_AUT_GEN_CASE = new LanAutoGeneratedCaseBuilder()
40             .setLanAutoGenerated(new LanAutoGeneratedBuilder()
41         .setRootBridgeMacAddress(MAC).setRootBridgePriority(PRIORITY).build()).build();
42     private static final byte[] VALUE = {(byte) 0xf2, (byte) 0x0c, (byte) 0xdd, (byte) 0x80, (byte) 0x9f, (byte) 0xf7,
43         (byte) 0x02, (byte) 0x02, (byte) 0x00};
44     private static final byte[] RESULT = {(byte) 0x02, (byte) 0xf2, (byte) 0x0c, (byte) 0xdd, (byte) 0x80, (byte) 0x9f,
45         (byte) 0xf7, (byte) 0x02, (byte) 0x02, (byte) 0x00};
46     private LanParser parser;
47
48     @Before
49     public void setUp() {
50         this.parser = new LanParser();
51     }
52
53     @Test
54     public void parserTest() {
55         final ByteBuf buff = Unpooled.buffer(VALUE_SIZE);
56         this.parser.serializeEsi(LAN_AUT_GEN_CASE, buff);
57         assertArrayEquals(RESULT, ByteArray.getAllBytes(buff));
58
59         final Esi acResult = this.parser.parseEsi(Unpooled.wrappedBuffer(VALUE));
60         assertEquals(LAN_AUT_GEN_CASE, acResult);
61
62         final Esi acmResult = this.parser.serializeEsi(createLanCont());
63         assertEquals(LAN_AUT_GEN_CASE, acmResult);
64     }
65
66     @Test(expected = IllegalArgumentException.class)
67     public void wrongCaseTest() {
68         this.parser.serializeEsi(new ArbitraryCaseBuilder().build(), null);
69     }
70
71     public static ChoiceNode createLanChoice() {
72         final DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> choice = Builders.choiceBuilder();
73         choice.withNodeIdentifier(NodeIdentifier.create(Esi.QNAME));
74         return choice.addChild(createLanCont()).build();
75     }
76
77     private static ContainerNode createLanCont() {
78         return createContBuilder(new NodeIdentifier(LanAutoGenerated.QNAME))
79                 .addChild(createValueBuilder(MAC_MODEL, BRIDGE_MAC_NID).build())
80                 .addChild(createValueBuilder(PRIORITY, RBP_NID).build()).build();
81     }
82 }