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