blob: 1e07e296c311a21915cccf70266a99cb7909f90d [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;
Michal Mach31097ad2017-01-25 11:02:55 +010037 private final boolean isRemoved;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070038 private final Type type;
39 private final long portSpeed;
tomca20e0c2014-09-03 23:22:24 -070040
Yuta HIGUCHI55710e72014-10-02 14:58:32 -070041 /**
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080042 * Creates a DEFAULT_SPEED COPPER port description using the supplied information.
Yuta HIGUCHI55710e72014-10-02 14:58:32 -070043 *
Thomas Vachuskad16ce182014-10-29 17:25:29 -070044 * @param number port number
45 * @param isEnabled port enabled state
46 * @param annotations optional key/value annotations map
Yuta HIGUCHI55710e72014-10-02 14:58:32 -070047 */
48 public DefaultPortDescription(PortNumber number, boolean isEnabled,
Thomas Vachuskad16ce182014-10-29 17:25:29 -070049 SparseAnnotations... annotations) {
50 this(number, isEnabled, Type.COPPER, DEFAULT_SPEED, annotations);
tomca20e0c2014-09-03 23:22:24 -070051 }
52
Yuta HIGUCHI55710e72014-10-02 14:58:32 -070053 /**
54 * Creates a port description using the supplied information.
55 *
Thomas Vachuskad16ce182014-10-29 17:25:29 -070056 * @param number port number
57 * @param isEnabled port enabled state
58 * @param type port type
59 * @param portSpeed port speed in Mbps
60 * @param annotations optional key/value annotations map
61 */
62 public DefaultPortDescription(PortNumber number, boolean isEnabled,
63 Type type, long portSpeed,
64 SparseAnnotations...annotations) {
Michal Mach31097ad2017-01-25 11:02:55 +010065 this(number, isEnabled, false, type, portSpeed, annotations);
66 }
67
68 /**
69 * Creates a port description using the supplied information.
70 *
71 * @param number port number
72 * @param isEnabled port enabled state
73 * @param isRemoved port removed state
74 * @param type port type
75 * @param portSpeed port speed in Mbps
76 * @param annotations optional key/value annotations map
77 */
78 public DefaultPortDescription(PortNumber number, boolean isEnabled, boolean isRemoved,
79 Type type, long portSpeed,
80 SparseAnnotations...annotations) {
Thomas Vachuskad16ce182014-10-29 17:25:29 -070081 super(annotations);
HIGUCHI Yutacc10558d2016-06-03 14:27:05 -070082 this.number = checkNotNull(number);
Thomas Vachuskad16ce182014-10-29 17:25:29 -070083 this.isEnabled = isEnabled;
Michal Mach31097ad2017-01-25 11:02:55 +010084 this.isRemoved = isRemoved;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070085 this.type = type;
86 this.portSpeed = portSpeed;
87 }
88
89 // Default constructor for serialization
Ray Milkeydbf59f02016-08-19 12:54:16 -070090 protected DefaultPortDescription() {
Thomas Vachuskad16ce182014-10-29 17:25:29 -070091 this.number = null;
92 this.isEnabled = false;
Michal Mach31097ad2017-01-25 11:02:55 +010093 this.isRemoved = false;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070094 this.portSpeed = DEFAULT_SPEED;
95 this.type = Type.COPPER;
96 }
97
98 /**
99 * Creates a port description using the supplied information.
100 *
101 * @param base PortDescription to get basic information from
102 * @param annotations optional key/value annotations map
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700103 */
104 public DefaultPortDescription(PortDescription base,
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700105 SparseAnnotations annotations) {
106 this(base.portNumber(), base.isEnabled(), base.type(), base.portSpeed(),
107 annotations);
Yuta HIGUCHI55710e72014-10-02 14:58:32 -0700108 }
109
tomca20e0c2014-09-03 23:22:24 -0700110 @Override
111 public PortNumber portNumber() {
112 return number;
113 }
114
115 @Override
tomd40fc7a2014-09-04 16:41:10 -0700116 public boolean isEnabled() {
117 return isEnabled;
tomca20e0c2014-09-03 23:22:24 -0700118 }
119
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700120 @Override
Michal Mach31097ad2017-01-25 11:02:55 +0100121 public boolean isRemoved() {
122 return isRemoved;
123 }
124
125 @Override
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700126 public Type type() {
127 return type;
128 }
129
130 @Override
131 public long portSpeed() {
132 return portSpeed;
133 }
134
135 @Override
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700136 public String toString() {
137 return MoreObjects.toStringHelper(getClass())
138 .add("number", number)
139 .add("isEnabled", isEnabled)
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700140 .add("type", type)
141 .add("portSpeed", portSpeed)
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700142 .add("annotations", annotations())
143 .toString();
144 }
145
HIGUCHI Yuta703a5af2015-11-18 23:43:50 -0800146 @Override
147 public int hashCode() {
148 return Objects.hashCode(super.hashCode(), number, isEnabled, type,
149 portSpeed);
150 }
151
152 @Override
153 public boolean equals(Object object) {
154 if (object != null && getClass() == object.getClass()) {
155 if (!super.equals(object)) {
156 return false;
157 }
158 DefaultPortDescription that = (DefaultPortDescription) object;
159 return Objects.equal(this.number, that.number)
160 && Objects.equal(this.isEnabled, that.isEnabled)
161 && Objects.equal(this.type, that.type)
162 && Objects.equal(this.portSpeed, that.portSpeed);
163 }
164 return false;
165 }
166
tomd1900f32014-09-03 14:08:16 -0700167}