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