Bump MRI upstreams
[bgpcep.git] / bgp / extensions / evpn / src / test / java / org / opendaylight / protocol / bgp / evpn / impl / nlri / EvpnNlriParserTest.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.RD;
13 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.RD_MODEL;
14 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.createValueBuilder;
15 import static org.opendaylight.protocol.bgp.evpn.impl.nlri.NlriModelUtil.RD_NID;
16 import static org.opendaylight.protocol.bgp.evpn.impl.nlri.SimpleEvpnNlriRegistryTest.EVPN_NID;
17
18 import io.netty.buffer.ByteBuf;
19 import io.netty.buffer.Unpooled;
20 import java.util.List;
21 import org.junit.Test;
22 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
23 import org.opendaylight.protocol.util.ByteArray;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.EvpnSubsequentAddressFamily;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.L2vpnAddressFamily;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.evpn.EvpnChoice;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.evpn.destination.EvpnDestination;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.evpn.destination.EvpnDestinationBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationEvpnCaseBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.evpn._case.DestinationEvpnBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.attributes.reach.MpReachNlriBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.attributes.reach.mp.reach.nlri.AdvertizedRoutes;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.attributes.reach.mp.reach.nlri.AdvertizedRoutesBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.attributes.unreach.MpUnreachNlri;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.attributes.unreach.MpUnreachNlriBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.attributes.unreach.mp.unreach.nlri.WithdrawnRoutes;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.attributes.unreach.mp.unreach.nlri.WithdrawnRoutesBuilder;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
39 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.builder.DataContainerNodeBuilder;
42 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
43 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableUnkeyedListEntryNodeBuilder;
44
45 public class EvpnNlriParserTest {
46     private static final NodeIdentifier EVPN_CHOICE_NID = new NodeIdentifier(EvpnChoice.QNAME);
47
48     private final List<EvpnDestination> dest = List.of(new EvpnDestinationBuilder()
49         .setRouteDistinguisher(RD)
50         .setEvpnChoice(IncMultEthTagRParserTest.createIncMultiCase()).build());
51     private final EvpnNlriParser parser = new EvpnNlriParser();
52
53     private static ChoiceNode createMACIpAdvChoice() {
54         return Builders.choiceBuilder()
55             .withNodeIdentifier(EVPN_CHOICE_NID)
56             .addChild(MACIpAdvRParserTest.createMacIpCont())
57             .build();
58     }
59
60     @Test
61     public void testSerializeNlri() {
62         final ByteBuf buffer = Unpooled.buffer();
63         EvpnNlriParser.serializeNlri(this.dest, buffer);
64         assertArrayEquals(IncMultEthTagRParserTest.RESULT, ByteArray.getAllBytes(buffer));
65
66     }
67
68     @Test
69     public void testMpUnreach() throws BGPParsingException {
70         final MpUnreachNlriBuilder mpReach = new MpUnreachNlriBuilder();
71         mpReach.setAfi(L2vpnAddressFamily.class);
72         mpReach.setSafi(EvpnSubsequentAddressFamily.class);
73         this.parser.parseNlri(Unpooled.wrappedBuffer(IncMultEthTagRParserTest.RESULT), mpReach, null);
74         assertEquals(createUnreach(), mpReach.build());
75     }
76
77     private MpUnreachNlri createUnreach() {
78         final MpUnreachNlriBuilder mpReachExpected = new MpUnreachNlriBuilder();
79         mpReachExpected.setAfi(L2vpnAddressFamily.class);
80         mpReachExpected.setSafi(EvpnSubsequentAddressFamily.class);
81         final WithdrawnRoutes wd = new WithdrawnRoutesBuilder().setDestinationType(
82             new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev200120.update
83                 .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationEvpnCaseBuilder()
84                 .setDestinationEvpn(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang
85                     .bgp.evpn.rev200120.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type
86                     .destination.evpn._case.DestinationEvpnBuilder()
87                     .setEvpnDestination(this.dest).build())
88                 .build())
89             .build();
90         return mpReachExpected.setWithdrawnRoutes(wd).build();
91     }
92
93     @Test
94     public void testMpReach() throws BGPParsingException {
95         final MpReachNlriBuilder mpReach = new MpReachNlriBuilder();
96         mpReach.setAfi(L2vpnAddressFamily.class);
97         mpReach.setSafi(EvpnSubsequentAddressFamily.class);
98         this.parser.parseNlri(Unpooled.wrappedBuffer(IncMultEthTagRParserTest.RESULT), mpReach, null);
99
100         final MpReachNlriBuilder mpReachExpected = new MpReachNlriBuilder();
101         mpReachExpected.setAfi(L2vpnAddressFamily.class);
102         mpReachExpected.setSafi(EvpnSubsequentAddressFamily.class);
103         final AdvertizedRoutes wd = new AdvertizedRoutesBuilder().setDestinationType(new DestinationEvpnCaseBuilder()
104                 .setDestinationEvpn(new DestinationEvpnBuilder().setEvpnDestination(this.dest).build())
105                 .build()).build();
106         mpReachExpected.setAdvertizedRoutes(wd);
107         assertEquals(mpReachExpected.build(), mpReach.build());
108     }
109
110     @Test
111     public void testNullMpReachNlri() throws BGPParsingException {
112         final MpReachNlriBuilder mpb = new MpReachNlriBuilder();
113         this.parser.parseNlri(Unpooled.buffer(), mpb, null);
114         assertEquals(new MpReachNlriBuilder().build(), mpb.build());
115     }
116
117     @Test
118     public void testNullMpUnReachNlri() throws BGPParsingException {
119         final MpUnreachNlriBuilder mpb = new MpUnreachNlriBuilder();
120         this.parser.parseNlri(Unpooled.buffer(), mpb, null);
121         assertEquals(new MpUnreachNlriBuilder().build(), mpb.build());
122     }
123
124     @Test
125     public void testExtractEvpnDestination() {
126         final DataContainerNodeBuilder<NodeIdentifier, UnkeyedListEntryNode> evpnBI =
127                 ImmutableUnkeyedListEntryNodeBuilder.create();
128         evpnBI.withNodeIdentifier(EVPN_NID);
129         evpnBI.withChild(createMACIpAdvChoice());
130         evpnBI.withChild(createValueBuilder(RD_MODEL, RD_NID).build());
131         final EvpnDestination destResult = EvpnNlriParser.extractEvpnDestination(evpnBI.build());
132         final EvpnDestination expected = new EvpnDestinationBuilder()
133                 .setRouteDistinguisher(RD)
134                 .setEvpnChoice(MACIpAdvRParserTest.createdExpectedResult()).build();
135         assertEquals(expected, destResult);
136     }
137
138     @Test
139     public void testExtractRouteKey() {
140         final DataContainerNodeBuilder<NodeIdentifier, UnkeyedListEntryNode> evpnBI =
141                 ImmutableUnkeyedListEntryNodeBuilder.create();
142         evpnBI.withNodeIdentifier(EVPN_CHOICE_NID);
143         evpnBI.withChild(createValueBuilder(RD_MODEL, RD_NID).build());
144         evpnBI.withChild(createMACIpAdvChoice());
145         final EvpnDestination destResult = EvpnNlriParser.extractRouteKeyDestination(evpnBI.build());
146         final EvpnDestination expected = new EvpnDestinationBuilder().setRouteDistinguisher(RD)
147                 .setEvpnChoice(MACIpAdvRParserTest.createdExpectedRouteKey()).build();
148         assertEquals(expected, destResult);
149     }
150 }