2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.forwardingrulesmanager;
11 import java.io.Serializable;
12 import java.net.Inet6Address;
13 import java.net.InetAddress;
14 import java.util.ArrayList;
15 import java.util.List;
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
20 import javax.xml.bind.annotation.XmlAccessType;
21 import javax.xml.bind.annotation.XmlAccessorType;
22 import javax.xml.bind.annotation.XmlElement;
23 import javax.xml.bind.annotation.XmlRootElement;
25 import org.opendaylight.controller.sal.action.Action;
26 import org.opendaylight.controller.sal.action.ActionType;
27 import org.opendaylight.controller.sal.action.Controller;
28 import org.opendaylight.controller.sal.action.Drop;
29 import org.opendaylight.controller.sal.action.Flood;
30 import org.opendaylight.controller.sal.action.HwPath;
31 import org.opendaylight.controller.sal.action.Loopback;
32 import org.opendaylight.controller.sal.action.Output;
33 import org.opendaylight.controller.sal.action.PopVlan;
34 import org.opendaylight.controller.sal.action.SetDlDst;
35 import org.opendaylight.controller.sal.action.SetDlSrc;
36 import org.opendaylight.controller.sal.action.SetNextHop;
37 import org.opendaylight.controller.sal.action.SetNwDst;
38 import org.opendaylight.controller.sal.action.SetNwSrc;
39 import org.opendaylight.controller.sal.action.SetNwTos;
40 import org.opendaylight.controller.sal.action.SetTpDst;
41 import org.opendaylight.controller.sal.action.SetTpSrc;
42 import org.opendaylight.controller.sal.action.SetVlanId;
43 import org.opendaylight.controller.sal.action.SetVlanPcp;
44 import org.opendaylight.controller.sal.action.SwPath;
45 import org.opendaylight.controller.sal.core.ContainerFlow;
46 import org.opendaylight.controller.sal.core.IContainer;
47 import org.opendaylight.controller.sal.core.Node;
48 import org.opendaylight.controller.sal.core.NodeConnector;
49 import org.opendaylight.controller.sal.flowprogrammer.Flow;
50 import org.opendaylight.controller.sal.match.Match;
51 import org.opendaylight.controller.sal.match.MatchType;
52 import org.opendaylight.controller.sal.utils.GlobalConstants;
53 import org.opendaylight.controller.sal.utils.HexEncode;
54 import org.opendaylight.controller.sal.utils.IPProtocols;
55 import org.opendaylight.controller.sal.utils.NetUtils;
56 import org.opendaylight.controller.sal.utils.NodeConnectorCreator;
57 import org.opendaylight.controller.sal.utils.ServiceHelper;
58 import org.opendaylight.controller.sal.utils.Status;
59 import org.opendaylight.controller.sal.utils.StatusCode;
60 import org.opendaylight.controller.switchmanager.ISwitchManager;
61 import org.opendaylight.controller.switchmanager.Switch;
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
66 * Configuration Java Object which represents a flow configuration information
67 * for Forwarding Rules Manager.
70 @XmlAccessorType(XmlAccessType.NONE)
71 public class FlowConfig implements Serializable {
72 private static final long serialVersionUID = 1L;
73 private static final Logger log = LoggerFactory.getLogger(FlowConfig.class);
74 public static final String staticFlowsGroup = "**StaticFlows";
75 public static final String internalStaticFlowsGroup = "**InternalStaticFlows";
76 public static final String internalStaticFlowBegin = "**";
77 private boolean dynamic;
78 private String status;
81 * The order of the object data defined below is used directly in the UI
82 * built using JSP. Hence try to keep the order in a more logical way.
85 private String installInHw;
91 private String ingressPort;
92 private String portGroup;
94 private String priority;
96 private String etherType;
98 private String vlanId;
100 private String vlanPriority;
102 private String dlSrc;
104 private String dlDst;
106 private String nwSrc;
108 private String nwDst;
110 private String protocol;
112 private String tosBits;
114 private String tpSrc;
116 private String tpDst;
118 private String cookie;
120 private String idleTimeout;
122 private String hardTimeout;
124 private List<String> actions;
126 private enum EtherIPType {
130 public FlowConfig() {
133 public FlowConfig(String installInHw, String name, Node node, String priority, String cookie, String ingressPort,
134 String portGroup, String vlanId, String vlanPriority, String etherType, String srcMac, String dstMac,
135 String protocol, String tosBits, String srcIP, String dstIP, String tpSrc, String tpDst,
136 String idleTimeout, String hardTimeout, List<String> actions) {
138 this.installInHw = installInHw;
141 this.priority = priority;
142 this.cookie = cookie;
143 this.ingressPort = ingressPort;
144 this.portGroup = portGroup;
145 this.vlanId = vlanId;
146 this.vlanPriority = vlanPriority;
147 this.etherType = etherType;
150 this.protocol = protocol;
151 this.tosBits = tosBits;
156 this.idleTimeout = idleTimeout;
157 this.hardTimeout = hardTimeout;
158 this.actions = actions;
159 this.status = StatusCode.SUCCESS.toString();
162 public FlowConfig(FlowConfig from) {
163 this.installInHw = from.installInHw;
164 this.name = from.name;
165 this.node = from.node;
166 this.priority = from.priority;
167 this.cookie = from.cookie;
168 this.ingressPort = from.ingressPort;
169 this.portGroup = from.portGroup;
170 this.vlanId = from.vlanId;
171 this.vlanPriority = from.vlanPriority;
172 this.etherType = from.etherType;
173 this.dlSrc = from.dlSrc;
174 this.dlDst = from.dlDst;
175 this.protocol = from.protocol;
176 this.tosBits = from.tosBits;
177 this.nwSrc = from.nwSrc;
178 this.nwDst = from.nwDst;
179 this.tpSrc = from.tpSrc;
180 this.tpDst = from.tpDst;
181 this.idleTimeout = from.idleTimeout;
182 this.hardTimeout = from.hardTimeout;
183 this.actions = new ArrayList<String>(from.actions);
186 public boolean installInHw() {
187 if (installInHw == null) {
188 // backward compatibility
189 installInHw = "true";
191 return installInHw.equals("true");
194 public void setInstallInHw(boolean inHw) {
195 installInHw = inHw ? "true" : "false";
198 public String getInstallInHw() {
202 public boolean isInternalFlow() {
203 // Controller generated static flows have name starting with "**"
204 return (this.name != null && this.name.startsWith(FlowConfig.internalStaticFlowBegin));
207 public String getName() {
211 public void setName(String name) {
218 public Node getNode() {
222 public void setNode(Node node) {
226 public String getPriority() {
230 public void setPriority(String priority) {
231 this.priority = priority;
234 public String getCookie() {
238 public void setCookie(String cookie) {
239 this.cookie = cookie;
242 public String getIngressPort() {
246 public void setIngressPort(String ingressPort) {
247 this.ingressPort = ingressPort;
250 public String getPortGroup() {
255 public String toString() {
256 return "FlowConfig [dynamic=" + dynamic + ", status=" + status + ", installInHw=" + installInHw + ", name="
257 + name + ", switchId=" + node + ", ingressPort=" + ingressPort + ", portGroup=" + portGroup
258 + ", etherType=" + etherType + ", priority=" + priority + ", vlanId=" + vlanId + ", vlanPriority="
259 + vlanPriority + ", dlSrc=" + dlSrc + ", dlDst=" + dlDst + ", nwSrc=" + nwSrc + ", nwDst=" + nwDst
260 + ", protocol=" + protocol + ", tosBits=" + tosBits + ", tpSrc=" + tpSrc + ", tpDst=" + tpDst
261 + ", cookie=" + cookie + ", idleTimeout=" + idleTimeout + ", hardTimeout=" + hardTimeout + ", actions="
265 public void setPortGroup(String portGroup) {
266 this.portGroup = portGroup;
269 public String getVlanId() {
273 public void setVlanId(String vlanId) {
274 this.vlanId = vlanId;
277 public String getVlanPriority() {
281 public void setVlanPriority(String vlanPriority) {
282 this.vlanPriority = vlanPriority;
285 public String getEtherType() {
289 public void setEtherType(String etherType) {
290 this.etherType = etherType;
293 public String getSrcMac() {
297 public void setSrcMac(String srcMac) {
301 public String getDstMac() {
305 public void setDstMac(String dstMac) {
309 public String getProtocol() {
313 public void setProtocol(String protocol) {
314 this.protocol = protocol;
317 public String getTosBits() {
321 public void setTosBits(String tos_bits) {
322 this.tosBits = tos_bits;
325 public String getSrcIp() {
329 public void setSrcIp(String src_ip) {
333 public String getDstIp() {
337 public void setDstIp(String dst_ip) {
341 public String getSrcPort() {
345 public void setSrcPort(String src_port) {
346 this.tpSrc = src_port;
349 public String getDstPort() {
353 public void setDstPort(String dst_port) {
354 this.tpDst = dst_port;
357 public String getIdleTimeout() {
361 public void setIdleTimeout(String idleTimeout) {
362 this.idleTimeout = idleTimeout;
365 public String getHardTimeout() {
369 public void setHardTimeout(String hardTimeout) {
370 this.hardTimeout = hardTimeout;
373 public boolean isIPv6() {
374 return NetUtils.isIPv6AddressValid(this.getSrcIp()) || NetUtils.isIPv6AddressValid(this.getDstIp());
377 public List<String> getActions() {
381 public void setActions(List<String> actions) {
382 this.actions = actions;
385 public boolean isPortGroupEnabled() {
386 return (portGroup != null);
389 public boolean isDynamic() {
393 public void setDynamic(boolean dynamic) {
394 this.dynamic = dynamic;
397 public String getStatus() {
401 public void setStatus(String status) {
402 this.status = status;
405 public boolean isStatusSuccessful() {
406 return status.equals(StatusCode.SUCCESS.toString());
410 public int hashCode() {
411 final int prime = 31;
413 result = prime * result + ((actions == null) ? 0 : actions.hashCode());
414 result = prime * result + ((cookie == null) ? 0 : cookie.hashCode());
415 result = prime * result + ((dlDst == null) ? 0 : dlDst.hashCode());
416 result = prime * result + ((dlSrc == null) ? 0 : dlSrc.hashCode());
417 result = prime * result + (dynamic ? 1231 : 1237);
418 result = prime * result + ((etherType == null) ? 0 : etherType.hashCode());
419 result = prime * result + ((ingressPort == null) ? 0 : ingressPort.hashCode());
420 result = prime * result + ((name == null) ? 0 : name.hashCode());
421 result = prime * result + ((nwDst == null) ? 0 : nwDst.hashCode());
422 result = prime * result + ((nwSrc == null) ? 0 : nwSrc.hashCode());
423 result = prime * result + ((portGroup == null) ? 0 : portGroup.hashCode());
424 result = prime * result + ((priority == null) ? 0 : priority.hashCode());
425 result = prime * result + ((protocol == null) ? 0 : protocol.hashCode());
426 result = prime * result + ((node == null) ? 0 : node.hashCode());
427 result = prime * result + ((tosBits == null) ? 0 : tosBits.hashCode());
428 result = prime * result + ((tpDst == null) ? 0 : tpDst.hashCode());
429 result = prime * result + ((tpSrc == null) ? 0 : tpSrc.hashCode());
430 result = prime * result + ((vlanId == null) ? 0 : vlanId.hashCode());
431 result = prime * result + ((vlanPriority == null) ? 0 : vlanPriority.hashCode());
432 result = prime * result + ((idleTimeout == null) ? 0 : idleTimeout.hashCode());
433 result = prime * result + ((hardTimeout == null) ? 0 : hardTimeout.hashCode());
438 public boolean equals(Object obj) {
445 if (getClass() != obj.getClass()) {
448 FlowConfig other = (FlowConfig) obj;
449 if (actions == null) {
450 if (other.actions != null) {
453 } else if (!actions.equals(other.actions)) {
456 if (cookie == null) {
457 if (other.cookie != null) {
460 } else if (!cookie.equals(other.cookie)) {
464 if (other.dlDst != null) {
467 } else if (!dlDst.equals(other.dlDst)) {
471 if (other.dlSrc != null) {
474 } else if (!dlSrc.equals(other.dlSrc)) {
477 if (dynamic != other.dynamic) {
480 if (etherType == null) {
481 if (other.etherType != null) {
484 } else if (!etherType.equals(other.etherType)) {
487 if (ingressPort == null) {
488 if (other.ingressPort != null) {
491 } else if (!ingressPort.equals(other.ingressPort)) {
495 if (other.name != null) {
498 } else if (!name.equals(other.name)) {
502 if (other.nwDst != null) {
505 } else if (!nwDst.equals(other.nwDst)) {
509 if (other.nwSrc != null) {
512 } else if (!nwSrc.equals(other.nwSrc)) {
515 if (portGroup == null) {
516 if (other.portGroup != null) {
519 } else if (!portGroup.equals(other.portGroup)) {
522 if (priority == null) {
523 if (other.priority != null) {
526 } else if (!priority.equals(other.priority)) {
529 if (protocol == null) {
530 if (other.protocol != null) {
533 } else if (!protocol.equals(other.protocol)) {
537 if (other.node != null) {
540 } else if (!node.equals(other.node)) {
543 if (tosBits == null) {
544 if (other.tosBits != null) {
547 } else if (!tosBits.equals(other.tosBits)) {
551 if (other.tpDst != null) {
554 } else if (!tpDst.equals(other.tpDst)) {
558 if (other.tpSrc != null) {
561 } else if (!tpSrc.equals(other.tpSrc)) {
564 if (vlanId == null) {
565 if (other.vlanId != null) {
568 } else if (!vlanId.equals(other.vlanId)) {
571 if (vlanPriority == null) {
572 if (other.vlanPriority != null) {
575 } else if (!vlanPriority.equals(other.vlanPriority)) {
578 if (idleTimeout == null) {
579 if (other.idleTimeout != null) {
582 } else if (!idleTimeout.equals(other.idleTimeout)) {
585 if (hardTimeout == null) {
586 if (other.hardTimeout != null) {
589 } else if (!hardTimeout.equals(other.hardTimeout)) {
595 public boolean isL2AddressValid(String mac) {
600 Pattern macPattern = Pattern.compile("([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}");
601 Matcher mm = macPattern.matcher(mac);
603 log.debug("Ethernet address {} is not valid. Example: 00:05:b9:7c:81:5f", mac);
609 public boolean isPortValid(Switch sw, Short port) {
611 log.debug("port {} is not valid", port);
616 log.debug("switch info is not available. Skip checking if port is part of a switch or not.");
620 Set<NodeConnector> nodeConnectorSet = sw.getNodeConnectors();
621 for (NodeConnector nodeConnector : nodeConnectorSet) {
622 if (((Short) nodeConnector.getID()).equals(port)) {
626 log.debug("port {} is not a valid port of node {}", port, sw.getNode());
630 public boolean isVlanIdValid(String vlanId) {
631 int vlan = Integer.decode(vlanId);
632 return ((vlan >= 0) && (vlan < 4096));
635 public boolean isVlanPriorityValid(String vlanPriority) {
636 int pri = Integer.decode(vlanPriority);
637 return ((pri >= 0) && (pri < 8));
640 public boolean isTOSBitsValid(String tosBits) {
641 int tos = Integer.decode(tosBits);
642 return ((tos >= 0) && (tos < 64));
645 public boolean isTpPortValid(String tpPort) {
646 int port = Integer.decode(tpPort);
647 return ((port > 0) && (port <= 0xffff));
650 public boolean isTimeoutValid(String timeout) {
651 int to = Integer.decode(timeout);
652 return ((to >= 0) && (to <= 0xffff));
655 private Status conflictWithContainerFlow(IContainer container) {
656 // Return true if it's default container
657 if (container.getName().equals(GlobalConstants.DEFAULT.toString())) {
658 return new Status(StatusCode.SUCCESS);
661 // No container flow = no conflict
662 List<ContainerFlow> cFlowList = container.getContainerFlows();
663 if (((cFlowList == null)) || cFlowList.isEmpty()) {
664 return new Status(StatusCode.SUCCESS);
667 // Check against each container's flow
668 Flow flow = this.getFlow();
670 // Configuration is rejected if it conflicts with _all_ the container
672 for (ContainerFlow cFlow : cFlowList) {
673 if (cFlow.allowsFlow(flow)) {
674 log.trace("Config is congruent with at least one container flow");
675 return new Status(StatusCode.SUCCESS);
678 String msg = "Flow Config conflicts with all existing container flows";
681 return new Status(StatusCode.BADREQUEST, msg);
684 public Status validate(IContainer container) {
685 EtherIPType etype = EtherIPType.ANY;
686 EtherIPType ipsrctype = EtherIPType.ANY;
687 EtherIPType ipdsttype = EtherIPType.ANY;
689 String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container.getName();
690 ISwitchManager switchManager = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class, containerName,
695 if (name == null || name.trim().isEmpty()) {
696 return new Status(StatusCode.BADREQUEST, "Invalid name");
700 return new Status(StatusCode.BADREQUEST, "Node is null");
703 if (switchManager != null) {
704 for (Switch device : switchManager.getNetworkDevices()) {
705 if (device.getNode().equals(node)) {
711 return new Status(StatusCode.BADREQUEST, String.format("Node %s not found", node));
714 log.debug("switchmanager is not set yet");
717 if (priority != null) {
718 if (Integer.decode(priority) < 0 || (Integer.decode(priority) > 65535)) {
719 return new Status(StatusCode.BADREQUEST, String.format("priority %s is not in the range 0 - 65535",
724 // make sure it's a valid number
725 if (cookie != null) {
729 if (ingressPort != null) {
730 Short port = Short.decode(ingressPort);
731 if (isPortValid(sw, port) == false) {
732 String msg = String.format("Ingress port %d is not valid for the Switch", port);
733 if (!containerName.equals(GlobalConstants.DEFAULT.toString())) {
734 msg += " in Container " + containerName;
736 return new Status(StatusCode.BADREQUEST, msg);
740 if ((vlanId != null) && !isVlanIdValid(vlanId)) {
741 return new Status(StatusCode.BADREQUEST, String.format("Vlan ID %s is not in the range 0 - 4095",
745 if ((vlanPriority != null) && !isVlanPriorityValid(vlanPriority)) {
746 return new Status(StatusCode.BADREQUEST, String.format("Vlan priority %s is not in the range 0 - 7",
749 if (etherType != null) {
750 int type = Integer.decode(etherType);
751 if ((type < 0) || (type > 0xffff)) {
752 return new Status(StatusCode.BADREQUEST, String.format("Ethernet type %s is not valid", etherType));
755 etype = EtherIPType.V4;
756 } else if (type == 0x86dd) {
757 etype = EtherIPType.V6;
762 if ((tosBits != null) && !isTOSBitsValid(tosBits)) {
763 return new Status(StatusCode.BADREQUEST, String.format("IP ToS bits %s is not in the range 0 - 63",
767 if ((tpSrc != null) && !isTpPortValid(tpSrc)) {
768 return new Status(StatusCode.BADREQUEST, String.format("Transport source port %s is not valid", tpSrc));
771 if ((tpDst != null) && !isTpPortValid(tpDst)) {
772 return new Status(StatusCode.BADREQUEST, String.format("Transport destination port %s is not valid",
776 if ((dlSrc != null) && !isL2AddressValid(dlSrc)) {
777 return new Status(StatusCode.BADREQUEST, String.format(
778 "Ethernet source address %s is not valid. Example: 00:05:b9:7c:81:5f", dlSrc));
781 if ((dlDst != null) && !isL2AddressValid(dlDst)) {
782 return new Status(StatusCode.BADREQUEST, String.format(
783 "Ethernet destination address %s is not valid. Example: 00:05:b9:7c:81:5f", dlDst));
787 if (NetUtils.isIPv4AddressValid(nwSrc)) {
788 ipsrctype = EtherIPType.V4;
789 } else if (NetUtils.isIPv6AddressValid(nwSrc)) {
790 ipsrctype = EtherIPType.V6;
792 return new Status(StatusCode.BADREQUEST, String.format("IP source address %s is not valid", nwSrc));
797 if (NetUtils.isIPv4AddressValid(nwDst)) {
798 ipdsttype = EtherIPType.V4;
799 } else if (NetUtils.isIPv6AddressValid(nwDst)) {
800 ipdsttype = EtherIPType.V6;
802 return new Status(StatusCode.BADREQUEST, String.format("IP destination address %s is not valid",
807 if (etype != EtherIPType.ANY) {
808 if ((ipsrctype != EtherIPType.ANY) && (ipsrctype != etype)) {
809 return new Status(StatusCode.BADREQUEST, String.format("Type mismatch between Ethernet & Src IP"));
811 if ((ipdsttype != EtherIPType.ANY) && (ipdsttype != etype)) {
812 return new Status(StatusCode.BADREQUEST, String.format("Type mismatch between Ethernet & Dst IP"));
815 if (ipsrctype != ipdsttype) {
816 if (!((ipsrctype == EtherIPType.ANY) || (ipdsttype == EtherIPType.ANY))) {
817 return new Status(StatusCode.BADREQUEST, String.format("IP Src Dest Type mismatch"));
821 if ((idleTimeout != null) && !isTimeoutValid(idleTimeout)) {
822 return new Status(StatusCode.BADREQUEST, String.format("Idle Timeout value %s is not valid",
826 if ((hardTimeout != null) && !isTimeoutValid(hardTimeout)) {
827 return new Status(StatusCode.BADREQUEST, String.format("Hard Timeout value %s is not valid",
832 if (actions != null && !actions.isEmpty()) {
833 for (String actiongrp : actions) {
834 // check output ports
835 sstr = Pattern.compile("OUTPUT=(.*)").matcher(actiongrp);
836 if (sstr.matches()) {
837 for (String t : sstr.group(1).split(",")) {
838 Matcher n = Pattern.compile("(?:(\\d+))").matcher(t);
840 if (n.group(1) != null) {
841 Short port = Short.parseShort(n.group(1));
842 if (isPortValid(sw, port) == false) {
843 String msg = String.format("Output port %d is not valid for this switch", port);
844 if (!containerName.equals(GlobalConstants.DEFAULT.toString())) {
845 msg += " in Container " + containerName;
847 return new Status(StatusCode.BADREQUEST, msg);
855 sstr = Pattern.compile(ActionType.FLOOD.toString()).matcher(actiongrp);
856 if (sstr.matches()) {
857 if (!containerName.equals(GlobalConstants.DEFAULT.toString())) {
858 return new Status(StatusCode.BADREQUEST, String.format(
859 "flood is not allowed in container %s", containerName));
864 sstr = Pattern.compile(ActionType.SET_NW_SRC.toString() + "=(.*)").matcher(actiongrp);
865 if (sstr.matches()) {
866 if (!NetUtils.isIPv4AddressValid(sstr.group(1))) {
867 return new Status(StatusCode.BADREQUEST, String.format("IP source address %s is not valid",
873 sstr = Pattern.compile(ActionType.SET_NW_DST.toString() + "=(.*)").matcher(actiongrp);
874 if (sstr.matches()) {
875 if (!NetUtils.isIPv4AddressValid(sstr.group(1))) {
876 return new Status(StatusCode.BADREQUEST, String.format(
877 "IP destination address %s is not valid", sstr.group(1)));
882 sstr = Pattern.compile(ActionType.SET_VLAN_ID.toString() + "=(.*)").matcher(actiongrp);
883 if (sstr.matches()) {
884 if ((sstr.group(1) != null) && !isVlanIdValid(sstr.group(1))) {
885 return new Status(StatusCode.BADREQUEST, String.format(
886 "Vlan ID %s is not in the range 0 - 4095", sstr.group(1)));
891 sstr = Pattern.compile(ActionType.SET_VLAN_PCP.toString() + "=(.*)").matcher(actiongrp);
892 if (sstr.matches()) {
893 if ((sstr.group(1) != null) && !isVlanPriorityValid(sstr.group(1))) {
894 return new Status(StatusCode.BADREQUEST, String.format(
895 "Vlan priority %s is not in the range 0 - 7", sstr.group(1)));
900 sstr = Pattern.compile(ActionType.SET_DL_SRC.toString() + "=(.*)").matcher(actiongrp);
901 if (sstr.matches()) {
902 if ((sstr.group(1) != null) && !isL2AddressValid(sstr.group(1))) {
903 return new Status(StatusCode.BADREQUEST, String.format(
904 "Ethernet source address %s is not valid. Example: 00:05:b9:7c:81:5f",
909 sstr = Pattern.compile(ActionType.SET_DL_DST.toString() + "=(.*)").matcher(actiongrp);
910 if (sstr.matches()) {
911 if ((sstr.group(1) != null) && !isL2AddressValid(sstr.group(1))) {
912 return new Status(StatusCode.BADREQUEST, String.format(
913 "Ethernet destination address %s is not valid. Example: 00:05:b9:7c:81:5f",
919 sstr = Pattern.compile(ActionType.SET_NW_TOS.toString() + "=(.*)").matcher(actiongrp);
920 if (sstr.matches()) {
921 if ((sstr.group(1) != null) && !isTOSBitsValid(sstr.group(1))) {
922 return new Status(StatusCode.BADREQUEST, String.format(
923 "IP ToS bits %s is not in the range 0 - 63", sstr.group(1)));
928 sstr = Pattern.compile(ActionType.SET_TP_SRC.toString() + "=(.*)").matcher(actiongrp);
929 if (sstr.matches()) {
930 if ((sstr.group(1) != null) && !isTpPortValid(sstr.group(1))) {
931 return new Status(StatusCode.BADREQUEST, String.format(
932 "Transport source port %s is not valid", sstr.group(1)));
937 sstr = Pattern.compile(ActionType.SET_TP_DST.toString() + "=(.*)").matcher(actiongrp);
938 if (sstr.matches()) {
939 if ((sstr.group(1) != null) && !isTpPortValid(sstr.group(1))) {
940 return new Status(StatusCode.BADREQUEST, String.format(
941 "Transport destination port %s is not valid", sstr.group(1)));
945 sstr = Pattern.compile(ActionType.SET_NEXT_HOP.toString() + "=(.*)").matcher(actiongrp);
946 if (sstr.matches()) {
947 if (!NetUtils.isIPAddressValid(sstr.group(1))) {
948 return new Status(StatusCode.BADREQUEST, String.format(
949 "IP destination address %s is not valid", sstr.group(1)));
955 // Check against the container flow
957 if (!containerName.equals(GlobalConstants.DEFAULT.toString()) && !(status = conflictWithContainerFlow(container)).isSuccess()) {
960 } catch (NumberFormatException e) {
961 return new Status(StatusCode.BADREQUEST, String.format("Invalid number format %s", e.getMessage()));
964 return new Status(StatusCode.SUCCESS);
967 public FlowEntry getFlowEntry() {
968 return new FlowEntry(FlowConfig.staticFlowsGroup, this.name, this.getFlow(), this.getNode());
971 public Flow getFlow() {
972 Match match = new Match();
974 if (this.ingressPort != null) {
975 match.setField(MatchType.IN_PORT,
976 NodeConnectorCreator.createOFNodeConnector(Short.parseShort(ingressPort), getNode()));
978 if (this.dlSrc != null) {
979 match.setField(MatchType.DL_SRC, HexEncode.bytesFromHexString(this.dlSrc));
981 if (this.dlDst != null) {
982 match.setField(MatchType.DL_DST, HexEncode.bytesFromHexString(this.dlDst));
984 if (this.etherType != null) {
985 match.setField(MatchType.DL_TYPE, Integer.decode(etherType).shortValue());
987 if (this.vlanId != null) {
988 match.setField(MatchType.DL_VLAN, Short.parseShort(this.vlanId));
990 if (this.vlanPriority != null) {
991 match.setField(MatchType.DL_VLAN_PR, Byte.parseByte(this.vlanPriority));
993 if (this.nwSrc != null) {
994 String parts[] = this.nwSrc.split("/");
995 InetAddress ip = NetUtils.parseInetAddress(parts[0]);
996 InetAddress mask = null;
998 if (parts.length > 1) {
999 maskLen = Integer.parseInt(parts[1]);
1001 maskLen = (ip instanceof Inet6Address) ? 128 : 32;
1003 mask = NetUtils.getInetNetworkMask(maskLen, ip instanceof Inet6Address);
1004 match.setField(MatchType.NW_SRC, ip, mask);
1006 if (this.nwDst != null) {
1007 String parts[] = this.nwDst.split("/");
1008 InetAddress ip = NetUtils.parseInetAddress(parts[0]);
1009 InetAddress mask = null;
1011 if (parts.length > 1) {
1012 maskLen = Integer.parseInt(parts[1]);
1014 maskLen = (ip instanceof Inet6Address) ? 128 : 32;
1016 mask = NetUtils.getInetNetworkMask(maskLen, ip instanceof Inet6Address);
1017 match.setField(MatchType.NW_DST, ip, mask);
1019 if (this.protocol != null) {
1020 match.setField(MatchType.NW_PROTO, IPProtocols.getProtocolNumberByte(this.protocol));
1022 if (this.tosBits != null) {
1023 match.setField(MatchType.NW_TOS, Byte.parseByte(this.tosBits));
1025 if (this.tpSrc != null) {
1026 match.setField(MatchType.TP_SRC, Integer.valueOf(this.tpSrc).shortValue());
1028 if (this.tpDst != null) {
1029 match.setField(MatchType.TP_DST, Integer.valueOf(this.tpDst).shortValue());
1032 Flow flow = new Flow(match, getActionList());
1033 if (this.cookie != null) {
1034 flow.setId(Long.parseLong(cookie));
1036 if (this.hardTimeout != null) {
1037 flow.setHardTimeout(Short.parseShort(this.hardTimeout));
1039 if (this.idleTimeout != null) {
1040 flow.setIdleTimeout(Short.parseShort(idleTimeout));
1042 if (this.priority != null) {
1043 flow.setPriority(Integer.decode(this.priority).shortValue());
1048 public boolean isByNameAndNodeIdEqual(FlowConfig that) {
1049 return (this.name.equals(that.name) && this.node.equals(that.node));
1052 public boolean isByNameAndNodeIdEqual(String name, Node node) {
1053 return (this.name.equals(name) && this.node.equals(node));
1056 public boolean onNode(Node node) {
1057 return this.node.equals(node);
1060 public void toggleInstallation() {
1061 installInHw = (installInHw == null) ? "true" : (installInHw.equals("true")) ? "false" : "true";
1065 * Parses the actions string and return the List of SAL Action No syntax
1066 * check run, as this function will be called when the config validation
1067 * check has already been performed
1069 private List<Action> getActionList() {
1070 List<Action> actionList = new ArrayList<Action>();
1072 if (actions != null) {
1074 for (String actiongrp : actions) {
1075 sstr = Pattern.compile(ActionType.OUTPUT + "=(.*)").matcher(actiongrp);
1076 if (sstr.matches()) {
1077 for (String t : sstr.group(1).split(",")) {
1078 Matcher n = Pattern.compile("(?:(\\d+))").matcher(t);
1080 if (n.group(1) != null) {
1081 short ofPort = Short.parseShort(n.group(1));
1082 actionList.add(new Output(NodeConnectorCreator.createOFNodeConnector(ofPort,
1090 sstr = Pattern.compile(ActionType.DROP.toString()).matcher(actiongrp);
1091 if (sstr.matches()) {
1092 actionList.add(new Drop());
1096 sstr = Pattern.compile(ActionType.LOOPBACK.toString()).matcher(actiongrp);
1097 if (sstr.matches()) {
1098 actionList.add(new Loopback());
1102 sstr = Pattern.compile(ActionType.FLOOD.toString()).matcher(actiongrp);
1103 if (sstr.matches()) {
1104 actionList.add(new Flood());
1108 sstr = Pattern.compile(ActionType.SW_PATH.toString()).matcher(actiongrp);
1109 if (sstr.matches()) {
1110 actionList.add(new SwPath());
1114 sstr = Pattern.compile(ActionType.HW_PATH.toString()).matcher(actiongrp);
1115 if (sstr.matches()) {
1116 actionList.add(new HwPath());
1120 sstr = Pattern.compile(ActionType.CONTROLLER.toString()).matcher(actiongrp);
1121 if (sstr.matches()) {
1122 actionList.add(new Controller());
1126 sstr = Pattern.compile(ActionType.SET_VLAN_ID.toString() + "=(.*)").matcher(actiongrp);
1127 if (sstr.matches()) {
1128 actionList.add(new SetVlanId(Short.parseShort(sstr.group(1))));
1132 sstr = Pattern.compile(ActionType.SET_VLAN_PCP.toString() + "=(.*)").matcher(actiongrp);
1133 if (sstr.matches()) {
1134 actionList.add(new SetVlanPcp(Byte.parseByte(sstr.group(1))));
1138 sstr = Pattern.compile(ActionType.POP_VLAN.toString()).matcher(actiongrp);
1139 if (sstr.matches()) {
1140 actionList.add(new PopVlan());
1144 sstr = Pattern.compile(ActionType.SET_DL_SRC.toString() + "=(.*)").matcher(actiongrp);
1145 if (sstr.matches()) {
1146 actionList.add(new SetDlSrc(HexEncode.bytesFromHexString(sstr.group(1))));
1150 sstr = Pattern.compile(ActionType.SET_DL_DST.toString() + "=(.*)").matcher(actiongrp);
1151 if (sstr.matches()) {
1152 actionList.add(new SetDlDst(HexEncode.bytesFromHexString(sstr.group(1))));
1155 sstr = Pattern.compile(ActionType.SET_NW_SRC.toString() + "=(.*)").matcher(actiongrp);
1156 if (sstr.matches()) {
1157 actionList.add(new SetNwSrc(NetUtils.parseInetAddress(sstr.group(1))));
1160 sstr = Pattern.compile(ActionType.SET_NW_DST.toString() + "=(.*)").matcher(actiongrp);
1161 if (sstr.matches()) {
1162 actionList.add(new SetNwDst(NetUtils.parseInetAddress(sstr.group(1))));
1166 sstr = Pattern.compile(ActionType.SET_NW_TOS.toString() + "=(.*)").matcher(actiongrp);
1167 if (sstr.matches()) {
1168 actionList.add(new SetNwTos(Byte.parseByte(sstr.group(1))));
1172 sstr = Pattern.compile(ActionType.SET_TP_SRC.toString() + "=(.*)").matcher(actiongrp);
1173 if (sstr.matches()) {
1174 actionList.add(new SetTpSrc(Integer.valueOf(sstr.group(1))));
1178 sstr = Pattern.compile(ActionType.SET_TP_DST.toString() + "=(.*)").matcher(actiongrp);
1179 if (sstr.matches()) {
1180 actionList.add(new SetTpDst(Integer.valueOf(sstr.group(1))));
1184 sstr = Pattern.compile(ActionType.SET_NEXT_HOP.toString() + "=(.*)").matcher(actiongrp);
1185 if (sstr.matches()) {
1186 actionList.add(new SetNextHop(NetUtils.parseInetAddress(sstr.group(1))));