f7d64fe2dede41f6f54c725b8f3d7cb97f7b62e2
[bgpcep.git] / pcep / spi / src / main / java / org / opendaylight / protocol / pcep / spi / pojo / SimpleObjectRegistry.java
1 /*
2  * Copyright (c) 2013 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.base.Preconditions;
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.ObjectRegistry;
16 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
17 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
18 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
19 import org.opendaylight.protocol.pcep.spi.UnknownObject;
20 import org.opendaylight.protocol.pcep.spi.VendorInformationObjectRegistry;
21 import org.opendaylight.protocol.util.Values;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.iana.rev130816.EnterpriseNumber;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vendor.information.objects.VendorInformationObject;
26 import org.opendaylight.yangtools.yang.binding.DataContainer;
27
28 public final class SimpleObjectRegistry implements ObjectRegistry {
29     private final HandlerRegistry<DataContainer, ObjectParser, ObjectSerializer> handlers = new HandlerRegistry<>();
30
31     private static final int MAX_OBJECT_TYPE = 15;
32     private static final int MAX_OBJECT_CLASS = 4;   
33     private final VendorInformationObjectRegistry viRegistry;
34
35     public SimpleObjectRegistry(final VendorInformationObjectRegistry viRegistry) {
36         this.viRegistry = viRegistry;
37
38     }
39
40     private static int createKey(final int objectClass, final int objectType) {
41         Preconditions.checkArgument(objectClass >= 0 && objectClass <= Values.UNSIGNED_BYTE_MAX_VALUE);
42         Preconditions.checkArgument(objectType >= 0 && objectType <= MAX_OBJECT_TYPE);
43         return (objectClass << MAX_OBJECT_CLASS) | objectType;
44     }
45
46     public AutoCloseable registerObjectParser(final int objectClass, final int objectType, final ObjectParser parser) {
47         Preconditions.checkArgument(objectClass >= 0 && objectClass <= Values.UNSIGNED_BYTE_MAX_VALUE, "Illegal object class %s",
48                 objectClass);
49         Preconditions.checkArgument(objectType >= 0 && objectType <= MAX_OBJECT_TYPE, "Illegal object type %s", objectType);
50         return this.handlers.registerParser(createKey(objectClass, objectType), parser);
51     }
52
53     public AutoCloseable registerObjectSerializer(final Class<? extends Object> objClass, final ObjectSerializer serializer) {
54         return this.handlers.registerSerializer(objClass, serializer);
55     }
56
57     @Override
58     public Object parseObject(final int objectClass, final int objectType, final ObjectHeader header, final ByteBuf buffer)
59             throws PCEPDeserializerException {
60         Preconditions.checkArgument(objectType >= 0 && objectType <= Values.UNSIGNED_SHORT_MAX_VALUE);
61         final ObjectParser parser = this.handlers.getParser(createKey(objectClass, objectType));
62
63         if (parser == null) {
64             if (!header.isProcessingRule()) {
65                 return null;
66             }
67             for (int type = 1; type <= MAX_OBJECT_TYPE; type++) {
68                 final ObjectParser objParser = this.handlers.getParser(createKey(objectClass, type));
69                 if (objParser != null) {
70                     return new UnknownObject(PCEPErrors.UNRECOGNIZED_OBJ_TYPE);
71                 }
72             }
73             return new UnknownObject(PCEPErrors.UNRECOGNIZED_OBJ_CLASS);
74         }
75         return parser.parseObject(header, buffer);
76     }
77
78     @Override
79     public void serializeObject(final Object object, final ByteBuf buffer) {
80         final ObjectSerializer serializer = this.handlers.getSerializer(object.getImplementedInterface());
81         if (serializer == null) {
82             return;
83         }
84         serializer.serializeObject(object, buffer);
85     }   
86     @Override
87     public Optional<? extends Object> parseVendorInformationObject(final EnterpriseNumber enterpriseNumber,
88             final ObjectHeader header, final ByteBuf buffer) throws PCEPDeserializerException {
89         return this.viRegistry.parseVendorInformationObject(enterpriseNumber, header, buffer);
90     }
91
92     @Override
93     public void serializeVendorInformationObject(final VendorInformationObject viObject, final ByteBuf buffer) {
94         this.viRegistry.serializeVendorInformationObject(viObject, buffer);
95     }
96 }