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;
14 import org.opendaylight.protocol.util.BitArray;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.BitmaskOperand;
18 * Parser class for BitmaskOperand.
20 public final class BitmaskOperandParser extends AbstractOperandParser<BitmaskOperand> {
22 public static final BitmaskOperandParser INSTANCE;
25 INSTANCE = new BitmaskOperandParser();
29 public static final String MATCH_VALUE = "match";
31 public static final String NOT_VALUE = "not";
33 private static final int NOT = 6;
34 private static final int MATCH = 7;
36 private BitmaskOperandParser() {
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));
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);
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));
63 public String toString(final BitmaskOperand op, final boolean isFirst) {
64 final StringBuilder buffer = new StringBuilder();
65 if (!op.isAndBit() && !isFirst) {
69 buffer.append("and ");
72 buffer.append("does ");
74 buffer.append("not ");
76 buffer.append("match ");
77 } else if (op.isNot()) {
78 buffer.append("is not ");
80 return buffer.toString();