DynamicRuleAPI
package com.spawnlabs.server.dmfs.api;
import com.spawnlabs.server.dmfs.DmfsInputException;
import com.spawnlabs.server.dmfs.DmfsNames;
import com.spawnlabs.server.dmfs.DynamicRuleActions;
import com.spawnlabs.server.dmfs.iptables.DmfsExecuteResult;
import com.spawnlabs.server.dmfs.iptables.IptablesStateViolationException;
import com.spawnlabs.server.dmfs.iptables.RuleExecutionException;
import com.spawnlabs.server.dmfs.template.MissingTemplateArgumentException;
import com.spawnlabs.server.dmfs.template.TemplateNotFoundException;
import com.spawnlabs.server.dmfs.template.TemplateReadingException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Map;
/**
* The methods in this class define the supported use-cases.
*/
public class DynamicRuleAPI {
private DynamicRuleActions dynamicRuleActions;
public DynamicRuleAPI(DynamicRuleActions dynamicRuleActions) {
this.dynamicRuleActions = dynamicRuleActions;
}
public DmfsExecuteResult applyRuleTemplate(String connectionClass, String templateName, Map<String, String> params) throws
InvalidArgumentException,
RuleExecutionException,
TemplateReadingException,
TemplateNotFoundException,
MissingTemplateArgumentException {
return dynamicRuleActions.applyRuleTemplate(connectionClass, templateName, params);
}
public DmfsExecuteResult playSessionStart(String connectionClass, String token, String rips, String ripn, String lportn) throws
InvalidArgumentException,
RuleExecutionException,
TemplateReadingException,
TemplateNotFoundException,
MissingTemplateArgumentException,
IptablesStateViolationException {
Integer lportnInt = validateInt(lportn);
InvalidArgumentException iae = null;
InetAddress ripsAddr = null;
try {
ripsAddr = getInetAddressFor(rips);
} catch (UnknownHostException e) {
iae = new InvalidArgumentException(DmfsInputException.INCORRECT_ARG, e, rips);
}
InetAddress ripnAddr = null;
try {
ripnAddr = getInetAddressFor(ripn);
} catch (UnknownHostException e) {
if (iae == null) {
iae = new InvalidArgumentException(DmfsInputException.INCORRECT_ARG, e, ripn);
}
else {
iae.add(e, ripn);
}
}
if (iae != null) {
throw iae;
}
if (DmfsNames.FULL_PLAY_SESSION.equals(connectionClass)) {
return dynamicRuleActions.fullPlaySessionStart(token, ripsAddr, ripnAddr, lportnInt);
}
throw new UnsupportedOperationException("Do not support connectionClass: " + connectionClass + " yet.");
}
public DmfsExecuteResult playSessionStop(String connectionClass, String token) throws
TemplateReadingException,
TemplateNotFoundException,
RuleExecutionException,
MissingTemplateArgumentException,
InvalidArgumentException,
IptablesStateViolationException {
if (DmfsNames.FULL_PLAY_SESSION.equals(connectionClass)) {
return dynamicRuleActions.fullPlaySessionStop(token);
}
throw new UnsupportedOperationException("Do not support connectionClass: " + connectionClass + " yet.");
}
public DmfsExecuteResult playSessionTighten(String connectionClass, String token, String rportn) throws
TemplateReadingException,
TemplateNotFoundException,
RuleExecutionException,
MissingTemplateArgumentException,
InvalidArgumentException {
Integer rportnInt = validateInt(rportn);
if (DmfsNames.FULL_PLAY_SESSION.equals(connectionClass)) {
return dynamicRuleActions.fullPlaySessionTighten(token, rportnInt);
}
throw new UnsupportedOperationException("Do not support connectionClass: " + connectionClass + " yet.");
}
public DmfsExecuteResult playSessionRelax(String connectionClass, String token) throws
TemplateReadingException,
TemplateNotFoundException,
RuleExecutionException,
MissingTemplateArgumentException {
if (DmfsNames.FULL_PLAY_SESSION.equals(connectionClass)) {
return dynamicRuleActions.fullPlaySessionRelax(token);
}
throw new UnsupportedOperationException("Do not support connectionClass: " + connectionClass + " yet.");
}
public DmfsExecuteResult playSessionQuery(String connectionClass, String token, boolean showRoute) throws
TemplateReadingException,
RuleExecutionException,
TemplateNotFoundException,
MissingTemplateArgumentException,
IptablesStateViolationException {
if (DmfsNames.FULL_PLAY_SESSION.equals(connectionClass)) {
return dynamicRuleActions.fullPlaySessionQuery(token, showRoute);
}
throw new UnsupportedOperationException("Do not support connectionClass: " + connectionClass + " yet.");
}
public DmfsExecuteResult queryAllPlaySessions() throws RuleExecutionException {
return dynamicRuleActions.queryAllPlaySessions();
}
private int validateInt(String port) throws InvalidArgumentException {
int rportnInt;
try {
rportnInt = Integer.valueOf(port);
} catch (NumberFormatException e) {
throw new InvalidArgumentException(DmfsInputException.INCORRECT_ARG, e, port);
}
return rportnInt;
}
protected InetAddress getInetAddressFor(String ip) throws UnknownHostException, InvalidArgumentException {
if (ip.contains(".")) {
return getIpv4Address(ip);
}
if (ip.contains(":")) {
return getIpv6Address(ip);
}
throw new InvalidArgumentException(DmfsInputException.INCORRECT_ARG, ip);
}
protected InetAddress getIpv4Address(String ip) throws InvalidArgumentException {
String[] bytesS = ip.split("\\.");
byte[] bytes = new byte[4];
try {
for (int i = 0; i < bytesS.length; i++) {
bytes[i] = Byte.valueOf(bytesS[i]);
}
} catch (ArrayIndexOutOfBoundsException e) {
throw new InvalidArgumentException(DmfsInputException.INCORRECT_ARG, ip);
} catch (NumberFormatException nfe) {
throw new InvalidArgumentException(DmfsInputException.INCORRECT_ARG, nfe, ip);
}
InetAddress address = null;
try {
address = InetAddress.getByAddress(bytes);
} catch (UnknownHostException ignored) {
}// would only happen if bytes.length was too long, and we're explicitly setting that length
return address;
}
protected InetAddress getIpv6Address(String ip) {
return null;
}
}