Bug 4827: MultiPathSupport utilities
[bgpcep.git] / bgp / flowspec / src / main / java / org / opendaylight / protocol / bgp / flowspec / handlers / BitmaskOperandParser.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.Set;
13
14 import org.opendaylight.protocol.util.BitArray;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.BitmaskOperand;
16
17 /**
18  * Parser class for BitmaskOperand.
19  */
20 public final class BitmaskOperandParser extends AbstractOperandParser<BitmaskOperand> {
21
22     public static final BitmaskOperandParser INSTANCE;
23
24     static {
25         INSTANCE = new BitmaskOperandParser();
26     }
27
28     @VisibleForTesting
29     public static final String MATCH_VALUE = "match";
30     @VisibleForTesting
31     public static final String NOT_VALUE = "not";
32
33     private static final int NOT = 6;
34     private static final int MATCH = 7;
35
36     private BitmaskOperandParser() {
37
38     }
39
40     @Override
41     public BitmaskOperand create(final Set<String> opValues) {
42         return new BitmaskOperand(opValues.contains(AND_BIT_VALUE), opValues.contains(END_OF_LIST_VALUE), opValues.contains(MATCH_VALUE), opValues.contains(NOT_VALUE));
43     }
44
45     @Override
46     public void serialize(final BitmaskOperand op, final int length, final ByteBuf buffer) {
47         final BitArray bs = new BitArray(OPERAND_LENGTH);
48         bs.set(END_OF_LIST, op.isEndOfList());
49         bs.set(AND_BIT, op.isAndBit());
50         bs.set(MATCH, op.isMatch());
51         bs.set(NOT, op.isNot());
52         final byte len = (byte) (Integer.numberOfTrailingZeros(length) << LENGTH_SHIFT);
53         buffer.writeByte(bs.toByte() | len);
54     }
55
56     @Override
57     public BitmaskOperand parse(final byte op) {
58         final BitArray bs = BitArray.valueOf(op);
59         return new BitmaskOperand(bs.get(AND_BIT), bs.get(END_OF_LIST), bs.get(MATCH), bs.get(NOT));
60     }
61
62     @Override
63     public String toString(final BitmaskOperand op, final boolean isFirst) {
64         final StringBuilder buffer = new StringBuilder();
65         if (!op.isAndBit() && !isFirst) {
66             buffer.append("or ");
67         }
68         if (op.isAndBit()) {
69             buffer.append("and ");
70         }
71         if (op.isMatch()) {
72             buffer.append("does ");
73             if (op.isNot()) {
74                 buffer.append("not ");
75             }
76             buffer.append("match ");
77         } else if (op.isNot()) {
78             buffer.append("is not ");
79         }
80         return buffer.toString();
81     }
82 }