Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present 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.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; |
| 20 | import org.onosproject.net.PortNumber; |
| 21 | import org.onosproject.net.SparseAnnotations; |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 22 | |
HIGUCHI Yuta | fdb82fa | 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; |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 37 | private final Type type; |
| 38 | private final long portSpeed; |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 39 | |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 40 | /** |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 41 | * Creates a DEFAULT_SPEED COPPER port description using the supplied information. |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 42 | * |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 43 | * @param number port number |
| 44 | * @param isEnabled port enabled state |
| 45 | * @param annotations optional key/value annotations map |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 46 | */ |
| 47 | public DefaultPortDescription(PortNumber number, boolean isEnabled, |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 48 | SparseAnnotations... annotations) { |
| 49 | this(number, isEnabled, Type.COPPER, DEFAULT_SPEED, annotations); |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 52 | /** |
| 53 | * Creates a port description using the supplied information. |
| 54 | * |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 55 | * @param number port number |
| 56 | * @param isEnabled port enabled state |
| 57 | * @param type port type |
| 58 | * @param portSpeed port speed in Mbps |
| 59 | * @param annotations optional key/value annotations map |
| 60 | */ |
| 61 | public DefaultPortDescription(PortNumber number, boolean isEnabled, |
| 62 | Type type, long portSpeed, |
| 63 | SparseAnnotations...annotations) { |
| 64 | super(annotations); |
HIGUCHI Yuta | fdb82fa | 2016-06-03 14:27:05 -0700 | [diff] [blame] | 65 | this.number = checkNotNull(number); |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 66 | this.isEnabled = isEnabled; |
| 67 | this.type = type; |
| 68 | this.portSpeed = portSpeed; |
| 69 | } |
| 70 | |
| 71 | // Default constructor for serialization |
| 72 | private DefaultPortDescription() { |
| 73 | this.number = null; |
| 74 | this.isEnabled = false; |
| 75 | this.portSpeed = DEFAULT_SPEED; |
| 76 | this.type = Type.COPPER; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Creates a port description using the supplied information. |
| 81 | * |
| 82 | * @param base PortDescription to get basic information from |
| 83 | * @param annotations optional key/value annotations map |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 84 | */ |
| 85 | public DefaultPortDescription(PortDescription base, |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 86 | SparseAnnotations annotations) { |
| 87 | this(base.portNumber(), base.isEnabled(), base.type(), base.portSpeed(), |
| 88 | annotations); |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 89 | } |
| 90 | |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 91 | @Override |
| 92 | public PortNumber portNumber() { |
| 93 | return number; |
| 94 | } |
| 95 | |
| 96 | @Override |
tom | d40fc7a | 2014-09-04 16:41:10 -0700 | [diff] [blame] | 97 | public boolean isEnabled() { |
| 98 | return isEnabled; |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 101 | @Override |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 102 | public Type type() { |
| 103 | return type; |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public long portSpeed() { |
| 108 | return portSpeed; |
| 109 | } |
| 110 | |
| 111 | @Override |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 112 | public String toString() { |
| 113 | return MoreObjects.toStringHelper(getClass()) |
| 114 | .add("number", number) |
| 115 | .add("isEnabled", isEnabled) |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 116 | .add("type", type) |
| 117 | .add("portSpeed", portSpeed) |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 118 | .add("annotations", annotations()) |
| 119 | .toString(); |
| 120 | } |
| 121 | |
HIGUCHI Yuta | 703a5af | 2015-11-18 23:43:50 -0800 | [diff] [blame] | 122 | @Override |
| 123 | public int hashCode() { |
| 124 | return Objects.hashCode(super.hashCode(), number, isEnabled, type, |
| 125 | portSpeed); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public boolean equals(Object object) { |
| 130 | if (object != null && getClass() == object.getClass()) { |
| 131 | if (!super.equals(object)) { |
| 132 | return false; |
| 133 | } |
| 134 | DefaultPortDescription that = (DefaultPortDescription) object; |
| 135 | return Objects.equal(this.number, that.number) |
| 136 | && Objects.equal(this.isEnabled, that.isEnabled) |
| 137 | && Objects.equal(this.type, that.type) |
| 138 | && Objects.equal(this.portSpeed, that.portSpeed); |
| 139 | } |
| 140 | return false; |
| 141 | } |
| 142 | |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 143 | } |