2 * Copyright (c) 2014 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.pcep.spi.pojo;
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.ObjectParser;
15 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
16 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
17 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
18 import org.opendaylight.protocol.pcep.spi.UnknownObject;
19 import org.opendaylight.protocol.pcep.spi.VendorInformationObjectRegistry;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.iana.rev130816.EnterpriseNumber;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.vendor.information.EnterpriseSpecificInformation;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.vendor.information.objects.VendorInformationObject;
25 import org.opendaylight.yangtools.yang.binding.DataContainer;
27 public class SimpleVendorInformationObjectRegistry implements VendorInformationObjectRegistry {
28 private final HandlerRegistry<DataContainer, ObjectParser, ObjectSerializer> handlers = new HandlerRegistry<>();
30 public AutoCloseable registerVendorInformationObjectParser(final EnterpriseNumber enterpriseNumber,
31 final ObjectParser parser) {
32 return this.handlers.registerParser(Ints.checkedCast(enterpriseNumber.getValue()), parser);
35 public AutoCloseable registerVendorInformationObjectSerializer(
36 final Class<? extends EnterpriseSpecificInformation> esInformationClass,
37 final ObjectSerializer serializer) {
38 return this.handlers.registerSerializer(esInformationClass, serializer);
42 public Optional<? extends Object> parseVendorInformationObject(final EnterpriseNumber enterpriseNumber,
43 final ObjectHeader header, final ByteBuf buffer)
44 throws PCEPDeserializerException {
45 final ObjectParser parser = this.handlers.getParser(Ints.checkedCast(enterpriseNumber.getValue()));
47 if (!header.isProcessingRule()) {
48 return Optional.empty();
50 return Optional.of(new UnknownObject(PCEPErrors.UNRECOGNIZED_OBJ_CLASS));
52 return Optional.of(parser.parseObject(header, buffer));
56 public void serializeVendorInformationObject(final VendorInformationObject viObject, final ByteBuf buffer) {
57 final ObjectSerializer serializer = this.handlers.getSerializer(
58 viObject.getEnterpriseSpecificInformation().getImplementedInterface());
59 if (serializer == null) {
62 serializer.serializeObject(viObject, buffer);