efe236272451169ebcf3b1ad496930294fec5882
[bgpcep.git] / bgp / evpn / src / test / java / org / opendaylight / protocol / bgp / evpn / impl / extended / communities / Layer2AttributesExtComTest.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
9 package org.opendaylight.protocol.bgp.evpn.impl.extended.communities;
10
11 import static org.junit.Assert.assertArrayEquals;
12 import static org.junit.Assert.assertEquals;
13 import static org.opendaylight.protocol.bgp.evpn.impl.EvpnTestUtil.COMMUNITY_VALUE_SIZE;
14
15 import io.netty.buffer.ByteBuf;
16 import io.netty.buffer.Unpooled;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
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.rev171213.NormalizationType;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.OperationalMode;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.evpn.routes.evpn.routes.evpn.route.attributes.extended.communities.extended.community.DefaultGatewayExtendedCommunityCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.evpn.routes.evpn.routes.evpn.route.attributes.extended.communities.extended.community.Layer2AttributesExtendedCommunityCase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.evpn.routes.evpn.routes.evpn.route.attributes.extended.communities.extended.community.Layer2AttributesExtendedCommunityCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev171213.layer._2.attributes.extended.community.Layer2AttributesExtendedCommunityBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.ExtendedCommunity;
29
30 public class Layer2AttributesExtComTest {
31     private static final byte[] EXPECTEDS = {(byte) 0x00, (byte) 0x07, (byte) 0x01, (byte) 0x01, (byte) 0x00, (byte) 0x00};
32     private static final byte[] EVPN_VPWS = {(byte) 0x00, (byte) 0x2f, (byte) 0x01, (byte) 0x01, (byte) 0x00, (byte) 0x00};
33     private static final byte[] EVPN_VPWS_2 = {(byte) 0x00, (byte) 0x57, (byte) 0x01, (byte) 0x01, (byte) 0x00, (byte) 0x00};
34     private Layer2AttributesExtCom parser;
35
36     @Before
37     public void setUp() {
38         this.parser = new Layer2AttributesExtCom();
39     }
40
41     @Test
42     public void handlerTest() throws BGPParsingException, BGPDocumentedException {
43         final ByteBuf buff = Unpooled.buffer(COMMUNITY_VALUE_SIZE);
44
45         final Layer2AttributesExtendedCommunityCase expected = new Layer2AttributesExtendedCommunityCaseBuilder()
46             .setLayer2AttributesExtendedCommunity(new Layer2AttributesExtendedCommunityBuilder().setBackupPe(true)
47                 .setControlWord(true).setPrimaryPe(true).setL2Mtu(257).build()).build();
48         this.parser.serializeExtendedCommunity(expected, buff);
49         final byte[] resultByte = ByteArray.getAllBytes(buff);
50         assertArrayEquals(EXPECTEDS, resultByte);
51
52         final ExtendedCommunity result = this.parser.parseExtendedCommunity(Unpooled.wrappedBuffer(EXPECTEDS));
53         assertEquals(expected, result);
54     }
55
56     @Test
57     public void evpnVpwsTest() throws BGPParsingException, BGPDocumentedException {
58         final ByteBuf buff = Unpooled.buffer(COMMUNITY_VALUE_SIZE);
59
60         final Layer2AttributesExtendedCommunityCase expected = new Layer2AttributesExtendedCommunityCaseBuilder()
61             .setLayer2AttributesExtendedCommunity(new Layer2AttributesExtendedCommunityBuilder().setBackupPe(true)
62                 .setControlWord(true).setPrimaryPe(true).setL2Mtu(257)
63                 .setModeOfOperation(OperationalMode.VlanAwareFxc)
64                 .setOperatingPer(NormalizationType.SingleVid)
65                 .build()).build();
66         this.parser.serializeExtendedCommunity(expected, buff);
67         final byte[] resultByte = ByteArray.getAllBytes(buff);
68         assertArrayEquals(EVPN_VPWS, resultByte);
69
70         final ExtendedCommunity result = this.parser.parseExtendedCommunity(Unpooled.wrappedBuffer(EVPN_VPWS));
71         assertEquals(expected, result);
72
73         final ByteBuf buff2 = Unpooled.buffer(COMMUNITY_VALUE_SIZE);
74         final Layer2AttributesExtendedCommunityCase expected2 = new Layer2AttributesExtendedCommunityCaseBuilder()
75             .setLayer2AttributesExtendedCommunity(new Layer2AttributesExtendedCommunityBuilder().setBackupPe(true)
76                 .setControlWord(true).setPrimaryPe(true).setL2Mtu(257)
77                 .setModeOfOperation(OperationalMode.VlanUnawareFxc)
78                 .setOperatingPer(NormalizationType.DoubleVid)
79                 .build()).build();
80         this.parser.serializeExtendedCommunity(expected2, buff2);
81         final byte[] resultByte2 = ByteArray.getAllBytes(buff2);
82         assertArrayEquals(EVPN_VPWS_2, resultByte2);
83
84         final ExtendedCommunity result2 = this.parser.parseExtendedCommunity(Unpooled.wrappedBuffer(EVPN_VPWS_2));
85         assertEquals(expected2, result2);
86     }
87
88     @Test(expected = IllegalArgumentException.class)
89     public void wrongCaseTest() {
90         this.parser.serializeExtendedCommunity(new DefaultGatewayExtendedCommunityCaseBuilder().build(), null);
91     }
92
93     @Test
94     public void testSubtype() {
95         assertEquals(4, this.parser.getSubType());
96     }
97 }