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