Merge "Remove bgp-update-api-config"
[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 org.opendaylight.protocol.concepts.HandlerRegistry;
11 import org.opendaylight.protocol.pcep.spi.ObjectParser;
12 import org.opendaylight.protocol.pcep.spi.ObjectRegistry;
13 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
14 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
15 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
16 import org.opendaylight.protocol.pcep.spi.UnknownObject;
17 import org.opendaylight.protocol.util.Values;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
20 import org.opendaylight.yangtools.yang.binding.DataContainer;
21
22 import com.google.common.base.Preconditions;
23
24 /**
25  *
26  */
27 public final class SimpleObjectRegistry implements ObjectRegistry {
28         private final HandlerRegistry<DataContainer, ObjectParser, ObjectSerializer> handlers = new HandlerRegistry<>();
29
30         private static int createKey(final int objectClass, final int objectType) {
31                 Preconditions.checkArgument(objectClass >= 0 && objectClass <= Values.UNSIGNED_BYTE_MAX_VALUE);
32                 Preconditions.checkArgument(objectType >= 0 && objectType <= 15);
33                 return (objectClass << 4) | objectType;
34         }
35
36         public AutoCloseable registerObjectParser(final int objectClass, final int objectType, final ObjectParser parser) {
37                 Preconditions.checkArgument(objectClass >= 0 && objectClass <= Values.UNSIGNED_BYTE_MAX_VALUE, "Illagal object class %s", objectClass);
38                 Preconditions.checkArgument(objectType >= 0 && objectType <= 15, "Illegal object type %s", objectType);
39                 return this.handlers.registerParser(createKey(objectClass, objectType), parser);
40         }
41
42         public AutoCloseable registerObjectSerializer(final Class<? extends Object> objClass, final ObjectSerializer serializer) {
43                 return this.handlers.registerSerializer(objClass, serializer);
44         }
45
46         @Override
47         public Object parseObject(int objectClass, int objectType, ObjectHeader header, byte[] buffer) throws PCEPDeserializerException {
48                 Preconditions.checkArgument(objectType >= 0 && objectType <= Values.UNSIGNED_SHORT_MAX_VALUE);
49                 final ObjectParser parser = this.handlers.getParser(createKey(objectClass, objectType));
50
51                 if (parser == null) {
52                     if(!header.isProcessingRule()) {
53                         return null;
54                     }
55
56                         final boolean foundClass = false;
57
58                         // FIXME: BUG-187: search the parsers, check classes
59
60                         if (!foundClass) {
61                                 return new UnknownObject(PCEPErrors.UNRECOGNIZED_OBJ_CLASS);
62                         } else {
63                                 return new UnknownObject(PCEPErrors.UNRECOGNIZED_OBJ_TYPE);
64                         }
65                 }
66                 return parser.parseObject(header, buffer);
67         }
68
69         @Override
70         public byte[] serializeObject(Object object) {
71                 final ObjectSerializer serializer = this.handlers.getSerializer(object.getImplementedInterface());
72                 if (serializer == null) {
73                         return null;
74                 }
75                 return serializer.serializeObject(object);
76         }
77 }