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;
13 import org.opendaylight.protocol.util.BitArray;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev180329.BitmaskOperand;
17 * Parser class for BitmaskOperand.
19 public final class BitmaskOperandParser extends AbstractOperandParser<BitmaskOperand> {
21 public static final BitmaskOperandParser INSTANCE;
24 INSTANCE = new BitmaskOperandParser();
28 public static final String MATCH_VALUE = "match";
30 public static final String NOT_VALUE = "not";
32 private static final int NOT = 6;
33 private static final int MATCH = 7;
35 private BitmaskOperandParser() { }
38 public BitmaskOperand create(final Set<String> opValues) {
39 return new BitmaskOperand(
40 opValues.contains(AND_BIT_VALUE),
41 opValues.contains(END_OF_LIST_VALUE),
42 opValues.contains(MATCH_VALUE),
43 opValues.contains(NOT_VALUE)
48 public void serialize(final BitmaskOperand op, final int length, final boolean endOfList,
49 final ByteBuf buffer) {
50 final BitArray bs = new BitArray(OPERAND_LENGTH);
51 bs.set(END_OF_LIST, endOfList);
52 bs.set(AND_BIT, op.isAndBit());
53 bs.set(MATCH, op.isMatch());
54 bs.set(NOT, op.isNot());
55 final byte len = (byte) (Integer.numberOfTrailingZeros(length) << LENGTH_SHIFT);
56 buffer.writeByte(bs.toByte() | len);
60 public BitmaskOperand parse(final byte op) {
61 final BitArray bs = BitArray.valueOf(op);
62 return new BitmaskOperand(bs.get(AND_BIT), bs.get(END_OF_LIST), bs.get(MATCH), bs.get(NOT));
66 public String toString(final BitmaskOperand op, final boolean isFirst) {
67 final StringBuilder buffer = new StringBuilder();
68 if (!op.isAndBit() && !isFirst) {
72 buffer.append("and ");
75 buffer.append("does ");
77 buffer.append("not ");
79 buffer.append("match ");
80 } else if (op.isNot()) {
81 buffer.append("is not ");
83 return buffer.toString();