7452e375783dc9402778d6222c54a8afd7a9d8e1
[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.util.Util;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
16 import org.opendaylight.yangtools.yang.binding.DataContainer;
17
18 import com.google.common.base.Preconditions;
19
20 /**
21  *
22  */
23 public final class SimpleObjectHandlerRegistry implements ObjectHandlerRegistry {
24         private final HandlerRegistry<DataContainer, ObjectParser, ObjectSerializer> handlers = new HandlerRegistry<>();
25
26         private static int createKey(final int objectClass, final int objectType) {
27                 Preconditions.checkArgument(objectClass >= 0 && objectClass <= Util.UNSIGNED_BYTE_MAX_VALUE);
28                 Preconditions.checkArgument(objectType >= 0 && objectType <= 15);
29                 return (objectClass << 4) | objectType;
30         }
31
32         public AutoCloseable registerObjectParser(final int objectClass, final int objectType, final ObjectParser parser) {
33                 Preconditions.checkArgument(objectClass >= 0 && objectClass <= Util.UNSIGNED_BYTE_MAX_VALUE);
34                 Preconditions.checkArgument(objectType >= 0 && objectType <= 15);
35                 return this.handlers.registerParser(createKey(objectClass, objectType), parser);
36         }
37
38         public AutoCloseable registerObjectSerializer(final Class<? extends Object> objClass, final ObjectSerializer serializer) {
39                 return this.handlers.registerSerializer(objClass, serializer);
40         }
41
42         @Override
43         public ObjectParser getObjectParser(final int objectClass, final int objectType) {
44                 return this.handlers.getParser(createKey(objectClass, objectType));
45         }
46
47         @Override
48         public ObjectSerializer getObjectSerializer(final Object object) {
49                 return this.handlers.getSerializer(object.getImplementedInterface());
50         }
51 }