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.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.apache.commons.lang3.builder.EqualsBuilder;
23 import org.apache.commons.lang3.builder.HashCodeBuilder;
24 import org.opendaylight.controller.sal.action.Action;
25 import org.opendaylight.controller.sal.action.ActionType;
26 import org.opendaylight.controller.sal.action.SetDlType;
27 import org.opendaylight.controller.sal.action.SetNwDst;
28 import org.opendaylight.controller.sal.action.SetNwSrc;
29 import org.opendaylight.controller.sal.match.Match;
30 import org.opendaylight.controller.sal.utils.EtherTypes;
33 * Represent a flow: match + actions + flow specific properties
37 @XmlAccessorType(XmlAccessType.NONE)
38 public class Flow implements Cloneable {
42 private List<Action> actions;
44 private short priority;
46 private short idleTimeout;
48 private short hardTimeout;
50 private long id; // unique identifier for this flow
57 public Flow(Match match, List<Action> actions) {
58 if (match.isIPv4() && actionsAreIPv6()) {
60 throw new Exception("Conflicting Match and Action list");
61 } catch (Exception e) {
66 this.actions = actions;
71 * Return a copy of the Match configured on this flow
75 public Match getMatch() {
80 * Set the Match for this flow
81 * This operation will overwrite an existing Match if present
85 public void setMatch(Match match) {
90 * Returns a copy of the actions list of this flow
93 public List<Action> getActions() {
94 return (actions == null) ? null : new ArrayList<Action>(actions);
98 * Set the actions list for this flow
99 * If a list is already present, it will be
100 * replaced with the passed one. During
101 * addition, only the valid actions will be added
102 * It is a no op if the passed actions is null
103 * An empty 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
122 * Information is derived from 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
132 * in the 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
164 public Flow clone() {
167 cloned = (Flow) super.clone();
168 cloned.match = this.getMatch();
169 cloned.actions = this.getActions();
170 } catch (CloneNotSupportedException e) {
177 public int hashCode() {
178 return HashCodeBuilder.reflectionHashCode(this);
182 public boolean equals(Object obj) {
183 return EqualsBuilder.reflectionEquals(this, obj);
187 public String toString() {
188 return "Flow[match = " + match + ", actions = " + actions
189 + ", priority = " + priority + ", id = " + id
190 + ", idleTimeout = " + idleTimeout + ", hardTimeout = "
194 public short getPriority() {
198 public void setPriority(short priority) {
199 this.priority = priority;
202 public short getIdleTimeout() {
206 public void setIdleTimeout(short idleTimeout) {
207 this.idleTimeout = idleTimeout;
210 public short getHardTimeout() {
214 public void setHardTimeout(short hardTimeout) {
215 this.hardTimeout = hardTimeout;
218 public long getId() {
222 public void setId(long id) {
227 * Adds the specified action to the list of action of this flow
230 * @return false if the passed action is null or not valid or if it fails to add it
232 public boolean addAction(Action action) {
233 if (action == null || !action.isValid()) {
236 return actions.add(action);
239 public boolean removeAction(Action action) {
240 if (action == null) {
243 return actions.remove(action);
247 * remove ALL actions of type actionType from the list of actions of this flow
250 * @return false if an action of that type is present and it fails to remove it
252 public boolean removeAction(ActionType actionType) {
253 Iterator<Action> actionIter = this.getActions().iterator();
254 while (actionIter.hasNext()) {
255 Action action = actionIter.next();
256 if (action.getType() == actionType) {
257 if (!this.removeAction(action))