3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7 * and is available at http://www.eclipse.org/legal/epl-v10.html
10 package org.opendaylight.controller.sal.flowprogrammer;
12 import java.io.Serializable;
13 import java.net.Inet6Address;
14 import java.util.ArrayList;
15 import java.util.Iterator;
16 import java.util.List;
18 import javax.xml.bind.annotation.XmlAccessType;
19 import javax.xml.bind.annotation.XmlAccessorType;
20 import javax.xml.bind.annotation.XmlElement;
21 import javax.xml.bind.annotation.XmlRootElement;
23 import org.apache.commons.lang3.builder.EqualsBuilder;
24 import org.apache.commons.lang3.builder.HashCodeBuilder;
25 import org.opendaylight.controller.sal.action.Action;
26 import org.opendaylight.controller.sal.action.ActionType;
27 import org.opendaylight.controller.sal.action.SetDlType;
28 import org.opendaylight.controller.sal.action.SetNwDst;
29 import org.opendaylight.controller.sal.action.SetNwSrc;
30 import org.opendaylight.controller.sal.match.Match;
31 import org.opendaylight.controller.sal.utils.EtherTypes;
34 * Represent a flow: match + actions + flow specific properties
38 @XmlAccessorType(XmlAccessType.NONE)
39 public class Flow implements Cloneable, Serializable {
40 private static final long serialVersionUID = 1L;
44 private List<Action> actions;
46 private short priority;
48 private short idleTimeout;
50 private short hardTimeout;
52 private long id; // unique identifier for this flow
59 public Flow(Match match, List<Action> actions) {
60 if (match.isIPv4() && actionsAreIPv6()) {
62 throw new Exception("Conflicting Match and Action list");
63 } catch (Exception e) {
68 this.actions = actions;
73 * Return a copy of the Match configured on this flow
77 public Match getMatch() {
82 * Set the Match for this flow
83 * This operation will overwrite an existing Match if present
87 public void setMatch(Match match) {
92 * Returns a copy of the actions list of this flow
95 public List<Action> getActions() {
96 return (actions == null) ? null : new ArrayList<Action>(actions);
100 * Set the actions list for this flow
101 * If a list is already present, it will be
102 * replaced with the passed one. During
103 * addition, only the valid actions will be added
104 * It is a no op if the passed actions is null
105 * An empty actions is a vlaid input
109 public void setActions(List<Action> actions) {
110 if (actions == null) {
114 this.actions = new ArrayList<Action>(actions.size());
115 for (Action action : actions) {
116 if (action.isValid()) {
117 this.actions.add(action);
123 * Returns whether the Flow is for IPv4 or IPv6
124 * Information is derived from match and actions list
128 public boolean isIPv6() {
129 return (match.isIPv6()) ? true : actionsAreIPv6();
133 * Returns true if it finds at least one action which is for IPv6
134 * in the list of actions for this Flow
138 private boolean actionsAreIPv6() {
139 if (this.actions != null) {
140 for (Action action : actions) {
141 switch (action.getType()) {
143 if (((SetNwSrc) action).getAddress() instanceof Inet6Address) {
148 if (((SetNwDst) action).getAddress() instanceof Inet6Address) {
153 if (((SetDlType) action).getDlType() == EtherTypes.IPv6
166 public Flow clone() {
169 cloned = (Flow) super.clone();
170 cloned.match = this.getMatch();
171 cloned.actions = this.getActions();
172 } catch (CloneNotSupportedException e) {
179 public int hashCode() {
180 return HashCodeBuilder.reflectionHashCode(this);
184 public boolean equals(Object obj) {
185 return EqualsBuilder.reflectionEquals(this, obj);
189 public String toString() {
190 return "Flow[match = " + match + ", actions = " + actions
191 + ", priority = " + priority + ", id = " + id
192 + ", idleTimeout = " + idleTimeout + ", hardTimeout = "
196 public short getPriority() {
200 public void setPriority(short priority) {
201 this.priority = priority;
204 public short getIdleTimeout() {
208 public void setIdleTimeout(short idleTimeout) {
209 this.idleTimeout = idleTimeout;
212 public short getHardTimeout() {
216 public void setHardTimeout(short hardTimeout) {
217 this.hardTimeout = hardTimeout;
220 public long getId() {
224 public void setId(long id) {
229 * Adds the specified action to the list of action of this flow
232 * @return false if the passed action is null or not valid or if it fails to add it
234 public boolean addAction(Action action) {
235 if (action == null || !action.isValid()) {
238 return actions.add(action);
241 public boolean removeAction(Action action) {
242 if (action == null) {
245 return actions.remove(action);
249 * remove ALL actions of type actionType from the list of actions of this flow
252 * @return false if an action of that type is present and it fails to remove it
254 public boolean removeAction(ActionType actionType) {
255 Iterator<Action> actionIter = this.getActions().iterator();
256 while (actionIter.hasNext()) {
257 Action action = actionIter.next();
258 if (action.getType() == actionType) {
259 if (!this.removeAction(action))