Code clean up
[bgpcep.git] / bgp / flowspec / src / main / java / org / opendaylight / protocol / bgp / flowspec / SimpleFlowspecTypeRegistry.java
1 /*
2  * Copyright (c) 2015 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.flowspec;
9
10 import static java.util.Objects.requireNonNull;
11
12 import io.netty.buffer.ByteBuf;
13 import org.opendaylight.protocol.bgp.flowspec.handlers.FlowspecTypeParser;
14 import org.opendaylight.protocol.bgp.flowspec.handlers.FlowspecTypeSerializer;
15 import org.opendaylight.protocol.concepts.HandlerRegistry;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.flowspec.destination.flowspec.FlowspecType;
17 import org.opendaylight.yangtools.yang.binding.DataContainer;
18
19 public class SimpleFlowspecTypeRegistry {
20     private final HandlerRegistry<DataContainer, FlowspecTypeParser, FlowspecTypeSerializer> handlers = new HandlerRegistry<>();
21
22     public FlowspecTypeParser getFlowspecTypeParser(final short type) {
23         return this.handlers.getParser(type);
24     }
25
26     public FlowspecTypeSerializer getFlowspecTypeSerializer(final FlowspecType fsType) {
27         return this.handlers.getSerializer(fsType.getImplementedInterface());
28     }
29
30     public void serializeFlowspecType(final FlowspecType fsType, final ByteBuf output) {
31         final FlowspecTypeSerializer serializer = getFlowspecTypeSerializer(fsType);
32         requireNonNull(serializer, "serializer for flowspec type " + fsType + " is not registered.");
33         serializer.serializeType(fsType, output);
34     }
35
36     public FlowspecType parseFlowspecType(final ByteBuf buffer) {
37         final short type = buffer.readUnsignedByte();
38         final FlowspecTypeParser parser = getFlowspecTypeParser(type);
39         requireNonNull(parser, "parser for flowspec type "+ type +" is not registered");
40         return parser.parseType(buffer);
41     }
42
43     public AutoCloseable registerFlowspecTypeParser(final int type, final FlowspecTypeParser parser) {
44         return this.handlers.registerParser(type, parser);
45     }
46
47     public AutoCloseable registerFlowspecTypeSerializer(final Class<? extends FlowspecType> typeClass, final FlowspecTypeSerializer serializer) {
48         return this.handlers.registerSerializer(typeClass, serializer);
49     }
50 }