BUG-47 : PCEP migration to generated DTOs.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / HandlerRegistryImpl.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.impl;
9
10 import java.util.Map;
11 import java.util.concurrent.ConcurrentHashMap;
12
13 import org.opendaylight.protocol.concepts.AbstractRegistration;
14 import org.opendaylight.protocol.pcep.impl.object.PCEPBandwidthObjectParser;
15 import org.opendaylight.protocol.pcep.impl.object.PCEPClassTypeObjectParser;
16 import org.opendaylight.protocol.pcep.impl.object.PCEPCloseObjectParser;
17 import org.opendaylight.protocol.pcep.impl.object.PCEPEndPointsObjectParser;
18 import org.opendaylight.protocol.pcep.impl.object.PCEPErrorObjectParser;
19 import org.opendaylight.protocol.pcep.impl.object.PCEPExplicitRouteObjectParser;
20 import org.opendaylight.protocol.pcep.impl.object.PCEPGlobalConstraintsObjectParser;
21 import org.opendaylight.protocol.pcep.impl.object.PCEPIncludeRouteObjectParser;
22 import org.opendaylight.protocol.pcep.impl.object.PCEPLoadBalancingObjectParser;
23 import org.opendaylight.protocol.pcep.impl.object.PCEPLspObjectParser;
24 import org.opendaylight.protocol.pcep.impl.object.PCEPLspaObjectParser;
25 import org.opendaylight.protocol.pcep.impl.object.PCEPMetricObjectParser;
26 import org.opendaylight.protocol.pcep.impl.object.PCEPNoPathObjectParser;
27 import org.opendaylight.protocol.pcep.impl.object.PCEPNotificationObjectParser;
28 import org.opendaylight.protocol.pcep.impl.object.PCEPObjectiveFunctionObjectParser;
29 import org.opendaylight.protocol.pcep.impl.object.PCEPOpenObjectParser;
30 import org.opendaylight.protocol.pcep.impl.object.PCEPPathKeyObjectParser;
31 import org.opendaylight.protocol.pcep.impl.object.PCEPReportedRouteObjectParser;
32 import org.opendaylight.protocol.pcep.impl.object.PCEPRequestParameterObjectParser;
33 import org.opendaylight.protocol.pcep.impl.object.PCEPSrpObjectParser;
34 import org.opendaylight.protocol.pcep.impl.object.PCEPSvecObjectParser;
35 import org.opendaylight.protocol.pcep.impl.subobject.EROAsNumberSubobjectParser;
36 import org.opendaylight.protocol.pcep.impl.tlv.LSPIdentifierIPv4TlvParser;
37 import org.opendaylight.protocol.pcep.impl.tlv.LSPIdentifierIPv6TlvParser;
38 import org.opendaylight.protocol.pcep.impl.tlv.LspDbVersionTlvParser;
39 import org.opendaylight.protocol.pcep.impl.tlv.LspSymbolicNameTlvParser;
40 import org.opendaylight.protocol.pcep.impl.tlv.LspUpdateErrorTlvParser;
41 import org.opendaylight.protocol.pcep.impl.tlv.NoPathVectorTlvParser;
42 import org.opendaylight.protocol.pcep.impl.tlv.OFListTlvParser;
43 import org.opendaylight.protocol.pcep.impl.tlv.OrderTlvParser;
44 import org.opendaylight.protocol.pcep.impl.tlv.OverloadedDurationTlvParser;
45 import org.opendaylight.protocol.pcep.impl.tlv.PCEStatefulCapabilityTlvParser;
46 import org.opendaylight.protocol.pcep.impl.tlv.PredundancyGroupTlvParser;
47 import org.opendaylight.protocol.pcep.impl.tlv.RSVPErrorSpecTlvParser;
48 import org.opendaylight.protocol.pcep.impl.tlv.ReqMissingTlvParser;
49 import org.opendaylight.protocol.pcep.spi.HandlerRegistry;
50 import org.opendaylight.protocol.pcep.spi.MessageParser;
51 import org.opendaylight.protocol.pcep.spi.MessageSerializer;
52 import org.opendaylight.protocol.pcep.spi.ObjectParser;
53 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
54 import org.opendaylight.protocol.pcep.spi.SubobjectParser;
55 import org.opendaylight.protocol.pcep.spi.SubobjectSerializer;
56 import org.opendaylight.protocol.pcep.spi.TlvParser;
57 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.BandwidthObject;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ClasstypeObject;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.CloseObject;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.EndpointsObject;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ExplicitRouteObject;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.GcObject;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.IncludeRouteObject;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.LoadBalancingObject;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.LspDbVersionTlv;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.LspErrorCodeTlv;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.LspIdentifiersTlv;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.LspObject;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.LspaObject;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.MetricObject;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.NoPathObject;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.NoPathVectorTlv;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.NotificationObject;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OfListTlv;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OfObject;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OpenObject;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OrderTlv;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OverloadDurationTlv;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PathKeyObject;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PcepErrorObject;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PredundancyGroupIdTlv;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ReportedRouteObject;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ReqMissingTlv;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.RpObject;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.RsvpErrorSpecTlv;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.SrpObject;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.StatefulCapabilityTlv;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.SvecObject;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.SymbolicPathNameTlv;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.AsNumberSubobject;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.CSubobject;
96
97 import com.google.common.base.Preconditions;
98
99 public class HandlerRegistryImpl implements HandlerRegistry {
100         public static final HandlerRegistry INSTANCE;
101
102         static {
103                 final HandlerRegistry reg = new HandlerRegistryImpl();
104
105                 // FIXME: fill this in
106
107                 // reg.registerMessageHandler(PCEPOpenMessage.class, 1, new PCEPOpenMessageParser());
108                 // reg.registerMessageHandler(PCEPNotificationMessage.class, 5, new PCEPNotificationMessageParser());
109                 // reg.registerMessageHandler(PCEPKeepAliveMessage.class, 2, new PCEPKeepAliveMessageParser());
110                 // reg.registerMessageHandler(PCEPReplyMessage.class, 4, new PCEPReplyMessageParser());
111                 // reg.registerMessageHandler(PCEPRequestMessage.class, 3, new PCEPRequestMessageParser());
112                 // reg.registerMessageHandler(PCEPErrorMessage.class, 6, new PCEPErrorMessageParser());
113                 // reg.registerMessageHandler(PCEPCloseMessage.class, 7, new PCEPCloseMessageParser());
114                 // reg.registerMessageHandler(PCEPUpdateRequestMessage.class, 11, new PCEPUpdateRequestMessageParser());
115                 // reg.registerMessageHandler(PCEPReportMessage.class, 10, new PCEPReportMessageParser());
116                 // reg.registerMessageHandler(PCCreateMessage.class, 12, new PCCreateMessageParser());
117
118                 reg.registerTlvParser(NoPathVectorTlvParser.TYPE, new NoPathVectorTlvParser());
119                 reg.registerTlvParser(OverloadedDurationTlvParser.TYPE, new OverloadedDurationTlvParser());
120                 reg.registerTlvParser(ReqMissingTlvParser.TYPE, new ReqMissingTlvParser());
121                 reg.registerTlvParser(OFListTlvParser.TYPE, new OFListTlvParser());
122                 reg.registerTlvParser(OrderTlvParser.TYPE, new OrderTlvParser());
123                 reg.registerTlvParser(PCEStatefulCapabilityTlvParser.TYPE, new PCEStatefulCapabilityTlvParser());
124                 reg.registerTlvParser(LspSymbolicNameTlvParser.TYPE, new LspSymbolicNameTlvParser());
125                 reg.registerTlvParser(LSPIdentifierIPv4TlvParser.TYPE, new LSPIdentifierIPv4TlvParser());
126                 reg.registerTlvParser(LSPIdentifierIPv6TlvParser.TYPE, new LSPIdentifierIPv6TlvParser());
127                 reg.registerTlvParser(LspUpdateErrorTlvParser.TYPE, new LspUpdateErrorTlvParser());
128                 reg.registerTlvParser(RSVPErrorSpecTlvParser.TYPE, new RSVPErrorSpecTlvParser());
129                 reg.registerTlvParser(LspDbVersionTlvParser.TYPE, new LspDbVersionTlvParser());
130                 reg.registerTlvParser(PredundancyGroupTlvParser.TYPE, new PredundancyGroupTlvParser());
131
132                 reg.registerTlvSerializer(NoPathVectorTlv.class, new NoPathVectorTlvParser());
133                 reg.registerTlvSerializer(OverloadDurationTlv.class, new OverloadedDurationTlvParser());
134                 reg.registerTlvSerializer(ReqMissingTlv.class, new ReqMissingTlvParser());
135                 reg.registerTlvSerializer(OfListTlv.class, new OFListTlvParser());
136                 reg.registerTlvSerializer(OrderTlv.class, new OrderTlvParser());
137                 reg.registerTlvSerializer(StatefulCapabilityTlv.class, new PCEStatefulCapabilityTlvParser());
138                 reg.registerTlvSerializer(SymbolicPathNameTlv.class, new LspSymbolicNameTlvParser());
139                 reg.registerTlvSerializer(LspIdentifiersTlv.class, new LSPIdentifierIPv4TlvParser());
140                 reg.registerTlvSerializer(LspErrorCodeTlv.class, new LspUpdateErrorTlvParser());
141                 reg.registerTlvSerializer(RsvpErrorSpecTlv.class, new RSVPErrorSpecTlvParser());
142                 reg.registerTlvSerializer(LspDbVersionTlv.class, new LspDbVersionTlvParser());
143                 reg.registerTlvSerializer(PredundancyGroupIdTlv.class, new PredundancyGroupTlvParser());
144
145                 reg.registerObjectParser(PCEPOpenObjectParser.CLASS, PCEPOpenObjectParser.TYPE, new PCEPOpenObjectParser(reg));
146                 reg.registerObjectParser(PCEPRequestParameterObjectParser.CLASS, PCEPRequestParameterObjectParser.TYPE,
147                                 new PCEPRequestParameterObjectParser(reg));
148                 reg.registerObjectParser(PCEPNoPathObjectParser.CLASS, PCEPNoPathObjectParser.TYPE, new PCEPNoPathObjectParser(reg));
149                 reg.registerObjectParser(PCEPEndPointsObjectParser.CLASS, PCEPEndPointsObjectParser.TYPE, new PCEPEndPointsObjectParser(reg));
150                 reg.registerObjectParser(PCEPEndPointsObjectParser.CLASS_6, PCEPEndPointsObjectParser.TYPE_6, new PCEPEndPointsObjectParser(reg));
151                 reg.registerObjectParser(PCEPBandwidthObjectParser.CLASS, PCEPBandwidthObjectParser.TYPE, new PCEPBandwidthObjectParser(reg));
152                 reg.registerObjectParser(PCEPBandwidthObjectParser.E_CLASS, PCEPBandwidthObjectParser.E_TYPE, new PCEPBandwidthObjectParser(reg));
153                 reg.registerObjectParser(PCEPMetricObjectParser.CLASS, PCEPMetricObjectParser.TYPE, new PCEPMetricObjectParser(reg));
154                 reg.registerObjectParser(PCEPExplicitRouteObjectParser.CLASS, PCEPExplicitRouteObjectParser.TYPE,
155                                 new PCEPExplicitRouteObjectParser(reg));
156                 reg.registerObjectParser(PCEPReportedRouteObjectParser.CLASS, PCEPReportedRouteObjectParser.TYPE,
157                                 new PCEPReportedRouteObjectParser(reg));
158                 reg.registerObjectParser(PCEPLspaObjectParser.CLASS, PCEPLspaObjectParser.TYPE, new PCEPLspaObjectParser(reg));
159                 reg.registerObjectParser(PCEPIncludeRouteObjectParser.CLASS, PCEPIncludeRouteObjectParser.TYPE,
160                                 new PCEPIncludeRouteObjectParser(reg));
161                 reg.registerObjectParser(PCEPSvecObjectParser.CLASS, PCEPSvecObjectParser.TYPE, new PCEPSvecObjectParser(reg));
162                 reg.registerObjectParser(PCEPNotificationObjectParser.CLASS, PCEPNotificationObjectParser.TYPE,
163                                 new PCEPNotificationObjectParser(reg));
164                 reg.registerObjectParser(PCEPErrorObjectParser.CLASS, PCEPErrorObjectParser.TYPE, new PCEPErrorObjectParser(reg));
165                 reg.registerObjectParser(PCEPLoadBalancingObjectParser.CLASS, PCEPLoadBalancingObjectParser.TYPE,
166                                 new PCEPLoadBalancingObjectParser(reg));
167                 reg.registerObjectParser(PCEPCloseObjectParser.CLASS, PCEPCloseObjectParser.TYPE, new PCEPCloseObjectParser(reg));
168                 reg.registerObjectParser(PCEPPathKeyObjectParser.CLASS, PCEPPathKeyObjectParser.TYPE, new PCEPPathKeyObjectParser(reg));
169                 reg.registerObjectParser(PCEPObjectiveFunctionObjectParser.CLASS, PCEPObjectiveFunctionObjectParser.TYPE,
170                                 new PCEPObjectiveFunctionObjectParser(reg));
171                 reg.registerObjectParser(PCEPClassTypeObjectParser.CLASS, PCEPClassTypeObjectParser.TYPE, new PCEPClassTypeObjectParser(reg));
172                 reg.registerObjectParser(PCEPGlobalConstraintsObjectParser.CLASS, PCEPGlobalConstraintsObjectParser.TYPE,
173                                 new PCEPGlobalConstraintsObjectParser(reg));
174                 reg.registerObjectParser(PCEPLspObjectParser.CLASS, PCEPLspObjectParser.TYPE, new PCEPLspObjectParser(reg));
175                 reg.registerObjectParser(PCEPSrpObjectParser.CLASS, PCEPSrpObjectParser.TYPE, new PCEPSrpObjectParser(reg));
176                 // reg.registerObjectParser(PCEPExcludeRouteObjectParser.CLASS, PCEPExcludeRouteObjectParser.TYPE, new
177                 // PCEPExcludeRouteObjectParser(reg));
178
179                 reg.registerObjectSerializer(OpenObject.class, new PCEPOpenObjectParser(reg));
180                 reg.registerObjectSerializer(RpObject.class, new PCEPRequestParameterObjectParser(reg));
181                 reg.registerObjectSerializer(NoPathObject.class, new PCEPNoPathObjectParser(reg));
182                 reg.registerObjectSerializer(EndpointsObject.class, new PCEPEndPointsObjectParser(reg));
183                 reg.registerObjectSerializer(BandwidthObject.class, new PCEPBandwidthObjectParser(reg));
184                 reg.registerObjectSerializer(MetricObject.class, new PCEPMetricObjectParser(reg));
185                 reg.registerObjectSerializer(ExplicitRouteObject.class, new PCEPExplicitRouteObjectParser(reg));
186                 reg.registerObjectSerializer(ReportedRouteObject.class, new PCEPReportedRouteObjectParser(reg));
187                 reg.registerObjectSerializer(LspaObject.class, new PCEPLspaObjectParser(reg));
188                 reg.registerObjectSerializer(IncludeRouteObject.class, new PCEPIncludeRouteObjectParser(reg));
189                 reg.registerObjectSerializer(SvecObject.class, new PCEPSvecObjectParser(reg));
190                 reg.registerObjectSerializer(NotificationObject.class, new PCEPNotificationObjectParser(reg));
191                 reg.registerObjectSerializer(PcepErrorObject.class, new PCEPErrorObjectParser(reg));
192                 reg.registerObjectSerializer(LoadBalancingObject.class, new PCEPLoadBalancingObjectParser(reg));
193                 reg.registerObjectSerializer(CloseObject.class, new PCEPCloseObjectParser(reg));
194                 reg.registerObjectSerializer(PathKeyObject.class, new PCEPPathKeyObjectParser(reg));
195                 reg.registerObjectSerializer(OfObject.class, new PCEPObjectiveFunctionObjectParser(reg));
196                 reg.registerObjectSerializer(ClasstypeObject.class, new PCEPClassTypeObjectParser(reg));
197                 reg.registerObjectSerializer(GcObject.class, new PCEPGlobalConstraintsObjectParser(reg));
198                 reg.registerObjectSerializer(LspObject.class, new PCEPLspObjectParser(reg));
199                 reg.registerObjectSerializer(SrpObject.class, new PCEPSrpObjectParser(reg));
200                 // reg.registerObjectSerializer(ExcludeRouteObject.class, new PCEPExcludeRouteObjectParser(reg));
201
202                 reg.registerSubobjectParser(EROAsNumberSubobjectParser.TYPE, new EROAsNumberSubobjectParser());
203
204                 reg.registerSubobjectSerializer(AsNumberSubobject.class, new EROAsNumberSubobjectParser());
205
206                 INSTANCE = reg;
207         }
208
209         private final Map<Integer, MessageParser> msgParsers = new ConcurrentHashMap<>();
210         private final Map<Class<? extends Message>, MessageSerializer> msgSerializers = new ConcurrentHashMap<>();
211
212         private final Map<Integer, ObjectParser> objParsers = new ConcurrentHashMap<>();
213         private final Map<Class<? extends Object>, ObjectSerializer> objSerializers = new ConcurrentHashMap<>();
214
215         private final Map<Integer, TlvParser> tlvParsers = new ConcurrentHashMap<>();
216         private final Map<Class<? extends Tlv>, TlvSerializer> tlvSerializers = new ConcurrentHashMap<>();
217
218         private final Map<Integer, SubobjectParser> subobjectParsers = new ConcurrentHashMap<>();
219         private final Map<Class<? extends CSubobject>, SubobjectSerializer> subobjectSerializers = new ConcurrentHashMap<>();
220
221         private HandlerRegistryImpl() {
222
223         }
224
225         @Override
226         public MessageParser getMessageParser(final int messageType) {
227                 Preconditions.checkArgument(messageType >= 0 && messageType <= 255);
228                 return this.msgParsers.get(messageType);
229         }
230
231         @Override
232         public MessageSerializer getMessageSerializer(final Message message) {
233                 final Class<? extends Message> c = message.getClass();
234                 for (final Map.Entry<Class<? extends Message>, MessageSerializer> s : this.msgSerializers.entrySet()) {
235                         if (s.getKey().isAssignableFrom(c)) {
236                                 return s.getValue();
237                         }
238                 }
239
240                 return null;
241         }
242
243         @Override
244         public synchronized ObjectParser getObjectParser(final int objectClass, final int objectType) {
245                 Preconditions.checkArgument(objectClass >= 0 && objectClass <= 255);
246                 Preconditions.checkArgument(objectType >= 0 && objectType <= 15);
247
248                 return this.objParsers.get((objectClass << 4) + objectType);
249         }
250
251         @Override
252         public ObjectSerializer getObjectSerializer(final Object object) {
253                 final Class<? extends Object> c = object.getClass();
254                 for (final Map.Entry<Class<? extends Object>, ObjectSerializer> s : this.objSerializers.entrySet()) {
255                         if (s.getKey().isAssignableFrom(c)) {
256                                 return s.getValue();
257                         }
258                 }
259
260                 return null;
261         }
262
263         @Override
264         public TlvParser getTlvParser(final int tlvType) {
265                 Preconditions.checkArgument(tlvType >= 0 && tlvType <= 65535);
266                 return this.tlvParsers.get(tlvType);
267         }
268
269         @Override
270         public TlvSerializer getTlvSerializer(final Tlv tlv) {
271                 final Class<? extends Tlv> c = tlv.getClass();
272                 for (final Map.Entry<Class<? extends Tlv>, TlvSerializer> s : this.tlvSerializers.entrySet()) {
273                         if (s.getKey().isAssignableFrom(c)) {
274                                 return s.getValue();
275                         }
276                 }
277
278                 return null;
279         }
280
281         @Override
282         public SubobjectSerializer getSubobjectSerializer(final CSubobject subobject) {
283                 final Class<? extends CSubobject> c = subobject.getClass();
284                 for (final Map.Entry<Class<? extends CSubobject>, SubobjectSerializer> s : this.subobjectSerializers.entrySet()) {
285                         if (s.getKey().isAssignableFrom(c)) {
286                                 return s.getValue();
287                         }
288                 }
289                 return null;
290         }
291
292         @Override
293         public SubobjectParser getSubobjectParser(final int subobjectType) {
294                 Preconditions.checkArgument(subobjectType >= 0 && subobjectType <= 65535);
295                 return this.subobjectParsers.get(subobjectType);
296         }
297
298         private synchronized void unregisterMessageParser(final Integer msgType) {
299                 this.msgParsers.remove(msgType);
300         }
301
302         @Override
303         public synchronized AutoCloseable registerMessageParser(final int msgType, final MessageParser parser) {
304                 Preconditions.checkArgument(msgType >= 0 && msgType <= 255);
305                 Preconditions.checkArgument(!this.msgParsers.containsKey(msgType), "Message type %s already registered", msgType);
306                 this.msgParsers.put(msgType, parser);
307
308                 return new AbstractRegistration() {
309                         @Override
310                         protected void removeRegistration() {
311                                 unregisterMessageParser(msgType);
312                         }
313                 };
314         }
315
316         private synchronized void unregisterMessageSerializer(final Class<? extends Message> msgClass) {
317                 this.msgSerializers.remove(msgClass);
318         }
319
320         @Override
321         public synchronized AutoCloseable registerMessageSerializer(final Class<? extends Message> msgClass, final MessageSerializer serializer) {
322                 Preconditions.checkArgument(!this.msgSerializers.containsKey(msgClass), "Message class %s already registered", msgClass);
323                 this.msgSerializers.put(msgClass, serializer);
324
325                 return new AbstractRegistration() {
326                         @Override
327                         protected void removeRegistration() {
328                                 unregisterMessageSerializer(msgClass);
329                         }
330                 };
331         }
332
333         private synchronized void unregisterObjectParser(final Integer key) {
334                 this.objParsers.remove(key);
335         }
336
337         @Override
338         public synchronized AutoCloseable registerObjectParser(final int objectClass, final int objectType, final ObjectParser parser) {
339                 Preconditions.checkArgument(objectClass >= 0 && objectClass <= 255);
340                 Preconditions.checkArgument(objectType >= 0 && objectType <= 15);
341
342                 final Integer key = (objectClass << 4) + objectType;
343                 Preconditions.checkArgument(!this.objParsers.containsKey(key), "Object class %s type %s already registered", objectClass,
344                                 objectType);
345                 this.objParsers.put(key, parser);
346
347                 return new AbstractRegistration() {
348                         @Override
349                         protected void removeRegistration() {
350                                 unregisterObjectParser(key);
351                         }
352                 };
353         }
354
355         private synchronized void unregisterObjectSerializer(final Class<? extends Object> objClass) {
356                 this.objSerializers.remove(objClass);
357         }
358
359         @Override
360         public synchronized AutoCloseable registerObjectSerializer(final Class<? extends Object> objClass, final ObjectSerializer serializer) {
361                 Preconditions.checkArgument(!this.objSerializers.containsKey(objClass), "Object class %s already registered", objClass);
362                 this.objSerializers.put(objClass, serializer);
363
364                 return new AbstractRegistration() {
365                         @Override
366                         protected void removeRegistration() {
367                                 unregisterObjectSerializer(objClass);
368                         }
369                 };
370         }
371
372         private synchronized void unregisterTlvParser(final int tlvType) {
373                 this.tlvParsers.remove(tlvType);
374         }
375
376         @Override
377         public synchronized AutoCloseable registerTlvParser(final int tlvType, final TlvParser parser) {
378                 Preconditions.checkArgument(tlvType >= 0 && tlvType <= 65535);
379                 Preconditions.checkArgument(!this.tlvParsers.containsKey(tlvType), "TLV type %s already registered", tlvType);
380
381                 this.tlvParsers.put(tlvType, parser);
382
383                 return new AbstractRegistration() {
384                         @Override
385                         protected void removeRegistration() {
386                                 unregisterTlvParser(tlvType);
387                         }
388                 };
389         }
390
391         private synchronized void unregisterTlvSerializer(final Class<? extends Tlv> tlvClass) {
392                 this.tlvSerializers.remove(tlvClass);
393         }
394
395         @Override
396         public synchronized AutoCloseable registerTlvSerializer(final Class<? extends Tlv> tlvClass, final TlvSerializer serializer) {
397                 Preconditions.checkArgument(!this.tlvSerializers.containsKey(tlvClass), "TLV class %s already registered", tlvClass);
398                 this.tlvSerializers.put(tlvClass, serializer);
399
400                 return new AbstractRegistration() {
401                         @Override
402                         protected void removeRegistration() {
403                                 unregisterTlvSerializer(tlvClass);
404                         }
405                 };
406         }
407
408         @Override
409         public AutoCloseable registerSubobjectSerializer(final Class<? extends CSubobject> subobjectClass, final SubobjectSerializer serializer) {
410                 Preconditions.checkArgument(!this.tlvSerializers.containsKey(subobjectClass), "Subobject class %s already registered",
411                                 subobjectClass);
412                 this.subobjectSerializers.put(subobjectClass, serializer);
413
414                 return new AbstractRegistration() {
415                         @Override
416                         protected void removeRegistration() {
417                                 unregisterSubobjectSerializer(subobjectClass);
418                         }
419                 };
420         }
421
422         private synchronized void unregisterSubobjectSerializer(final Class<? extends CSubobject> subobjectClass) {
423                 this.subobjectSerializers.remove(subobjectClass);
424         }
425
426         @Override
427         public AutoCloseable registerSubobjectParser(final int subobjectType, final SubobjectParser parser) {
428                 Preconditions.checkArgument(subobjectType >= 0 && subobjectType <= 65535);
429                 Preconditions.checkArgument(!this.subobjectParsers.containsKey(subobjectType), "Subobject type %s already registered",
430                                 subobjectType);
431
432                 this.subobjectParsers.put(subobjectType, parser);
433
434                 return new AbstractRegistration() {
435                         @Override
436                         protected void removeRegistration() {
437                                 unregisterSubobjectParser(subobjectType);
438                         }
439                 };
440         }
441
442         private synchronized void unregisterSubobjectParser(final int subobjectType) {
443                 this.subobjectParsers.remove(subobjectType);
444         }
445 }