blob: e545f69e7ce8f7e0c2adfde16bae1b25e502856a [file] [log] [blame]
Jian Li9ee9c8b2019-01-24 11:48:12 +09001/*
2 * Copyright 2019-present Open Networking Foundation
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 */
16package org.onosproject.k8snetworking.api;
17
18import com.google.common.base.MoreObjects;
19import com.google.common.base.Objects;
20import org.onlab.packet.IpAddress;
21import org.onlab.packet.MacAddress;
22import org.onosproject.net.DeviceId;
23import org.onosproject.net.PortNumber;
24
25import static com.google.common.base.Preconditions.checkArgument;
26
27/**
28 * Default implementation of kubernetes port.
29 */
30public final class DefaultK8sPort implements K8sPort {
31
32 private static final String NOT_NULL_MSG = "Port % cannot be null";
33
34 private final String networkId;
35 private final String portId;
36 private final MacAddress macAddress;
37 private final IpAddress ipAddress;
38 private final DeviceId deviceId;
39 private final PortNumber portNumber;
40 private final State state;
41
42 // private constructor not intended for external invocation
43 private DefaultK8sPort(String networkId, String portId, MacAddress macAddress,
44 IpAddress ipAddress, DeviceId deviceId,
45 PortNumber portNumber, State state) {
46 this.networkId = networkId;
47 this.portId = portId;
48 this.macAddress = macAddress;
49 this.ipAddress = ipAddress;
50 this.deviceId = deviceId;
51 this.portNumber = portNumber;
52 this.state = state;
53 }
54
55 @Override
56 public String networkId() {
57 return networkId;
58 }
59
60 @Override
61 public String portId() {
62 return portId;
63 }
64
65 @Override
66 public MacAddress macAddress() {
67 return macAddress;
68 }
69
70 @Override
71 public IpAddress ipAddress() {
72 return ipAddress;
73 }
74
75 @Override
76 public DeviceId deviceId() {
77 return deviceId;
78 }
79
80 @Override
81 public PortNumber portNumber() {
82 return portNumber;
83 }
84
85 @Override
86 public State state() {
87 return state;
88 }
89
90 @Override
91 public K8sPort updateState(State newState) {
92 return new Builder()
93 .networkId(networkId)
94 .portId(portId)
95 .macAddress(macAddress)
96 .ipAddress(ipAddress)
97 .deviceId(deviceId)
98 .portNumber(portNumber)
99 .state(newState)
100 .build();
101 }
102
103 @Override
104 public boolean equals(Object o) {
105 if (this == o) {
106 return true;
107 }
108 if (o == null || getClass() != o.getClass()) {
109 return false;
110 }
111 DefaultK8sPort that = (DefaultK8sPort) o;
112 return Objects.equal(networkId, that.networkId) &&
113 Objects.equal(portId, that.portId) &&
114 Objects.equal(macAddress, that.macAddress) &&
115 Objects.equal(ipAddress, that.ipAddress) &&
116 Objects.equal(deviceId, that.deviceId) &&
117 Objects.equal(portNumber, that.portNumber) &&
118 state == that.state;
119 }
120
121 @Override
122 public int hashCode() {
123 return Objects.hashCode(networkId, portId, macAddress, ipAddress,
124 deviceId, portNumber, state);
125 }
126
127 @Override
128 public String toString() {
129 return MoreObjects.toStringHelper(this)
130 .add("networkId", networkId)
131 .add("portId", portId)
132 .add("macAddress", macAddress)
133 .add("ipAddress", ipAddress)
134 .add("deviceId", deviceId)
135 .add("portNumber", portNumber)
136 .add("state", state)
137 .toString();
138 }
139
140 /**
141 * Returns new builder instance.
142 *
143 * @return kubernetes port builder
144 */
145 public static Builder builder() {
146 return new Builder();
147 }
148
149 /**
150 * Default builder implementation.
151 */
152 public static final class Builder implements K8sPort.Builder {
153
154 private String networkId;
155 private String portId;
156 private MacAddress macAddress;
157 private IpAddress ipAddress;
158 private DeviceId deviceId;
159 private PortNumber portNumber;
160 private State state;
161
162 // private constructor not intended to use from external
163 private Builder() {
164 }
165
166 @Override
167 public K8sPort build() {
168 checkArgument(networkId != null, NOT_NULL_MSG, "networkId");
169 checkArgument(portId != null, NOT_NULL_MSG, "portId");
170 checkArgument(macAddress != null, NOT_NULL_MSG, "macAddress");
171 checkArgument(ipAddress != null, NOT_NULL_MSG, "ipAddress");
172 checkArgument(deviceId != null, NOT_NULL_MSG, "deviceId");
173 checkArgument(portNumber != null, NOT_NULL_MSG, "portNumber");
174 checkArgument(state != null, NOT_NULL_MSG, "state");
175
176 return new DefaultK8sPort(networkId, portId, macAddress, ipAddress,
177 deviceId, portNumber, state);
178 }
179
180 @Override
181 public Builder networkId(String networkId) {
182 this.networkId = networkId;
183 return this;
184 }
185
186 @Override
187 public Builder portId(String portId) {
188 this.portId = portId;
189 return this;
190 }
191
192 @Override
193 public Builder macAddress(MacAddress macAddress) {
194 this.macAddress = macAddress;
195 return this;
196 }
197
198 @Override
199 public Builder ipAddress(IpAddress ipAddress) {
200 this.ipAddress = ipAddress;
201 return this;
202 }
203
204 @Override
205 public Builder deviceId(DeviceId deviceId) {
206 this.deviceId = deviceId;
207 return this;
208 }
209
210 @Override
211 public Builder portNumber(PortNumber portNumber) {
212 this.portNumber = portNumber;
213 return this;
214 }
215
216 @Override
217 public Builder state(State state) {
218 this.state = state;
219 return this;
220 }
221 }
222}