summaryrefslogtreecommitdiff
path: root/src/main/java/com/lumaserv/bgp/protocol/attribute/ASPathAttribute.java
blob: b39e473ac23d96b30dd1f858800a32d873d894be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.lumaserv.bgp.protocol.attribute;

import lombok.Getter;
import lombok.Setter;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

@Getter
@Setter
public class ASPathAttribute implements PathAttribute {

    List<Segment> segments = new ArrayList<>();

    public ASPathAttribute(byte typeCode, byte[] data) {
        int offset = 0;
        while (offset < data.length) {
            Segment segment = new Segment().setType(data[offset]);
            byte length = data[offset + 1];
            for(int i=0; i<length; i++)
                segment.asns.add(((data[offset + 2 + (i * 2)] & 0xFF) << 8) | (data[offset + 3 + (i * 2)] & 0xFF));
            offset += 2 + (length * 2);
            segments.add(segment);
        }
    }

    public byte getTypeCode() {
        return 2;
    }

    public void build(ByteBuffer b) {
    }

    @Getter
    @Setter
    public static class Segment {
        byte type;
        List<Integer> asns = new ArrayList<>();

        public String toString() {
            StringBuffer buf = new StringBuffer();

            buf.append("(");
            for(Integer asn : asns) {
                if (buf.length() > 1)
                    buf.append(" ");
                buf.append(asn);
            }
            buf.append(")");
            return buf.toString();
        }
    }

    public String toString() {
        StringBuffer buf = new StringBuffer();

        buf.append("[");
        for(Segment segment : segments) {
            if (buf.length() > 1)
                buf.append(", ");
            buf.append(segment);
        }
        buf.append("]");
        return buf.toString();
    }
}