Open Kilda Java Documentation
Flow.java
Go to the documentation of this file.
1 /* Copyright 2017 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.messaging.model;
17 
18 import static com.google.common.base.MoreObjects.toStringHelper;
19 
23 
24 import com.fasterxml.jackson.annotation.JsonCreator;
25 import com.fasterxml.jackson.annotation.JsonIgnore;
26 import com.fasterxml.jackson.annotation.JsonProperty;
27 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
28 
29 import lombok.Builder;
30 import lombok.Getter;
31 import lombok.Setter;
32 
33 import java.io.Serializable;
34 import java.util.Objects;
35 
39 @JsonSerialize
40 @Getter
41 @Setter
42 public class Flow implements Serializable {
46  private static final long serialVersionUID = 1L;
47 
48  private static long MASK_COOKIE_FLAGS = 0x0000_0000_FFFF_FFFFL;
49 
53  @JsonProperty(Utils.FLOW_ID)
54  private String flowId;
55 
59  @JsonProperty("bandwidth")
60  private long bandwidth;
61 
65  @JsonProperty("ignore_bandwidth")
66  private boolean ignoreBandwidth;
67 
71  @JsonProperty("cookie")
72  private long cookie;
73 
77  @JsonProperty("description")
78  private String description;
79 
83  @JsonProperty("last_updated")
84  private String lastUpdated;
85 
89  @JsonProperty("src_switch")
90  private SwitchId sourceSwitch;
91 
95  @JsonProperty("dst_switch")
96  private SwitchId destinationSwitch;
97 
101  @JsonProperty("src_port")
102  private int sourcePort;
103 
107  @JsonProperty("dst_port")
108  private int destinationPort;
109 
113  @JsonProperty("src_vlan")
114  private int sourceVlan;
115 
119  @JsonProperty("dst_vlan")
120  private int destinationVlan;
121 
125  @JsonProperty("meter_id")
126  private int meterId;
127 
131  @JsonProperty("transit_vlan")
132  private int transitVlan;
133 
137  @JsonProperty(Utils.FLOW_PATH)
138  private PathInfoData flowPath;
139  // TODO: why is PathInfoData an event, vs a model (ie package name)?
140 
144  @JsonProperty("state")
145  private FlowState state;
146 
150  public Flow() {
151  }
152 
158  public Flow(Flow flow) {
159  this.flowId = flow.getFlowId();
160  this.bandwidth = flow.getBandwidth();
161  this.ignoreBandwidth = flow.isIgnoreBandwidth();
162  this.cookie = flow.getCookie();
163  this.description = flow.getDescription();
164  this.lastUpdated = flow.getLastUpdated();
165  this.sourceSwitch = flow.getSourceSwitch();
166  this.destinationSwitch = flow.getDestinationSwitch();
167  this.sourcePort = flow.getSourcePort();
168  this.destinationPort = flow.getDestinationPort();
169  this.sourceVlan = flow.getSourceVlan();
170  this.destinationVlan = flow.getDestinationVlan();
171  this.transitVlan = flow.getTransitVlan();
172  this.meterId = flow.getMeterId();
173  this.flowPath = flow.getFlowPath();
174  this.state = flow.getState();
175  }
176 
197  @JsonCreator
198  @Builder
199  public Flow(@JsonProperty(Utils.FLOW_ID) final String flowId,
200  @JsonProperty("bandwidth") final long bandwidth,
201  @JsonProperty("ignore_bandwidth") Boolean ignoreBandwidth,
202  @JsonProperty("cookie") final long cookie,
203  @JsonProperty("description") final String description,
204  @JsonProperty("last_updated") final String lastUpdated,
205  @JsonProperty("src_switch") final SwitchId sourceSwitch,
206  @JsonProperty("dst_switch") final SwitchId destinationSwitch,
207  @JsonProperty("src_port") final int sourcePort,
208  @JsonProperty("dst_port") final int destinationPort,
209  @JsonProperty("src_vlan") final int sourceVlan,
210  @JsonProperty("dst_vlan") final int destinationVlan,
211  @JsonProperty("meter_id") final int meterId,
212  @JsonProperty("transit_vlan") final int transitVlan,
213  @JsonProperty(Utils.FLOW_PATH) final PathInfoData flowPath,
214  @JsonProperty("state") FlowState state) {
215  this.flowId = flowId;
216  this.bandwidth = bandwidth;
217  setIgnoreBandwidth(ignoreBandwidth);
218  this.cookie = cookie;
219  this.description = description;
220  this.lastUpdated = lastUpdated;
221  this.sourceSwitch = sourceSwitch;
222  this.destinationSwitch = destinationSwitch;
223  this.sourcePort = sourcePort;
224  this.destinationPort = destinationPort;
225  this.sourceVlan = sourceVlan;
226  this.destinationVlan = destinationVlan;
227  this.transitVlan = transitVlan;
228  this.meterId = meterId;
229  this.flowPath = flowPath;
230  this.state = state;
231  }
232 
247  public Flow(String flowId, long bandwidth, boolean ignoreBandwidth, String description,
248  SwitchId sourceSwitch, int sourcePort, int sourceVlan,
249  SwitchId destinationSwitch, int destinationPort, int destinationVlan) {
250  this.flowId = flowId;
251  this.bandwidth = bandwidth;
252  this.ignoreBandwidth = ignoreBandwidth;
253  this.description = description;
254  this.sourceSwitch = sourceSwitch;
255  this.destinationSwitch = destinationSwitch;
256  this.sourcePort = sourcePort;
257  this.destinationPort = destinationPort;
258  this.sourceVlan = sourceVlan;
259  this.destinationVlan = destinationVlan;
260  }
261 
267  public void setIgnoreBandwidth(Boolean ignoreBandwidth) {
268  if (ignoreBandwidth == null) {
269  ignoreBandwidth = false;
270  }
271  this.ignoreBandwidth = ignoreBandwidth;
272  }
273 
277  @JsonIgnore
278  public boolean isOneSwitchFlow() {
279  // TODO(surabujin): there is no decision how it should react on null values in source/dest switches
280  if (sourceSwitch == null || destinationSwitch == null) {
281  return sourceSwitch == null && destinationSwitch == null;
282  }
283  return sourceSwitch.equals(destinationSwitch);
284  }
285 
286  @JsonIgnore
287  public long getFlagglessCookie() {
288  return cookie & MASK_COOKIE_FLAGS;
289  }
290 
296  @JsonIgnore
297  public boolean isForward() {
298  boolean isForward = cookieMarkedAsFroward();
299  boolean isReversed = cookieMarkedAsReversed();
300 
301  if (isForward && isReversed) {
302  throw new IllegalArgumentException(
303  "Invalid cookie flags combinations - it mark as forward and reverse flow at same time.");
304  }
305 
306  return isForward;
307  }
308 
309  @JsonIgnore
310  public boolean isReverse() {
311  return !isForward();
312  }
313 
314  private boolean cookieMarkedAsFroward() {
315  boolean isMatch;
316 
317  if ((cookie & 0xE000000000000000L) != 0) {
318  isMatch = (cookie & 0x4000000000000000L) != 0;
319  } else {
320  isMatch = (cookie & 0x0080000000000000L) == 0;
321  }
322  return isMatch;
323 
324  }
325 
326  private boolean cookieMarkedAsReversed() {
327  boolean isMatch;
328  if ((cookie & 0xE000000000000000L) != 0) {
329  isMatch = (cookie & 0x2000000000000000L) != 0;
330  } else {
331  isMatch = (cookie & 0x0080000000000000L) != 0;
332  }
333  return isMatch;
334  }
335 
342  public boolean containsSwitchInPath(SwitchId switchId) {
343  return flowPath.getPath().stream()
344  .anyMatch(node -> node.getSwitchId().equals(switchId));
345  }
346 
350  @Override
351  public boolean equals(Object object) {
352  if (this == object) {
353  return true;
354  }
355  if (object == null || getClass() != object.getClass()) {
356  return false;
357  }
358 
359  Flow flow = (Flow) object;
360  return Objects.equals(getFlowId(), flow.getFlowId())
361  && getBandwidth() == flow.getBandwidth()
362  && Objects.equals(getDescription(), flow.getDescription())
363  && getState() == flow.getState()
364  && Objects.equals(getSourceSwitch(), flow.getSourceSwitch())
365  && getSourcePort() == flow.getSourcePort()
366  && getSourceVlan() == flow.getSourceVlan()
367  && Objects.equals(getDestinationSwitch(), flow.getDestinationSwitch())
368  && getDestinationPort() == flow.getDestinationPort()
369  && getDestinationVlan() == flow.getDestinationVlan();
370  }
371 
375  @Override
376  public int hashCode() {
377  return Objects.hash(flowId, bandwidth, description, state,
378  sourceSwitch, sourcePort, sourceVlan, destinationSwitch, destinationPort, destinationVlan);
379  }
380 
384  @Override
385  public String toString() {
386  return toStringHelper(this)
387  .add(Utils.FLOW_ID, flowId)
388  .add("bandwidth", bandwidth)
389  .add("ignore_bandwidth", ignoreBandwidth)
390  .add("description", description)
391  .add("state", state)
392  .add("src_switch", sourceSwitch)
393  .add("src_port", sourcePort)
394  .add("src_vlan", sourceVlan)
395  .add("dst_switch", destinationSwitch)
396  .add("dst_port", destinationPort)
397  .add("dst_vlan", destinationVlan)
398  .add("cookie", cookie)
399  .add("transit_vlan", transitVlan)
400  .add("meter_id", meterId)
401  .add("last_updated", lastUpdated)
402  .add(Utils.FLOW_PATH, flowPath)
403  .toString();
404  }
405 }
void setIgnoreBandwidth(Boolean ignoreBandwidth)
Definition: Flow.java:267
Flow(@JsonProperty(Utils.FLOW_ID) final String flowId, @JsonProperty("bandwidth") final long bandwidth, @JsonProperty("ignore_bandwidth") Boolean ignoreBandwidth, @JsonProperty("cookie") final long cookie, @JsonProperty("description") final String description, @JsonProperty("last_updated") final String lastUpdated, @JsonProperty("src_switch") final SwitchId sourceSwitch, @JsonProperty("dst_switch") final SwitchId destinationSwitch, @JsonProperty("src_port") final int sourcePort, @JsonProperty("dst_port") final int destinationPort, @JsonProperty("src_vlan") final int sourceVlan, @JsonProperty("dst_vlan") final int destinationVlan, @JsonProperty("meter_id") final int meterId, @JsonProperty("transit_vlan") final int transitVlan, @JsonProperty(Utils.FLOW_PATH) final PathInfoData flowPath, @JsonProperty("state") FlowState state)
Definition: Flow.java:199
Flow(String flowId, long bandwidth, boolean ignoreBandwidth, String description, SwitchId sourceSwitch, int sourcePort, int sourceVlan, SwitchId destinationSwitch, int destinationPort, int destinationVlan)
Definition: Flow.java:247
static final String FLOW_PATH
Definition: Utils.java:65
boolean containsSwitchInPath(SwitchId switchId)
Definition: Flow.java:342
static final String FLOW_ID
Definition: Utils.java:61
boolean equals(Object object)
Definition: Flow.java:351