MVPN RFC6514 Extendend communities
[bgpcep.git] / bgp / parser-spi / src / main / java / org / opendaylight / protocol / bgp / parser / spi / pojo / SimpleBgpPrefixSidTlvRegistry.java
1 /*
2  * Copyright (c) 2016 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.spi.pojo;
9
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;
21
22 public final class SimpleBgpPrefixSidTlvRegistry implements BgpPrefixSidTlvRegistry {
23
24     private final HandlerRegistry<DataContainer, BgpPrefixSidTlvParser, BgpPrefixSidTlvSerializer> handlers = new HandlerRegistry<>();
25
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);
29     }
30
31     AutoCloseable registerBgpPrefixSidTlvSerializer(final Class<? extends BgpPrefixSidTlv> tlvClass, final BgpPrefixSidTlvSerializer serializer) {
32         return this.handlers.registerSerializer(tlvClass, serializer);
33     }
34
35     @Override
36     public BgpPrefixSidTlv parseBgpPrefixSidTlv(final int type, final ByteBuf buffer) {
37         final BgpPrefixSidTlvParser parser = this.handlers.getParser(type);
38         if (parser == null) {
39             return null;
40         }
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));
44     }
45
46     @Override
47     public void serializeBgpPrefixSidTlv(final BgpPrefixSidTlv tlv, final ByteBuf bytes) {
48         final BgpPrefixSidTlvSerializer serializer = this.handlers.getSerializer(tlv.getImplementedInterface());
49         if (serializer == null) {
50             return;
51         }
52         final ByteBuf valueBuf = Unpooled.buffer();
53         serializer.serializeBgpPrefixSidTlv(tlv, valueBuf);
54         BgpPrefixSidTlvUtil.formatBgpPrefixSidTlv(serializer.getType(), valueBuf, bytes);
55     }
56
57 }