Mark AD-SAL interfaces as deprecated
[controller.git] / opendaylight / adsal / sal / api / src / main / java / org / opendaylight / controller / sal / flowprogrammer / Flow.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.sal.flowprogrammer;
10
11 import org.opendaylight.controller.sal.action.Action;
12 import org.opendaylight.controller.sal.action.ActionType;
13 import org.opendaylight.controller.sal.action.SetDlType;
14 import org.opendaylight.controller.sal.action.SetNwDst;
15 import org.opendaylight.controller.sal.action.SetNwSrc;
16 import org.opendaylight.controller.sal.match.Match;
17 import org.opendaylight.controller.sal.utils.EtherTypes;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 import javax.xml.bind.annotation.XmlAccessType;
22 import javax.xml.bind.annotation.XmlAccessorType;
23 import javax.xml.bind.annotation.XmlElement;
24 import javax.xml.bind.annotation.XmlRootElement;
25 import java.io.Serializable;
26 import java.net.Inet6Address;
27 import java.util.ArrayList;
28 import java.util.Iterator;
29 import java.util.List;
30
31 /**
32  * Represent a flow: match + actions + flow specific properties
33  */
34 @XmlRootElement
35 @XmlAccessorType(XmlAccessType.NONE)
36 @Deprecated
37 public class Flow implements Cloneable, Serializable {
38     protected static final Logger logger = LoggerFactory.getLogger(Flow.class);
39     private static final long serialVersionUID = 1L;
40     @XmlElement
41     private Match match;
42     @XmlElement
43     private List<Action> actions;
44     @XmlElement
45     private short priority;
46     @XmlElement
47     private short idleTimeout;
48     @XmlElement
49     private short hardTimeout;
50     @XmlElement
51     private long id; // unique identifier for this flow
52
53     public Flow() {
54         match = null;
55         actions = null;
56     }
57
58     public Flow(Match match, List<Action> actions) {
59         if (match.isIPv4() && actionsAreIPv6()) {
60             try {
61                 throw new Exception("Conflicting Match and Action list");
62             } catch (Exception e) {
63                 logger.error("", e);
64             }
65         } else {
66             this.match = match;
67             this.actions = actions;
68         }
69     }
70
71     /**
72      * Return a copy of the Match configured on this flow
73      *
74      * @return
75      */
76     public Match getMatch() {
77         return match.clone();
78     }
79
80     /**
81      * Set the Match for this flow This operation will overwrite an existing
82      * Match if present
83      *
84      * @param match
85      */
86     public void setMatch(Match match) {
87         this.match = match;
88     }
89
90     /**
91      * Returns a copy of the actions list of this flow
92      *
93      * @return
94      */
95     public List<Action> getActions() {
96         return (actions == null) ? null : new ArrayList<Action>(actions);
97     }
98
99     /**
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
104      *
105      * @param actions
106      */
107     public void setActions(List<Action> actions) {
108         if (actions == null) {
109             return;
110         }
111
112         this.actions = new ArrayList<Action>(actions.size());
113         for (Action action : actions) {
114             if (action.isValid()) {
115                 this.actions.add(action);
116             }
117         }
118     }
119
120     /**
121      * Returns whether the Flow is for IPv4 or IPv6 Information is derived from
122      * match and actions list
123      *
124      * @return
125      */
126     public boolean isIPv6() {
127         return (match.isIPv6()) ? true : actionsAreIPv6();
128     }
129
130     /**
131      * Returns true if it finds at least one action which is for IPv6 in the
132      * list of actions for this Flow
133      *
134      * @return
135      */
136     private boolean actionsAreIPv6() {
137         if (this.actions != null) {
138             for (Action action : actions) {
139                 switch (action.getType()) {
140                 case SET_NW_SRC:
141                     if (((SetNwSrc) action).getAddress() instanceof Inet6Address) {
142                         return true;
143                     }
144                     break;
145                 case SET_NW_DST:
146                     if (((SetNwDst) action).getAddress() instanceof Inet6Address) {
147                         return true;
148                     }
149                     break;
150                 case SET_DL_TYPE:
151                     if (((SetDlType) action).getDlType() == EtherTypes.IPv6.intValue()) {
152                         return true;
153                     }
154                     break;
155                 default:
156                 }
157             }
158         }
159         return false;
160     }
161
162     @Override
163     public Flow clone() {
164         Flow cloned = null;
165         try {
166             cloned = (Flow) super.clone();
167             cloned.match = this.getMatch();
168             cloned.actions = this.getActions();
169         } catch (CloneNotSupportedException e) {
170             logger.error("", e);
171         }
172         return cloned;
173     }
174
175     @Override
176     public int hashCode() {
177         final int prime = 31;
178         int result = 1;
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;
185         return result;
186     }
187
188     @Override
189     public boolean equals(Object obj) {
190         if (this == obj) {
191             return true;
192         }
193         if (obj == null) {
194             return false;
195         }
196         if (getClass() != obj.getClass()) {
197             return false;
198         }
199         Flow other = (Flow) obj;
200         if (actions == null) {
201             if (other.actions != null) {
202                 return false;
203             }
204         } else if (!actions.equals(other.actions)) {
205             return false;
206         }
207         if (hardTimeout != other.hardTimeout) {
208             return false;
209         }
210         if (id != other.id) {
211             return false;
212         }
213         if (idleTimeout != other.idleTimeout) {
214             return false;
215         }
216         if (match == null) {
217             if (other.match != null) {
218                 return false;
219             }
220         } else if (!match.equals(other.match)) {
221             return false;
222         }
223         if (priority != other.priority) {
224             return false;
225         }
226         return true;
227     }
228
229     @Override
230     public String toString() {
231         return "Flow[match = " + match + ", actions = " + actions + ", priority = " + priority + ", id = " + id
232                 + ", idleTimeout = " + idleTimeout + ", hardTimeout = " + hardTimeout + "]";
233     }
234
235     public short getPriority() {
236         return priority;
237     }
238
239     public void setPriority(short priority) {
240         this.priority = priority;
241     }
242
243     public short getIdleTimeout() {
244         return idleTimeout;
245     }
246
247     public void setIdleTimeout(short idleTimeout) {
248         this.idleTimeout = idleTimeout;
249     }
250
251     public short getHardTimeout() {
252         return hardTimeout;
253     }
254
255     public void setHardTimeout(short hardTimeout) {
256         this.hardTimeout = hardTimeout;
257     }
258
259     public long getId() {
260         return id;
261     }
262
263     public void setId(long id) {
264         this.id = id;
265     }
266
267     /**
268      * Adds the specified action to the list of action of this flow
269      *
270      * @param action
271      * @return false if the passed action is null or not valid or if it fails to
272      *         add it
273      */
274     public boolean addAction(Action action) {
275         if (action == null || !action.isValid()) {
276             return false;
277         }
278         return actions.add(action);
279     }
280
281     public boolean removeAction(Action action) {
282         if (action == null) {
283             return false;
284         }
285         return actions.remove(action);
286     }
287
288     /**
289      * remove ALL actions of type actionType from the list of actions of this
290      * flow
291      *
292      * @param actionType
293      * @return false if an action of that type is present and it fails to remove
294      *         it
295      */
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)) {
302                     return false;
303                 }
304             }
305         }
306         return true;
307     }
308
309 }