Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 | |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 18 | import com.google.common.primitives.UnsignedLongs; |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 19 | |
Marc De Leenheer | 171c2e8 | 2015-04-13 17:20:37 -0700 | [diff] [blame] | 20 | import java.util.Objects; |
| 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; |
Marc De Leenheer | 171c2e8 | 2015-04-13 17:20:37 -0700 | [diff] [blame] | 51 | private final String name; |
Marc De Leenheer | 76d8974 | 2015-04-16 15:03:03 -0700 | [diff] [blame] | 52 | private final boolean hasName; |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 53 | |
| 54 | // Public creation is prohibited |
| 55 | private PortNumber(long number) { |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 56 | this.number = number; |
Marc De Leenheer | 171c2e8 | 2015-04-13 17:20:37 -0700 | [diff] [blame] | 57 | this.name = UnsignedLongs.toString(number); |
Marc De Leenheer | 76d8974 | 2015-04-16 15:03:03 -0700 | [diff] [blame] | 58 | this.hasName = false; |
Marc De Leenheer | 171c2e8 | 2015-04-13 17:20:37 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | private PortNumber(long number, String name) { |
| 62 | this.number = number; |
| 63 | this.name = name; |
Marc De Leenheer | 76d8974 | 2015-04-16 15:03:03 -0700 | [diff] [blame] | 64 | this.hasName = true; |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Returns the port number representing the specified long value. |
| 69 | * |
| 70 | * @param number port number as long value |
| 71 | * @return port number |
| 72 | */ |
| 73 | public static PortNumber portNumber(long number) { |
| 74 | return new PortNumber(number); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Returns the port number representing the specified string value. |
| 79 | * |
| 80 | * @param string port number as string value |
| 81 | * @return port number |
| 82 | */ |
| 83 | public static PortNumber portNumber(String string) { |
| 84 | return new PortNumber(UnsignedLongs.decode(string)); |
| 85 | } |
| 86 | |
| 87 | /** |
Marc De Leenheer | 171c2e8 | 2015-04-13 17:20:37 -0700 | [diff] [blame] | 88 | * Returns the port number representing the specified long value and name. |
| 89 | * |
| 90 | * @param number port number as long value |
| 91 | * @param name port name as string value |
| 92 | * @return port number |
| 93 | */ |
| 94 | public static PortNumber portNumber(long number, String name) { |
| 95 | return new PortNumber(number, name); |
| 96 | } |
| 97 | |
| 98 | /** |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 99 | * Indicates whether or not this port number is a reserved logical one or |
| 100 | * whether it corresponds to a normal physical port of a device or NIC. |
| 101 | * |
| 102 | * @return true if logical port number |
| 103 | */ |
| 104 | public boolean isLogical() { |
Marc De Leenheer | 76d8974 | 2015-04-16 15:03:03 -0700 | [diff] [blame] | 105 | if (hasName) { |
| 106 | return false; |
| 107 | } else { |
| 108 | return (number < 0 || number > MAX_NUMBER); |
| 109 | } |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /** |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 113 | * Returns the backing long value. |
| 114 | * |
| 115 | * @return port number as long |
| 116 | */ |
| 117 | public long toLong() { |
| 118 | return number; |
| 119 | } |
| 120 | |
Marc De Leenheer | c0e37ec | 2015-04-14 22:53:23 -0700 | [diff] [blame] | 121 | /** |
| 122 | * Returns the backing string value. |
| 123 | * |
| 124 | * @return port name as string value |
| 125 | */ |
| 126 | public String name() { |
| 127 | return name; |
| 128 | } |
| 129 | |
Marc De Leenheer | 76d8974 | 2015-04-16 15:03:03 -0700 | [diff] [blame] | 130 | /** |
| 131 | * Indicates whether this port number was created with a port name, |
| 132 | * or only with a number. |
| 133 | * |
| 134 | * @return true if port was created with name |
| 135 | */ |
| 136 | public boolean hasName() { |
| 137 | return hasName; |
| 138 | } |
| 139 | |
Ray Milkey | 8717be2 | 2015-01-12 14:16:27 -0800 | [diff] [blame] | 140 | private String decodeLogicalPort() { |
| 141 | if (number == CONTROLLER_NUMBER) { |
| 142 | return "CONTROLLER"; |
| 143 | } else if (number == LOCAL_NUMBER) { |
| 144 | return "LOCAL"; |
| 145 | } else if (number == ALL_NUMBER) { |
| 146 | return "ALL"; |
| 147 | } else if (number == FLOOD_NUMBER) { |
| 148 | return "FLOOD"; |
| 149 | } else if (number == NORMAL_NUMBER) { |
| 150 | return "NORMAL"; |
| 151 | } else if (number == TABLE_NUMBER) { |
| 152 | return "TABLE"; |
| 153 | } else if (number == IN_PORT_NUMBER) { |
| 154 | return "IN_PORT"; |
| 155 | } |
| 156 | return "UNKNOWN"; |
| 157 | } |
| 158 | |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 159 | @Override |
| 160 | public String toString() { |
Ray Milkey | 8717be2 | 2015-01-12 14:16:27 -0800 | [diff] [blame] | 161 | if (!isLogical()) { |
Marc De Leenheer | 171c2e8 | 2015-04-13 17:20:37 -0700 | [diff] [blame] | 162 | return name; |
Ray Milkey | 8717be2 | 2015-01-12 14:16:27 -0800 | [diff] [blame] | 163 | } else { |
| 164 | return decodeLogicalPort(); |
| 165 | } |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public int hashCode() { |
| 170 | return Objects.hash(number); |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public boolean equals(Object obj) { |
tom | fc9a4ff | 2014-09-22 18:22:47 -0700 | [diff] [blame] | 175 | if (this == obj) { |
| 176 | return true; |
| 177 | } |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 178 | if (obj instanceof PortNumber) { |
| 179 | final PortNumber other = (PortNumber) obj; |
| 180 | return this.number == other.number; |
| 181 | } |
| 182 | return false; |
| 183 | } |
tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame] | 184 | } |