blob: 89cc6e857a0df9a3fe3252a3a7878a166fbbf828 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.device;
tomd1900f32014-09-03 14:08:16 -070017
Thomas Vachuskad16ce182014-10-29 17:25:29 -070018import com.google.common.base.MoreObjects;
Brian O'Connorabafb502014-12-02 22:26:20 -080019import org.onosproject.net.AbstractDescription;
20import org.onosproject.net.PortNumber;
21import org.onosproject.net.SparseAnnotations;
tomca20e0c2014-09-03 23:22:24 -070022
HIGUCHI Yutacc10558d2016-06-03 14:27:05 -070023import static com.google.common.base.Preconditions.checkNotNull;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import static org.onosproject.net.Port.Type;
HIGUCHI Yuta703a5af2015-11-18 23:43:50 -080025import com.google.common.base.Objects;
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070026
tomd1900f32014-09-03 14:08:16 -070027/**
28 * Default implementation of immutable port description.
29 */
Yuta HIGUCHI55710e72014-10-02 14:58:32 -070030public class DefaultPortDescription extends AbstractDescription
31 implements PortDescription {
tomca20e0c2014-09-03 23:22:24 -070032
Thomas Vachuskad16ce182014-10-29 17:25:29 -070033 private static final long DEFAULT_SPEED = 1_000;
34
tomca20e0c2014-09-03 23:22:24 -070035 private final PortNumber number;
tomd40fc7a2014-09-04 16:41:10 -070036 private final boolean isEnabled;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070037 private final Type type;
38 private final long portSpeed;
tomca20e0c2014-09-03 23:22:24 -070039
Yuta HIGUCHI55710e72014-10-02 14:58:32 -070040 /**
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080041 * Creates a DEFAULT_SPEED COPPER port description using the supplied information.
Yuta HIGUCHI55710e72014-10-02 14:58:32 -070042 *
Thomas Vachuskad16ce182014-10-29 17:25:29 -070043 * @param number port number
44 * @param isEnabled port enabled state
45 * @param annotations optional key/value annotations map
Yuta HIGUCHI55710e72014-10-02 14:58:32 -070046 */
47 public DefaultPortDescription(PortNumber number, boolean isEnabled,
Thomas Vachuskad16ce182014-10-29 17:25:29 -070048 SparseAnnotations... annotations) {
49 this(number, isEnabled, Type.COPPER, DEFAULT_SPEED, annotations);
tomca20e0c2014-09-03 23:22:24 -070050 }
51
Yuta HIGUCHI55710e72014-10-02 14:58:32 -070052 /**
53 * Creates a port description using the supplied information.
54 *
Thomas Vachuskad16ce182014-10-29 17:25:29 -070055 * @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 Yutacc10558d2016-06-03 14:27:05 -070065 this.number = checkNotNull(number);
Thomas Vachuskad16ce182014-10-29 17:25:29 -070066 this.isEnabled = isEnabled;
67 this.type = type;
68 this.portSpeed = portSpeed;
69 }
70
71 // Default constructor for serialization
Ray Milkeydbf59f02016-08-19 12:54:16 -070072 protected DefaultPortDescription() {
Thomas Vachuskad16ce182014-10-29 17:25:29 -070073 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 HIGUCHI55710e72014-10-02 14:58:32 -070084 */
85 public DefaultPortDescription(PortDescription base,
Thomas Vachuskad16ce182014-10-29 17:25:29 -070086 SparseAnnotations annotations) {
87 this(base.portNumber(), base.isEnabled(), base.type(), base.portSpeed(),
88 annotations);
Yuta HIGUCHI55710e72014-10-02 14:58:32 -070089 }
90
Yuta HIGUCHI7438f5a2017-02-15 22:09:46 -080091 /**
92 * Creates a port description using the supplied information.
93 *
94 * @param base port description to copy fields from
95 * @param annotations to be used in the copied description.
96 * Note: Annotations on {@code base} will be ignored.
97 * @return copied port description
98 */
99 public static DefaultPortDescription copyReplacingAnnotation(PortDescription base,
100 SparseAnnotations annotations) {
101 return new DefaultPortDescription(base, annotations);
102 }
103
tomca20e0c2014-09-03 23:22:24 -0700104 @Override
105 public PortNumber portNumber() {
106 return number;
107 }
108
109 @Override
tomd40fc7a2014-09-04 16:41:10 -0700110 public boolean isEnabled() {
111 return isEnabled;
tomca20e0c2014-09-03 23:22:24 -0700112 }
113
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700114 @Override
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700115 public Type type() {
116 return type;
117 }
118
119 @Override
120 public long portSpeed() {
121 return portSpeed;
122 }
123
124 @Override
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700125 public String toString() {
126 return MoreObjects.toStringHelper(getClass())
127 .add("number", number)
128 .add("isEnabled", isEnabled)
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700129 .add("type", type)
130 .add("portSpeed", portSpeed)
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700131 .add("annotations", annotations())
132 .toString();
133 }
134
HIGUCHI Yuta703a5af2015-11-18 23:43:50 -0800135 @Override
136 public int hashCode() {
137 return Objects.hashCode(super.hashCode(), number, isEnabled, type,
138 portSpeed);
139 }
140
141 @Override
142 public boolean equals(Object object) {
143 if (object != null && getClass() == object.getClass()) {
144 if (!super.equals(object)) {
145 return false;
146 }
147 DefaultPortDescription that = (DefaultPortDescription) object;
148 return Objects.equal(this.number, that.number)
149 && Objects.equal(this.isEnabled, that.isEnabled)
150 && Objects.equal(this.type, that.type)
151 && Objects.equal(this.portSpeed, that.portSpeed);
152 }
153 return false;
154 }
155
tomd1900f32014-09-03 14:08:16 -0700156}