Code clean up
[bgpcep.git] / bgp / extensions / evpn / src / test / java / org / opendaylight / protocol / bgp / evpn / impl / nlri / IncMultEthTagRParserTest.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.nlri;
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.IP;
13 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.IP_MODEL;
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.nlri.EthADRParserTest.ETI;
17 import static org.opendaylight.protocol.bgp.evpn.impl.nlri.EthADRParserTest.ROUDE_DISTIN;
18 import static org.opendaylight.protocol.bgp.evpn.impl.nlri.EthADRParserTest.WRONG_VALUE;
19 import static org.opendaylight.protocol.bgp.evpn.impl.nlri.MACIpAdvRParserTest.createEti;
20 import static org.opendaylight.protocol.bgp.evpn.impl.nlri.NlriModelUtil.ORI_NID;
21
22 import io.netty.buffer.Unpooled;
23 import java.util.ArrayList;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.opendaylight.protocol.bgp.evpn.impl.esi.types.ESIActivator;
27 import org.opendaylight.protocol.util.ByteArray;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.EvpnChoice;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.evpn.choice.EsRouteCaseBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.evpn.choice.IncMultiEthernetTagResCase;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.evpn.choice.IncMultiEthernetTagResCaseBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.inc.multi.ethernet.tag.res.IncMultiEthernetTagResBuilder;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
36 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
37 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
38
39 public class IncMultEthTagRParserTest {
40     static final byte[] RESULT = {
41         (byte) 0x03, (byte) 0x11,
42         (byte) 0x00, (byte) 0x01, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04, (byte) 0x01, (byte) 0x02,
43         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0a,
44         (byte) 0x20, (byte) 0x7f, (byte) 0x00, (byte) 0x00, (byte) 0x01
45     };
46     private static final byte[] VALUE = {
47         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0a,
48         (byte) 0x20, (byte) 0x7f, (byte) 0x00, (byte) 0x00, (byte) 0x01
49     };
50     private IncMultEthTagRParser parser;
51
52     @Before
53     public void setUp() {
54         this.parser = new IncMultEthTagRParser();
55         ESIActivator.registerEsiTypeParsers(new ArrayList<>());
56     }
57
58     @Test
59     public void parserTest() {
60
61         final IncMultiEthernetTagResCase expected = IncMultEthTagRParserTest.createIncMultiCase();
62         assertArrayEquals(RESULT, ByteArray.getAllBytes(this.parser.serializeEvpn(expected,
63                 Unpooled.wrappedBuffer(ROUDE_DISTIN))));
64
65         final EvpnChoice result = this.parser.parseEvpn(Unpooled.wrappedBuffer(VALUE));
66         assertEquals(expected, result);
67
68         final DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> choice = Builders.choiceBuilder();
69         choice.withNodeIdentifier(IncMultEthTagRParser.INC_MULT_ROUTE_NID);
70         final ContainerNode incMult = createContBuilder(IncMultEthTagRParser.INC_MULT_ROUTE_NID).addChild(createEti())
71             .addChild(createValueBuilder(IP_MODEL, ORI_NID).build()).build();
72         final EvpnChoice modelResult = this.parser.serializeEvpnModel(incMult);
73         assertEquals(expected, modelResult);
74
75         final EvpnChoice keyResult = this.parser.createRouteKey(incMult);
76         assertEquals(expected, keyResult);
77     }
78
79     static IncMultiEthernetTagResCase createIncMultiCase() {
80         return new IncMultiEthernetTagResCaseBuilder().setIncMultiEthernetTagRes(
81                 new IncMultiEthernetTagResBuilder().setEthernetTagId(ETI).setOrigRouteIp(IP).build()).build();
82     }
83
84     @Test(expected = IllegalArgumentException.class)
85     public void wrongCaseTest() {
86         this.parser.serializeEvpn(new EsRouteCaseBuilder().build(), null);
87     }
88
89     @Test(expected = IllegalArgumentException.class)
90     public void wrongSizeTest() {
91         this.parser.parseEvpn(Unpooled.wrappedBuffer(WRONG_VALUE));
92     }
93 }