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.sal.flowprogrammer;
11 import java.io.Serializable;
12 import java.net.Inet6Address;
13 import java.util.ArrayList;
14 import java.util.Iterator;
15 import java.util.List;
17 import javax.xml.bind.annotation.XmlAccessType;
18 import javax.xml.bind.annotation.XmlAccessorType;
19 import javax.xml.bind.annotation.XmlElement;
20 import javax.xml.bind.annotation.XmlRootElement;
22 import org.opendaylight.controller.sal.action.Action;
23 import org.opendaylight.controller.sal.action.ActionType;
24 import org.opendaylight.controller.sal.action.SetDlType;
25 import org.opendaylight.controller.sal.action.SetNwDst;
26 import org.opendaylight.controller.sal.action.SetNwSrc;
27 import org.opendaylight.controller.sal.match.Match;
28 import org.opendaylight.controller.sal.utils.EtherTypes;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * Represent a flow: match + actions + flow specific properties
36 @XmlAccessorType(XmlAccessType.NONE)
37 public class Flow implements Cloneable, Serializable {
38 protected static final Logger logger = LoggerFactory.getLogger(Flow.class);
39 private static final long serialVersionUID = 1L;
43 private List<Action> actions;
45 private short priority;
47 private short idleTimeout;
49 private short hardTimeout;
51 private long id; // unique identifier for this flow
58 public Flow(Match match, List<Action> actions) {
59 if (match.isIPv4() && actionsAreIPv6()) {
61 throw new Exception("Conflicting Match and Action list");
62 } catch (Exception e) {
67 this.actions = actions;
72 * Return a copy of the Match configured on this flow
76 public Match getMatch() {
81 * Set the Match for this flow This operation will overwrite an existing
86 public void setMatch(Match match) {
91 * 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 If a list is already present, it will
101 * be replaced with the passed one. During addition, only the valid actions
102 * will be added It is a no op if the passed actions is null An empty
103 * actions is a vlaid input
107 public void setActions(List<Action> actions) {
108 if (actions == null) {
112 this.actions = new ArrayList<Action>(actions.size());
113 for (Action action : actions) {
114 if (action.isValid()) {
115 this.actions.add(action);
121 * Returns whether the Flow is for IPv4 or IPv6 Information is derived from
122 * match and actions list
126 public boolean isIPv6() {
127 return (match.isIPv6()) ? true : actionsAreIPv6();
131 * Returns true if it finds at least one action which is for IPv6 in the
132 * list of actions for this Flow
136 private boolean actionsAreIPv6() {
137 if (this.actions != null) {
138 for (Action action : actions) {
139 switch (action.getType()) {
141 if (((SetNwSrc) action).getAddress() instanceof Inet6Address) {
146 if (((SetNwDst) action).getAddress() instanceof Inet6Address) {
151 if (((SetDlType) action).getDlType() == EtherTypes.IPv6.intValue()) {
163 public Flow clone() {
166 cloned = (Flow) super.clone();
167 cloned.match = this.getMatch();
168 cloned.actions = this.getActions();
169 } catch (CloneNotSupportedException e) {
176 public int hashCode() {
177 final int prime = 31;
179 result = prime * result + ((actions == null) ? 0 : actions.hashCode());
180 result = prime * result + hardTimeout;
181 result = prime * result + (int) (id ^ (id >>> 32));
182 result = prime * result + idleTimeout;
183 result = prime * result + ((match == null) ? 0 : match.hashCode());
184 result = prime * result + priority;
189 public boolean equals(Object obj) {
196 if (getClass() != obj.getClass()) {
199 Flow other = (Flow) obj;
200 if (actions == null) {
201 if (other.actions != null) {
204 } else if (!actions.equals(other.actions)) {
207 if (hardTimeout != other.hardTimeout) {
210 if (id != other.id) {
213 if (idleTimeout != other.idleTimeout) {
217 if (other.match != null) {
220 } else if (!match.equals(other.match)) {
223 if (priority != other.priority) {
230 public String toString() {
231 return "Flow[match = " + match + ", actions = " + actions + ", priority = " + priority + ", id = " + id
232 + ", idleTimeout = " + idleTimeout + ", hardTimeout = " + hardTimeout + "]";
235 public short getPriority() {
239 public void setPriority(short priority) {
240 this.priority = priority;
243 public short getIdleTimeout() {
247 public void setIdleTimeout(short idleTimeout) {
248 this.idleTimeout = idleTimeout;
251 public short getHardTimeout() {
255 public void setHardTimeout(short hardTimeout) {
256 this.hardTimeout = hardTimeout;
259 public long getId() {
263 public void setId(long id) {
268 * Adds the specified action to the list of action of this flow
271 * @return false if the passed action is null or not valid or if it fails to
274 public boolean addAction(Action action) {
275 if (action == null || !action.isValid()) {
278 return actions.add(action);
281 public boolean removeAction(Action action) {
282 if (action == null) {
285 return actions.remove(action);
289 * remove ALL actions of type actionType from the list of actions of this
293 * @return false if an action of that type is present and it fails to remove
296 public boolean removeAction(ActionType actionType) {
297 Iterator<Action> actionIter = this.getActions().iterator();
298 while (actionIter.hasNext()) {
299 Action action = actionIter.next();
300 if (action.getType() == actionType) {
301 if (!this.removeAction(action)) {