2 * Copyright (c) 2016 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 com.google.common.base.Preconditions;
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.Unpooled;
13 import org.opendaylight.protocol.bgp.parser.spi.BgpPrefixSidTlvParser;
14 import org.opendaylight.protocol.bgp.parser.spi.BgpPrefixSidTlvRegistry;
15 import org.opendaylight.protocol.bgp.parser.spi.BgpPrefixSidTlvSerializer;
16 import org.opendaylight.protocol.bgp.parser.spi.BgpPrefixSidTlvUtil;
17 import org.opendaylight.protocol.concepts.HandlerRegistry;
18 import org.opendaylight.protocol.util.Values;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.bgp.prefix.sid.bgp.prefix.sid.tlvs.BgpPrefixSidTlv;
20 import org.opendaylight.yangtools.yang.binding.DataContainer;
22 public final class SimpleBgpPrefixSidTlvRegistry implements BgpPrefixSidTlvRegistry {
24 private final HandlerRegistry<DataContainer, BgpPrefixSidTlvParser, BgpPrefixSidTlvSerializer> handlers = new HandlerRegistry<>();
26 AutoCloseable registerBgpPrefixSidTlvParser(final int tlvType, final BgpPrefixSidTlvParser parser) {
27 Preconditions.checkArgument(tlvType >= 0 && tlvType <= Values.UNSIGNED_BYTE_MAX_VALUE);
28 return this.handlers.registerParser(tlvType, parser);
31 AutoCloseable registerBgpPrefixSidTlvSerializer(final Class<? extends BgpPrefixSidTlv> tlvClass, final BgpPrefixSidTlvSerializer serializer) {
32 return this.handlers.registerSerializer(tlvClass, serializer);
36 public BgpPrefixSidTlv parseBgpPrefixSidTlv(final int type, final ByteBuf buffer) {
37 final BgpPrefixSidTlvParser parser = this.handlers.getParser(type);
41 final int length = buffer.readUnsignedShort();
42 Preconditions.checkState(length <= buffer.readableBytes(), "Length of BGP prefix SID TLV exceeds readable bytes of income.");
43 return parser.parseBgpPrefixSidTlv(buffer.readBytes(length));
47 public void serializeBgpPrefixSidTlv(final BgpPrefixSidTlv tlv, final ByteBuf bytes) {
48 final BgpPrefixSidTlvSerializer serializer = this.handlers.getSerializer(tlv.getImplementedInterface());
49 if (serializer == null) {
52 final ByteBuf valueBuf = Unpooled.buffer();
53 serializer.serializeBgpPrefixSidTlv(tlv, valueBuf);
54 BgpPrefixSidTlvUtil.formatBgpPrefixSidTlv(serializer.getType(), valueBuf, bytes);