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