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