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