BUG-5904 Support 2-byte AS Route Distinguisher
[bgpcep.git] / bgp / concepts / src / main / java / org / opendaylight / bgp / concepts / RouteDistinguisherUtil.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 package org.opendaylight.bgp.concepts;
9
10 import com.google.common.base.Preconditions;
11 import io.netty.buffer.ByteBuf;
12 import org.opendaylight.protocol.util.ByteBufWriteUtil;
13 import org.opendaylight.protocol.util.Ipv4Util;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.RdAs;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.RdIpv4;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.RdTwoOctetAs;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.RouteDistinguisher;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.RouteDistinguisherBuilder;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * Utility class for of RouteDistinguisher serialization and parsing.
25  * https://tools.ietf.org/html/rfc4364#section-4.2
26  */
27 public final class RouteDistinguisherUtil {
28     private static final Logger LOG = LoggerFactory.getLogger(RouteDistinguisherUtil.class);
29
30     private enum RD_TYPE {
31         AS_2BYTE(0),
32         IPV4(1),
33         AS_4BYTE(2),
34         INVALID(-1);
35
36         public final int value;
37
38         RD_TYPE(int val) {
39             value = val;
40         }
41
42         public static RD_TYPE valueOf(final int value) {
43             for (RD_TYPE type : values()) {
44                 if (type.value == value) {
45                     return type;
46                 }
47             }
48             return INVALID;
49         }
50     }
51
52     private static final String SEPARATOR = ":";
53     public static final int RD_LENGTH = 8;
54
55     private RouteDistinguisherUtil() {
56         throw new UnsupportedOperationException();
57     }
58
59     /**
60      * Serializes route distinguisher according to type and writes into ByteBuf.
61      *
62      * @param distinguisher
63      * @param byteAggregator
64      */
65     public static void serializeRouteDistinquisher(final RouteDistinguisher distinguisher, final ByteBuf byteAggregator) {
66         Preconditions.checkNotNull(distinguisher);
67         Preconditions.checkState(byteAggregator != null && byteAggregator.isWritable(RD_LENGTH), "Cannot write Route Distinguisher to provided buffer.");
68         if (distinguisher.getRdTwoOctetAs() != null) {
69             final String[] values = distinguisher.getRdTwoOctetAs().getValue().split(SEPARATOR);
70             byteAggregator.writeShort(RD_TYPE.AS_2BYTE.value);
71             ByteBufWriteUtil.writeUnsignedShort(Integer.parseInt(values[1]), byteAggregator);
72             final long assignedNumber = Integer.parseUnsignedInt(values[2]);
73             ByteBufWriteUtil.writeUnsignedInt(assignedNumber, byteAggregator);
74         } else if (distinguisher.getRdAs() != null) {
75             final String[] values = distinguisher.getRdAs().getValue().split(SEPARATOR);
76             byteAggregator.writeShort(RD_TYPE.AS_4BYTE.value);
77             final long admin = Integer.parseUnsignedInt(values[0]);
78             ByteBufWriteUtil.writeUnsignedInt(admin, byteAggregator);
79             ByteBufWriteUtil.writeUnsignedShort(Integer.parseInt(values[1]), byteAggregator);
80         } else if (distinguisher.getRdIpv4() != null) {
81             final String[] values = distinguisher.getRdIpv4().getValue().split(SEPARATOR);
82             final Ipv4Address ip = new Ipv4Address(values[0]);
83             byteAggregator.writeShort(RD_TYPE.IPV4.value);
84             ByteBufWriteUtil.writeIpv4Address(ip, byteAggregator);
85             ByteBufWriteUtil.writeUnsignedShort(Integer.parseInt(values[1]), byteAggregator);
86         } else {
87             LOG.warn("Unable to serialize Route Distinguisher. Invalid RD value found. RD={}", distinguisher);
88         }
89     }
90
91     /**
92      * Parses three types of route distinguisher from given ByteBuf.
93      *
94      * @param buffer
95      * @return RouteDistinguisher
96      */
97     public static RouteDistinguisher parseRouteDistinguisher(final ByteBuf buffer) {
98         Preconditions.checkState(buffer != null && buffer.isReadable(RD_LENGTH), "Cannot read Route Distinguisher from provided buffer.");
99         final int type = buffer.readUnsignedShort();
100         final RD_TYPE rdType = RD_TYPE.valueOf(type);
101         final StringBuilder routeDistiguisher = new StringBuilder();
102         switch (rdType) {
103         case AS_2BYTE:
104             routeDistiguisher.append(type);
105             routeDistiguisher.append(SEPARATOR);
106             routeDistiguisher.append(buffer.readUnsignedShort());
107             routeDistiguisher.append(SEPARATOR);
108             routeDistiguisher.append(buffer.readUnsignedInt());
109             return new RouteDistinguisher(new RdTwoOctetAs(routeDistiguisher.toString()));
110         case IPV4:
111             routeDistiguisher.append(Ipv4Util.addressForByteBuf(buffer).getValue());
112             routeDistiguisher.append(SEPARATOR);
113             routeDistiguisher.append(buffer.readUnsignedShort());
114             return new RouteDistinguisher(new RdIpv4(routeDistiguisher.toString()));
115         case AS_4BYTE:
116             routeDistiguisher.append(buffer.readUnsignedInt());
117             routeDistiguisher.append(SEPARATOR);
118             routeDistiguisher.append(buffer.readUnsignedShort());
119             return new RouteDistinguisher(new RdAs(routeDistiguisher.toString()));
120         default:
121             // now that this RD type is not supported, we want to read the remain 6 bytes
122             // in order to get the byte index correct
123             for (int i = 0; i < 6; i++) {
124                 routeDistiguisher.append("0x").append(Integer.toHexString(buffer.readByte() & 0xFF)).append(" ");
125             }
126             LOG.debug("Invalid Route Distinguisher: type={}, rawRouteDistinguisherValue={}", type, routeDistiguisher.toString());
127             throw new IllegalArgumentException("Invalid Route Distinguisher type " + type);
128         }
129     }
130
131     public static RouteDistinguisher parseRouteDistinguisher(final String str) {
132         return str == null ? null : RouteDistinguisherBuilder.getDefaultInstance(str);
133     }
134
135     public static RouteDistinguisher parseRouteDistinguisher(final Object obj) {
136         if (obj instanceof String) {
137             return RouteDistinguisherBuilder.getDefaultInstance((String) obj);
138         } else if (obj instanceof RouteDistinguisher) {
139             return (RouteDistinguisher) obj;
140         } else {
141             return null;
142         }
143     }
144 }