Enforce findbug & checkstyle under evon module
[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     static ChoiceNode createMACIpAdvChoice() {
54         final DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> choice = Builders.choiceBuilder();
55         choice.withNodeIdentifier(EVPN_CHOICE_NID);
56         return choice.addChild(MACIpAdvRParserTest.createMacIpCont()).build();
57     }
58
59     @Before
60     public void setUp() {
61         ESIActivator.registerEsiTypeParsers(new ArrayList<>());
62         NlriActivator.registerNlriParsers(new ArrayList<>());
63         this.dest = Collections.singletonList(new EvpnDestinationBuilder()
64                 .setRouteDistinguisher(RD)
65                 .setEvpnChoice(IncMultEthTagRParserTest.createIncMultiCase()).build());
66         this.parser = new EvpnNlriParser();
67     }
68
69     @Test
70     public void testSerializeNlri() throws BGPParsingException {
71         final ByteBuf buffer = Unpooled.buffer();
72         EvpnNlriParser.serializeNlri(this.dest, buffer);
73         assertArrayEquals(IncMultEthTagRParserTest.RESULT, ByteArray.getAllBytes(buffer));
74
75     }
76
77     @Test
78     public void testMpUnreach() throws BGPParsingException {
79         final MpUnreachNlriBuilder mpReach = new MpUnreachNlriBuilder();
80         this.parser.parseNlri(Unpooled.wrappedBuffer(IncMultEthTagRParserTest.RESULT), mpReach);
81         assertEquals(createUnreach(), mpReach.build());
82     }
83
84     private MpUnreachNlri createUnreach() {
85         final MpUnreachNlriBuilder mpReachExpected = new MpUnreachNlriBuilder();
86         final WithdrawnRoutes wd = new WithdrawnRoutesBuilder().setDestinationType(
87                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.update
88                         .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationEvpnCaseBuilder()
89                         .setDestinationEvpn(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp
90                                 .evpn.rev171213.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type
91                                 .destination.evpn._case.DestinationEvpnBuilder()
92                                 .setEvpnDestination(this.dest).build()).build()).build();
93         return mpReachExpected.setWithdrawnRoutes(wd).build();
94     }
95
96     @Test
97     public void testMpReach() throws BGPParsingException {
98         final MpReachNlriBuilder mpReach = new MpReachNlriBuilder();
99         this.parser.parseNlri(Unpooled.wrappedBuffer(IncMultEthTagRParserTest.RESULT), mpReach);
100
101         final MpReachNlriBuilder mpReachExpected = new MpReachNlriBuilder();
102         final AdvertizedRoutes wd = new AdvertizedRoutesBuilder().setDestinationType(new DestinationEvpnCaseBuilder()
103                 .setDestinationEvpn(new DestinationEvpnBuilder().setEvpnDestination(this.dest).build())
104                 .build()).build();
105         mpReachExpected.setAdvertizedRoutes(wd);
106         assertEquals(mpReachExpected.build(), mpReach.build());
107     }
108
109     @Test
110     public void testNullMpReachNlri() throws BGPParsingException {
111         final MpReachNlriBuilder mpb = new MpReachNlriBuilder();
112         this.parser.parseNlri(Unpooled.buffer(), mpb);
113         assertEquals(new MpReachNlriBuilder().build(), mpb.build());
114     }
115
116     @Test
117     public void testNullMpUnReachNlri() throws BGPParsingException {
118         final MpUnreachNlriBuilder mpb = new MpUnreachNlriBuilder();
119         this.parser.parseNlri(Unpooled.buffer(), mpb);
120         assertEquals(new MpUnreachNlriBuilder().build(), mpb.build());
121     }
122
123     @Test
124     public void testExtractEvpnDestination() throws BGPParsingException {
125         final DataContainerNodeAttrBuilder<NodeIdentifier, UnkeyedListEntryNode> evpnBI =
126                 ImmutableUnkeyedListEntryNodeBuilder.create();
127         evpnBI.withNodeIdentifier(EVPN_NID);
128         evpnBI.withChild(createMACIpAdvChoice());
129         evpnBI.withChild(createValueBuilder(RD_MODEL, RD_NID).build());
130         final EvpnDestination destResult = EvpnNlriParser.extractEvpnDestination(evpnBI.build());
131         final EvpnDestination expected = new EvpnDestinationBuilder()
132                 .setRouteDistinguisher(RD)
133                 .setEvpnChoice(MACIpAdvRParserTest.createdExpectedResult()).build();
134         assertEquals(expected, destResult);
135     }
136
137     @Test
138     public void testExtractRouteKey() throws BGPParsingException {
139         final DataContainerNodeAttrBuilder<NodeIdentifier, UnkeyedListEntryNode> evpnBI =
140                 ImmutableUnkeyedListEntryNodeBuilder.create();
141         evpnBI.withNodeIdentifier(EVPN_CHOICE_NID);
142         evpnBI.withChild(createValueBuilder(RD_MODEL, RD_NID).build());
143         evpnBI.withChild(createMACIpAdvChoice());
144         final EvpnDestination destResult = EvpnNlriParser.extractRouteKeyDestination(evpnBI.build());
145         final EvpnDestination expected = new EvpnDestinationBuilder().setRouteDistinguisher(RD)
146                 .setEvpnChoice(MACIpAdvRParserTest.createdExpectedRouteKey()).build();
147         assertEquals(expected, destResult);
148     }
149 }