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;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * Represent a flow: match + actions + flow specific properties
40 @XmlAccessorType(XmlAccessType.NONE)
41 public class Flow implements Cloneable, Serializable {
42 protected static final Logger logger = LoggerFactory
43 .getLogger(Flow.class);
44 private static final long serialVersionUID = 1L;
48 private List<Action> actions;
50 private short priority;
52 private short idleTimeout;
54 private short hardTimeout;
56 private long id; // unique identifier for this flow
63 public Flow(Match match, List<Action> actions) {
64 if (match.isIPv4() && actionsAreIPv6()) {
66 throw new Exception("Conflicting Match and Action list");
67 } catch (Exception e) {
72 this.actions = actions;
77 * Return a copy of the Match configured on this flow
81 public Match getMatch() {
86 * Set the Match for this flow
87 * This operation will overwrite an existing Match if present
91 public void setMatch(Match match) {
96 * Returns a copy of the actions list of this flow
99 public List<Action> getActions() {
100 return (actions == null) ? null : new ArrayList<Action>(actions);
104 * Set the actions list for this flow
105 * If a list is already present, it will be
106 * replaced with the passed one. During
107 * addition, only the valid actions will be added
108 * It is a no op if the passed actions is null
109 * An empty actions is a vlaid input
113 public void setActions(List<Action> actions) {
114 if (actions == null) {
118 this.actions = new ArrayList<Action>(actions.size());
119 for (Action action : actions) {
120 if (action.isValid()) {
121 this.actions.add(action);
127 * Returns whether the Flow is for IPv4 or IPv6
128 * Information is derived from match and actions list
132 public boolean isIPv6() {
133 return (match.isIPv6()) ? true : actionsAreIPv6();
137 * Returns true if it finds at least one action which is for IPv6
138 * in the list of actions for this Flow
142 private boolean actionsAreIPv6() {
143 if (this.actions != null) {
144 for (Action action : actions) {
145 switch (action.getType()) {
147 if (((SetNwSrc) action).getAddress() instanceof Inet6Address) {
152 if (((SetNwDst) action).getAddress() instanceof Inet6Address) {
157 if (((SetDlType) action).getDlType() == EtherTypes.IPv6
170 public Flow clone() {
173 cloned = (Flow) super.clone();
174 cloned.match = this.getMatch();
175 cloned.actions = this.getActions();
176 } catch (CloneNotSupportedException e) {
183 public int hashCode() {
184 return HashCodeBuilder.reflectionHashCode(this);
188 public boolean equals(Object obj) {
189 return EqualsBuilder.reflectionEquals(this, obj);
193 public String toString() {
194 return "Flow[match = " + match + ", actions = " + actions
195 + ", priority = " + priority + ", id = " + id
196 + ", idleTimeout = " + idleTimeout + ", hardTimeout = "
200 public short getPriority() {
204 public void setPriority(short priority) {
205 this.priority = priority;
208 public short getIdleTimeout() {
212 public void setIdleTimeout(short idleTimeout) {
213 this.idleTimeout = idleTimeout;
216 public short getHardTimeout() {
220 public void setHardTimeout(short hardTimeout) {
221 this.hardTimeout = hardTimeout;
224 public long getId() {
228 public void setId(long id) {
233 * Adds the specified action to the list of action of this flow
236 * @return false if the passed action is null or not valid or if it fails to add it
238 public boolean addAction(Action action) {
239 if (action == null || !action.isValid()) {
242 return actions.add(action);
245 public boolean removeAction(Action action) {
246 if (action == null) {
249 return actions.remove(action);
253 * remove ALL actions of type actionType from the list of actions of this flow
256 * @return false if an action of that type is present and it fails to remove it
258 public boolean removeAction(ActionType actionType) {
259 Iterator<Action> actionIter = this.getActions().iterator();
260 while (actionIter.hasNext()) {
261 Action action = actionIter.next();
262 if (action.getType() == actionType) {
263 if (!this.removeAction(action))