Open Kilda Java Documentation
SwitchId.java
Go to the documentation of this file.
1 /* Copyright 2018 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 
19 
20 import com.fasterxml.jackson.annotation.JsonValue;
21 import com.google.common.annotations.VisibleForTesting;
22 
23 import java.io.Serializable;
24 import java.util.Objects;
25 
29 public class SwitchId implements Serializable, Comparable<SwitchId> {
30 
31  private static final long serialVersionUID = 1L;
32 
36  private final long switchId;
37 
41  public SwitchId(String switchId) {
42  long value;
43  try {
44  value = Long.parseUnsignedLong(switchId.replaceAll("[-:]", ""), 16);
45  } catch (NumberFormatException e) {
46  throw new SwitchIdFormatException(String.format("Can not parse input string: \"%s\"", switchId));
47  }
48 
49  this.switchId = value;
50  }
51 
55  public SwitchId(long switchId) {
56  this.switchId = switchId;
57  }
58 
64  public long toLong() {
65  return switchId;
66  }
67 
73  public String toMacAddress() {
74  return colonSeparatedBytes(toHexArray(), 4);
75  }
76 
80  @Override
81  @JsonValue
82  public String toString() {
83  return colonSeparatedBytes(toHexArray(), 0);
84  }
85 
91  public String toOtsdFormat() {
92  return "SW" + new String(toHexArray()).toUpperCase();
93  }
94 
95  @VisibleForTesting
96  String colonSeparatedBytes(char[] hex, int offset) {
97  if (offset < 0 || offset % 2 != 0 || offset >= hex.length) {
98  throw new IllegalArgumentException(String.format(
99  "Illegal offset value %d (expect offset > 0 and offset %% 2 == 0 and offset < hex.length)", offset));
100  }
101 
102  int length = hex.length - offset;
103  length += length / 2 - 1;
104  char[] buffer = new char[length];
105  int dst = 0;
106  for (int src = offset; src < hex.length; src++) {
107  if (offset < src && src % 2 == 0) {
108  buffer[dst++] = ':';
109  }
110  buffer[dst++] = hex[src];
111  }
112 
113  return new String(buffer);
114  }
115 
116  private char[] toHexArray() {
117  String hexString = String.format("%016x", switchId);
118  return hexString.toCharArray();
119  }
120 
124  @Override
125  public boolean equals(Object o) {
126  if (this == o) {
127  return true;
128  }
129 
130  if (o == null || getClass() != o.getClass()) {
131  return false;
132  }
133 
134  SwitchId other = (SwitchId) o;
135 
136  return switchId == other.switchId;
137  }
138 
142  @Override
143  public int hashCode() {
144  return Objects.hash(switchId);
145  }
146 
147 
151  @Override
152  public int compareTo(SwitchId other) {
153  return Long.compareUnsigned(switchId, other.switchId);
154  }
155 }
value
Definition: nodes.py:62