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 io.netty.buffer.ByteBuf;
11 import org.opendaylight.protocol.util.ByteBufWriteUtil;
12 import org.opendaylight.protocol.util.Ipv4Util;
13 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.RdAs;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.RdIpv4;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.RouteDistinguisher;
19 * Utility class for of RouteDistinguisher serialization and parsing.
20 * https://tools.ietf.org/html/rfc4364#section-4.2
22 public final class RouteDistinguisherUtil {
24 private static final int IPV4_TYPE = 1;
25 private static final int AS_4BYTE_TYPE = 2;
26 private static final String SEPARATOR = ":";
27 public static final int RD_LENGTH = 8;
29 private RouteDistinguisherUtil() {
30 throw new UnsupportedOperationException();
34 * Serializes route distinguisher according to type and writes into ByteBuf.
36 * @param distinquisher
37 * @param byteAggregator
39 public static void serializeRouteDistinquisher(final RouteDistinguisher distinquisher, final ByteBuf byteAggregator) {
40 if (distinquisher.getRdAs() != null) {
41 final String[] values = distinquisher.getRdAs().getValue().split(SEPARATOR);
42 byteAggregator.writeShort(AS_4BYTE_TYPE);
43 final long admin = Integer.parseUnsignedInt(values[0]);
44 ByteBufWriteUtil.writeUnsignedInt(admin, byteAggregator);
45 ByteBufWriteUtil.writeUnsignedShort(Integer.parseInt(values[1]), byteAggregator);
46 } else if (distinquisher.getRdIpv4() != null) {
47 final String[] values = distinquisher.getRdIpv4().getValue().split(SEPARATOR);
48 final Ipv4Address ip = new Ipv4Address(values[0]);
49 byteAggregator.writeShort(IPV4_TYPE);
50 ByteBufWriteUtil.writeIpv4Address(ip, byteAggregator);
51 ByteBufWriteUtil.writeUnsignedShort(Integer.parseInt(values[1]), byteAggregator);
56 * Parses three types of route distinguisher from given ByteBuf.
59 * @return RouteDistinguisher
61 public static RouteDistinguisher parseRouteDistinguisher(final ByteBuf buffer) {
62 final int type = buffer.readUnsignedShort();
63 final StringBuilder routeDistiguisher = new StringBuilder();
66 routeDistiguisher.append(Ipv4Util.addressForByteBuf(buffer).getValue());
67 routeDistiguisher.append(SEPARATOR);
68 routeDistiguisher.append(buffer.readUnsignedShort());
69 return new RouteDistinguisher(new RdIpv4(routeDistiguisher.toString()));
71 routeDistiguisher.append(buffer.readUnsignedInt());
72 routeDistiguisher.append(SEPARATOR);
73 routeDistiguisher.append(buffer.readUnsignedShort());
74 return new RouteDistinguisher(new RdAs(routeDistiguisher.toString()));