summaryrefslogtreecommitdiff
path: root/src/main/java/com/lumaserv/bgp/protocol/attribute/OriginAttribute.java
blob: 8cc34742f39010d80d944945de4391b645fb8fb8 (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
package com.lumaserv.bgp.protocol.attribute;

import java.nio.ByteBuffer;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

import java.util.EnumSet;


@AllArgsConstructor
@Getter
@Setter
public class OriginAttribute implements PathAttribute {

    @Getter
    enum Origin {
        IGP(0),
        EGP(1),
        INCOMPLETE(2);

        byte code;

        Origin(int code) {
            this.code = (byte)code;
        }

        public static Origin fromByte(byte code) {
            return EnumSet.allOf(Origin.class)
                .stream()
                .filter(e -> e.getCode() == code)
                .findAny()
                .orElseThrow(() -> new IllegalArgumentException("unknown origin: " + code));
        }
    }

    Origin origin;

    public OriginAttribute(byte typeCode, byte[] data) {
        origin = Origin.fromByte(data[0]);
    }

    public byte getTypeCode() {
        return 1;
    }

    public void build(ByteBuffer b) {
        b.put(origin);
    }

    public String toString() {
        return origin.toString();
    }
}