2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.flowspec;
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.fail;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import org.junit.Test;
17 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
18 import org.opendaylight.protocol.util.ByteArray;
19 import org.opendaylight.protocol.util.NoopReferenceCache;
20 import org.opendaylight.protocol.util.ReferenceCache;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150114.Dscp;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150114.update.path.attributes.extended.communities.extended.community.RedirectExtendedCommunityCase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150114.update.path.attributes.extended.communities.extended.community.RedirectExtendedCommunityCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150114.update.path.attributes.extended.communities.extended.community.TrafficActionExtendedCommunityCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150114.update.path.attributes.extended.communities.extended.community.TrafficActionExtendedCommunityCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150114.update.path.attributes.extended.communities.extended.community.TrafficMarkingExtendedCommunityCase;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150114.update.path.attributes.extended.communities.extended.community.TrafficMarkingExtendedCommunityCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150114.update.path.attributes.extended.communities.extended.community.TrafficRateExtendedCommunityCase;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150114.update.path.attributes.extended.communities.extended.community.TrafficRateExtendedCommunityCaseBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150114.update.path.attributes.extended.communities.extended.community.redirect.extended.community._case.RedirectExtendedCommunityBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150114.update.path.attributes.extended.communities.extended.community.traffic.action.extended.community._case.TrafficActionExtendedCommunityBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150114.update.path.attributes.extended.communities.extended.community.traffic.marking.extended.community._case.TrafficMarkingExtendedCommunityBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150114.update.path.attributes.extended.communities.extended.community.traffic.rate.extended.community._case.TrafficRateExtendedCommunityBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.ExtendedCommunities;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ShortAsNumber;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth;
39 public class FSExtendedCommunitiesAttributeParserTest {
41 private final ReferenceCache ref = NoopReferenceCache.getInstance();
43 private static final byte[] communitiesBytes = { (byte)128, 6, 0, 72, 0, 1, 2, 3,
44 (byte)128, 7, 0, 0, 0, 0, 0, 3,
45 (byte)128, 8, 0, 35, 4, 2, 8, 7,
46 (byte)128, 9, 0, 0, 0, 0, 0, 63 };
49 public void testExtendedCommunitiesParser() {
50 final FSExtendedCommunitiesAttributeParser parser = new FSExtendedCommunitiesAttributeParser(this.ref);
51 final PathAttributesBuilder pa = new PathAttributesBuilder();
53 parser.parseAttribute(Unpooled.copiedBuffer(communitiesBytes), pa);
54 } catch (final BGPDocumentedException e1) {
55 fail("Not expected exception: " + e1);
58 final TrafficRateExtendedCommunityCase expected = new TrafficRateExtendedCommunityCaseBuilder().setTrafficRateExtendedCommunity(
59 new TrafficRateExtendedCommunityBuilder().setInformativeAs(new ShortAsNumber(72L))
60 .setLocalAdministrator(new Bandwidth(new byte[] { 0, 1, 2, 3 })).build()).build();
61 ExtendedCommunities ex = pa.getExtendedCommunities().get(0);
62 final TrafficRateExtendedCommunityCase result = (TrafficRateExtendedCommunityCase) ex.getExtendedCommunity();
63 assertEquals(expected.getTrafficRateExtendedCommunity().getInformativeAs(), result.getTrafficRateExtendedCommunity().getInformativeAs());
64 assertArrayEquals(expected.getTrafficRateExtendedCommunity().getLocalAdministrator().getValue(),
65 result.getTrafficRateExtendedCommunity().getLocalAdministrator().getValue());
67 final TrafficActionExtendedCommunityCase expected1 = new TrafficActionExtendedCommunityCaseBuilder().setTrafficActionExtendedCommunity(
68 new TrafficActionExtendedCommunityBuilder().setSample(true).setTerminalAction(true).build()).build();
69 ex = pa.getExtendedCommunities().get(1);
70 final TrafficActionExtendedCommunityCase result1 = (TrafficActionExtendedCommunityCase) ex.getExtendedCommunity();
71 assertEquals(expected1.getTrafficActionExtendedCommunity().isSample(), result1.getTrafficActionExtendedCommunity().isSample());
72 assertEquals(expected1.getTrafficActionExtendedCommunity().isTerminalAction(), result1.getTrafficActionExtendedCommunity().isTerminalAction());
74 final RedirectExtendedCommunityCase expected2 = new RedirectExtendedCommunityCaseBuilder().setRedirectExtendedCommunity(
75 new RedirectExtendedCommunityBuilder().setGlobalAdministrator(new ShortAsNumber(35L)).setLocalAdministrator(
76 new byte[] { 4, 2, 8, 7 }).build()).build();
77 ex = pa.getExtendedCommunities().get(2);
78 final RedirectExtendedCommunityCase result2 = (RedirectExtendedCommunityCase) ex.getExtendedCommunity();
79 assertEquals(expected2.getRedirectExtendedCommunity().getGlobalAdministrator(),
80 result2.getRedirectExtendedCommunity().getGlobalAdministrator());
81 assertArrayEquals(expected2.getRedirectExtendedCommunity().getLocalAdministrator(),
82 result2.getRedirectExtendedCommunity().getLocalAdministrator());
84 final TrafficMarkingExtendedCommunityCase expected3 = new TrafficMarkingExtendedCommunityCaseBuilder().setTrafficMarkingExtendedCommunity(
85 new TrafficMarkingExtendedCommunityBuilder().setGlobalAdministrator(new Dscp((short) 63)).build()).build();
86 ex = pa.getExtendedCommunities().get(3);
87 final TrafficMarkingExtendedCommunityCase result3 = (TrafficMarkingExtendedCommunityCase) ex.getExtendedCommunity();
88 assertEquals(expected3.getTrafficMarkingExtendedCommunity().getGlobalAdministrator(),
89 result3.getTrafficMarkingExtendedCommunity().getGlobalAdministrator());
91 final ByteBuf serializedBuffer = Unpooled.buffer();
92 parser.serializeAttribute(pa.build(), serializedBuffer);
93 assertArrayEquals(new byte[]{ (byte)192, 16, 32, (byte)128, 6, 0, 72, 0, 1, 2, 3,
94 (byte)128, 7, 0, 0, 0, 0, 0, 3,
95 (byte)128, 8, 0, 35, 4, 2, 8, 7,
96 (byte)128, 9, 0, 0, 0, 0, 0, 63 }, ByteArray.readAllBytes(serializedBuffer));
99 parser.parseAttribute(Unpooled.copiedBuffer(new byte[] { 11, 11, 21, 45, 5, 4, 3, 1 }), pa);
100 fail("Exception should have occured.");
101 } catch (final BGPDocumentedException e) {
102 assertEquals("Could not parse Extended Community type: 11", e.getMessage());