2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.flowspec.handlers;
10 import com.google.common.annotations.VisibleForTesting;
11 import io.netty.buffer.ByteBuf;
12 import java.util.List;
15 import org.opendaylight.protocol.util.BitArray;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.NumericOperand;
19 * Common parent class for numeric operands.
21 * @param <N> numeric operand type
23 abstract public class AbstractNumericOperandParser<N> extends AbstractOperandParser<NumericOperand> {
26 public static final String EQUALS_VALUE = "equals";
28 public static final String GREATER_THAN_VALUE = "greater-than";
30 public static final String LESS_THAN_VALUE = "less-than";
32 protected static final int LESS_THAN = 5;
33 protected static final int GREATER_THAN = 6;
34 protected static final int EQUAL = 7;
37 * Serializes specific numeric operand type depending on the length field value.
39 * @param list of operands to be serialized
40 * @param nlriByteBuf where the operands will be serialized
42 protected abstract <T extends N> void serialize(final List<T> list, final ByteBuf nlriByteBuf);
44 protected abstract <T extends N> String toString(final List<T> list);
47 public final NumericOperand create(final Set<String> operandValues) {
48 return new NumericOperand(operandValues.contains(AND_BIT_VALUE), operandValues.contains(END_OF_LIST_VALUE), operandValues.contains(EQUALS_VALUE), operandValues.contains(GREATER_THAN_VALUE), operandValues.contains(LESS_THAN_VALUE));
52 public final void serialize(final NumericOperand operand, final int length, final ByteBuf buffer) {
53 final BitArray operandValues = new BitArray(OPERAND_LENGTH);
54 operandValues.set(END_OF_LIST, operand.isEndOfList());
55 operandValues.set(AND_BIT, operand.isAndBit());
56 operandValues.set(LESS_THAN, operand.isLessThan());
57 operandValues.set(GREATER_THAN, operand.isGreaterThan());
58 operandValues.set(EQUAL, operand.isEquals());
59 final byte byteLength = (byte) (Integer.numberOfTrailingZeros(length) << LENGTH_SHIFT);
60 buffer.writeByte(operandValues.toByte() | byteLength);
64 public final NumericOperand parse(final byte operand) {
65 final BitArray operandValues = BitArray.valueOf(operand);
66 return new NumericOperand(operandValues.get(AND_BIT), operandValues.get(END_OF_LIST), operandValues.get(EQUAL), operandValues.get(GREATER_THAN), operandValues.get(LESS_THAN));
70 public String toString(final NumericOperand operand, final boolean isFirst) {
71 final StringBuilder buffer = new StringBuilder();
72 if (operand.isAndBit()) {
73 buffer.append("and ");
74 } else if (!isFirst) {
77 if (operand.isLessThan()) {
78 buffer.append("is less than ");
79 if (operand.isEquals()) {
80 buffer.append("or equals to ");
82 return buffer.toString();
84 if (operand.isGreaterThan()) {
85 buffer.append("is greater than ");
86 if (operand.isEquals()) {
87 buffer.append("or equals to ");
89 return buffer.toString();
91 if (operand.isEquals()) {
92 buffer.append("equals to ");
94 return buffer.toString();