com.github.sdnwiselab.sdnwise.flowtable.FlowTableWindow Maven / Gradle / Ivy
/*
* Copyright (C) 2015 SDN-WISE
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package com.github.sdnwiselab.sdnwise.flowtable;
import java.util.Arrays;
/**
*
* @author seby
*/
public class FlowTableWindow implements FlowTableInterface {
public final static byte SIZE = 4;
public final static byte COPY_SIZE = 4;
// memory
public final static byte SDN_WISE_PACKET = 1;
public final static byte SDN_WISE_STATUS = 0;
// size
public final static byte SDN_WISE_SIZE_0 = 0;
public final static byte SDN_WISE_SIZE_1 = 2;
public final static byte SDN_WISE_SIZE_2 = 4;
// operators
public final static byte SDN_WISE_EQUAL = 8;
public final static byte SDN_WISE_NOT_EQUAL = 16;
public final static byte SDN_WISE_BIGGER = 24;
public final static byte SDN_WISE_LESS = 32;
public final static byte SDN_WISE_EQUAL_OR_BIGGER = 40;
public final static byte SDN_WISE_EQUAL_OR_LESS = 48;
private final static byte operationIndex = 0;
private final static byte offsetIndex = 1;
private final static byte highValueIndex = 2;
private final static byte lowValueIndex = 3;
private final byte[] window = new byte[SIZE];
public FlowTableWindow() {
window[operationIndex] = 0;
window[offsetIndex] = 0;
window[highValueIndex] = 0;
window[lowValueIndex] = 0;
}
public FlowTableWindow(byte[] value) {
if (value.length == 4) {
window[operationIndex] = value[0];
window[offsetIndex] = value[1];
window[highValueIndex] = value[2];
window[lowValueIndex] = value[3];
} else {
window[operationIndex] = 0;
window[offsetIndex] = 0;
window[highValueIndex] = 0;
window[lowValueIndex] = 0;
}
}
public FlowTableWindow(int op, int pos, int value_h, int value_l) {
this.window[operationIndex] = (byte) op;
this.window[offsetIndex] = (byte) pos;
this.window[highValueIndex] = (byte) value_h;
this.window[lowValueIndex] = (byte) value_l;
}
public int getSize() {
return (window[operationIndex] & 0x06) & 0xFF;
}
public int getOperator() {
return (window[operationIndex] & 0xF8) & 0xFF;
}
public int getLocation() {
return (window[operationIndex] & 0x01) & 0xFF;
}
public FlowTableWindow setSize(int value) {
window[operationIndex] = (byte) ((window[operationIndex] & ~(0x06)) | value);
return this;
}
public FlowTableWindow setOperator(int value) {
window[operationIndex] = (byte) ((window[operationIndex] & ~(0xF8)) | value);
return this;
}
public FlowTableWindow setLocation(int value) {
window[operationIndex] = (byte) ((window[operationIndex] & ~(0x01)) | value);
return this;
}
public int getPos() {
return window[offsetIndex] & 0xFF;
}
public FlowTableWindow setPos(int pos) {
this.window[offsetIndex] = (byte) pos;
return this;
}
public int getValueHigh() {
return window[highValueIndex] & 0xFF;
}
public FlowTableWindow setValueHigh(int valueHigh) {
this.window[highValueIndex] = (byte) valueHigh;
return this;
}
public int getValueLow() {
return window[lowValueIndex] & 0xFF;
}
public FlowTableWindow setValueLow(int valueLow) {
this.window[lowValueIndex] = (byte) valueLow;
return this;
}
public int getOp() {
return window[operationIndex] & 0xFF;
}
public void setOp(int op) {
this.window[operationIndex] = (byte) op;
}
@Override
public String toString() {
return (window[operationIndex] & 0xFF) + ","
+ (window[offsetIndex] & 0xFF) + ","
+ (window[highValueIndex] & 0xFF) + ","
+ (window[lowValueIndex] & 0xFF) + " ";
}
@Override
public byte[] toByteArray() {
return Arrays.copyOf(window, SIZE);
}
/**
*
* @return
*/
public String getOperatorToString() {
if (getSize() != SDN_WISE_SIZE_0) {
switch (getOperator()) {
case (8):
return "=";
case (16):
return "!=";
case (24):
return ">";
case (32):
return "<";
case (40):
return ">=";
case (48):
return "<=";
}
}
return "";
}
/**
*
* @return
*/
public String getSizeToString() {
if (getSize() != SDN_WISE_SIZE_0) {
return (getSize() / 2) + "";
}
return "";
}
/**
*
* @return
*/
public String getMemoryToString() {
if (getSize() != SDN_WISE_SIZE_0) {
return getLocation() == SDN_WISE_STATUS ? "STATUS_REG" : "PACKET";
}
return "";
}
/**
*
* @return
*/
public String getAddressToString() {
if (getSize() != SDN_WISE_SIZE_0) {
if (getLocation() == SDN_WISE_STATUS) {
return getPos() + "";
} else {
switch (getPos()) {
case (0):
return "LENGTH";
case (1):
return "NET_ID";
case (2):
return "SRC_HIGH";
case (3):
return "SRC_LOW";
case (4):
return "DST_HIGH";
case (5):
return "DST_LOW";
case (6):
return "TYPE";
case (7):
return "TTL";
case (8):
return "NEXT_HOP_HIGH";
case (9):
return "NEXT_HOP_LOW";
default:
return getPos() + "";
}
}
}
return "";
}
/**
*
* @return
*/
public String getValueToString() {
if (getSize() != 0) {
return Integer.toHexString(window[highValueIndex] * 256
+ window[lowValueIndex]);
} else {
return "";
}
}
}