Parameter registry uses ByteBuf instead of byte[]
[bgpcep.git] / bgp / parser-spi / src / main / java / org / opendaylight / protocol / bgp / parser / spi / pojo / SimpleParameterRegistry.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.bgp.parser.spi.pojo;
9
10 import com.google.common.base.Preconditions;
11
12 import io.netty.buffer.ByteBuf;
13
14 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
15 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
16 import org.opendaylight.protocol.bgp.parser.spi.ParameterParser;
17 import org.opendaylight.protocol.bgp.parser.spi.ParameterRegistry;
18 import org.opendaylight.protocol.bgp.parser.spi.ParameterSerializer;
19 import org.opendaylight.protocol.concepts.HandlerRegistry;
20 import org.opendaylight.protocol.util.Values;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.BgpParameters;
22 import org.opendaylight.yangtools.yang.binding.DataContainer;
23
24 final class SimpleParameterRegistry implements ParameterRegistry {
25     private final HandlerRegistry<DataContainer, ParameterParser, ParameterSerializer> handlers = new HandlerRegistry<>();
26
27     AutoCloseable registerParameterParser(final int messageType, final ParameterParser parser) {
28         Preconditions.checkArgument(messageType >= 0 && messageType <= Values.UNSIGNED_BYTE_MAX_VALUE);
29         return this.handlers.registerParser(messageType, parser);
30     }
31
32     AutoCloseable registerParameterSerializer(final Class<? extends BgpParameters> paramClass, final ParameterSerializer serializer) {
33         return this.handlers.registerSerializer(paramClass, serializer);
34     }
35
36     @Override
37     public BgpParameters parseParameter(final int parameterType, final ByteBuf buffer) throws BGPParsingException, BGPDocumentedException {
38         final ParameterParser parser = this.handlers.getParser(parameterType);
39         if (parser == null) {
40             return null;
41         }
42         return parser.parseParameter(buffer);
43     }
44
45     public void serializeParameter(final BgpParameters parameter, ByteBuf bytes) {
46         final ParameterSerializer serializer = this.handlers.getSerializer(parameter.getImplementedInterface());
47         if (serializer == null) {
48             return;
49         }
50         serializer.serializeParameter(parameter,bytes);
51     }
52 }