X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fforwardingrulesmanager%2Fapi%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fforwardingrulesmanager%2FFlowConfig.java;h=de7597730f9062a85d7071b3986044c4874a4e7a;hb=25344a336764b540d4cbd7ce28151058af69049b;hp=2b9696ddb9cdf058640caa671735757556fb1a5a;hpb=639a0c98ee7d38c8e1423bce4460d0402353d2a3;p=controller.git diff --git a/opendaylight/forwardingrulesmanager/api/src/main/java/org/opendaylight/controller/forwardingrulesmanager/FlowConfig.java b/opendaylight/forwardingrulesmanager/api/src/main/java/org/opendaylight/controller/forwardingrulesmanager/FlowConfig.java index 2b9696ddb9..de7597730f 100644 --- a/opendaylight/forwardingrulesmanager/api/src/main/java/org/opendaylight/controller/forwardingrulesmanager/FlowConfig.java +++ b/opendaylight/forwardingrulesmanager/api/src/main/java/org/opendaylight/controller/forwardingrulesmanager/FlowConfig.java @@ -13,7 +13,6 @@ import java.net.Inet6Address; import java.net.InetAddress; import java.util.ArrayList; import java.util.List; -import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -26,6 +25,7 @@ import org.opendaylight.controller.sal.action.Action; import org.opendaylight.controller.sal.action.ActionType; import org.opendaylight.controller.sal.action.Controller; import org.opendaylight.controller.sal.action.Drop; +import org.opendaylight.controller.sal.action.Enqueue; import org.opendaylight.controller.sal.action.Flood; import org.opendaylight.controller.sal.action.HwPath; import org.opendaylight.controller.sal.action.Loopback; @@ -71,9 +71,11 @@ import org.slf4j.LoggerFactory; public class FlowConfig implements Serializable { private static final long serialVersionUID = 1L; private static final Logger log = LoggerFactory.getLogger(FlowConfig.class); - public static final String staticFlowsGroup = "**StaticFlows"; - public static final String internalStaticFlowsGroup = "**InternalStaticFlows"; - public static final String internalStaticFlowBegin = "**"; + private static final String NAMEREGEX = "^[a-zA-Z0-9]+$"; + public static final String STATICFLOWGROUP = "__StaticFlows__"; + public static final String INTERNALSTATICFLOWGROUP = "__InternalStaticFlows__"; + public static final String INTERNALSTATICFLOWBEGIN = "__"; + public static final String INTERNALSTATICFLOWEND = "__"; private boolean dynamic; private String status; @@ -186,13 +188,13 @@ public class FlowConfig implements Serializable { public boolean installInHw() { if (installInHw == null) { // backward compatibility - installInHw = "true"; + installInHw = Boolean.toString(true); } - return installInHw.equals("true"); + return Boolean.valueOf(installInHw); } public void setInstallInHw(boolean inHw) { - installInHw = inHw ? "true" : "false"; + installInHw = String.valueOf(inHw); } public String getInstallInHw() { @@ -200,8 +202,9 @@ public class FlowConfig implements Serializable { } public boolean isInternalFlow() { - // Controller generated static flows have name starting with "**" - return (this.name != null && this.name.startsWith(FlowConfig.internalStaticFlowBegin)); + return (this.name != null && + this.name.startsWith(FlowConfig.INTERNALSTATICFLOWBEGIN) && + this.name.endsWith(FlowConfig.INTERNALSTATICFLOWEND)); } public String getName() { @@ -606,25 +609,13 @@ public class FlowConfig implements Serializable { return true; } - public boolean isPortValid(Switch sw, Short port) { - if (port < 1) { - log.debug("port {} is not valid", port); - return false; - } - + public boolean isPortValid(Switch sw, String port) { if (sw == null) { log.debug("switch info is not available. Skip checking if port is part of a switch or not."); return true; } - - Set nodeConnectorSet = sw.getNodeConnectors(); - for (NodeConnector nodeConnector : nodeConnectorSet) { - if (((Short) nodeConnector.getID()).equals(port)) { - return true; - } - } - log.debug("port {} is not a valid port of node {}", port, sw.getNode()); - return false; + NodeConnector nc = NodeConnectorCreator.createNodeConnector(port, sw.getNode()); + return sw.getNodeConnectors().contains(nc); } public boolean isVlanIdValid(String vlanId) { @@ -644,7 +635,7 @@ public class FlowConfig implements Serializable { public boolean isTpPortValid(String tpPort) { int port = Integer.decode(tpPort); - return ((port > 0) && (port <= 0xffff)); + return ((port >= 0) && (port <= 0xffff)); } public boolean isTimeoutValid(String timeout) { @@ -652,6 +643,11 @@ public class FlowConfig implements Serializable { return ((to >= 0) && (to <= 0xffff)); } + public boolean isProtocolValid(String protocol) { + IPProtocols proto = IPProtocols.fromString(protocol); + return (proto != null); + } + private Status conflictWithContainerFlow(IContainer container) { // Return true if it's default container if (container.getName().equals(GlobalConstants.DEFAULT.toString())) { @@ -692,7 +688,7 @@ public class FlowConfig implements Serializable { Switch sw = null; try { - if (name == null || name.trim().isEmpty()) { + if (name == null || name.trim().isEmpty() || !name.matches(FlowConfig.NAMEREGEX)) { return new Status(StatusCode.BADREQUEST, "Invalid name"); } @@ -727,9 +723,8 @@ public class FlowConfig implements Serializable { } if (ingressPort != null) { - Short port = Short.decode(ingressPort); - if (isPortValid(sw, port) == false) { - String msg = String.format("Ingress port %d is not valid for the Switch", port); + if (!isPortValid(sw, ingressPort)) { + String msg = String.format("Ingress port %s is not valid for the Switch", ingressPort); if (!containerName.equals(GlobalConstants.DEFAULT.toString())) { msg += " in Container " + containerName; } @@ -759,6 +754,10 @@ public class FlowConfig implements Serializable { } } + if ((protocol != null) && !isProtocolValid(protocol)) { + return new Status(StatusCode.BADREQUEST, String.format("Protocol %s is not valid", protocol)); + } + if ((tosBits != null) && !isTOSBitsValid(tosBits)) { return new Status(StatusCode.BADREQUEST, String.format("IP ToS bits %s is not in the range 0 - 63", tosBits)); @@ -829,127 +828,154 @@ public class FlowConfig implements Serializable { } Matcher sstr; - if (actions != null && !actions.isEmpty()) { - for (String actiongrp : actions) { - // check output ports - sstr = Pattern.compile("OUTPUT=(.*)").matcher(actiongrp); - if (sstr.matches()) { - for (String t : sstr.group(1).split(",")) { - Matcher n = Pattern.compile("(?:(\\d+))").matcher(t); - if (n.matches()) { - if (n.group(1) != null) { - Short port = Short.parseShort(n.group(1)); - if (isPortValid(sw, port) == false) { - String msg = String.format("Output port %d is not valid for this switch", port); - if (!containerName.equals(GlobalConstants.DEFAULT.toString())) { - msg += " in Container " + containerName; - } - return new Status(StatusCode.BADREQUEST, msg); + if (actions == null || actions.isEmpty()) { + return new Status(StatusCode.BADREQUEST, "Actions value is null or empty"); + } + for (String actiongrp : actions) { + // check output ports + sstr = Pattern.compile("OUTPUT=(.*)").matcher(actiongrp); + if (sstr.matches()) { + for (String t : sstr.group(1).split(",")) { + Matcher n = Pattern.compile("(?:(\\d+))").matcher(t); + if (n.matches()) { + String port = n.group(1); + if (port != null) { + if (!isPortValid(sw, port)) { + String msg = String.format("Output port %s is not valid for this switch", port); + if (!containerName.equals(GlobalConstants.DEFAULT.toString())) { + msg += " in Container " + containerName; } + return new Status(StatusCode.BADREQUEST, msg); } } + } else { + String msg = String.format("Output port %s is not valid", t); + return new Status(StatusCode.BADREQUEST, msg); } - continue; } - // Check src IP - sstr = Pattern.compile(ActionType.FLOOD.toString()).matcher(actiongrp); - if (sstr.matches()) { - if (!containerName.equals(GlobalConstants.DEFAULT.toString())) { - return new Status(StatusCode.BADREQUEST, String.format( - "flood is not allowed in container %s", containerName)); + continue; + } + // check enqueue + sstr = Pattern.compile("ENQUEUE=(.*)").matcher(actiongrp); + if (sstr.matches()) { + for (String t : sstr.group(1).split(",")) { + Matcher n = Pattern.compile("(?:(\\d+:\\d+))").matcher(t); + if (n.matches()) { + if (n.group(1) != null) { + String port = n.group(1).split(":")[0]; + if (!isPortValid(sw, port)) { + String msg = String.format("Output port %d is not valid for this switch", port); + if (!containerName.equals(GlobalConstants.DEFAULT.toString())) { + msg += " in Container " + containerName; + } + return new Status(StatusCode.BADREQUEST, msg); + } + } + } else { + String msg = String.format("Enqueue port %s is not valid", t); + return new Status(StatusCode.BADREQUEST, msg); } - continue; } - // Check src IP - sstr = Pattern.compile(ActionType.SET_NW_SRC.toString() + "=(.*)").matcher(actiongrp); - if (sstr.matches()) { - if (!NetUtils.isIPv4AddressValid(sstr.group(1))) { - return new Status(StatusCode.BADREQUEST, String.format("IP source address %s is not valid", - sstr.group(1))); - } - continue; + continue; + } + // Check src IP + sstr = Pattern.compile(ActionType.FLOOD.toString()).matcher(actiongrp); + if (sstr.matches()) { + if (!containerName.equals(GlobalConstants.DEFAULT.toString())) { + return new Status(StatusCode.BADREQUEST, String.format( + "flood is not allowed in container %s", containerName)); } - // Check dst IP - sstr = Pattern.compile(ActionType.SET_NW_DST.toString() + "=(.*)").matcher(actiongrp); - if (sstr.matches()) { - if (!NetUtils.isIPv4AddressValid(sstr.group(1))) { - return new Status(StatusCode.BADREQUEST, String.format( - "IP destination address %s is not valid", sstr.group(1))); - } - continue; + continue; + } + // Check src IP + sstr = Pattern.compile(ActionType.SET_NW_SRC.toString() + "=(.*)").matcher(actiongrp); + if (sstr.matches()) { + if (!NetUtils.isIPv4AddressValid(sstr.group(1))) { + return new Status(StatusCode.BADREQUEST, String.format("IP source address %s is not valid", + sstr.group(1))); } + continue; + } + // Check dst IP + sstr = Pattern.compile(ActionType.SET_NW_DST.toString() + "=(.*)").matcher(actiongrp); + if (sstr.matches()) { + if (!NetUtils.isIPv4AddressValid(sstr.group(1))) { + return new Status(StatusCode.BADREQUEST, String.format( + "IP destination address %s is not valid", sstr.group(1))); + } + continue; + } - sstr = Pattern.compile(ActionType.SET_VLAN_ID.toString() + "=(.*)").matcher(actiongrp); - if (sstr.matches()) { - if ((sstr.group(1) != null) && !isVlanIdValid(sstr.group(1))) { - return new Status(StatusCode.BADREQUEST, String.format( - "Vlan ID %s is not in the range 0 - 4095", sstr.group(1))); - } - continue; + sstr = Pattern.compile(ActionType.SET_VLAN_ID.toString() + "=(.*)").matcher(actiongrp); + if (sstr.matches()) { + if ((sstr.group(1) != null) && !isVlanIdValid(sstr.group(1))) { + return new Status(StatusCode.BADREQUEST, String.format( + "Vlan ID %s is not in the range 0 - 4095", sstr.group(1))); } + continue; + } - sstr = Pattern.compile(ActionType.SET_VLAN_PCP.toString() + "=(.*)").matcher(actiongrp); - if (sstr.matches()) { - if ((sstr.group(1) != null) && !isVlanPriorityValid(sstr.group(1))) { - return new Status(StatusCode.BADREQUEST, String.format( - "Vlan priority %s is not in the range 0 - 7", sstr.group(1))); - } - continue; + sstr = Pattern.compile(ActionType.SET_VLAN_PCP.toString() + "=(.*)").matcher(actiongrp); + if (sstr.matches()) { + if ((sstr.group(1) != null) && !isVlanPriorityValid(sstr.group(1))) { + return new Status(StatusCode.BADREQUEST, String.format( + "Vlan priority %s is not in the range 0 - 7", sstr.group(1))); } + continue; + } - sstr = Pattern.compile(ActionType.SET_DL_SRC.toString() + "=(.*)").matcher(actiongrp); - if (sstr.matches()) { - if ((sstr.group(1) != null) && !isL2AddressValid(sstr.group(1))) { - return new Status(StatusCode.BADREQUEST, String.format( - "Ethernet source address %s is not valid. Example: 00:05:b9:7c:81:5f", - sstr.group(1))); - } - continue; + sstr = Pattern.compile(ActionType.SET_DL_SRC.toString() + "=(.*)").matcher(actiongrp); + if (sstr.matches()) { + if ((sstr.group(1) != null) && !isL2AddressValid(sstr.group(1))) { + return new Status(StatusCode.BADREQUEST, String.format( + "Ethernet source address %s is not valid. Example: 00:05:b9:7c:81:5f", + sstr.group(1))); } - sstr = Pattern.compile(ActionType.SET_DL_DST.toString() + "=(.*)").matcher(actiongrp); - if (sstr.matches()) { - if ((sstr.group(1) != null) && !isL2AddressValid(sstr.group(1))) { - return new Status(StatusCode.BADREQUEST, String.format( - "Ethernet destination address %s is not valid. Example: 00:05:b9:7c:81:5f", - sstr.group(1))); - } - continue; + continue; + } + sstr = Pattern.compile(ActionType.SET_DL_DST.toString() + "=(.*)").matcher(actiongrp); + if (sstr.matches()) { + if ((sstr.group(1) != null) && !isL2AddressValid(sstr.group(1))) { + return new Status(StatusCode.BADREQUEST, String.format( + "Ethernet destination address %s is not valid. Example: 00:05:b9:7c:81:5f", + sstr.group(1))); } + continue; + } - sstr = Pattern.compile(ActionType.SET_NW_TOS.toString() + "=(.*)").matcher(actiongrp); - if (sstr.matches()) { - if ((sstr.group(1) != null) && !isTOSBitsValid(sstr.group(1))) { - return new Status(StatusCode.BADREQUEST, String.format( - "IP ToS bits %s is not in the range 0 - 63", sstr.group(1))); - } - continue; + sstr = Pattern.compile(ActionType.SET_NW_TOS.toString() + "=(.*)").matcher(actiongrp); + if (sstr.matches()) { + if ((sstr.group(1) != null) && !isTOSBitsValid(sstr.group(1))) { + return new Status(StatusCode.BADREQUEST, String.format( + "IP ToS bits %s is not in the range 0 - 63", sstr.group(1))); } + continue; + } - sstr = Pattern.compile(ActionType.SET_TP_SRC.toString() + "=(.*)").matcher(actiongrp); - if (sstr.matches()) { - if ((sstr.group(1) != null) && !isTpPortValid(sstr.group(1))) { - return new Status(StatusCode.BADREQUEST, String.format( - "Transport source port %s is not valid", sstr.group(1))); - } - continue; + sstr = Pattern.compile(ActionType.SET_TP_SRC.toString() + "=(.*)").matcher(actiongrp); + if (sstr.matches()) { + if ((sstr.group(1) != null) && !isTpPortValid(sstr.group(1))) { + return new Status(StatusCode.BADREQUEST, String.format( + "Transport source port %s is not valid", sstr.group(1))); } + continue; + } - sstr = Pattern.compile(ActionType.SET_TP_DST.toString() + "=(.*)").matcher(actiongrp); - if (sstr.matches()) { - if ((sstr.group(1) != null) && !isTpPortValid(sstr.group(1))) { - return new Status(StatusCode.BADREQUEST, String.format( - "Transport destination port %s is not valid", sstr.group(1))); - } - continue; + sstr = Pattern.compile(ActionType.SET_TP_DST.toString() + "=(.*)").matcher(actiongrp); + if (sstr.matches()) { + if ((sstr.group(1) != null) && !isTpPortValid(sstr.group(1))) { + return new Status(StatusCode.BADREQUEST, String.format( + "Transport destination port %s is not valid", sstr.group(1))); } - sstr = Pattern.compile(ActionType.SET_NEXT_HOP.toString() + "=(.*)").matcher(actiongrp); - if (sstr.matches()) { - if (!NetUtils.isIPAddressValid(sstr.group(1))) { - return new Status(StatusCode.BADREQUEST, String.format( - "IP destination address %s is not valid", sstr.group(1))); - } - continue; + continue; + } + sstr = Pattern.compile(ActionType.SET_NEXT_HOP.toString() + "=(.*)").matcher(actiongrp); + if (sstr.matches()) { + if (!NetUtils.isIPAddressValid(sstr.group(1))) { + return new Status(StatusCode.BADREQUEST, String.format( + "IP destination address %s is not valid", sstr.group(1))); } + continue; } } // Check against the container flow @@ -965,7 +991,8 @@ public class FlowConfig implements Serializable { } public FlowEntry getFlowEntry() { - return new FlowEntry(FlowConfig.staticFlowsGroup, this.name, this.getFlow(), this.getNode()); + String group = this.isInternalFlow() ? FlowConfig.INTERNALSTATICFLOWGROUP : FlowConfig.STATICFLOWGROUP; + return new FlowEntry(group, this.name, this.getFlow(), this.getNode()); } public Flow getFlow() { @@ -973,7 +1000,7 @@ public class FlowConfig implements Serializable { if (this.ingressPort != null) { match.setField(MatchType.IN_PORT, - NodeConnectorCreator.createOFNodeConnector(Short.parseShort(ingressPort), getNode())); + NodeConnector.fromString(String.format("%s|%s@%s", node.getType(), ingressPort, node.toString()))); } if (this.dlSrc != null) { match.setField(MatchType.DL_SRC, HexEncode.bytesFromHexString(this.dlSrc)); @@ -1016,7 +1043,7 @@ public class FlowConfig implements Serializable { mask = NetUtils.getInetNetworkMask(maskLen, ip instanceof Inet6Address); match.setField(MatchType.NW_DST, ip, mask); } - if (this.protocol != null) { + if (IPProtocols.fromString(this.protocol) != IPProtocols.ANY) { match.setField(MatchType.NW_PROTO, IPProtocols.getProtocolNumberByte(this.protocol)); } if (this.tosBits != null) { @@ -1058,7 +1085,7 @@ public class FlowConfig implements Serializable { } public void toggleInstallation() { - installInHw = (installInHw == null) ? "true" : (installInHw.equals("true")) ? "false" : "true"; + installInHw = (installInHw == null) ? Boolean.toString(false) : Boolean.toString(!Boolean.valueOf(installInHw)); } /* @@ -1078,9 +1105,28 @@ public class FlowConfig implements Serializable { Matcher n = Pattern.compile("(?:(\\d+))").matcher(t); if (n.matches()) { if (n.group(1) != null) { - short ofPort = Short.parseShort(n.group(1)); - actionList.add(new Output(NodeConnectorCreator.createOFNodeConnector(ofPort, - this.getNode()))); + String nc = String.format("%s|%s@%s", node.getType(), n.group(1), node.toString()); + actionList.add(new Output(NodeConnector.fromString(nc))); + } + } + } + continue; + } + + sstr = Pattern.compile(ActionType.ENQUEUE + "=(.*)").matcher(actiongrp); + if (sstr.matches()) { + for (String t : sstr.group(1).split(",")) { + Matcher n = Pattern.compile("(?:(\\d+:\\d+))").matcher(t); + if (n.matches()) { + if (n.group(1) != null) { + String parts[] = n.group(1).split(":"); + String nc = String.format("%s|%s@%s", node.getType(), parts[0], node.toString()); + if (parts.length == 1) { + actionList.add(new Enqueue(NodeConnector.fromString(nc))); + } else { + actionList + .add(new Enqueue(NodeConnector.fromString(nc), Integer.parseInt(parts[1]))); + } } } }