Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 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'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net; |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 17 | |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 18 | import java.util.Objects; |
| 19 | |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 20 | import com.google.common.primitives.UnsignedLongs; |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 21 | |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 22 | /** |
| 23 | * Representation of a port number. |
| 24 | */ |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 25 | public final class PortNumber { |
| 26 | |
tom | a1d16b6 | 2014-10-02 23:45:11 -0700 | [diff] [blame] | 27 | public static final PortNumber P0 = portNumber(0); |
| 28 | |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 29 | // TODO: revisit the max and the logical port value assignments |
| 30 | |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 31 | private static final long MAX_NUMBER = (2L * Integer.MAX_VALUE) + 1; |
| 32 | |
alshabib | 63d5afe | 2014-09-15 09:40:24 -0700 | [diff] [blame] | 33 | |
Ray Milkey | 8717be2 | 2015-01-12 14:16:27 -0800 | [diff] [blame] | 34 | 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); |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 49 | |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 50 | private final long number; |
| 51 | |
| 52 | // Public creation is prohibited |
| 53 | private PortNumber(long number) { |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 54 | this.number = number; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Returns the port number representing the specified long value. |
| 59 | * |
| 60 | * @param number port number as long value |
| 61 | * @return port number |
| 62 | */ |
| 63 | public static PortNumber portNumber(long number) { |
| 64 | return new PortNumber(number); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Returns the port number representing the specified string value. |
| 69 | * |
| 70 | * @param string port number as string value |
| 71 | * @return port number |
| 72 | */ |
| 73 | public static PortNumber portNumber(String string) { |
| 74 | return new PortNumber(UnsignedLongs.decode(string)); |
| 75 | } |
| 76 | |
| 77 | /** |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 78 | * Indicates whether or not this port number is a reserved logical one or |
| 79 | * whether it corresponds to a normal physical port of a device or NIC. |
| 80 | * |
| 81 | * @return true if logical port number |
| 82 | */ |
| 83 | public boolean isLogical() { |
| 84 | return number < 0 || number > MAX_NUMBER; |
| 85 | } |
| 86 | |
| 87 | /** |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 88 | * Returns the backing long value. |
| 89 | * |
| 90 | * @return port number as long |
| 91 | */ |
| 92 | public long toLong() { |
| 93 | return number; |
| 94 | } |
| 95 | |
Ray Milkey | 8717be2 | 2015-01-12 14:16:27 -0800 | [diff] [blame] | 96 | private String decodeLogicalPort() { |
| 97 | if (number == CONTROLLER_NUMBER) { |
| 98 | return "CONTROLLER"; |
| 99 | } else if (number == LOCAL_NUMBER) { |
| 100 | return "LOCAL"; |
| 101 | } else if (number == ALL_NUMBER) { |
| 102 | return "ALL"; |
| 103 | } else if (number == FLOOD_NUMBER) { |
| 104 | return "FLOOD"; |
| 105 | } else if (number == NORMAL_NUMBER) { |
| 106 | return "NORMAL"; |
| 107 | } else if (number == TABLE_NUMBER) { |
| 108 | return "TABLE"; |
| 109 | } else if (number == IN_PORT_NUMBER) { |
| 110 | return "IN_PORT"; |
| 111 | } |
| 112 | return "UNKNOWN"; |
| 113 | } |
| 114 | |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 115 | @Override |
| 116 | public String toString() { |
Ray Milkey | 8717be2 | 2015-01-12 14:16:27 -0800 | [diff] [blame] | 117 | if (!isLogical()) { |
| 118 | return UnsignedLongs.toString(number); |
| 119 | } else { |
| 120 | return decodeLogicalPort(); |
| 121 | } |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public int hashCode() { |
| 126 | return Objects.hash(number); |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public boolean equals(Object obj) { |
tom | fc9a4ff | 2014-09-22 18:22:47 -0700 | [diff] [blame] | 131 | if (this == obj) { |
| 132 | return true; |
| 133 | } |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 134 | if (obj instanceof PortNumber) { |
| 135 | final PortNumber other = (PortNumber) obj; |
| 136 | return this.number == other.number; |
| 137 | } |
| 138 | return false; |
| 139 | } |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 140 | } |