Bug-2230: Revision of module RSVP
[bgpcep.git] / pcep / spi / src / main / java / org / opendaylight / protocol / pcep / spi / pojo / SimpleLabelRegistry.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 com.google.common.base.Preconditions;
11
12 import io.netty.buffer.ByteBuf;
13
14 import org.opendaylight.protocol.concepts.HandlerRegistry;
15 import org.opendaylight.protocol.pcep.spi.LabelParser;
16 import org.opendaylight.protocol.pcep.spi.LabelRegistry;
17 import org.opendaylight.protocol.pcep.spi.LabelSerializer;
18 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
19 import org.opendaylight.protocol.util.Values;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.label.subobject.LabelType;
21 import org.opendaylight.yangtools.yang.binding.DataContainer;
22
23 public class SimpleLabelRegistry implements LabelRegistry {
24     private final HandlerRegistry<DataContainer, LabelParser, LabelSerializer> handlers = new HandlerRegistry<>();
25
26     public AutoCloseable registerLabelParser(final int cType, final LabelParser parser) {
27         Preconditions.checkArgument(cType >= 0 && cType <= Values.UNSIGNED_BYTE_MAX_VALUE);
28         return this.handlers.registerParser(cType, parser);
29     }
30
31     public AutoCloseable registerLabelSerializer(final Class<? extends LabelType> labelClass, final LabelSerializer serializer) {
32         return this.handlers.registerSerializer(labelClass, serializer);
33     }
34
35     @Override
36     public LabelType parseLabel(final int cType, final ByteBuf buffer) throws PCEPDeserializerException {
37         Preconditions.checkArgument(cType >= 0 && cType <= Values.UNSIGNED_BYTE_MAX_VALUE);
38         final LabelParser parser = this.handlers.getParser(cType);
39         if (parser == null) {
40             return null;
41         }
42         return parser.parseLabel(buffer);
43     }
44
45     @Override
46     public void serializeLabel(final boolean unidirectional, final boolean global, final LabelType label, final ByteBuf buffer) {
47         final LabelSerializer serializer = this.handlers.getSerializer(label.getImplementedInterface());
48         if (serializer != null) {
49             serializer.serializeLabel(unidirectional, global, label, buffer);
50         }
51     }
52 }