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;
10 import com.google.common.annotations.VisibleForTesting;
11 import io.netty.buffer.ByteBuf;
12 import java.util.List;
14 import org.opendaylight.protocol.util.BitArray;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.NumericOperand;
18 * Common parent class for numeric operands.
20 * @param <N> numeric operand type
22 abstract class AbstractNumericOperandParser<N> extends AbstractOperandParser<NumericOperand> {
25 static final String EQUALS_VALUE = "equals";
27 static final String GREATER_THAN_VALUE = "greater-than";
29 static final String LESS_THAN_VALUE = "less-than";
31 protected static final int LESS_THAN = 5;
32 protected static final int GREATER_THAN = 6;
33 protected static final int EQUAL = 7;
36 * Serializes specific numeric operand type depending on the length field value.
38 * @param list of operands to be serialized
39 * @param nlriByteBuf where the operands will be serialized
41 protected abstract <T extends N> void serialize(final List<T> list, final ByteBuf nlriByteBuf);
43 protected abstract <T extends N> String toString(final List<T> list);
46 protected final NumericOperand create(final Set<String> operandValues) {
47 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));
51 public final void serialize(final NumericOperand operand, final int length, final ByteBuf buffer) {
52 final BitArray operandValues = new BitArray(OPERAND_LENGTH);
53 operandValues.set(END_OF_LIST, operand.isEndOfList());
54 operandValues.set(AND_BIT, operand.isAndBit());
55 operandValues.set(LESS_THAN, operand.isLessThan());
56 operandValues.set(GREATER_THAN, operand.isGreaterThan());
57 operandValues.set(EQUAL, operand.isEquals());
58 final byte byteLength = (byte) (Integer.numberOfTrailingZeros(length) << LENGTH_SHIFT);
59 buffer.writeByte(operandValues.toByte() | byteLength);
63 protected final NumericOperand parse(final byte operand) {
64 final BitArray operandValues = BitArray.valueOf(operand);
65 return new NumericOperand(operandValues.get(AND_BIT), operandValues.get(END_OF_LIST), operandValues.get(EQUAL), operandValues.get(GREATER_THAN), operandValues.get(LESS_THAN));
69 protected String toString(final NumericOperand operand, final boolean isFirst) {
70 final StringBuilder buffer = new StringBuilder();
71 if (operand.isAndBit()) {
72 buffer.append("and ");
73 } else if (!isFirst) {
76 if (operand.isLessThan()) {
77 buffer.append("is less than ");
78 if (operand.isEquals()) {
79 buffer.append("or equals to ");
81 return buffer.toString();
83 if (operand.isGreaterThan()) {
84 buffer.append("is greater than ");
85 if (operand.isEquals()) {
86 buffer.append("or equals to ");
88 return buffer.toString();
90 if (operand.isEquals()) {
91 buffer.append("equals to ");
93 return buffer.toString();