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 static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkState;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import org.opendaylight.protocol.bgp.parser.spi.BgpPrefixSidTlvParser;
16 import org.opendaylight.protocol.bgp.parser.spi.BgpPrefixSidTlvRegistry;
17 import org.opendaylight.protocol.bgp.parser.spi.BgpPrefixSidTlvSerializer;
18 import org.opendaylight.protocol.bgp.parser.spi.BgpPrefixSidTlvUtil;
19 import org.opendaylight.protocol.concepts.HandlerRegistry;
20 import org.opendaylight.protocol.util.Values;
21 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;
22 import org.opendaylight.yangtools.concepts.Registration;
23 import org.opendaylight.yangtools.yang.binding.DataContainer;
25 public final class SimpleBgpPrefixSidTlvRegistry implements BgpPrefixSidTlvRegistry {
27 private final HandlerRegistry<DataContainer, BgpPrefixSidTlvParser, BgpPrefixSidTlvSerializer> handlers =
28 new HandlerRegistry<>();
30 Registration registerBgpPrefixSidTlvParser(final int tlvType, final BgpPrefixSidTlvParser parser) {
31 checkArgument(tlvType >= 0 && tlvType <= Values.UNSIGNED_BYTE_MAX_VALUE);
32 return this.handlers.registerParser(tlvType, parser);
35 Registration registerBgpPrefixSidTlvSerializer(final Class<? extends BgpPrefixSidTlv> tlvClass,
36 final BgpPrefixSidTlvSerializer serializer) {
37 return this.handlers.registerSerializer(tlvClass, serializer);
41 public BgpPrefixSidTlv parseBgpPrefixSidTlv(final int type, final ByteBuf buffer) {
42 final BgpPrefixSidTlvParser parser = this.handlers.getParser(type);
46 final int length = buffer.readUnsignedShort();
47 checkState(length <= buffer.readableBytes(),
48 "Length of BGP prefix SID TLV exceeds readable bytes of income.");
49 return parser.parseBgpPrefixSidTlv(buffer.readBytes(length));
53 public void serializeBgpPrefixSidTlv(final BgpPrefixSidTlv tlv, final ByteBuf bytes) {
54 final BgpPrefixSidTlvSerializer serializer = this.handlers.getSerializer(tlv.implementedInterface());
55 if (serializer == null) {
58 final ByteBuf valueBuf = Unpooled.buffer();
59 serializer.serializeBgpPrefixSidTlv(tlv, valueBuf);
60 BgpPrefixSidTlvUtil.formatBgpPrefixSidTlv(serializer.getType(), valueBuf, bytes);