blob: db19807621dc58652128c038e0b16d187b5cb6d8 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net;
tom0eb04ca2014-08-25 14:34:51 -070017
alshabib4680bb62014-09-04 17:15:08 -070018import com.google.common.primitives.UnsignedLongs;
tomca20e0c2014-09-03 23:22:24 -070019
Marc De Leenheer171c2e82015-04-13 17:20:37 -070020import java.util.Objects;
21
tom0eb04ca2014-08-25 14:34:51 -070022/**
23 * Representation of a port number.
24 */
tomca20e0c2014-09-03 23:22:24 -070025public final class PortNumber {
26
toma1d16b62014-10-02 23:45:11 -070027 public static final PortNumber P0 = portNumber(0);
28
tom613d8142014-09-11 15:09:37 -070029 // TODO: revisit the max and the logical port value assignments
30
tomca20e0c2014-09-03 23:22:24 -070031 private static final long MAX_NUMBER = (2L * Integer.MAX_VALUE) + 1;
32
alshabib63d5afe2014-09-15 09:40:24 -070033
Ray Milkey8717be22015-01-12 14:16:27 -080034 static final long IN_PORT_NUMBER = -8L;
35 static final long TABLE_NUMBER = -7L;
36 static final long NORMAL_NUMBER = -6L;
37 static final long FLOOD_NUMBER = -5L;
38 static final long ALL_NUMBER = -4L;
39 static final long LOCAL_NUMBER = -2L;
40 static final long CONTROLLER_NUMBER = -3L;
41
42 public static final PortNumber IN_PORT = new PortNumber(IN_PORT_NUMBER);
43 public static final PortNumber TABLE = new PortNumber(TABLE_NUMBER);
44 public static final PortNumber NORMAL = new PortNumber(NORMAL_NUMBER);
45 public static final PortNumber FLOOD = new PortNumber(FLOOD_NUMBER);
46 public static final PortNumber ALL = new PortNumber(ALL_NUMBER);
47 public static final PortNumber LOCAL = new PortNumber(LOCAL_NUMBER);
48 public static final PortNumber CONTROLLER = new PortNumber(CONTROLLER_NUMBER);
tom613d8142014-09-11 15:09:37 -070049
tomca20e0c2014-09-03 23:22:24 -070050 private final long number;
Marc De Leenheer171c2e82015-04-13 17:20:37 -070051 private final String name;
tomca20e0c2014-09-03 23:22:24 -070052
53 // Public creation is prohibited
54 private PortNumber(long number) {
tomca20e0c2014-09-03 23:22:24 -070055 this.number = number;
Marc De Leenheer171c2e82015-04-13 17:20:37 -070056 this.name = UnsignedLongs.toString(number);
57 }
58
59 private PortNumber(long number, String name) {
60 this.number = number;
61 this.name = name;
tomca20e0c2014-09-03 23:22:24 -070062 }
63
64 /**
65 * Returns the port number representing the specified long value.
66 *
67 * @param number port number as long value
68 * @return port number
69 */
70 public static PortNumber portNumber(long number) {
71 return new PortNumber(number);
72 }
73
74 /**
75 * Returns the port number representing the specified string value.
76 *
77 * @param string port number as string value
78 * @return port number
79 */
80 public static PortNumber portNumber(String string) {
81 return new PortNumber(UnsignedLongs.decode(string));
82 }
83
84 /**
Marc De Leenheer171c2e82015-04-13 17:20:37 -070085 * Returns the port number representing the specified long value and name.
86 *
87 * @param number port number as long value
88 * @param name port name as string value
89 * @return port number
90 */
91 public static PortNumber portNumber(long number, String name) {
92 return new PortNumber(number, name);
93 }
94
95 /**
tom613d8142014-09-11 15:09:37 -070096 * Indicates whether or not this port number is a reserved logical one or
97 * whether it corresponds to a normal physical port of a device or NIC.
98 *
99 * @return true if logical port number
100 */
101 public boolean isLogical() {
102 return number < 0 || number > MAX_NUMBER;
103 }
104
105 /**
tomca20e0c2014-09-03 23:22:24 -0700106 * Returns the backing long value.
107 *
108 * @return port number as long
109 */
110 public long toLong() {
111 return number;
112 }
113
Ray Milkey8717be22015-01-12 14:16:27 -0800114 private String decodeLogicalPort() {
115 if (number == CONTROLLER_NUMBER) {
116 return "CONTROLLER";
117 } else if (number == LOCAL_NUMBER) {
118 return "LOCAL";
119 } else if (number == ALL_NUMBER) {
120 return "ALL";
121 } else if (number == FLOOD_NUMBER) {
122 return "FLOOD";
123 } else if (number == NORMAL_NUMBER) {
124 return "NORMAL";
125 } else if (number == TABLE_NUMBER) {
126 return "TABLE";
127 } else if (number == IN_PORT_NUMBER) {
128 return "IN_PORT";
129 }
130 return "UNKNOWN";
131 }
132
tomca20e0c2014-09-03 23:22:24 -0700133 @Override
134 public String toString() {
Ray Milkey8717be22015-01-12 14:16:27 -0800135 if (!isLogical()) {
Marc De Leenheer171c2e82015-04-13 17:20:37 -0700136 return name;
Ray Milkey8717be22015-01-12 14:16:27 -0800137 } else {
138 return decodeLogicalPort();
139 }
tomca20e0c2014-09-03 23:22:24 -0700140 }
141
142 @Override
143 public int hashCode() {
144 return Objects.hash(number);
145 }
146
147 @Override
148 public boolean equals(Object obj) {
tomfc9a4ff2014-09-22 18:22:47 -0700149 if (this == obj) {
150 return true;
151 }
tomca20e0c2014-09-03 23:22:24 -0700152 if (obj instanceof PortNumber) {
153 final PortNumber other = (PortNumber) obj;
154 return this.number == other.number;
155 }
156 return false;
157 }
tom0eb04ca2014-08-25 14:34:51 -0700158}