Open Kilda Java Documentation
FlowDirectionHelper.java
Go to the documentation of this file.
1 package org.openkilda.wfm.topology.stats;
2 
3 public class FlowDirectionHelper {
4 
5 
6  public enum Direction
7  {
10  REVERSE
11  }
12 
13 
20  static public Direction findDirection(long cookie) throws FlowCookieException {
21  // Kilda flow first number represents direction with 4 = forward and 2 = reverse
22  // Legacy flow Cookies 0x10400000005d803 is first switch in forward direction
23  // 0x18400000005d803 is first switch in reverse direction
24  // first number represents switch seqId in path
25  // second number represents forward/reverse
26  // third number no idea
27  // rest is the same for the flow
28  Direction direction = Direction.UNKNOWN;
29  try {
30  direction = getLegacyDirection(cookie);
31  } catch (FlowCookieException e) {
32  direction = getKildaDirection(cookie);
33  }
34  return direction;
35  }
36 
37  static public boolean isLegacyCookie(long cookie) {
38  // A legacy cookie will have a value of 0 for the high order nibble
39  // and the second nibble of >= 1
40  // and the third nibble will be 0 or 8
41  // and the fourth octet will be 4
42  long firstNibble = cookie >>> 60 & 0xf;
43  long switchSeqId = cookie >>> 56 & 0xf;
44  long param = cookie >>> 48 & 0xf;
45  return (firstNibble == 0) && (switchSeqId > 0) && (param == 4);
46  }
47 
48  static public boolean isKildaCookie(long cookie) {
49  // A Kilda cookie (with a smallish number of flows) will have a 8, 2 or 4 in the highest nibble
50  // and the second, third, forth nibble will be 0
51  long flowType = cookie >>> 60 & 0xf;
52  long nibbles = cookie >>> 48 & 0xfff;
53  return ((flowType == 2) || (flowType == 4) || (flowType == 8)) && nibbles == 0;
54  }
55 
56  static public Direction getKildaDirection(long cookie) throws FlowCookieException {
57  // high order nibble represents type of flow with a 2 representing a forward flow
58  // and a 4 representing the reverse flow
59  if (!isKildaCookie(cookie)) {
60  throw new FlowCookieException(cookie + " is not a Kilda flow");
61  }
62  long direction = cookie >>> 60 & 0xff;
63  if ((direction != 2) && (direction != 4)) {
64  throw new FlowCookieException("unknown direction for " + cookie);
65  }
66  return direction == 4 ? Direction.FORWARD : Direction.REVERSE;
67  }
68 
69  static public Direction getLegacyDirection(long cookie) throws FlowCookieException {
70  // Direction is the 3rd nibble from the top
71  // If nibble is 0 it is forward and 8 is reverse
72  if (!isLegacyCookie(cookie)) {
73  throw new FlowCookieException(cookie + " is not a legacy flow");
74  }
75  long direction = cookie >>> 52 & 0xf;
76  if ((direction != 0) && (direction != 8)) {
77  throw new FlowCookieException("unknown direction for " + cookie);
78  }
79  return direction == 0 ? Direction.FORWARD : Direction.REVERSE;
80  }
81 
82 }