cdcd6e049dff3872c95c9590e6e7004994b3dc09
[bgpcep.git] / bgp / evpn / src / test / java / org / opendaylight / protocol / bgp / evpn / impl / nlri / MACIpAdvRParserTest.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.IPV6;
14 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.IPV6_MODEL;
15 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.IP_MODEL;
16 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.MAC;
17 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.MAC_MODEL;
18 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.MPLS_LABEL;
19 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.MPLS_LABEL2;
20 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.MPLS_LABEL2_MODEL;
21 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.MPLS_LABEL_MODEL;
22 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.VLAN;
23 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.createContBuilder;
24 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.createValueBuilder;
25 import static org.opendaylight.protocol.bgp.evpn.impl.esi.types.LanParserTest.LAN_AUT_GEN_CASE;
26 import static org.opendaylight.protocol.bgp.evpn.impl.nlri.EthADRParserTest.ETI;
27 import static org.opendaylight.protocol.bgp.evpn.impl.nlri.EthADRParserTest.ROUDE_DISTIN;
28 import static org.opendaylight.protocol.bgp.evpn.impl.nlri.NlriModelUtil.ETI_NID;
29 import static org.opendaylight.protocol.bgp.evpn.impl.nlri.NlriModelUtil.IP_NID;
30 import static org.opendaylight.protocol.bgp.evpn.impl.nlri.NlriModelUtil.MAC_NID;
31 import static org.opendaylight.protocol.bgp.evpn.impl.nlri.NlriModelUtil.MPLS1_NID;
32 import static org.opendaylight.protocol.bgp.evpn.impl.nlri.NlriModelUtil.MPLS2_NID;
33 import static org.opendaylight.protocol.bgp.evpn.impl.nlri.NlriModelUtil.VLAN_NID;
34
35 import io.netty.buffer.Unpooled;
36 import java.util.ArrayList;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.opendaylight.protocol.bgp.evpn.impl.esi.types.ESIActivator;
40 import org.opendaylight.protocol.bgp.evpn.impl.esi.types.LanParserTest;
41 import org.opendaylight.protocol.util.ByteArray;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.EvpnChoice;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.evpn.choice.EsRouteCaseBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.evpn.choice.MacIpAdvRouteCase;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.evpn.choice.MacIpAdvRouteCaseBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.mac.ip.adv.route.MacIpAdvRouteBuilder;
47 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
48 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
49 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
50 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
51 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
52
53 public class MACIpAdvRParserTest {
54     private static final byte[] VALUE = {
55         (byte) 0x02, (byte) 0xf2, (byte) 0x0c, (byte) 0xdd, (byte) 0x80, (byte) 0x9f, (byte) 0xf7, (byte) 0x02,
56         (byte) 0x02, (byte) 0x00,
57         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0a,
58         (byte) 0x30, (byte) 0xf2, (byte) 0x0c, (byte) 0xdd, (byte) 0x80, (byte) 0x9f, (byte) 0xf7,
59         (byte) 0x20, (byte) 0x7f, (byte) 0x00, (byte) 0x00, (byte) 0x01,
60         (byte) 0x05, (byte) 0xdc, (byte) 0x10,
61         (byte) 0x05, (byte) 0xdc, (byte) 0x20
62     };
63     private static final byte[] RESULT = {
64         (byte) 0x02, (byte) 0x28,
65         (byte) 0x00, (byte) 0x01, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04, (byte) 0x01, (byte) 0x02,
66         (byte) 0x02, (byte) 0xf2, (byte) 0x0c, (byte) 0xdd, (byte) 0x80, (byte) 0x9f, (byte) 0xf7, (byte) 0x02,
67         (byte) 0x02, (byte) 0x00,
68         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0a,
69         (byte) 0x30, (byte) 0xf2, (byte) 0x0c, (byte) 0xdd, (byte) 0x80, (byte) 0x9f, (byte) 0xf7,
70         (byte) 0x20, (byte) 0x7f, (byte) 0x00, (byte) 0x00, (byte) 0x01,
71         (byte) 0x05, (byte) 0xdc, (byte) 0x10,
72         (byte) 0x05, (byte) 0xdc, (byte) 0x20
73     };
74     private static final byte[] VALUE2 = {
75         (byte) 0x02, (byte) 0xf2, (byte) 0x0c, (byte) 0xdd, (byte) 0x80, (byte) 0x9f, (byte) 0xf7, (byte) 0x02,
76         (byte) 0x02, (byte) 0x00,
77         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0a,
78         (byte) 0x30, (byte) 0xf2, (byte) 0x0c, (byte) 0xdd, (byte) 0x80, (byte) 0x9f, (byte) 0xf7,
79         (byte) 0x80, 0x20, (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
80         0x01, (byte) 0x05, (byte) 0xdc, (byte) 0x10,
81     };
82     private static final byte[] RESULT2 = {
83         (byte) 0x02, (byte) 0x31,
84         (byte) 0x00, (byte) 0x01, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04, (byte) 0x01, (byte) 0x02,  //RD
85         (byte) 0x02, (byte) 0xf2, (byte) 0x0c, (byte) 0xdd, (byte) 0x80, (byte) 0x9f, (byte) 0xf7, (byte) 0x02,
86         (byte) 0x02, (byte) 0x00, //ESI
87         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0a, // ETI
88         (byte) 0x30, (byte) 0xf2, (byte) 0x0c, (byte) 0xdd, (byte) 0x80, (byte) 0x9f, (byte) 0xf7, // MAC
89         (byte) 0x80,//IPV6
90         (byte) 0x20, (byte) 0x01, (byte) 0x00, (byte) 0x00,
91         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
92         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
93         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01,
94         (byte) 0x05, (byte) 0xdc, (byte) 0x10, //MPLS
95     };
96
97     private MACIpAdvRParser parser;
98
99     @Before
100     public void setUp() {
101         this.parser = new MACIpAdvRParser();
102         ESIActivator.registerEsiTypeParsers(new ArrayList<>());
103     }
104
105     @Test
106     public void parserCase1Test() {
107         final MacIpAdvRouteCase expected = createdExpectedResult();
108         assertArrayEquals(RESULT, ByteArray.getAllBytes(this.parser.serializeEvpn(expected,
109                 Unpooled.wrappedBuffer(ROUDE_DISTIN))));
110
111         final EvpnChoice result = this.parser.parseEvpn(Unpooled.wrappedBuffer(VALUE));
112         assertEquals(expected, result);
113         final EvpnChoice modelResult = this.parser.serializeEvpnModel(createMacIpCont());
114         assertEquals(expected, modelResult);
115
116         final EvpnChoice keyResult = this.parser.createRouteKey(createMacIpCont());
117         assertEquals(createdExpectedRouteKey(), keyResult);
118     }
119
120     static MacIpAdvRouteCase createdExpectedResult() {
121         return new MacIpAdvRouteCaseBuilder().setMacIpAdvRoute(new MacIpAdvRouteBuilder().setEsi(LAN_AUT_GEN_CASE)
122                 .setEthernetTagId(ETI).setMacAddress(MAC).setIpAddress(IP).setMplsLabel1(MPLS_LABEL)
123                 .setMplsLabel2(MPLS_LABEL2).build()).build();
124     }
125
126     static MacIpAdvRouteCase createdExpectedRouteKey() {
127         return new MacIpAdvRouteCaseBuilder().setMacIpAdvRoute(new MacIpAdvRouteBuilder().setEthernetTagId(ETI)
128                 .setMacAddress(MAC).setIpAddress(IP).build()).build();
129     }
130
131     static ContainerNode createMacIpCont() {
132         return createContBuilder(MACIpAdvRParser.MAC_IP_ADV_ROUTE_NID)
133             .addChild(LanParserTest.createLanChoice())
134             .addChild(createEti())
135             .addChild(createValueBuilder(MAC_MODEL, MAC_NID).build())
136             .addChild(createValueBuilder(IP_MODEL, IP_NID).build())
137             .addChild(createValueBuilder(MPLS_LABEL_MODEL, MPLS1_NID).build())
138             .addChild(createValueBuilder(MPLS_LABEL2_MODEL, MPLS2_NID).build())
139             .build();
140     }
141
142     public static ContainerNode createEti() {
143         return createContBuilder(ETI_NID).withChild(createValueBuilder(VLAN, VLAN_NID).build()).build();
144     }
145
146     @Test
147     public void parserCase2Test() {
148
149         final MacIpAdvRouteCase expected = new MacIpAdvRouteCaseBuilder().setMacIpAdvRoute(new MacIpAdvRouteBuilder()
150                 .setEsi(LAN_AUT_GEN_CASE).setEthernetTagId(ETI).setMacAddress(MAC).setIpAddress(IPV6)
151                 .setMplsLabel1(MPLS_LABEL).build()).build();
152         assertArrayEquals(RESULT2, ByteArray.getAllBytes(this.parser.serializeEvpn(expected,
153                 Unpooled.wrappedBuffer(ROUDE_DISTIN))));
154
155         final EvpnChoice result = this.parser.parseEvpn(Unpooled.wrappedBuffer(VALUE2));
156         assertEquals(expected, result);
157
158         final DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> choice = Builders.choiceBuilder();
159         choice.withNodeIdentifier(MACIpAdvRParser.MAC_IP_ADV_ROUTE_NID);
160         final ContainerNode macIp = createContBuilder(MACIpAdvRParser.MAC_IP_ADV_ROUTE_NID)
161                 .addChild(LanParserTest.createLanChoice()).addChild(createEti())
162             .addChild(createValueBuilder(MAC_MODEL, MAC_NID).build())
163                 .addChild(createValueBuilder(IPV6_MODEL, IP_NID).build())
164             .addChild(createValueBuilder(MPLS_LABEL_MODEL, MPLS1_NID).build()).build();
165         final EvpnChoice modelResult = this.parser.serializeEvpnModel(macIp);
166
167         assertEquals(expected, modelResult);
168     }
169
170     @Test(expected = IllegalArgumentException.class)
171     public void wrongCaseTest() {
172         this.parser.serializeEvpn(new EsRouteCaseBuilder().build(), null);
173     }
174 }