Bug-479: Implementation of Vendor-Information TLV
[bgpcep.git] / pcep / spi / src / main / java / org / opendaylight / protocol / pcep / spi / pojo / SimpleVendorInformationTlvRegistry.java
1 /*
2  * Copyright (c) 2014 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
9 package org.opendaylight.protocol.pcep.spi.pojo;
10
11 import com.google.common.base.Optional;
12 import com.google.common.primitives.Ints;
13
14 import io.netty.buffer.ByteBuf;
15
16 import org.opendaylight.protocol.concepts.HandlerRegistry;
17 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
18 import org.opendaylight.protocol.pcep.spi.TlvParser;
19 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
20 import org.opendaylight.protocol.pcep.spi.VendorInformationTlvRegistry;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.iana.rev130816.EnterpriseNumber;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vendor.information.EnterpriseSpecificInformation;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vendor.information.tlvs.VendorInformationTlv;
24 import org.opendaylight.yangtools.yang.binding.DataContainer;
25
26 public class SimpleVendorInformationTlvRegistry implements VendorInformationTlvRegistry {
27
28     private final HandlerRegistry<DataContainer, TlvParser, TlvSerializer> handlers = new HandlerRegistry<>();
29
30     public AutoCloseable registerVendorInformationTlvParser(final EnterpriseNumber enterpriseNumber, final TlvParser parser) {
31         return this.handlers.registerParser(Ints.checkedCast(enterpriseNumber.getValue()), parser);
32     }
33
34     public AutoCloseable registerVendorInformationTlvSerializer(final Class<? extends EnterpriseSpecificInformation> esInformationClass, final TlvSerializer serializer) {
35         return this.handlers.registerSerializer(esInformationClass, serializer);
36     }
37
38     @Override
39     public Optional<VendorInformationTlv> parseVendorInformationTlv(final EnterpriseNumber enterpriseNumber, final ByteBuf buffer) throws PCEPDeserializerException {
40         final TlvParser parser = this.handlers.getParser(Ints.checkedCast(enterpriseNumber.getValue()));
41         if (parser == null) {
42             return Optional.absent();
43         }
44         return Optional.of((VendorInformationTlv) parser.parseTlv(buffer));
45     }
46
47     @Override
48     public void serializeVendorInformationTlv(final VendorInformationTlv viTlv, final ByteBuf buffer) {
49         final TlvSerializer serializer = this.handlers.getSerializer(viTlv.getEnterpriseSpecificInformation().getImplementedInterface());
50         if (serializer == null) {
51             return;
52         }
53         serializer.serializeTlv(viTlv, buffer);
54     }
55
56
57 }