2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.bgp.concepts;
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;
24 * Utility class for of RouteDistinguisher serialization and parsing.
25 * https://tools.ietf.org/html/rfc4364#section-4.2
27 public final class RouteDistinguisherUtil {
28 private static final Logger LOG = LoggerFactory.getLogger(RouteDistinguisherUtil.class);
30 private enum RD_TYPE {
36 public final int value;
42 public static RD_TYPE valueOf(final int value) {
43 for (RD_TYPE type : values()) {
44 if (type.value == value) {
52 private static final String SEPARATOR = ":";
53 public static final int RD_LENGTH = 8;
55 private RouteDistinguisherUtil() {
56 throw new UnsupportedOperationException();
60 * Serializes route distinguisher according to type and writes into ByteBuf.
62 * @param distinguisher
63 * @param byteAggregator
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);
87 LOG.warn("Unable to serialize Route Distinguisher. Invalid RD value found. RD={}", distinguisher);
92 * Parses three types of route distinguisher from given ByteBuf.
95 * @return RouteDistinguisher
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();
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()));
111 routeDistiguisher.append(Ipv4Util.addressForByteBuf(buffer).getValue());
112 routeDistiguisher.append(SEPARATOR);
113 routeDistiguisher.append(buffer.readUnsignedShort());
114 return new RouteDistinguisher(new RdIpv4(routeDistiguisher.toString()));
116 routeDistiguisher.append(buffer.readUnsignedInt());
117 routeDistiguisher.append(SEPARATOR);
118 routeDistiguisher.append(buffer.readUnsignedShort());
119 return new RouteDistinguisher(new RdAs(routeDistiguisher.toString()));
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(" ");
126 LOG.debug("Invalid Route Distinguisher: type={}, rawRouteDistinguisherValue={}", type, routeDistiguisher.toString());
127 throw new IllegalArgumentException("Invalid Route Distinguisher type " + type);
131 public static RouteDistinguisher parseRouteDistinguisher(final String str) {
132 return str == null ? null : RouteDistinguisherBuilder.getDefaultInstance(str);
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;