@XmlElement
private String name;
+ /** The vlan. */
+ @XmlElement
+ private String dlVlan;
+
/** The network Source. */
@XmlElement
private String nwSrc;
public ContainerFlowConfig(String name, String srcIP, String dstIP, String proto, String srcPort,
String dstPort) {
this.name = name;
+ this.dlVlan = null;
this.nwSrc = srcIP;
this.nwDst = dstIP;
this.protocol = proto;
//this.unidirectional = false;
}
+ public ContainerFlowConfig(String name, String dlVlan, String srcIP, String dstIP, String proto, String srcPort,
+ String dstPort) {
+ this.name = name;
+ this.dlVlan = dlVlan;
+ this.nwSrc = srcIP;
+ this.nwDst = dstIP;
+ this.protocol = proto;
+ this.tpSrc = srcPort;
+ this.tpDst = dstPort;
+ }
+
public ContainerFlowConfig(ContainerFlowConfig containerFlowConfig) {
this.name = containerFlowConfig.name;
+ this.dlVlan = containerFlowConfig.dlVlan;
this.nwSrc = containerFlowConfig.nwSrc;
this.nwDst = containerFlowConfig.nwDst;
this.protocol = containerFlowConfig.protocol;
return name;
}
+ /**
+ * Returns the vlan id.
+ *
+ * @return the Vlan Id
+ */
+ public String getVlan() {
+ return (dlVlan == null || dlVlan.isEmpty()) ? null : dlVlan;
+ }
+
/**
* Returns the Source IP Address.
*
result = prime * result
+ ((protocol == null) ? 0 : protocol.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
+ result = prime * result + ((dlVlan == null) ? 0 : dlVlan.hashCode());
result = prime * result + ((nwDst == null) ? 0 : nwDst.hashCode());
result = prime * result + ((tpDst == null) ? 0 : tpDst.hashCode());
result = prime * result + ((nwSrc == null) ? 0 : nwSrc.hashCode());
return false;
}
ContainerFlowConfig other = (ContainerFlowConfig) obj;
- if (matchName(other) && matchSrcIP(other)
+ if (matchName(other) && matchDlVlan(other) && matchSrcIP(other)
&& matchDstIP(other) && matchProtocol(other)
&& matchSrcPort(other) && matchDstPort(other)) {
return true;
return name.equals(flowSpec.name);
}
+ /**
+ * Match Source IP Address.
+ *
+ * @param flowSpec Flow Specification
+ * @return true, if successful
+ */
+ private boolean matchDlVlan(ContainerFlowConfig flowSpec) {
+ if (dlVlan == flowSpec.dlVlan) {
+ return true;
+ }
+ if (dlVlan == null || flowSpec.dlVlan == null) {
+ return false;
+ }
+ return dlVlan.equals(flowSpec.dlVlan);
+ }
/**
* Match Source IP Address.
return this.tpDst.equals(flowSpec.tpDst);
}
+ /**
+ * Returns the vlan id number
+ *
+ * @return the vlan id number
+ */
+ public Short getVlanId() {
+ Short vlan = 0;
+ try {
+ vlan = Short.parseShort(dlVlan);
+ } catch (NumberFormatException e) {
+
+ }
+ return vlan;
+ }
+
/**
* Returns the Source IP Address mask length.
*
}
/**
- * Returns the protocol
+ * Get the IP protocol value
*
* @return the protocol
*/
public Short getProtoNum() {
- return protocol == null ? IPProtocols.ANY.shortValue() : IPProtocols.getProtocolNumberShort(protocol);
+ return protocol == null ? null : IPProtocols.getProtocolNumberShort(protocol);
}
/**
if (!hasValidName()) {
return new Status(StatusCode.BADREQUEST, "Invalid name");
}
- Status status = validateIPs();
+ Status status = validateVlan();
+ if (!status.isSuccess()) {
+ return status;
+ }
+ status = validateIPs();
if (!status.isSuccess()) {
return status;
}
if(!hasValidProtocol()) {
return new Status(StatusCode.BADREQUEST, "Invalid IP protocol");
}
+ if (!hasValidPorts()) {
+ return new Status(StatusCode.BADREQUEST, "Invalid Source or Destination Port");
+ }
if (this.getMatches().get(0).getMatches() == 0) {
return new Status(StatusCode.BADREQUEST, "Flow Spec is empty");
}
return (name != null && !name.isEmpty() && name.matches(regexName));
}
+ /**
+ * Validates the vlan number
+ *
+ * @return the result of the check as Status object
+ */
+ private Status validateVlan() {
+ if (dlVlan != null) {
+ short vlanId = 0;
+ try {
+ vlanId = Short.parseShort(dlVlan);
+ } catch (NumberFormatException e) {
+ return new Status(StatusCode.BADREQUEST, "Invalid vlan id");
+ }
+ if (vlanId < 0 || vlanId > 0xfff) {
+ return new Status(StatusCode.BADREQUEST, "Invalid vlan id");
+ }
+ }
+ return new Status(StatusCode.SUCCESS);
+ }
+
/**
* Validates the network addresses, checks syntax and semantic
*
return new Status(StatusCode.SUCCESS);
}
+ /**
+ * Validate the protocol field. Either it can be a enum defined in IPProtocols.java
+ * or a valid IP proto value between 0 and 255, see:
+ * http://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml
+ * for more details.
+ *
+ * @return true if a valid protocol value
+ */
private boolean hasValidProtocol() {
- if (protocol != null && !protocol.isEmpty()) {
- return (this.getProtoNum() != 0 || protocol.equalsIgnoreCase("any"));
+ IPProtocols p = IPProtocols.fromString(protocol);
+ return p != null;
+ }
+
+ /**
+ *
+ * @param tpPort
+ * String representing the transport protocol port number
+ * @return true if tpPort contains a decimal value between 0 and 65535
+ */
+ private boolean hasValidPort(String tpPort) {
+ try {
+ int port = Integer.decode(tpPort);
+ return ((port >= 0) && (port <= 0xffff));
+ } catch (NumberFormatException e) {
+ return false;
+ }
+ }
+
+ /**
+ * Validate the transport protocol source and destination ports as
+ * entered by users.
+ *
+ * @return true if ports are defined and are in valid range
+ */
+ private boolean hasValidPorts() {
+ if (tpSrc !=null && !tpSrc.isEmpty()) {
+ if (!hasValidPort(tpSrc)) {
+ return false;
+ }
+ }
+
+ if (tpDst !=null && !tpDst.isEmpty()) {
+ return hasValidPort(tpDst);
}
return true;
}
+
/**
* Returns the matches.
* If unidirectional flag is set, there will be only one match in the list
List<Match> matches = new ArrayList<Match>();
Match match = new Match();
+ if (this.dlVlan != null && !this.dlVlan.isEmpty()) {
+ match.setField(MatchType.DL_VLAN, this.getVlanId());
+ }
if (this.nwSrc != null && !this.nwSrc.trim().isEmpty()) {
String parts[] = this.nwSrc.split("/");
InetAddress ip = NetUtils.parseInetAddress(parts[0]);
mask = NetUtils.getInetNetworkMask(maskLen, ip instanceof Inet6Address);
match.setField(MatchType.NW_DST, ip, mask);
}
- if (this.protocol != null && !this.protocol.trim().isEmpty() && !this.protocol.equalsIgnoreCase("any")) {
- match.setField(MatchType.NW_PROTO, IPProtocols
- .getProtocolNumberByte(this.protocol));
+ if (IPProtocols.fromString(this.protocol) != IPProtocols.ANY) {
+ match.setField(MatchType.NW_PROTO, IPProtocols.getProtocolNumberByte(this.protocol));
}
if (this.tpSrc != null && !this.tpSrc.trim().isEmpty()) {
- Short srcPort = 0;
- try {
- srcPort = Short.parseShort(tpSrc);
- } catch (NumberFormatException e) {
- throw e;
- }
- match.setField(MatchType.TP_SRC, srcPort);
+ match.setField(MatchType.TP_SRC, Integer.valueOf(tpSrc).shortValue());
}
if (this.tpDst != null && !this.tpDst.trim().isEmpty()) {
- Short dstPort = 0;
- try {
- dstPort = Short.parseShort(tpDst);
- } catch (NumberFormatException e) {
- throw e;
- }
- match.setField(MatchType.TP_DST, dstPort);
+ match.setField(MatchType.TP_DST, Integer.valueOf(tpDst).shortValue());
}
matches.add(match);
*/
@Override
public String toString() {
- return "Container Flow={name:" + name + " nwSrc:" + nwSrc + " nwDst:" + nwDst + " " + "protocol:" + protocol
+ return "Container Flow={name:" + name + " dlVlan:" + dlVlan + " nwSrc:" + nwSrc + " nwDst:" + nwDst + " " + "protocol:" + protocol
+ " tpSrc:" + tpSrc + " tpDst:" + tpDst + "}";
}
}