Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
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.device; |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 17 | |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 18 | import com.google.common.base.MoreObjects; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 19 | import org.onosproject.net.AbstractDescription; |
Yuta HIGUCHI | 9f2d724 | 2017-05-18 17:17:32 -0700 | [diff] [blame] | 20 | import org.onosproject.net.DefaultAnnotations; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 21 | import org.onosproject.net.PortNumber; |
| 22 | import org.onosproject.net.SparseAnnotations; |
HIGUCHI Yuta | cc10558d | 2016-06-03 14:27:05 -0700 | [diff] [blame] | 23 | import static com.google.common.base.Preconditions.checkNotNull; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 24 | import static org.onosproject.net.Port.Type; |
HIGUCHI Yuta | 703a5af | 2015-11-18 23:43:50 -0800 | [diff] [blame] | 25 | import com.google.common.base.Objects; |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 26 | |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 27 | /** |
| 28 | * Default implementation of immutable port description. |
| 29 | */ |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 30 | public class DefaultPortDescription extends AbstractDescription |
| 31 | implements PortDescription { |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 32 | |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 33 | private static final long DEFAULT_SPEED = 1_000; |
| 34 | |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 35 | private final PortNumber number; |
tom | d40fc7a | 2014-09-04 16:41:10 -0700 | [diff] [blame] | 36 | private final boolean isEnabled; |
Michal Mach | ce77433 | 2017-01-25 11:02:55 +0100 | [diff] [blame] | 37 | private final boolean isRemoved; |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 38 | private final Type type; |
| 39 | private final long portSpeed; |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 40 | |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 41 | /** |
Michal Mach | ce77433 | 2017-01-25 11:02:55 +0100 | [diff] [blame] | 42 | * Creates a port description using the supplied information. |
| 43 | * |
| 44 | * @param number port number |
| 45 | * @param isEnabled port enabled state |
| 46 | * @param isRemoved port removed state |
| 47 | * @param type port type |
| 48 | * @param portSpeed port speed in Mbps |
| 49 | * @param annotations optional key/value annotations map |
| 50 | */ |
Ray Milkey | f031e30 | 2018-09-07 10:07:24 -0700 | [diff] [blame] | 51 | private DefaultPortDescription(PortNumber number, boolean isEnabled, boolean isRemoved, |
Michal Mach | ce77433 | 2017-01-25 11:02:55 +0100 | [diff] [blame] | 52 | Type type, long portSpeed, |
| 53 | SparseAnnotations...annotations) { |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 54 | super(annotations); |
HIGUCHI Yuta | cc10558d | 2016-06-03 14:27:05 -0700 | [diff] [blame] | 55 | this.number = checkNotNull(number); |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 56 | this.isEnabled = isEnabled; |
Michal Mach | ce77433 | 2017-01-25 11:02:55 +0100 | [diff] [blame] | 57 | this.isRemoved = isRemoved; |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 58 | this.type = type; |
| 59 | this.portSpeed = portSpeed; |
| 60 | } |
| 61 | |
| 62 | // Default constructor for serialization |
Ray Milkey | dbf59f0 | 2016-08-19 12:54:16 -0700 | [diff] [blame] | 63 | protected DefaultPortDescription() { |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 64 | this.number = null; |
| 65 | this.isEnabled = false; |
Michal Mach | ce77433 | 2017-01-25 11:02:55 +0100 | [diff] [blame] | 66 | this.isRemoved = false; |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 67 | this.portSpeed = DEFAULT_SPEED; |
| 68 | this.type = Type.COPPER; |
| 69 | } |
| 70 | |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 71 | @Override |
| 72 | public PortNumber portNumber() { |
| 73 | return number; |
| 74 | } |
| 75 | |
| 76 | @Override |
tom | d40fc7a | 2014-09-04 16:41:10 -0700 | [diff] [blame] | 77 | public boolean isEnabled() { |
| 78 | return isEnabled; |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 81 | @Override |
Michal Mach | ce77433 | 2017-01-25 11:02:55 +0100 | [diff] [blame] | 82 | public boolean isRemoved() { |
| 83 | return isRemoved; |
| 84 | } |
| 85 | |
| 86 | @Override |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 87 | public Type type() { |
| 88 | return type; |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public long portSpeed() { |
| 93 | return portSpeed; |
| 94 | } |
| 95 | |
| 96 | @Override |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 97 | public String toString() { |
| 98 | return MoreObjects.toStringHelper(getClass()) |
| 99 | .add("number", number) |
| 100 | .add("isEnabled", isEnabled) |
Palash Kala | 3b4177b | 2017-08-22 15:48:15 +0900 | [diff] [blame] | 101 | .add("isRemoved", isRemoved) |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 102 | .add("type", type) |
| 103 | .add("portSpeed", portSpeed) |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 104 | .add("annotations", annotations()) |
| 105 | .toString(); |
| 106 | } |
| 107 | |
HIGUCHI Yuta | 703a5af | 2015-11-18 23:43:50 -0800 | [diff] [blame] | 108 | @Override |
| 109 | public int hashCode() { |
| 110 | return Objects.hashCode(super.hashCode(), number, isEnabled, type, |
| 111 | portSpeed); |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public boolean equals(Object object) { |
| 116 | if (object != null && getClass() == object.getClass()) { |
| 117 | if (!super.equals(object)) { |
| 118 | return false; |
| 119 | } |
| 120 | DefaultPortDescription that = (DefaultPortDescription) object; |
| 121 | return Objects.equal(this.number, that.number) |
| 122 | && Objects.equal(this.isEnabled, that.isEnabled) |
| 123 | && Objects.equal(this.type, that.type) |
| 124 | && Objects.equal(this.portSpeed, that.portSpeed); |
| 125 | } |
| 126 | return false; |
| 127 | } |
| 128 | |
Yuta HIGUCHI | 9f2d724 | 2017-05-18 17:17:32 -0700 | [diff] [blame] | 129 | /** |
| 130 | * Creates port description builder with default parameters. |
| 131 | * |
| 132 | * @return builder |
| 133 | */ |
| 134 | public static Builder builder() { |
| 135 | return new Builder(); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Creates port description builder inheriting with default parameters, |
| 140 | * from specified port description. |
| 141 | * |
| 142 | * @param desc to inherit default from |
| 143 | * @return builder |
| 144 | */ |
| 145 | public static Builder builder(PortDescription desc) { |
| 146 | return new Builder(desc); |
| 147 | } |
| 148 | |
| 149 | public static class Builder { |
| 150 | private PortNumber number; |
| 151 | private boolean isEnabled = true; |
| 152 | private boolean isRemoved = false; |
| 153 | private Type type = Type.COPPER; |
| 154 | private long portSpeed = DEFAULT_SPEED; |
| 155 | private SparseAnnotations annotations = DefaultAnnotations.EMPTY; |
| 156 | |
| 157 | Builder() {} |
| 158 | |
| 159 | Builder(PortDescription desc) { |
| 160 | this.number = desc.portNumber(); |
| 161 | this.isEnabled = desc.isEnabled(); |
| 162 | this.isRemoved = desc.isRemoved(); |
| 163 | this.type = desc.type(); |
| 164 | this.portSpeed = desc.portSpeed(); |
| 165 | this.annotations = desc.annotations(); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Sets mandatory field PortNumber. |
| 170 | * |
| 171 | * @param number to set |
| 172 | * @return self |
Yuta HIGUCHI | 53e4796 | 2018-03-01 23:50:48 -0800 | [diff] [blame] | 173 | */ |
| 174 | public Builder withPortNumber(PortNumber number) { |
Yuta HIGUCHI | 9f2d724 | 2017-05-18 17:17:32 -0700 | [diff] [blame] | 175 | this.number = checkNotNull(number); |
| 176 | return this; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Sets enabled state. |
| 181 | * |
| 182 | * @param enabled state |
| 183 | * @return self |
| 184 | */ |
| 185 | public Builder isEnabled(boolean enabled) { |
| 186 | this.isEnabled = enabled; |
| 187 | return this; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Sets removed state. |
| 192 | * |
| 193 | * @param removed state |
| 194 | * @return self |
| 195 | */ |
| 196 | public Builder isRemoved(boolean removed) { |
| 197 | this.isRemoved = removed; |
| 198 | return this; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Sets port type. |
| 203 | * |
| 204 | * @param type of the port |
| 205 | * @return self |
| 206 | */ |
| 207 | public Builder type(Type type) { |
| 208 | this.type = type; |
| 209 | return this; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Sets port speed. |
| 214 | * |
| 215 | * @param mbps port speed in Mbps |
| 216 | * @return self |
| 217 | */ |
| 218 | public Builder portSpeed(long mbps) { |
| 219 | this.portSpeed = mbps; |
| 220 | return this; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Sets annotations. |
| 225 | * |
| 226 | * @param annotations of the port |
| 227 | * @return self |
| 228 | */ |
| 229 | public Builder annotations(SparseAnnotations annotations) { |
| 230 | this.annotations = checkNotNull(annotations); |
| 231 | return this; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Builds the port description. |
| 236 | * |
| 237 | * @return port description |
| 238 | */ |
| 239 | public DefaultPortDescription build() { |
| 240 | return new DefaultPortDescription(number, isEnabled, isRemoved, type, portSpeed, annotations); |
| 241 | } |
| 242 | } |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 243 | } |