4b2d95fced0ef9b71b383b344e8a0d88771e7bfa
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / open / CapabilityParameterParser.java
1 /*
2  * Copyright (c) 2013 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.protocol.bgp.parser.impl.message.open;
9
10 import java.util.Arrays;
11 import java.util.Map.Entry;
12
13 import org.opendaylight.protocol.bgp.concepts.BGPTableType;
14 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
15 import org.opendaylight.protocol.bgp.parser.impl.ParserUtil;
16 import org.opendaylight.protocol.bgp.parser.parameter.AS4BytesCapability;
17 import org.opendaylight.protocol.bgp.parser.parameter.CapabilityParameter;
18 import org.opendaylight.protocol.bgp.parser.parameter.GracefulCapability;
19 import org.opendaylight.protocol.bgp.parser.parameter.MultiprotocolCapability;
20 import org.opendaylight.protocol.util.ByteArray;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.SubsequentAddressFamily;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import com.google.common.primitives.UnsignedBytes;
28
29 /**
30  * Parser for BGP Capability Parameter.
31  */
32 public final class CapabilityParameterParser {
33
34         private static final Logger logger = LoggerFactory.getLogger(CapabilityParameterParser.class);
35
36         private static final int CODE_SIZE = 1; // bytes
37         private static final int LENGTH_SIZE = 1; // bytes
38         private static final int AFI_SIZE = 2; // bytes
39         private static final int SAFI_SIZE = 1; // bytes
40
41         private CapabilityParameterParser() {
42
43         }
44
45         /**
46          * Serializes given BGP Capability Parameter to byte array.
47          * 
48          * @param param BGP Capability to be serialized
49          * @return BGP Capability converted to byte array
50          */
51         public static byte[] put(final CapabilityParameter cap) {
52                 if (cap == null) {
53                         throw new IllegalArgumentException("BGP Capability cannot be null");
54                 }
55                 logger.trace("Started serializing BGP Capability: {}", cap);
56                 byte[] value = null;
57                 if (cap instanceof MultiprotocolCapability) {
58                         value = putMultiProtocolParameterValue((MultiprotocolCapability) cap);
59                 } else if (cap instanceof GracefulCapability) {
60                         value = putGracefulParameterValue((GracefulCapability) cap);
61                 } else if (cap instanceof AS4BytesCapability) {
62                         value = putAS4BytesParameterValue((AS4BytesCapability) cap);
63                 }
64                 final byte[] bytes = new byte[CODE_SIZE + LENGTH_SIZE + value.length];
65                 bytes[0] = ByteArray.intToBytes(cap.getCode())[Integer.SIZE / Byte.SIZE - 1];
66                 bytes[1] = ByteArray.intToBytes(value.length)[Integer.SIZE / Byte.SIZE - 1];
67                 System.arraycopy(value, 0, bytes, CODE_SIZE + LENGTH_SIZE, value.length);
68                 logger.trace("BGP Parameter serialized to: {}", Arrays.toString(bytes));
69                 return bytes;
70         }
71
72         /**
73          * Parses given byte array to Capability Parameter. Only Multiprotocol capability is supported.
74          * 
75          * @param bytes byte array representing BGP Parameters
76          * @return list of BGP Parameters
77          * @throws BGPParsingException if the parsing was unsuccessful
78          */
79         public static CapabilityParameter parse(final byte[] bytes) throws BGPParsingException {
80                 if (bytes == null || bytes.length == 0) {
81                         throw new IllegalArgumentException("Byte array cannot be null or empty.");
82                 }
83                 logger.trace("Started parsing of BGP Capability: {}", Arrays.toString(bytes));
84                 int byteOffset = 0;
85                 final int capCode = UnsignedBytes.toInt(bytes[byteOffset++]);
86                 final int capLength = UnsignedBytes.toInt(bytes[byteOffset++]);
87                 if (capCode == MultiprotocolCapability.CODE) {
88                         logger.trace("Parsed BGP Capability.");
89                         return parseMultiProtocolParameterValue(ByteArray.subByte(bytes, byteOffset, capLength));
90                 } else if (capCode == AS4BytesCapability.CODE) {
91                         logger.trace("Parsed AS4B Capability.");
92                         return parseAS4BParameterValue(ByteArray.subByte(bytes, byteOffset, capLength));
93                 } else {
94                         logger.debug("Only Multiprotocol Capability Parameter is supported. Received code {}", capCode);
95                 }
96                 return null;
97         }
98
99         private static byte[] putGracefulParameterValue(final GracefulCapability param) {
100                 final int RESTART_FLAGS_SIZE = 4; // bits
101                 final int TIMER_SIZE = 12; // bits
102                 final int AFI_SIZE = 2; // bytes
103                 final int SAFI_SIZE = 1; // bytes
104                 final int AF_FLAGS_SIZE = 1; // bytes
105                 final byte[] bytes = new byte[(RESTART_FLAGS_SIZE + TIMER_SIZE + (AFI_SIZE * Byte.SIZE + SAFI_SIZE * Byte.SIZE + AF_FLAGS_SIZE
106                                 * Byte.SIZE)
107                                 * param.getTableTypes().size())
108                                 / Byte.SIZE];
109                 if (param.isRestartFlag()) {
110                         bytes[0] = (byte) 0x80;
111                 }
112                 int index = (RESTART_FLAGS_SIZE + TIMER_SIZE) / Byte.SIZE;
113                 for (final Entry<BGPTableType, Boolean> entry : param.getTableTypes().entrySet()) {
114                         final byte[] a = putAfi(entry.getKey().getAddressFamily());
115                         final byte s = putSafi(entry.getKey().getSubsequentAddressFamily());
116                         final byte f = (entry.getValue()) ? (byte) 0x80 : (byte) 0x00;
117                         System.arraycopy(a, 0, bytes, index, AFI_SIZE);
118                         index += AFI_SIZE;
119                         bytes[index] = s;
120                         index += SAFI_SIZE;
121                         bytes[index] = f;
122                         index += AF_FLAGS_SIZE;
123                 }
124                 return bytes;
125         }
126
127         private static byte[] putMultiProtocolParameterValue(final MultiprotocolCapability param) {
128                 final byte[] a = putAfi(param.getAfi());
129                 final byte s = putSafi(param.getSafi());
130
131                 final byte[] bytes = new byte[AFI_SIZE + SAFI_SIZE + 1]; // 2 byte is reserved 2B AFI + 1B Reserved + 1B SAFI
132                 System.arraycopy(a, 0, bytes, 0, AFI_SIZE);
133                 bytes[AFI_SIZE + 1] = s; // +1 = reserved
134                 return bytes;
135         }
136
137         private static byte[] putAS4BytesParameterValue(final AS4BytesCapability param) {
138                 return ByteArray.subByte(ByteArray.longToBytes(param.getASNumber().getValue()), 4, 4);
139         }
140
141         private static MultiprotocolCapability parseMultiProtocolParameterValue(final byte[] bytes) throws BGPParsingException {
142                 final Class<? extends AddressFamily> afi = ParserUtil.afiForValue(ByteArray.bytesToInt(ByteArray.subByte(bytes, 0, AFI_SIZE)));
143                 if (afi == null) {
144                         throw new BGPParsingException("Address Family Identifier: '" + ByteArray.bytesToInt(ByteArray.subByte(bytes, 0, AFI_SIZE))
145                                         + "' not supported.");
146                 }
147                 final Class<? extends SubsequentAddressFamily> safi = ParserUtil.safiForValue(ByteArray.bytesToInt(ByteArray.subByte(bytes,
148                                 AFI_SIZE + 1, SAFI_SIZE)));
149                 if (safi == null) {
150                         throw new BGPParsingException("Subsequent Address Family Identifier: '"
151                                         + ByteArray.bytesToInt(ByteArray.subByte(bytes, AFI_SIZE + 1, SAFI_SIZE)) + "' not supported.");
152                 }
153                 return new MultiprotocolCapability(new BGPTableType(afi, safi));
154         }
155
156         private static AS4BytesCapability parseAS4BParameterValue(final byte[] bytes) {
157                 return new AS4BytesCapability(new AsNumber(ByteArray.bytesToLong(bytes)));
158         }
159
160         private static byte[] putAfi(final Class<? extends AddressFamily> afi) {
161                 final byte[] a = ByteArray.intToBytes(ParserUtil.valueForAfi(afi));
162                 return ByteArray.subByte(a, Integer.SIZE / Byte.SIZE - AFI_SIZE, AFI_SIZE);
163         }
164
165         private static byte putSafi(final Class<? extends SubsequentAddressFamily> safi) {
166                 final byte[] a = ByteArray.intToBytes(ParserUtil.valueForSafi(safi));
167                 return ByteArray.subByte(a, Integer.SIZE / Byte.SIZE - SAFI_SIZE, SAFI_SIZE)[0];
168         }
169 }