4eac844cd6557cbd95acbbc2e2e0ebc265b487d6
[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 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;
17
18 /**
19  * Utility class for of RouteDistinguisher serialization and parsing.
20  * https://tools.ietf.org/html/rfc4364#section-4.2
21  */
22 public final class RouteDistinguisherUtil {
23
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;
28
29     private RouteDistinguisherUtil() {
30         throw new UnsupportedOperationException();
31     }
32
33     /**
34      * Serializes route distinguisher according to type and writes into ByteBuf.
35      *
36      * @param distinquisher
37      * @param byteAggregator
38      */
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);
52         }
53     }
54
55     /**
56      * Parses three types of route distinguisher from given ByteBuf.
57      *
58      * @param buffer
59      * @return RouteDistinguisher
60      */
61     public static RouteDistinguisher parseRouteDistinguisher(final ByteBuf buffer) {
62         final int type = buffer.readUnsignedShort();
63         final StringBuilder routeDistiguisher = new StringBuilder();
64         switch (type) {
65         case IPV4_TYPE:
66             routeDistiguisher.append(Ipv4Util.addressForByteBuf(buffer).getValue());
67             routeDistiguisher.append(SEPARATOR);
68             routeDistiguisher.append(buffer.readUnsignedShort());
69             return new RouteDistinguisher(new RdIpv4(routeDistiguisher.toString()));
70         case AS_4BYTE_TYPE:
71             routeDistiguisher.append(buffer.readUnsignedInt());
72             routeDistiguisher.append(SEPARATOR);
73             routeDistiguisher.append(buffer.readUnsignedShort());
74             return new RouteDistinguisher(new RdAs(routeDistiguisher.toString()));
75         default:
76             break;
77         }
78         return null;
79     }
80 }