Bug 4827: MultiPathSupport utilities
[bgpcep.git] / bgp / flowspec / src / main / java / org / opendaylight / protocol / bgp / flowspec / handlers / NumericTwoByteOperandParser.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.handlers;
9
10 import io.netty.buffer.ByteBuf;
11 import io.netty.buffer.Unpooled;
12 import java.util.List;
13
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.NumericOperand;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.NumericTwoByteValue;
16
17 /**
18  * Parser class for NumericTwoByteValues.
19  */
20 public final class NumericTwoByteOperandParser extends AbstractNumericByteOperandParser<NumericTwoByteValue, Integer> {
21
22     public static final NumericTwoByteOperandParser INSTANCE;
23
24     static {
25         INSTANCE = new NumericTwoByteOperandParser();
26     }
27
28     private NumericTwoByteOperandParser() {
29
30     }
31
32     /**
33      * Serializes Flowspec component type that has maximum of 2B sized value field and numeric operand.
34      *
35      * @param list of operands to be serialized
36      * @param nlriByteBuf where the operands will be serialized
37      */
38     @Override
39     public <T extends NumericTwoByteValue> void serialize(final List<T> list, final ByteBuf nlriByteBuf) {
40         for (final T operand : list) {
41             final ByteBuf protoBuf = Unpooled.buffer();
42             Util.writeShortest(operand.getValue(), protoBuf);
43             super.serialize(operand.getOp(), protoBuf.readableBytes(), nlriByteBuf);
44             nlriByteBuf.writeBytes(protoBuf);
45         }
46     }
47
48     @Override
49     protected <T extends NumericTwoByteValue> Integer getValue(final T item) {
50         return item.getValue();
51     }
52
53     @Override
54     <T extends NumericTwoByteValue> NumericOperand getOp(final T item) {
55         return item.getOp();
56     }
57 }