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