2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.parser.spi.pojo;
10 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
11 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
12 import org.opendaylight.protocol.bgp.parser.spi.ParameterParser;
13 import org.opendaylight.protocol.bgp.parser.spi.ParameterRegistry;
14 import org.opendaylight.protocol.bgp.parser.spi.ParameterSerializer;
15 import org.opendaylight.protocol.concepts.HandlerRegistry;
16 import org.opendaylight.protocol.util.Util;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.open.BgpParameters;
18 import org.opendaylight.yangtools.yang.binding.DataContainer;
20 import com.google.common.base.Preconditions;
22 final class SimpleParameterRegistry implements ParameterRegistry {
23 private final HandlerRegistry<DataContainer, ParameterParser, ParameterSerializer> handlers = new HandlerRegistry<>();
25 AutoCloseable registerParameterParser(final int messageType, final ParameterParser parser) {
26 Preconditions.checkArgument(messageType >= 0 && messageType <= Util.UNSIGNED_BYTE_MAX_VALUE);
27 return this.handlers.registerParser(messageType, parser);
30 AutoCloseable registerParameterSerializer(final Class<? extends BgpParameters> paramClass, final ParameterSerializer serializer) {
31 return this.handlers.registerSerializer(paramClass, serializer);
35 public BgpParameters parseParameter(final int parameterType, final byte[] bytes) throws BGPParsingException, BGPDocumentedException {
36 final ParameterParser parser = this.handlers.getParser(parameterType);
41 return parser.parseParameter(bytes);
45 public byte[] serializeParameter(final BgpParameters parameter) {
46 final ParameterSerializer serializer = this.handlers.getSerializer(parameter.getImplementedInterface());
47 if (serializer == null) {
51 return serializer.serializeParameter(parameter);