7b0775c620bf9f9b5ac7a81bcb4bba335140346c
[bgpcep.git] / pcep / spi / src / main / java / org / opendaylight / protocol / pcep / spi / pojo / SimpleVendorInformationObjectRegistry.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.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;
26
27 public class SimpleVendorInformationObjectRegistry implements VendorInformationObjectRegistry {
28     private final HandlerRegistry<DataContainer, ObjectParser, ObjectSerializer> handlers = new HandlerRegistry<>();
29
30     public AutoCloseable registerVendorInformationObjectParser(final EnterpriseNumber enterpriseNumber,
31             final ObjectParser parser) {
32         return this.handlers.registerParser(Ints.checkedCast(enterpriseNumber.getValue()), parser);
33     }
34
35     public AutoCloseable registerVendorInformationObjectSerializer(
36             final Class<? extends EnterpriseSpecificInformation> esInformationClass,
37             final ObjectSerializer serializer) {
38         return this.handlers.registerSerializer(esInformationClass, serializer);
39     }
40
41     @Override
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()));
46         if (parser == null) {
47             if (!header.isProcessingRule()) {
48                 return Optional.empty();
49             }
50             return Optional.of(new UnknownObject(PCEPErrors.UNRECOGNIZED_OBJ_CLASS));
51         }
52         return Optional.of(parser.parseObject(header, buffer));
53     }
54
55     @Override
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) {
60             return;
61         }
62         serializer.serializeObject(viObject, buffer);
63     }
64 }