Bump MDSAL to 4.0.0
[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 static com.google.common.base.Preconditions.checkArgument;
11
12 import io.netty.buffer.ByteBuf;
13 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
14 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
15 import org.opendaylight.protocol.bgp.parser.spi.ParameterParser;
16 import org.opendaylight.protocol.bgp.parser.spi.ParameterRegistry;
17 import org.opendaylight.protocol.bgp.parser.spi.ParameterSerializer;
18 import org.opendaylight.protocol.concepts.HandlerRegistry;
19 import org.opendaylight.protocol.util.Values;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.BgpParameters;
21 import org.opendaylight.yangtools.concepts.Registration;
22 import org.opendaylight.yangtools.yang.binding.DataContainer;
23
24 final class SimpleParameterRegistry implements ParameterRegistry {
25     private final HandlerRegistry<DataContainer, ParameterParser, ParameterSerializer> handlers =
26             new HandlerRegistry<>();
27
28     Registration registerParameterParser(final int messageType, final ParameterParser parser) {
29         // 255 is explicitly excluded because it is handled in OPEN message parser
30         checkArgument(messageType >= 0 && messageType < Values.UNSIGNED_BYTE_MAX_VALUE);
31         return this.handlers.registerParser(messageType, parser);
32     }
33
34     Registration registerParameterSerializer(final Class<? extends BgpParameters> paramClass,
35             final ParameterSerializer serializer) {
36         return this.handlers.registerSerializer(paramClass, serializer);
37     }
38
39     @Override
40     public BgpParameters parseParameter(final int parameterType, final ByteBuf buffer) throws BGPParsingException,
41             BGPDocumentedException {
42         final ParameterParser parser = this.handlers.getParser(parameterType);
43         if (parser == null) {
44             return null;
45         }
46         return parser.parseParameter(buffer);
47     }
48
49     @Override
50     public void serializeParameter(final BgpParameters parameter, final ByteBuf bytes) {
51         final ParameterSerializer serializer = this.handlers.getSerializer(parameter.implementedInterface());
52         if (serializer == null) {
53             return;
54         }
55         serializer.serializeParameter(parameter,bytes);
56     }
57 }