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 | */ |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 16 | package org.onlab.onos.net.device; |
| 17 | |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 18 | import com.google.common.base.MoreObjects; |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 19 | import org.onlab.onos.net.AbstractDescription; |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 20 | import org.onlab.onos.net.PortNumber; |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 21 | import org.onlab.onos.net.SparseAnnotations; |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 22 | |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 23 | import static org.onlab.onos.net.Port.Type; |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 24 | |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 25 | /** |
| 26 | * Default implementation of immutable port description. |
| 27 | */ |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 28 | public class DefaultPortDescription extends AbstractDescription |
| 29 | implements PortDescription { |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 30 | |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 31 | private static final long DEFAULT_SPEED = 1_000; |
| 32 | |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 33 | private final PortNumber number; |
tom | d40fc7a | 2014-09-04 16:41:10 -0700 | [diff] [blame] | 34 | private final boolean isEnabled; |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 35 | private final Type type; |
| 36 | private final long portSpeed; |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 37 | |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 38 | /** |
| 39 | * Creates a port description using the supplied information. |
| 40 | * |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 41 | * @param number port number |
| 42 | * @param isEnabled port enabled state |
| 43 | * @param annotations optional key/value annotations map |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 44 | */ |
| 45 | public DefaultPortDescription(PortNumber number, boolean isEnabled, |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 46 | SparseAnnotations... annotations) { |
| 47 | this(number, isEnabled, Type.COPPER, DEFAULT_SPEED, annotations); |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 50 | /** |
| 51 | * Creates a port description using the supplied information. |
| 52 | * |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 53 | * @param number port number |
| 54 | * @param isEnabled port enabled state |
| 55 | * @param type port type |
| 56 | * @param portSpeed port speed in Mbps |
| 57 | * @param annotations optional key/value annotations map |
| 58 | */ |
| 59 | public DefaultPortDescription(PortNumber number, boolean isEnabled, |
| 60 | Type type, long portSpeed, |
| 61 | SparseAnnotations...annotations) { |
| 62 | super(annotations); |
| 63 | this.number = number; |
| 64 | this.isEnabled = isEnabled; |
| 65 | this.type = type; |
| 66 | this.portSpeed = portSpeed; |
| 67 | } |
| 68 | |
| 69 | // Default constructor for serialization |
| 70 | private DefaultPortDescription() { |
| 71 | this.number = null; |
| 72 | this.isEnabled = false; |
| 73 | this.portSpeed = DEFAULT_SPEED; |
| 74 | this.type = Type.COPPER; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Creates a port description using the supplied information. |
| 79 | * |
| 80 | * @param base PortDescription to get basic information from |
| 81 | * @param annotations optional key/value annotations map |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 82 | */ |
| 83 | public DefaultPortDescription(PortDescription base, |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 84 | SparseAnnotations annotations) { |
| 85 | this(base.portNumber(), base.isEnabled(), base.type(), base.portSpeed(), |
| 86 | annotations); |
Yuta HIGUCHI | 55710e7 | 2014-10-02 14:58:32 -0700 | [diff] [blame] | 87 | } |
| 88 | |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 89 | @Override |
| 90 | public PortNumber portNumber() { |
| 91 | return number; |
| 92 | } |
| 93 | |
| 94 | @Override |
tom | d40fc7a | 2014-09-04 16:41:10 -0700 | [diff] [blame] | 95 | public boolean isEnabled() { |
| 96 | return isEnabled; |
tom | ca20e0c | 2014-09-03 23:22:24 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 99 | @Override |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 100 | public Type type() { |
| 101 | return type; |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public long portSpeed() { |
| 106 | return portSpeed; |
| 107 | } |
| 108 | |
| 109 | @Override |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 110 | public String toString() { |
| 111 | return MoreObjects.toStringHelper(getClass()) |
| 112 | .add("number", number) |
| 113 | .add("isEnabled", isEnabled) |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 114 | .add("type", type) |
| 115 | .add("portSpeed", portSpeed) |
Yuta HIGUCHI | 9ee60f6 | 2014-10-09 10:00:01 -0700 | [diff] [blame] | 116 | .add("annotations", annotations()) |
| 117 | .toString(); |
| 118 | } |
| 119 | |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 120 | } |