Five more Equals/HashCode/StringBuilder replacements
[controller.git] / opendaylight / forwardingrulesmanager / api / src / main / java / org / opendaylight / controller / forwardingrulesmanager / FlowEntry.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.forwardingrulesmanager;
10
11 import java.io.Serializable;
12 import java.util.Date;
13
14 import org.opendaylight.controller.sal.core.ContainerFlow;
15 import org.opendaylight.controller.sal.core.Node;
16 import org.opendaylight.controller.sal.flowprogrammer.Flow;
17 import org.opendaylight.controller.sal.match.Match;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * Represents a flow applications request Forwarding Rules Manager to install on
23  * a network node. A FlowEntry is constituted of a flow (match + actions), the
24  * target network node, and the flow name. It also includes a group name. For
25  * instance the flows constituting a policy all share the same group name.
26  */
27 public class FlowEntry implements Cloneable, Serializable {
28     protected static final Logger logger = LoggerFactory
29             .getLogger(FlowEntry.class);
30     private static final long serialVersionUID = 1L;
31     private static final Logger log = LoggerFactory.getLogger(FlowEntry.class);
32     private String groupName; // group name
33     private String flowName; // flow name (may be null)
34     private Node node; // network node where to install the flow
35     private Flow flow; // match + action
36
37     public FlowEntry(String groupName, String flowName, Flow flow, Node node) {
38         this.groupName = groupName;
39         this.flow = flow;
40         this.node = node;
41         this.flowName = (flowName != null) ? flowName : constructFlowName();
42     }
43
44     public String getGroupName() {
45         return groupName;
46     }
47
48     public void setGroupName(String name) {
49         this.groupName = name;
50     }
51
52     /**
53      * Return the actual Flow contained in this entry
54      * 
55      * @return the flow
56      */
57     public Flow getFlow() {
58         return flow;
59     }
60
61     public Node getNode() {
62         return node;
63     }
64
65     public void setNode(Node n) {
66         this.node = n;
67     }
68
69     public String getFlowName() {
70         return flowName;
71     }
72
73     public void setFlowName(String n) {
74         this.flowName = n;
75     }
76
77     @Override
78     public FlowEntry clone() {
79         FlowEntry cloned = null;
80         try {
81             cloned = (FlowEntry) super.clone();
82             cloned.flow = this.flow.clone();
83         } catch (CloneNotSupportedException e) {
84             log.warn("exception in clone", e);
85         }
86         return cloned;
87     }
88
89     @Override
90     public int hashCode() {
91         final int prime = 31;
92         int result = 1;
93         result = prime * result + ((flow == null) ? 0 : flow.hashCode());
94         result = prime * result
95                 + ((flowName == null) ? 0 : flowName.hashCode());
96         result = prime * result
97                 + ((groupName == null) ? 0 : groupName.hashCode());
98         result = prime * result + ((node == null) ? 0 : node.hashCode());
99         return result;
100     }
101
102     @Override
103     public boolean equals(Object obj) {
104         if (this == obj)
105             return true;
106         if (obj == null)
107             return false;
108         if (getClass() != obj.getClass())
109             return false;
110         FlowEntry other = (FlowEntry) obj;
111         if (flow == null) {
112             if (other.flow != null)
113                 return false;
114         } else if (!flow.equals(other.flow))
115             return false;
116         if (flowName == null) {
117             if (other.flowName != null)
118                 return false;
119         } else if (!flowName.equals(other.flowName))
120             return false;
121         if (groupName == null) {
122             if (other.groupName != null)
123                 return false;
124         } else if (!groupName.equals(other.groupName))
125             return false;
126         if (node == null) {
127             if (other.node != null)
128                 return false;
129         } else if (!node.equals(other.node))
130             return false;
131         return true;
132     }
133
134     @Override
135     public String toString() {
136         return "FlowEntry[flowName = " + flowName + ", groupName = "
137                 + groupName + ",node = " + node + ", flow = " + flow + "]";
138     }
139
140     private String constructFlowName() {
141         return this.groupName + "_" + new Date().toString();
142     }
143
144     public boolean equalsByNodeAndName(Node node, String flowName) {
145         return this.node.equals(node) && this.flowName.equals(flowName);
146     }
147
148     /**
149      * Merges the current Flow with the passed Container Flow
150      * 
151      * Note: Container Flow merging is not an injective function. Be m1 and m2
152      * two different matches, and be f() the flow merge function, such that y1 =
153      * f(m1) and y2 = f(m2) are the two merged matches, we may have: y1 = y2
154      * 
155      * 
156      * @param containerFlow
157      * @return this merged FlowEntry
158      */
159     public FlowEntry mergeWith(ContainerFlow containerFlow) {
160         Match myMatch = flow.getMatch();
161
162         // Based on this flow direction, rearrange the match
163         Match match = containerFlow.getMatch();
164
165         // Merge
166         myMatch.mergeWithFilter(match);
167
168         // Replace this Flow's match with merged version
169         flow.setMatch(myMatch);
170
171         return this;
172     }
173 }