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