com.github.sdnwiselab.sdnwise.flowtable.FlowTableAction 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;
import static com.github.sdnwiselab.sdnwise.flowtable.FlowTableWindow.*;
/**
*
* @author seby
*/
public class FlowTableAction implements FlowTableInterface{
public final static byte SIZE = 4;
public final static byte COPY_SIZE = 4;
// multimatch
public final static byte SDN_WISE_MULTI = 2;
public final static byte SDN_WISE_NOT_MULTI = 0;
// actions
public final static byte SDN_WISE_FORWARD_UNICAST = 4;
public final static byte SDN_WISE_FORWARD_BROADCAST = 8;
public final static byte SDN_WISE_DROP = 12;
public final static byte SDN_WISE_MODIFY = 16;
public final static byte SDN_WISE_AGGREGATE = 20;
public final static byte SDN_WISE_FORWARD_UP = 24;
private final byte[] action = new byte[SIZE];
private final byte actionIndex = 0;
private final byte positionIndex = 1;
private final byte highValueIndex = 2;
private final byte lowValueIndex = 3;
public FlowTableAction(int act, int pos, int valueHigh, int valueLow) {
this.action[actionIndex]= (byte) act;
this.action[positionIndex]= (byte) pos;
this.action[highValueIndex]= (byte) valueHigh;
this.action[lowValueIndex]= (byte) valueLow;
}
public FlowTableAction(byte[] value) {
if (value.length == 4) {
action[actionIndex]= value[0];
action[positionIndex]= value[1];
action[highValueIndex]= value[2];
action[lowValueIndex]= value[3];
} else {
action[actionIndex]= 0;
action[positionIndex]= 0;
action[highValueIndex]= 0;
action[lowValueIndex]= 0;
}
}
public FlowTableAction() {
action[actionIndex]= 0;
action[positionIndex]= 0;
action[highValueIndex]= 0;
action[lowValueIndex]= 0;
}
public boolean getMultimatch() {
return (((action[actionIndex]& 0x02) >> 1) == 1);
}
public int getType() {
return (action[actionIndex]& 0xFC) & 0xFF;
}
public int getLocation() {
return (action[actionIndex]& 0x01) & 0xFF;
}
public FlowTableAction setMultimatch(boolean value) {
action[actionIndex]= (byte) ((action[actionIndex]& ~(0x02)) |
((value ? 2 : 0)));
return this;
}
public FlowTableAction setType(int value) {
action[actionIndex]= (byte) ((action[actionIndex]& ~(0xFC)) | value);
return this;
}
public FlowTableAction setLocation(int value) {
action[actionIndex]= (byte) ((action[actionIndex]& ~(0x01)) | value);
return this;
}
public int getPos() {
return action[positionIndex]& 0xFF;
}
public FlowTableAction setPos(int pos) {
this.action[positionIndex]= (byte) pos;
return this;
}
public int getValueHigh() {
return action[highValueIndex]& 0xFF;
}
public FlowTableAction setValueHigh(int valueHigh) {
this.action[highValueIndex]= (byte) valueHigh;
return this;
}
public int getValueLow() {
return action[lowValueIndex]& 0xFF;
}
public FlowTableAction setValueLow(int valueLow) {
this.action[lowValueIndex]= (byte) valueLow;
return this;
}
public int getAct() {
return action[actionIndex]& 0xFF;
}
public void setAct(int act) {
this.action[actionIndex]= (byte) act;
}
@Override
public String toString() {
return (action[actionIndex]& 0xFF)
+ "," + (action[positionIndex]& 0xFF) + "," +
(action[highValueIndex]& 0xFF) + "," +
(action[lowValueIndex]& 0xFF) + " ";
}
@Override
public byte[] toByteArray() {
return Arrays.copyOf(action,SIZE);
}
/**
*
* @return
*/
public String getTypeToString() {
switch (getType()) {
case (4):
return "FORWARD_UNICAST";
case (8):
return "FORWARD_BROADCAST";
case (12):
return "DROP";
case (16):
return "MODIFY";
case (20):
return "AGGREGATE";
case (24):
return "FORWARD_TO_APP";
}
return "";
}
/**
*
* @return
*/
public String getMemoryToString() {
return getLocation() == 0 ? "STATUS_REG" : "PACKET";
}
/**
*
* @return
*/
public String getAddressToString() {
if (getLocation() == SDN_WISE_STATUS) {
return action[positionIndex] + "";
} else {
switch (action[positionIndex]) {
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 action[positionIndex] + "";
}
}
}
/**
*
* @return
*/
public String getValueToString() {
return Integer.toHexString(action[highValueIndex] * 256 +
action[lowValueIndex]);
}
}