Bump MDSAL to 4.0.0
[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 package org.opendaylight.protocol.pcep.spi.pojo;
9
10 import com.google.common.primitives.Ints;
11 import io.netty.buffer.ByteBuf;
12 import java.util.Optional;
13 import org.opendaylight.protocol.concepts.HandlerRegistry;
14 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
15 import org.opendaylight.protocol.pcep.spi.TlvParser;
16 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
17 import org.opendaylight.protocol.pcep.spi.VendorInformationTlvRegistry;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.iana.rev130816.EnterpriseNumber;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.vendor.information.EnterpriseSpecificInformation;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.vendor.information.tlvs.VendorInformationTlv;
21 import org.opendaylight.yangtools.concepts.Registration;
22 import org.opendaylight.yangtools.yang.binding.DataContainer;
23
24 public class SimpleVendorInformationTlvRegistry implements VendorInformationTlvRegistry {
25
26     private final HandlerRegistry<DataContainer, TlvParser, TlvSerializer> handlers = new HandlerRegistry<>();
27
28     public Registration registerVendorInformationTlvParser(final EnterpriseNumber enterpriseNumber,
29             final TlvParser parser) {
30         return this.handlers.registerParser(Ints.checkedCast(enterpriseNumber.getValue()), parser);
31     }
32
33     public Registration registerVendorInformationTlvSerializer(
34             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,
40             final ByteBuf buffer) throws PCEPDeserializerException {
41         final TlvParser parser = this.handlers.getParser(Ints.checkedCast(enterpriseNumber.getValue()));
42         if (parser == null) {
43             return Optional.empty();
44         }
45         return Optional.of((VendorInformationTlv) parser.parseTlv(buffer));
46     }
47
48     @Override
49     public void serializeVendorInformationTlv(final VendorInformationTlv viTlv, final ByteBuf buffer) {
50         final TlvSerializer serializer = this.handlers.getSerializer(
51             viTlv.getEnterpriseSpecificInformation().implementedInterface());
52         if (serializer == null) {
53             return;
54         }
55         serializer.serializeTlv(viTlv, buffer);
56     }
57 }