8ba809e410dcd49022577cc564b8fa6aae47547a
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / BGPOpenMessageParser.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;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.Preconditions;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.ByteBufUtil;
15 import io.netty.buffer.Unpooled;
16 import java.util.ArrayList;
17 import java.util.List;
18 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
19 import org.opendaylight.protocol.bgp.parser.BGPError;
20 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
21 import org.opendaylight.protocol.bgp.parser.spi.MessageParser;
22 import org.opendaylight.protocol.bgp.parser.spi.MessageSerializer;
23 import org.opendaylight.protocol.bgp.parser.spi.MessageUtil;
24 import org.opendaylight.protocol.bgp.parser.spi.ParameterRegistry;
25 import org.opendaylight.protocol.util.Ipv4Util;
26 import org.opendaylight.protocol.util.Values;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.Open;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.OpenBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.open.message.BgpParameters;
32 import org.opendaylight.yangtools.yang.binding.Notification;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * Parser for BGP Open message.
38  */
39 public final class BGPOpenMessageParser implements MessageParser, MessageSerializer {
40
41     private static final Logger LOG = LoggerFactory.getLogger(BGPOpenMessageParser.class);
42
43     public static final int TYPE = 1;
44
45     private static final int VERSION_SIZE = 1;
46     private static final int AS_SIZE = 2;
47     private static final int HOLD_TIME_SIZE = 2;
48     private static final int BGP_ID_SIZE = 4;
49     private static final int OPT_PARAM_LENGTH_SIZE = 1;
50
51     private static final int MIN_MSG_LENGTH = VERSION_SIZE + AS_SIZE + HOLD_TIME_SIZE + BGP_ID_SIZE + OPT_PARAM_LENGTH_SIZE;
52
53     private static final int BGP_VERSION = 4;
54
55     public static final int AS_TRANS = 23456;
56
57     private final ParameterRegistry reg;
58
59     public BGPOpenMessageParser(final ParameterRegistry reg) {
60         this.reg = requireNonNull(reg);
61     }
62
63     /**
64      * Serializes given BGP Open message to byte array, without the header.
65      *
66      * @param msg BGP Open message to be serialized.
67      * @param bytes ByteBuf where the message will be serialized
68      */
69     @Override
70     public void serializeMessage(final Notification msg, final ByteBuf bytes) {
71         Preconditions.checkArgument(msg instanceof Open, "Message needs to be of type Open");
72         final Open open = (Open) msg;
73         final ByteBuf msgBody = Unpooled.buffer();
74
75         msgBody.writeByte(BGP_VERSION);
76
77         // When our AS number does not fit into two bytes, we report it as AS_TRANS
78         int openAS = open.getMyAsNumber();
79         if (openAS > Values.UNSIGNED_SHORT_MAX_VALUE) {
80             openAS = AS_TRANS;
81         }
82         msgBody.writeShort(openAS);
83         msgBody.writeShort(open.getHoldTimer());
84         msgBody.writeBytes(Ipv4Util.bytesForAddress(open.getBgpIdentifier()));
85
86         final ByteBuf paramsBuffer = Unpooled.buffer();
87         if (open.getBgpParameters() != null) {
88             for (final BgpParameters param : open.getBgpParameters()) {
89                 this.reg.serializeParameter(param, paramsBuffer);
90             }
91         }
92         msgBody.writeByte(paramsBuffer.writerIndex());
93         msgBody.writeBytes(paramsBuffer);
94
95         MessageUtil.formatMessage(TYPE, msgBody, bytes);
96     }
97
98     /**
99      * Parses given byte array to BGP Open message
100      *
101      * @param body byte array representing BGP Open message, without header
102      * @param messageLength the length of the message
103      * @return {@link Open} BGP Open Message
104      * @throws BGPDocumentedException if the parsing was unsuccessful
105      */
106     @Override
107     public Open parseMessageBody(final ByteBuf body, final int messageLength) throws BGPDocumentedException {
108         Preconditions.checkArgument(body != null, "Buffer cannot be null.");
109
110         if (body.readableBytes() < MIN_MSG_LENGTH) {
111             throw BGPDocumentedException.badMessageLength("Open message too small.", messageLength);
112         }
113         final int version = body.readUnsignedByte();
114         if (version != BGP_VERSION) {
115             throw new BGPDocumentedException("BGP Protocol version " + version + " not supported.", BGPError.VERSION_NOT_SUPPORTED);
116         }
117         final AsNumber as = new AsNumber((long) body.readUnsignedShort());
118         final int holdTime = body.readUnsignedShort();
119         if (holdTime == 1 || holdTime == 2) {
120             throw new BGPDocumentedException("Hold time value not acceptable.", BGPError.HOLD_TIME_NOT_ACC);
121         }
122         Ipv4Address bgpId = null;
123         try {
124             bgpId = Ipv4Util.addressForByteBuf(body);
125         } catch (final IllegalArgumentException e) {
126             throw new BGPDocumentedException("BGP Identifier is not a valid IPv4 Address", BGPError.BAD_BGP_ID, e);
127         }
128         final int optLength = body.readUnsignedByte();
129
130         final List<BgpParameters> optParams = new ArrayList<>();
131         if (optLength > 0) {
132             fillParams(body.slice(), optParams);
133         }
134         LOG.debug("BGP Open message was parsed: AS = {}, holdTimer = {}, bgpId = {}, optParams = {}", as, holdTime, bgpId, optParams);
135         return new OpenBuilder().setMyAsNumber(as.getValue().intValue()).setHoldTimer(holdTime).setBgpIdentifier(bgpId).setBgpParameters(
136             optParams).build();
137     }
138
139     private void fillParams(final ByteBuf buffer, final List<BgpParameters> params) throws BGPDocumentedException {
140         Preconditions.checkArgument(buffer != null && buffer.isReadable(), "BUffer cannot be null or empty.");
141         if (LOG.isTraceEnabled()) {
142             LOG.trace("Started parsing of BGP parameter: {}", ByteBufUtil.hexDump(buffer));
143         }
144         while (buffer.isReadable()) {
145             if (buffer.readableBytes() <= 2) {
146                 throw new BGPDocumentedException("Malformed parameter encountered (" + buffer.readableBytes() + " bytes left)", BGPError.OPT_PARAM_NOT_SUPPORTED);
147             }
148             final int paramType = buffer.readUnsignedByte();
149             final int paramLength = buffer.readUnsignedByte();
150             final ByteBuf paramBody = buffer.readSlice(paramLength);
151
152             final BgpParameters param;
153             try {
154                 param = this.reg.parseParameter(paramType, paramBody);
155             } catch (final BGPParsingException e) {
156                 throw new BGPDocumentedException("Optional parameter not parsed", BGPError.UNSPECIFIC_OPEN_ERROR, e);
157             }
158             if (param != null) {
159                 params.add(param);
160             } else {
161                 LOG.debug("Ignoring BGP Parameter type: {}", paramType);
162             }
163         }
164         LOG.trace("Parsed BGP parameters: {}", params);
165     }
166 }