Allow AttributeRegistry to signal treat-as-withdraw
[bgpcep.git] / bgp / extensions / mvpn / src / test / java / org / opendaylight / protocol / bgp / mvpn / impl / attributes / PEDistinguisherLabelsAttributeHandlerTest.java
1 /*
2  * Copyright (c) 2018 AT&T Intellectual Property. 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.mvpn.impl.attributes;
10
11 import static org.junit.Assert.assertArrayEquals;
12 import static org.junit.Assert.assertEquals;
13
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import java.util.ArrayList;
17 import java.util.Collections;
18 import java.util.List;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.protocol.bgp.mvpn.impl.BGPActivator;
22 import org.opendaylight.protocol.bgp.parser.spi.AttributeRegistry;
23 import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext;
24 import org.opendaylight.protocol.bgp.parser.spi.pojo.SimpleBGPExtensionProviderContext;
25 import org.opendaylight.protocol.util.ByteArray;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.rev180417.bgp.rib.route.PeDistinguisherLabelsAttributeAugmentation;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.rev180417.bgp.rib.route.PeDistinguisherLabelsAttributeAugmentationBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.rev180417.pe.distinguisher.labels.attribute.PeDistinguisherLabelsAttributeBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.rev180417.pe.distinguisher.labels.attribute.pe.distinguisher.labels.attribute.PeDistinguisherLabelAttribute;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mvpn.rev180417.pe.distinguisher.labels.attribute.pe.distinguisher.labels.attribute.PeDistinguisherLabelAttributeBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.MplsLabel;
36
37 public final class PEDistinguisherLabelsAttributeHandlerTest {
38     /**
39      * ATT - TYPE - ATT LENGTH.
40      * PE ADDRESS - MPLS LABEL
41      * PE ADDRESS - MPLS LABEL
42      */
43     private static final byte[] PE_DISTINGUISHER_LABELS = {
44         (byte) 0xC0, (byte) 0x1B, (byte) 0x0e,
45         (byte) 0x7F, (byte) 0x00, (byte) 0x00, (byte) 0x01,
46         (byte) 0x00, (byte) 0x00, (byte) 0x10,
47         (byte) 0x7F, (byte) 0x00, (byte) 0x00, (byte) 0x02,
48         (byte) 0x00, (byte) 0x00, (byte) 0x20,
49     };
50     private AttributeRegistry handler;
51
52     @Before
53     public void setUp() {
54         final BGPExtensionProviderContext ctx = new SimpleBGPExtensionProviderContext();
55
56         final org.opendaylight.protocol.bgp.parser.impl.BGPActivator inetActivator =
57                 new org.opendaylight.protocol.bgp.parser.impl.BGPActivator();
58         inetActivator.start(ctx);
59         final BGPActivator bgpActivator = new BGPActivator();
60         bgpActivator.start(ctx);
61         this.handler = ctx.getAttributeRegistry();
62     }
63
64     @Test
65     public void testPEDistinguisherLabelsHandler() throws Exception {
66         final Attributes expected = buildPEDistinguisherLabelsAttributAttribute();
67         final ByteBuf actual = Unpooled.buffer();
68         this.handler.serializeAttribute(expected, actual);
69         assertArrayEquals(PE_DISTINGUISHER_LABELS, ByteArray.readAllBytes(actual));
70         final Attributes actualAttr = this.handler.parseAttributes(
71                 Unpooled.wrappedBuffer(PE_DISTINGUISHER_LABELS), null).getAttributes();
72         assertEquals(expected, actualAttr);
73     }
74
75     private static Attributes buildPEDistinguisherLabelsAttributAttribute() {
76         final List<PeDistinguisherLabelAttribute> peAtt = new ArrayList<>(2);
77
78         peAtt.add(new PeDistinguisherLabelAttributeBuilder()
79                 .setPeAddress(new IpAddress(new Ipv4Address("127.0.0.1")))
80                 .setMplsLabel(new MplsLabel(1L))
81                 .build());
82         peAtt.add(new PeDistinguisherLabelAttributeBuilder()
83                 .setPeAddress(new IpAddress(new Ipv4Address("127.0.0.2")))
84                 .setMplsLabel(new MplsLabel(2L))
85                 .build());
86         return new AttributesBuilder()
87                 .setUnrecognizedAttributes(Collections.emptyList())
88                 .addAugmentation(PeDistinguisherLabelsAttributeAugmentation.class,
89                         new PeDistinguisherLabelsAttributeAugmentationBuilder()
90                                 .setPeDistinguisherLabelsAttribute(new PeDistinguisherLabelsAttributeBuilder()
91                                         .setPeDistinguisherLabelAttribute(peAtt).build()).build()).build();
92     }
93 }