import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
import org.opendaylight.protocol.bgp.parser.BGPError;
import org.opendaylight.protocol.bgp.parser.BGPParsingException;
-import org.opendaylight.protocol.bgp.parser.impl.message.open.BGPParameterParser;
+import org.opendaylight.protocol.bgp.parser.impl.message.open.SimpleParameterRegistry;
import org.opendaylight.protocol.bgp.parser.spi.MessageParser;
import org.opendaylight.protocol.bgp.parser.spi.MessageSerializer;
import org.opendaylight.protocol.bgp.parser.spi.MessageUtil;
+import org.opendaylight.protocol.bgp.parser.spi.ParameterRegistry;
import org.opendaylight.protocol.concepts.Ipv4Util;
import org.opendaylight.protocol.util.ByteArray;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
private static final int BGP_VERSION = 4;
+ @Deprecated
+ private static final ParameterRegistry reg = SimpleParameterRegistry.INSTANCE;
+
private BGPOpenMessageParser() {
}
if (open.getBgpParameters() != null) {
for (final BgpParameters param : open.getBgpParameters()) {
- final byte[] p = BGPParameterParser.put(param);
- optParams.put(p, p.length);
- optParamsLength += p.length;
+ final byte[] p = reg.serializeParameter(param);
+ if (p != null) {
+ optParams.put(p, p.length);
+ optParamsLength += p.length;
+ }
}
}
List<BgpParameters> optParams = Lists.newArrayList();
if (optLength > 0) {
+ fillParams(ByteArray.subByte(body, MIN_MSG_LENGTH, optLength), optParams);
+ }
+ logger.trace("Open message was parsed: AS = {}, holdTimer = {}, bgpId = {}, optParams = {}", as, holdTime, bgpId, optParams);
+ return new OpenBuilder().setMyAsNumber(as.getValue().intValue()).setHoldTimer((int) holdTime).setBgpIdentifier(bgpId).setBgpParameters(
+ optParams).build();
+ }
+
+ private void fillParams(final byte[] bytes, final List<BgpParameters> params) throws BGPDocumentedException {
+ if (bytes == null || bytes.length == 0) {
+ throw new IllegalArgumentException("Byte array cannot be null or empty.");
+ }
+
+ logger.trace("Started parsing of BGP parameter: {}", Arrays.toString(bytes));
+ int byteOffset = 0;
+ while (byteOffset < bytes.length) {
+ if (byteOffset + 2 >= bytes.length) {
+ // FIXME: throw a BGPDocumentedException here?
+ throw new IllegalArgumentException("Malformed parameter encountered (" + (bytes.length - byteOffset) + " bytes left)");
+ }
+
+ final int paramType = UnsignedBytes.toInt(bytes[byteOffset++]);
+ final int paramLength = UnsignedBytes.toInt(bytes[byteOffset++]);
+ final byte[] paramBody = ByteArray.subByte(bytes, byteOffset, paramLength);
+ byteOffset += paramLength;
+
+ final BgpParameters param;
try {
- optParams = BGPParameterParser.parse(ByteArray.subByte(body, MIN_MSG_LENGTH, optLength));
+ param = reg.parseParameter(paramType, paramBody);
} catch (final BGPParsingException e) {
throw new BGPDocumentedException("Optional parameter not parsed: ." + e.getMessage(), BGPError.UNSPECIFIC_OPEN_ERROR);
}
+
+ if (param != null) {
+ params.add(param);
+ } else {
+ logger.debug("Ignoring BGP Parameter type: {}", paramType);
+ }
}
- logger.trace("Open message was parsed: AS = {}, holdTimer = {}, bgpId = {}, optParams = {}", as, holdTime, bgpId, optParams);
- return new OpenBuilder().setMyAsNumber(as.getValue().intValue()).setHoldTimer((int) holdTime).setBgpIdentifier(bgpId).setBgpParameters(
- optParams).build();
+
+ logger.trace("Parsed BGP parameters: {}", Arrays.toString(params.toArray()));
}
}
+++ /dev/null
-/*
- * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.protocol.bgp.parser.impl.message.open;
-
-import java.util.Arrays;
-import java.util.List;
-
-import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
-import org.opendaylight.protocol.bgp.parser.BGPParsingException;
-import org.opendaylight.protocol.util.ByteArray;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.open.BgpParameters;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.open.BgpParametersBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.open.bgp.parameters.CParameters;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.google.common.collect.Lists;
-import com.google.common.primitives.UnsignedBytes;
-
-/**
- * Parser for parameters in BGP Open message.
- */
-public final class BGPParameterParser {
-
- private static final Logger logger = LoggerFactory.getLogger(BGPParameterParser.class);
-
- private static final int TYPE_SIZE = 1; // bytes
- private static final int LENGTH_SIZE = 1; // bytes
- private static final int CAPABILITIES_OPT_PARAM_TYPE = 2;
-
- private BGPParameterParser() {
-
- }
-
- /**
- * Serializes given BGP Parameter to byte array. Currently supported only Capability parameters.
- *
- * @param param BGP Parameter to be serialized
- * @return BGP Parameter converted to byte array
- */
- public static byte[] put(final BgpParameters param) {
- if (param == null) {
- throw new IllegalArgumentException("BGP Parameter cannot be null");
- }
- logger.trace("Started serializing BGPParameter: {}", param);
-
- byte[] value = null;
-
- value = CapabilityParameterParser.put(param.getCParameters());
-
- if (value == null) {
- logger.debug("BGP Parameter not supported.");
- return new byte[] {};
- }
-
- final byte[] bytes = new byte[TYPE_SIZE + LENGTH_SIZE + value.length];
- System.arraycopy(ByteArray.intToBytes(2), 3, bytes, 0, TYPE_SIZE);
- System.arraycopy(ByteArray.intToBytes(value.length), 3, bytes, TYPE_SIZE, LENGTH_SIZE);
- System.arraycopy(value, 0, bytes, TYPE_SIZE + LENGTH_SIZE, value.length);
- logger.trace("BGP Parameter serialized to: {}", Arrays.toString(bytes));
- return bytes;
- }
-
- /**
- * Parses given byte array to a list of BGP Parameters. Currently supporting only Capability parameters.
- *
- * @param bytes byte array representing BGP Parameters
- * @return list of BGP Parameters
- * @throws BGPParsingException if the parsing was unsuccessful
- * @throws BGPDocumentedException
- */
- public static List<BgpParameters> parse(final byte[] bytes) throws BGPParsingException, BGPDocumentedException {
- if (bytes == null || bytes.length == 0) {
- throw new IllegalArgumentException("Byte array cannot be null or empty.");
- }
- logger.trace("Started parsing of BGP parameter: {}", Arrays.toString(bytes));
- int byteOffset = 0;
- final List<BgpParameters> params = Lists.newArrayList();
- while (byteOffset < bytes.length) {
- final int paramType = UnsignedBytes.toInt(bytes[byteOffset++]);
- final int paramLength = UnsignedBytes.toInt(bytes[byteOffset++]);
- if (paramType == CAPABILITIES_OPT_PARAM_TYPE) {
- final CParameters cparam = CapabilityParameterParser.parse(ByteArray.subByte(bytes, byteOffset, paramLength));
- if (cparam == null) {
- byteOffset += paramLength;
- continue;
- }
- final BgpParameters param = new BgpParametersBuilder().setCParameters(cparam).build();
- if (param != null) {
- params.add(param);
- }
- } else {
- logger.debug("BGP Parameter not recognized. Type: {}", paramType);
- }
- byteOffset += paramLength;
- }
- logger.trace("Parsed BGP parameters: {}", Arrays.toString(params.toArray()));
- return params;
- }
-}
import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
import org.opendaylight.protocol.bgp.parser.BGPParsingException;
+import org.opendaylight.protocol.bgp.parser.spi.CapabilityRegistry;
+import org.opendaylight.protocol.bgp.parser.spi.ParameterParser;
+import org.opendaylight.protocol.bgp.parser.spi.ParameterSerializer;
+import org.opendaylight.protocol.bgp.parser.spi.ParameterUtil;
import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.open.BgpParameters;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.open.BgpParametersBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.open.bgp.parameters.CParameters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import com.google.common.base.Preconditions;
import com.google.common.primitives.UnsignedBytes;
/**
* Parser for BGP Capability Parameter.
*/
-public final class CapabilityParameterParser {
+public final class CapabilityParameterParser implements ParameterParser, ParameterSerializer {
+ public static final int TYPE = 2;
private static final Logger logger = LoggerFactory.getLogger(CapabilityParameterParser.class);
+ private final CapabilityRegistry reg;
- private CapabilityParameterParser() {
-
- }
-
- /**
- * Serializes given BGP Capability Parameter to byte array.
- *
- * @param param BGP Capability to be serialized
- * @return BGP Capability converted to byte array
- */
- public static byte[] put(final CParameters cap) {
- if (cap == null) {
- throw new IllegalArgumentException("BGP Capability cannot be null");
- }
- logger.trace("Started serializing BGP Capability: {}", cap);
-
- byte[] bytes = SimpleCapabilityRegistry.INSTANCE.serializeCapability(cap);
- if (bytes == null) {
- throw new IllegalArgumentException("Unhandled capability " + cap);
- }
-
- logger.trace("BGP Parameter serialized to: {}", Arrays.toString(bytes));
- return bytes;
+ public CapabilityParameterParser(final CapabilityRegistry reg) {
+ this.reg = Preconditions.checkNotNull(reg);
}
- /**
- * Parses given byte array to Capability Parameter. Only Multiprotocol capability is supported.
- *
- * @param bytes byte array representing BGP Parameters
- * @return list of BGP Parameters
- * @throws BGPParsingException if the parsing was unsuccessful
- * @throws BGPDocumentedException
- */
- public static CParameters parse(final byte[] bytes) throws BGPParsingException, BGPDocumentedException {
+ @Override
+ public BgpParameters parseParameter(final byte[] bytes) throws BGPParsingException, BGPDocumentedException {
if (bytes == null || bytes.length == 0) {
throw new IllegalArgumentException("Byte array cannot be null or empty.");
}
final int capLength = UnsignedBytes.toInt(bytes[byteOffset++]);
final byte[] paramBody = ByteArray.subByte(bytes, byteOffset, capLength);
- final CParameters ret = SimpleCapabilityRegistry.INSTANCE.parseCapability(capCode, paramBody);
+ final CParameters ret = reg.parseCapability(capCode, paramBody);
if (ret == null) {
logger.debug("Ignoring unsupported capability {}", capCode);
+ return null;
}
- return ret;
+
+ return new BgpParametersBuilder().setCParameters(ret).build();
+ }
+
+ @Override
+ public byte[] serializeParameter(final BgpParameters parameter) {
+ final CParameters cap = parameter.getCParameters();
+
+ logger.trace("Started serializing BGP Capability: {}", cap);
+
+ byte[] bytes = reg.serializeCapability(cap);
+ if (bytes == null) {
+ throw new IllegalArgumentException("Unhandled capability class" + cap.getImplementedInterface());
+ }
+
+ logger.trace("BGP capability serialized to: {}", Arrays.toString(bytes));
+
+ return ParameterUtil.formatParameter(TYPE, bytes);
}
}
*/
package org.opendaylight.protocol.bgp.parser.impl.message.open;
+import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
+import org.opendaylight.protocol.bgp.parser.BGPParsingException;
import org.opendaylight.protocol.bgp.parser.spi.ParameterParser;
import org.opendaylight.protocol.bgp.parser.spi.ParameterRegistry;
import org.opendaylight.protocol.bgp.parser.spi.ParameterSerializer;
import com.google.common.base.Preconditions;
-public final class ParameterRegistryImpl extends HandlerRegistry<DataContainer, ParameterParser, ParameterSerializer> implements ParameterRegistry {
+public final class SimpleParameterRegistry implements ParameterRegistry {
public static final ParameterRegistry INSTANCE;
static {
- final ParameterRegistry reg = new ParameterRegistryImpl();
+ final ParameterRegistry reg = new SimpleParameterRegistry();
- // FIXME: fix registry
+ final CapabilityParameterParser cpp = new CapabilityParameterParser(SimpleCapabilityRegistry.INSTANCE);
+ reg.registerParameterParser(CapabilityParameterParser.TYPE, cpp);
+ reg.registerParameterSerializer(BgpParameters.class, cpp);
INSTANCE = reg;
}
+ private final HandlerRegistry<DataContainer, ParameterParser, ParameterSerializer> handlers = new HandlerRegistry<>();
+
@Override
public AutoCloseable registerParameterParser(final int messageType, final ParameterParser parser) {
Preconditions.checkArgument(messageType >= 0 && messageType <= 255);
- return super.registerParser(messageType, parser);
+ return handlers.registerParser(messageType, parser);
}
@Override
- public ParameterParser getParameterParser(final int messageType) {
- return super.getParser(messageType);
+ public AutoCloseable registerParameterSerializer(final Class<? extends BgpParameters> paramClass, final ParameterSerializer serializer) {
+ return handlers.registerSerializer(paramClass, serializer);
}
@Override
- public AutoCloseable registerParameterSerializer(final Class<? extends BgpParameters> paramClass, final ParameterSerializer serializer) {
- return super.registerSerializer(paramClass, serializer);
+ public BgpParameters parseParameter(final int parameterType, final byte[] bytes) throws BGPParsingException, BGPDocumentedException {
+ final ParameterParser parser = handlers.getParser(parameterType);
+ if (parser == null) {
+ return null;
+ }
+
+ return parser.parseParameter(bytes);
}
@Override
- public ParameterSerializer getParameterSerializer(final BgpParameters message) {
- return super.getSerializer(message.getImplementedInterface());
+ public byte[] serializeParameter(final BgpParameters parameter) {
+ final ParameterSerializer serializer = handlers.getSerializer(parameter.getImplementedInterface());
+ if (serializer == null) {
+ return null;
+ }
+
+ return serializer.serializeParameter(parameter);
}
}
import org.opendaylight.protocol.bgp.parser.BGPError;
import org.opendaylight.protocol.bgp.parser.BGPParsingException;
import org.opendaylight.protocol.bgp.parser.impl.message.update.AsPathSegmentParser.SegmentType;
+import org.opendaylight.protocol.bgp.parser.spi.AttributeRegistry;
import org.opendaylight.protocol.concepts.IPv4;
import org.opendaylight.protocol.concepts.Ipv4Util;
import org.opendaylight.protocol.util.ByteArray;
private static final int TYPE_LENGTH = 1;
+ private static AttributeRegistry reg = SimpleAttributeRegistry.INSTANCE;
+
private PathAttributeParser() {
}
final byte[] attrBody = ByteArray.subByte(bytes, hdrLength, attrLength);
- boolean found = SimpleAttributeRegistry.INSTANCE.parseAttribute(UnsignedBytes.toInt(bytes[1]), attrBody, builder);
+ boolean found = reg.parseAttribute(UnsignedBytes.toInt(bytes[1]), attrBody, builder);
if (!optional && !found) {
throw new BGPDocumentedException("Well known attribute not recognized.", BGPError.WELL_KNOWN_ATTR_NOT_RECOGNIZED);
}
*/
package org.opendaylight.protocol.bgp.parser.spi;
+import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
import org.opendaylight.protocol.bgp.parser.BGPParsingException;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.open.BgpParameters;
public interface ParameterParser {
- public BgpParameters parseParameter(final byte[] bytes) throws BGPParsingException;
+ public BgpParameters parseParameter(final byte[] bytes) throws BGPParsingException, BGPDocumentedException;
}
*/
package org.opendaylight.protocol.bgp.parser.spi;
+import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
+import org.opendaylight.protocol.bgp.parser.BGPParsingException;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.open.BgpParameters;
public interface ParameterRegistry {
public AutoCloseable registerParameterParser(int parameterType, ParameterParser parser);
- public ParameterParser getParameterParser(int parameterType);
-
public AutoCloseable registerParameterSerializer(Class<? extends BgpParameters> paramClass, ParameterSerializer serializer);
- public ParameterSerializer getParameterSerializer(BgpParameters object);
+
+ public BgpParameters parseParameter(int parameterType, final byte[] bytes) throws BGPParsingException, BGPDocumentedException;
+ public byte[] serializeParameter(final BgpParameters parameter);
}
--- /dev/null
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.protocol.bgp.parser.spi;
+
+import com.google.common.primitives.UnsignedBytes;
+
+public final class ParameterUtil {
+ private ParameterUtil() {
+
+ }
+
+ public static byte[] formatParameter(final int type, final byte[] value) {
+ final byte[] bytes = new byte[2 + value.length];
+ bytes[0] = UnsignedBytes.checkedCast(type);
+ bytes[1] = UnsignedBytes.checkedCast(value.length);
+ System.arraycopy(value, 0, bytes, 2, value.length);
+ return bytes;
+ }
+}