blob: 049a336a0390c84e6c2cc3ef0fab801eb1020cc6 [file] [log] [blame]
Jian Lie1fe06a2021-01-08 03:18:35 +09001/*
2 * Copyright 2021-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.kubevirtnetworking.api;
17
18import com.google.common.base.MoreObjects;
19import org.onlab.packet.IpAddress;
20import org.onlab.packet.MacAddress;
21import org.onosproject.net.DeviceId;
22import org.onosproject.net.PortNumber;
23
24import java.util.Objects;
25
26import static com.google.common.base.Preconditions.checkArgument;
27
28/**
29 * Default implementation of kubernetes port.
30 */
31public final class DefaultKubevirtPort implements KubevirtPort {
32
33 private static final String NOT_NULL_MSG = "Port % cannot be null";
34
35 private final MacAddress macAddress;
36 private final IpAddress ipAddress;
37 private final DeviceId deviceId;
38 private final PortNumber portNumber;
39
40 /**
41 * Default constructor.
42 *
43 * @param macAddress MAC address
44 * @param ipAddress IP address
45 * @param deviceId device identifier
46 * @param portNumber port number
47 */
48 public DefaultKubevirtPort(MacAddress macAddress, IpAddress ipAddress,
49 DeviceId deviceId, PortNumber portNumber) {
50 this.macAddress = macAddress;
51 this.ipAddress = ipAddress;
52 this.deviceId = deviceId;
53 this.portNumber = portNumber;
54 }
55
56 @Override
57 public MacAddress macAddress() {
58 return macAddress;
59 }
60
61 @Override
62 public IpAddress ipAddress() {
63 return ipAddress;
64 }
65
66 @Override
67 public DeviceId deviceId() {
68 return deviceId;
69 }
70
71 @Override
72 public PortNumber portNumber() {
73 return portNumber;
74 }
75
76 @Override
77 public KubevirtPort updatePortNumber(PortNumber portNumber) {
78 return null;
79 }
80
81 @Override
82 public KubevirtPort updateDeviceId(DeviceId deviceId) {
83 return null;
84 }
85
86 @Override
87 public boolean equals(Object o) {
88 if (this == o) {
89 return true;
90 }
91 if (o == null || getClass() != o.getClass()) {
92 return false;
93 }
94 DefaultKubevirtPort that = (DefaultKubevirtPort) o;
95 return macAddress.equals(that.macAddress) && ipAddress.equals(that.ipAddress) &&
96 deviceId.equals(that.deviceId) && portNumber.equals(that.portNumber);
97 }
98
99 @Override
100 public int hashCode() {
101 return Objects.hash(macAddress, ipAddress, deviceId, portNumber);
102 }
103
104 @Override
105 public String toString() {
106 return MoreObjects.toStringHelper(this)
107 .add("macAddress", macAddress)
108 .add("ipAddress", ipAddress)
109 .add("deviceId", deviceId)
110 .add("portNumber", portNumber)
111 .toString();
112 }
113
114 /**
115 * Returns new builder instance.
116 *
117 * @return kubevirt port builder
118 */
119 public static Builder builder() {
120 return new Builder();
121 }
122
123 /**
124 * Default builder implementation.
125 */
126 public static final class Builder implements KubevirtPort.Builder {
127
128 private MacAddress macAddress;
129 private IpAddress ipAddress;
130 private DeviceId deviceId;
131 private PortNumber portNumber;
132
133 // private constructor not intended to use from external
134 private Builder() {
135 }
136
137 @Override
138 public KubevirtPort build() {
139 checkArgument(macAddress != null, NOT_NULL_MSG, "macAddress");
140 checkArgument(ipAddress != null, NOT_NULL_MSG, "ipAddress");
141
142 return new DefaultKubevirtPort(macAddress, ipAddress, deviceId, portNumber);
143 }
144
145 @Override
146 public Builder macAddress(MacAddress macAddress) {
147 this.macAddress = macAddress;
148 return this;
149 }
150
151 @Override
152 public Builder ipAddress(IpAddress ipAddress) {
153 this.ipAddress = ipAddress;
154 return this;
155 }
156
157 @Override
158 public Builder deviceId(DeviceId deviceId) {
159 this.deviceId = deviceId;
160 return this;
161 }
162
163 @Override
164 public Builder portNumber(PortNumber portNumber) {
165 this.portNumber = portNumber;
166 return this;
167 }
168 }
169}