blob: a507e4930338f22085cc9630dff2611a904a59e3 [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
Brian O'Connorabafb502014-12-02 22:26:20 -080023import static org.onosproject.net.Port.Type;
HIGUCHI Yuta703a5af2015-11-18 23:43:50 -080024import com.google.common.base.Objects;
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -070025
tomd1900f32014-09-03 14:08:16 -070026/**
27 * Default implementation of immutable port description.
28 */
Yuta HIGUCHI55710e72014-10-02 14:58:32 -070029public class DefaultPortDescription extends AbstractDescription
30 implements PortDescription {
tomca20e0c2014-09-03 23:22:24 -070031
Thomas Vachuskad16ce182014-10-29 17:25:29 -070032 private static final long DEFAULT_SPEED = 1_000;
33
tomca20e0c2014-09-03 23:22:24 -070034 private final PortNumber number;
tomd40fc7a2014-09-04 16:41:10 -070035 private final boolean isEnabled;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070036 private final Type type;
37 private final long portSpeed;
tomca20e0c2014-09-03 23:22:24 -070038
Yuta HIGUCHI55710e72014-10-02 14:58:32 -070039 /**
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080040 * Creates a DEFAULT_SPEED COPPER port description using the supplied information.
Yuta HIGUCHI55710e72014-10-02 14:58:32 -070041 *
Thomas Vachuskad16ce182014-10-29 17:25:29 -070042 * @param number port number
43 * @param isEnabled port enabled state
44 * @param annotations optional key/value annotations map
Yuta HIGUCHI55710e72014-10-02 14:58:32 -070045 */
46 public DefaultPortDescription(PortNumber number, boolean isEnabled,
Thomas Vachuskad16ce182014-10-29 17:25:29 -070047 SparseAnnotations... annotations) {
48 this(number, isEnabled, Type.COPPER, DEFAULT_SPEED, annotations);
tomca20e0c2014-09-03 23:22:24 -070049 }
50
Yuta HIGUCHI55710e72014-10-02 14:58:32 -070051 /**
52 * Creates a port description using the supplied information.
53 *
Thomas Vachuskad16ce182014-10-29 17:25:29 -070054 * @param number port number
55 * @param isEnabled port enabled state
56 * @param type port type
57 * @param portSpeed port speed in Mbps
58 * @param annotations optional key/value annotations map
59 */
60 public DefaultPortDescription(PortNumber number, boolean isEnabled,
61 Type type, long portSpeed,
62 SparseAnnotations...annotations) {
63 super(annotations);
64 this.number = number;
65 this.isEnabled = isEnabled;
66 this.type = type;
67 this.portSpeed = portSpeed;
68 }
69
70 // Default constructor for serialization
71 private DefaultPortDescription() {
72 this.number = null;
73 this.isEnabled = false;
74 this.portSpeed = DEFAULT_SPEED;
75 this.type = Type.COPPER;
76 }
77
78 /**
79 * Creates a port description using the supplied information.
80 *
81 * @param base PortDescription to get basic information from
82 * @param annotations optional key/value annotations map
Yuta HIGUCHI55710e72014-10-02 14:58:32 -070083 */
84 public DefaultPortDescription(PortDescription base,
Thomas Vachuskad16ce182014-10-29 17:25:29 -070085 SparseAnnotations annotations) {
86 this(base.portNumber(), base.isEnabled(), base.type(), base.portSpeed(),
87 annotations);
Yuta HIGUCHI55710e72014-10-02 14:58:32 -070088 }
89
tomca20e0c2014-09-03 23:22:24 -070090 @Override
91 public PortNumber portNumber() {
92 return number;
93 }
94
95 @Override
tomd40fc7a2014-09-04 16:41:10 -070096 public boolean isEnabled() {
97 return isEnabled;
tomca20e0c2014-09-03 23:22:24 -070098 }
99
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700100 @Override
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700101 public Type type() {
102 return type;
103 }
104
105 @Override
106 public long portSpeed() {
107 return portSpeed;
108 }
109
110 @Override
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700111 public String toString() {
112 return MoreObjects.toStringHelper(getClass())
113 .add("number", number)
114 .add("isEnabled", isEnabled)
Thomas Vachuskad16ce182014-10-29 17:25:29 -0700115 .add("type", type)
116 .add("portSpeed", portSpeed)
Yuta HIGUCHI9ee60f62014-10-09 10:00:01 -0700117 .add("annotations", annotations())
118 .toString();
119 }
120
HIGUCHI Yuta703a5af2015-11-18 23:43:50 -0800121 @Override
122 public int hashCode() {
123 return Objects.hashCode(super.hashCode(), number, isEnabled, type,
124 portSpeed);
125 }
126
127 @Override
128 public boolean equals(Object object) {
129 if (object != null && getClass() == object.getClass()) {
130 if (!super.equals(object)) {
131 return false;
132 }
133 DefaultPortDescription that = (DefaultPortDescription) object;
134 return Objects.equal(this.number, that.number)
135 && Objects.equal(this.isEnabled, that.isEnabled)
136 && Objects.equal(this.type, that.type)
137 && Objects.equal(this.portSpeed, that.portSpeed);
138 }
139 return false;
140 }
141
tomd1900f32014-09-03 14:08:16 -0700142}