Create common parent for extensions families
[bgpcep.git] / bgp / extensions / evpn / src / test / java / org / opendaylight / protocol / bgp / evpn / impl / nlri / EvpnNlriAttributesParserTest.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 java.util.Collections.singletonList;
11 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.RD;
12
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import java.util.ArrayList;
16 import org.junit.Assert;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.opendaylight.protocol.bgp.evpn.impl.esi.types.ESIActivator;
20 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
21 import org.opendaylight.protocol.util.ByteArray;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.evpn.destination.EvpnDestinationBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationEvpnCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.evpn._case.DestinationEvpnBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1Builder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2Builder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlri;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlriBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpUnreachNlri;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpUnreachNlriBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.reach.nlri.AdvertizedRoutes;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.unreach.nlri.WithdrawnRoutes;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
39
40 public class EvpnNlriAttributesParserTest {
41     private EvpnNlriParser parser;
42
43     @Before
44     public void setUp() {
45         ESIActivator.registerEsiTypeParsers(new ArrayList<>());
46         NlriActivator.registerNlriParsers(new ArrayList<>());
47         this.parser = new EvpnNlriParser();
48     }
49
50     @Test
51     public void testAttributes1() throws BGPParsingException {
52         final ByteBuf buffer = Unpooled.buffer();
53         final Attributes att = new AttributesBuilder().addAugmentation(Attributes1.class,
54             new Attributes1Builder().setMpReachNlri(createReach()).build()).build();
55         this.parser.serializeAttribute(att, buffer);
56         Assert.assertArrayEquals(IncMultEthTagRParserTest.RESULT, ByteArray.getAllBytes(buffer));
57     }
58
59     private static MpReachNlri createReach() {
60         final MpReachNlriBuilder mpReachExpected = new MpReachNlriBuilder();
61         final AdvertizedRoutes wd = new AdvertizedRoutesBuilder().setDestinationType(new DestinationEvpnCaseBuilder()
62             .setDestinationEvpn(new DestinationEvpnBuilder().setEvpnDestination(
63                     singletonList(new EvpnDestinationBuilder()
64                             .setRouteDistinguisher(RD)
65                             .setEvpnChoice(IncMultEthTagRParserTest.createIncMultiCase())
66                             .build())).build()).build()).build();
67         return mpReachExpected.setAdvertizedRoutes(wd).build();
68     }
69
70     @Test
71     public void testAttributes2() throws BGPParsingException {
72         final ByteBuf buffer = Unpooled.buffer();
73         final Attributes att = new AttributesBuilder().addAugmentation(Attributes2.class,
74             new Attributes2Builder().setMpUnreachNlri(createUnreach()).build()).build();
75         this.parser.serializeAttribute(att, buffer);
76         Assert.assertArrayEquals(IncMultEthTagRParserTest.RESULT, ByteArray.getAllBytes(buffer));
77     }
78
79     private static MpUnreachNlri createUnreach() {
80         final MpUnreachNlriBuilder mpReachExpected = new MpUnreachNlriBuilder();
81         final WithdrawnRoutes wd = new WithdrawnRoutesBuilder()
82                 .setDestinationType(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn
83                         .rev180329.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type
84                         .DestinationEvpnCaseBuilder().setDestinationEvpn(new org.opendaylight.yang.gen.v1.urn
85                         .opendaylight.params.xml.ns.yang.bgp.evpn.rev180329.update.attributes.mp.unreach.nlri.withdrawn
86                         .routes.destination.type.destination.evpn._case.DestinationEvpnBuilder()
87                         .setEvpnDestination(singletonList(new EvpnDestinationBuilder()
88                         .setRouteDistinguisher(RD).setEvpnChoice(IncMultEthTagRParserTest.createIncMultiCase())
89                                 .build())).build()).build()).build();
90         return mpReachExpected.setWithdrawnRoutes(wd).build();
91     }
92
93 }