bd4d2883acc68aa4818a555065f614bc992528a9
[bgpcep.git] / bgp / flowspec / src / main / java / org / opendaylight / protocol / bgp / flowspec / AbstractNumericOperandParser.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 com.google.common.annotations.VisibleForTesting;
11 import io.netty.buffer.ByteBuf;
12 import java.util.List;
13 import java.util.Set;
14 import org.opendaylight.protocol.util.BitArray;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.NumericOperand;
16
17 /**
18  * Common parent class for numeric operands.
19  *
20  * @param <N> numeric operand type
21  */
22 abstract class AbstractNumericOperandParser<N> extends AbstractOperandParser<NumericOperand> {
23
24     @VisibleForTesting
25     static final String EQUALS_VALUE = "equals";
26     @VisibleForTesting
27     static final String GREATER_THAN_VALUE = "greater-than";
28     @VisibleForTesting
29     static final String LESS_THAN_VALUE = "less-than";
30
31     protected static final int LESS_THAN = 5;
32     protected static final int GREATER_THAN = 6;
33     protected static final int EQUAL = 7;
34
35     /**
36      * Serializes specific numeric operand type depending on the length field value.
37      *
38      * @param list of operands to be serialized
39      * @param nlriByteBuf where the operands will be serialized
40      */
41     protected abstract <T extends N> void serialize(final List<T> list, final ByteBuf nlriByteBuf);
42
43     protected abstract <T extends N> String toString(final List<T> list);
44
45     @Override
46     protected final NumericOperand create(final Set<String> opValues) {
47         return new NumericOperand(opValues.contains(AND_BIT_VALUE), opValues.contains(END_OF_LIST_VALUE), opValues.contains(EQUALS_VALUE), opValues.contains(GREATER_THAN_VALUE), opValues.contains(LESS_THAN_VALUE));
48     }
49
50     @Override
51     public final void serialize(final NumericOperand op, final int length, final ByteBuf buffer) {
52         final BitArray bs = new BitArray(OPERAND_LENGTH);
53         bs.set(END_OF_LIST, op.isEndOfList());
54         bs.set(AND_BIT, op.isAndBit());
55         bs.set(LESS_THAN, op.isLessThan());
56         bs.set(GREATER_THAN, op.isGreaterThan());
57         bs.set(EQUAL, op.isEquals());
58         final byte len = (byte) (Integer.numberOfTrailingZeros(length) << LENGTH_SHIFT);
59         buffer.writeByte(bs.toByte() | len);
60     }
61
62     @Override
63     protected final NumericOperand parse(final byte op) {
64         final BitArray bs = BitArray.valueOf(op);
65         return new NumericOperand(bs.get(AND_BIT), bs.get(END_OF_LIST), bs.get(EQUAL), bs.get(GREATER_THAN), bs.get(LESS_THAN));
66     }
67
68     @Override
69     protected String toString(final NumericOperand op, final boolean isFirst) {
70         final StringBuilder buffer = new StringBuilder();
71         if (!op.isAndBit() && !isFirst) {
72             buffer.append("or ");
73         }
74         if (op.isAndBit()) {
75             buffer.append("and ");
76         }
77         if (op.isLessThan() && op.isEquals()) {
78             buffer.append("is less than or equal to ");
79             return buffer.toString();
80         } else if (op.isGreaterThan() && op.isEquals()) {
81             buffer.append("is greater than or equal to ");
82             return buffer.toString();
83         }
84         if (op.isEquals()) {
85             buffer.append("equals to ");
86         }
87         if (op.isLessThan()) {
88             buffer.append("is less than ");
89         }
90         if (op.isGreaterThan()) {
91             buffer.append("is greater than ");
92         }
93         return buffer.toString();
94     }
95 }