blob: 84548a273eab78e3c6730c4152203ff87d553271 [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");
Jian Li9ee9c8b2019-01-24 11:48:12 +0900172
173 return new DefaultK8sPort(networkId, portId, macAddress, ipAddress,
174 deviceId, portNumber, state);
175 }
176
177 @Override
178 public Builder networkId(String networkId) {
179 this.networkId = networkId;
180 return this;
181 }
182
183 @Override
184 public Builder portId(String portId) {
185 this.portId = portId;
186 return this;
187 }
188
189 @Override
190 public Builder macAddress(MacAddress macAddress) {
191 this.macAddress = macAddress;
192 return this;
193 }
194
195 @Override
196 public Builder ipAddress(IpAddress ipAddress) {
197 this.ipAddress = ipAddress;
198 return this;
199 }
200
201 @Override
202 public Builder deviceId(DeviceId deviceId) {
203 this.deviceId = deviceId;
204 return this;
205 }
206
207 @Override
208 public Builder portNumber(PortNumber portNumber) {
209 this.portNumber = portNumber;
210 return this;
211 }
212
213 @Override
214 public Builder state(State state) {
215 this.state = state;
216 return this;
217 }
218 }
219}