Bump versions to 0.21.7-SNAPSHOT
[bgpcep.git] / bgp / extensions / flowspec / src / main / java / org / opendaylight / protocol / bgp / flowspec / handlers / 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.handlers;
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.rev200120.NumericOperand;
16
17 /**
18  * Common parent class for numeric operands.
19  *
20  * @param <N> numeric operand type
21  */
22 public abstract class AbstractNumericOperandParser<N> extends AbstractOperandParser<NumericOperand> {
23
24     @VisibleForTesting
25     public static final String EQUALS_VALUE = "equals";
26     @VisibleForTesting
27     public static final String GREATER_THAN_VALUE = "greater-than";
28     @VisibleForTesting
29     public static final String LESS_THAN_VALUE = "less-than";
30
31     private static final int LESS_THAN = 5;
32     private static final int GREATER_THAN = 6;
33     private static final int EQUAL = 7;
34
35     @Override
36     public final NumericOperand create(final Set<String> operandValues) {
37         return new NumericOperand(
38                 operandValues.contains(AND_BIT_VALUE),
39                 operandValues.contains(END_OF_LIST_VALUE),
40                 operandValues.contains(EQUALS_VALUE),
41                 operandValues.contains(GREATER_THAN_VALUE),
42                 operandValues.contains(LESS_THAN_VALUE)
43         );
44     }
45
46     @Override
47     public final void serialize(final NumericOperand operand, final int length,
48             final boolean endOfList, final ByteBuf buffer) {
49         final BitArray operandValues = new BitArray(OPERAND_LENGTH);
50         operandValues.set(END_OF_LIST, endOfList);
51         operandValues.set(AND_BIT, operand.getAndBit());
52         operandValues.set(LESS_THAN, operand.getLessThan());
53         operandValues.set(GREATER_THAN, operand.getGreaterThan());
54         operandValues.set(EQUAL, operand.getEquals());
55         final byte byteLength = (byte) (Integer.numberOfTrailingZeros(length) << LENGTH_SHIFT);
56         buffer.writeByte(operandValues.toByte() | byteLength);
57     }
58
59     /**
60      * Serializes specific numeric operand type depending on the length field value.
61      *
62      * @param list of operands to be serialized
63      * @param nlriByteBuf where the operands will be serialized
64      */
65     protected abstract <T extends N> void serialize(List<T> list, ByteBuf nlriByteBuf);
66
67     @Override
68     public final NumericOperand parse(final byte operand) {
69         final BitArray operandValues = BitArray.valueOf(operand);
70         return new NumericOperand(
71                 operandValues.get(AND_BIT),
72                 operandValues.get(END_OF_LIST),
73                 operandValues.get(EQUAL),
74                 operandValues.get(GREATER_THAN),
75                 operandValues.get(LESS_THAN)
76         );
77     }
78
79     @Override
80     public String toString(final NumericOperand operand, final boolean isFirst) {
81         final StringBuilder buffer = new StringBuilder();
82         if (operand.getAndBit()) {
83             buffer.append("and ");
84         } else if (!isFirst) {
85             buffer.append("or ");
86         }
87         if (operand.getLessThan()) {
88             buffer.append("is less than ");
89             if (operand.getEquals()) {
90                 buffer.append("or equals to ");
91             }
92             return buffer.toString();
93         }
94         if (operand.getGreaterThan()) {
95             buffer.append("is greater than ");
96             if (operand.getEquals()) {
97                 buffer.append("or equals to ");
98             }
99             return buffer.toString();
100         }
101         if (operand.getEquals()) {
102             buffer.append("equals to ");
103         }
104         return buffer.toString();
105     }
106
107     protected abstract <T extends N> String toString(List<T> list);
108 }