Bug-1399: Added Linkstate attribute type configuration
[bgpcep.git] / bgp / linkstate / src / test / java / org / opendaylight / protocol / bgp / linkstate / LinkstateAttributeParserTest.java
1 /*
2  * Copyright (c) 2013 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.linkstate;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertTrue;
15
16 import com.google.common.collect.Lists;
17 import io.netty.buffer.ByteBuf;
18 import io.netty.buffer.Unpooled;
19 import java.util.Arrays;
20 import org.junit.Test;
21 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
22 import org.opendaylight.protocol.util.ByteArray;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.LinkProtectionType;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.LinkstateAddressFamily;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.LinkstateSubsequentAddressFamily;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.NlriType;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.PathAttributes1;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.destination.CLinkstateDestinationBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.link.state.attribute.LinkAttributesCase;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.link.state.attribute.NodeAttributesCase;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.link.state.attribute.PrefixAttributesCase;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.link.state.attribute.link.attributes._case.LinkAttributes;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.link.state.attribute.node.attributes._case.NodeAttributes;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.link.state.attribute.prefix.attributes._case.PrefixAttributes;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLinkstateCaseBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.linkstate._case.DestinationLinkstateBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes1Builder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes2Builder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpReachNlriBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpUnreachNlriBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
44
45 public class LinkstateAttributeParserTest {
46
47     private static final byte[] LINK_ATTR =  { 0x04, 0x04, 0, 0x04, 0x2a, 0x2a, 0x2a, 0x2a, 0x04, 0x06, 0, 0x04, 0x2b, 0x2b, 0x2b, 0x2b,
48         0x04, 0x40, 0, 0x04, 0, 0, 0, 0, 0x04, 0x41, 0, 0x04, 0x49, (byte) 0x98, (byte) 0x96, (byte) 0x80, 0x04, 0x42, 0, 0x04,
49         0x46, 0x43, 0x50, 0, 0x04, 0x43, 0, 0x20, 0x46, 0x43, 0x50, 0, 0x46, 0x43, 0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
50         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x04, 0x44, 0, 0x08, 0, 0, 0, 0, 0, 0, 0, 0, 0x04, 0x45, 0, 0x02, 0, 0x08, 0x04, 0x46, 0, 0x01,
51         (byte) 0xc0, 0x04, 0x47, 0, 0x03, 0, 0, 0x0a, 0x04, 0x48, 0, 0x08, 0x12, 0x34, 0x56, 0x78, 0x10, 0x30, 0x50, 0x70, 0x04, 0x4a,
52         0, 0x05, 0x31, 0x32, 0x4b, 0x2d, 0x32, 0x04, (byte) 0x88, 0, 0x01, 0x0a };
53
54     private static final byte[] NODE_ATTR = { 0x01, 0x07, 0, 0x04, 0, 0x2a, 0, 0x2b, 0x04, 0, 0, 0x01, (byte) 0xb0, 0x04, 0x02, 0,
55         0x05, 0x31, 0x32, 0x4b, 0x2d, 0x32, 0x04, 0x03, 0, 0x01, 0x72, 0x04, 0x03, 0, 0x01, 0x73, 0x04, 0x04, 0, 0x04,
56         0x29, 0x29, 0x29, 0x29, 0x04, (byte) 0x88, 0, 0x01, 0x0a };
57
58     private static final byte[] NODE_ATTR_S = { 0x01, 0x07, 0, 0x04, 0, 0x2a, 0, 0x2b, 0x04, 0, 0, 0x01, (byte) 0xb0, 0x04, 0x02, 0,
59         0x05, 0x31, 0x32, 0x4b, 0x2d, 0x32, 0x04, 0x03, 0, 0x01, 0x73, 0x04, 0x03, 0, 0x01, 0x72, 0x04, 0x04, 0, 0x04,
60         0x29, 0x29, 0x29, 0x29};
61
62     private static final byte[] P4_ATTR = { 0x04, (byte) 0x80, 0, 0x01, (byte) 0x80, 0x04, (byte) 0x81, 0, 0x08, 0x12, 0x34, 0x56, 0x78,
63         0x10, 0x30, 0x50, 0x70, 0x04, (byte) 0x82, 0, 0x08, 0x12, 0x34, 0x56, 0x78, 0x10, 0x30, 0x50, 0x70, 0x04, (byte) 0x83, 0, 0x04,
64         0, 0, 0, 0x0a, 0x04, (byte) 0x84, 0, 0x04, 0x0a, 0x19, 0x02, 0x1b, 0x04, (byte) 0x88, 0, 0x01, 0x0a };
65
66     private final LinkstateAttributeParser parser = new LinkstateAttributeParser(false);
67
68     private static PathAttributesBuilder createBuilder(final NlriType type) {
69         return new PathAttributesBuilder().addAugmentation(
70             org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes1.class,
71             new PathAttributes1Builder().setMpReachNlri(
72                 new MpReachNlriBuilder().setAfi(LinkstateAddressFamily.class).setSafi(LinkstateSubsequentAddressFamily.class).setAdvertizedRoutes(
73                     new AdvertizedRoutesBuilder().setDestinationType(
74                         new DestinationLinkstateCaseBuilder().setDestinationLinkstate(
75                             new DestinationLinkstateBuilder().setCLinkstateDestination(
76                                 Lists.newArrayList(new CLinkstateDestinationBuilder().setNlriType(type).build())).build()).build()).build()).build()).build());
77     }
78
79     private static PathAttributesBuilder createUnreachBuilder(final NlriType type) {
80         return new PathAttributesBuilder().addAugmentation(
81             org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes2.class,
82             new PathAttributes2Builder().setMpUnreachNlri(
83                 new MpUnreachNlriBuilder().setAfi(LinkstateAddressFamily.class).setSafi(LinkstateSubsequentAddressFamily.class).setWithdrawnRoutes(
84                     new WithdrawnRoutesBuilder().setDestinationType(
85                         new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationLinkstateCaseBuilder().setDestinationLinkstate(
86                             new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.destination.linkstate._case.DestinationLinkstateBuilder().setCLinkstateDestination(
87                                 Lists.newArrayList(new CLinkstateDestinationBuilder().setNlriType(type).build())).build()).build()).build()).build()).build());
88     }
89
90     @Test
91     public void testPositiveLinks() throws BGPParsingException {
92         final PathAttributesBuilder builder = createBuilder(NlriType.Link);
93         this.parser.parseAttribute(Unpooled.copiedBuffer(LINK_ATTR), builder);
94         final PathAttributes1 attrs = builder.getAugmentation(PathAttributes1.class);
95         final LinkAttributes ls = ((LinkAttributesCase) attrs.getLinkstatePathAttribute().getLinkStateAttribute()).getLinkAttributes();
96         assertNotNull(ls);
97
98         assertEquals("42.42.42.42", ls.getLocalIpv4RouterId().getValue());
99         assertEquals("43.43.43.43", ls.getRemoteIpv4RouterId().getValue());
100         assertEquals(Long.valueOf(0L), ls.getAdminGroup().getValue());
101         assertArrayEquals(new byte[] { (byte) 0x49, (byte) 0x98, (byte) 0x96, (byte) 0x80 }, ls.getMaxLinkBandwidth().getValue());
102         assertArrayEquals(new byte[] { (byte) 0x46, (byte) 0x43, (byte) 0x50, (byte) 0x00 }, ls.getMaxReservableBandwidth().getValue());
103         assertNotNull(ls.getUnreservedBandwidth());
104         assertEquals(8, ls.getUnreservedBandwidth().size());
105         assertEquals(LinkProtectionType.Dedicated1to1, ls.getLinkProtection());
106         assertTrue(ls.getMplsProtocol().isLdp());
107         assertTrue(ls.getMplsProtocol().isRsvpte());
108         assertEquals(new Long(10), ls.getMetric().getValue());
109         assertEquals(2, ls.getSharedRiskLinkGroups().size());
110         assertEquals(305419896, ls.getSharedRiskLinkGroups().get(0).getValue().intValue());
111         assertEquals("12K-2", ls.getLinkName());
112
113         //serialization
114         final ByteBuf buff = Unpooled.buffer();
115         this.parser.serializeAttribute(builder.build(), buff);
116         buff.skipBytes(3);
117         // there is unresolved TLV at the end, that needs to be cut off
118         assertArrayEquals(ByteArray.subByte(LINK_ATTR, 0, LINK_ATTR.length -5), ByteArray.getAllBytes(buff));
119     }
120
121     @Test
122     public void testPositiveNodes() throws BGPParsingException {
123         final PathAttributesBuilder builder = createBuilder(NlriType.Node);
124         this.parser.parseAttribute(Unpooled.copiedBuffer(NODE_ATTR), builder);
125
126         final PathAttributes1 attrs = builder.getAugmentation(PathAttributes1.class);
127         final NodeAttributes ls = ((NodeAttributesCase) attrs.getLinkstatePathAttribute().getLinkStateAttribute()).getNodeAttributes();
128         assertNotNull(ls);
129
130         assertEquals(2, ls.getTopologyIdentifier().size());
131         assertEquals(42, ls.getTopologyIdentifier().get(0).getValue().intValue());
132         assertTrue(ls.getNodeFlags().isOverload());
133         assertFalse(ls.getNodeFlags().isAttached());
134         assertTrue(ls.getNodeFlags().isExternal());
135         assertTrue(ls.getNodeFlags().isAbr());
136
137         assertEquals("12K-2", ls.getDynamicHostname());
138         assertEquals(2, ls.getIsisAreaId().size());
139         assertEquals("41.41.41.41", ls.getIpv4RouterId().getValue());
140
141         //serialization
142         final ByteBuf buff = Unpooled.buffer();
143         this.parser.serializeAttribute(builder.build(), buff);
144         buff.skipBytes(3);
145         // there is unresolved TLV at the end, that needs to be cut off
146         assertTrue(Arrays.equals(ByteArray.subByte(NODE_ATTR, 0, NODE_ATTR.length -5), ByteArray.getAllBytes(buff)) || Arrays.equals(NODE_ATTR_S, ByteArray.getAllBytes(buff)));
147     }
148
149     @Test
150     public void testPositiveV4Prefixes() throws BGPParsingException {
151         final PathAttributesBuilder builder = createUnreachBuilder(NlriType.Ipv4Prefix);
152         this.parser.parseAttribute(Unpooled.copiedBuffer(P4_ATTR), builder);
153
154         final PathAttributes1 attrs = builder.getAugmentation(PathAttributes1.class);
155         final PrefixAttributes ls = ((PrefixAttributesCase) attrs.getLinkstatePathAttribute().getLinkStateAttribute()).getPrefixAttributes();
156         assertNotNull(ls);
157
158         assertTrue(ls.getIgpBits().getUpDown().isUpDown());
159         assertEquals(2, ls.getRouteTags().size());
160         assertArrayEquals(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78 }, ls.getRouteTags().get(0).getValue());
161         assertEquals(1, ls.getExtendedTags().size());
162         assertArrayEquals(new byte[] { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x10, (byte) 0x30, (byte) 0x50,
163             (byte) 0x70 }, ls.getExtendedTags().get(0).getValue());
164         assertEquals(10, ls.getPrefixMetric().getValue().intValue());
165         assertEquals("10.25.2.27", ls.getOspfForwardingAddress().getIpv4Address().getValue());
166
167         //serialization
168         final ByteBuf buff = Unpooled.buffer();
169         this.parser.serializeAttribute(builder.build(), buff);
170         buff.skipBytes(3);
171         // there is unresolved TLV at the end, that needs to be cut off
172         assertArrayEquals(ByteArray.subByte(P4_ATTR, 0, P4_ATTR.length -5), ByteArray.getAllBytes(buff));
173     }
174 }