blob: 556d0bc0305eeaa14184c4e0ee690927f6d3a3d2 [file] [log] [blame]
Jian Li47e7af72021-03-05 01:32:04 +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.IpPrefix;
20
21import java.util.Objects;
22
23import static com.google.common.base.Preconditions.checkArgument;
24
25/**
26 * Default implementation class of kubevirt security group rule.
27 */
28public final class DefaultKubevirtSecurityGroupRule implements KubevirtSecurityGroupRule {
29
30 private static final String NOT_NULL_MSG = "Security Group Rule % cannot be null";
31
32 private final String id;
33 private final String securityGroupId;
34 private final String direction;
35 private final String etherType;
36 private final Integer portRangeMax;
37 private final Integer portRangeMin;
38 private final String protocol;
39 private final IpPrefix remoteIpPrefix;
40 private final String remoteGroupId;
41
42 /**
43 * A default constructor.
44 *
45 * @param id security group rule identifier
46 * @param securityGroupId security group identifier
47 * @param direction traffic direction
48 * @param etherType ethernet type
49 * @param portRangeMax maximum port range
50 * @param portRangeMin minimum port range
51 * @param protocol network protocol
52 * @param remoteIpPrefix remote IP prefix
53 * @param remoteGroupId remote group identifier
54 */
55 public DefaultKubevirtSecurityGroupRule(String id, String securityGroupId,
56 String direction, String etherType,
57 Integer portRangeMax, Integer portRangeMin,
58 String protocol, IpPrefix remoteIpPrefix,
59 String remoteGroupId) {
60 this.id = id;
61 this.securityGroupId = securityGroupId;
62 this.direction = direction;
63 this.etherType = etherType;
64 this.portRangeMax = portRangeMax;
65 this.portRangeMin = portRangeMin;
66 this.protocol = protocol;
67 this.remoteIpPrefix = remoteIpPrefix;
68 this.remoteGroupId = remoteGroupId;
69 }
70
71 @Override
72 public String id() {
73 return id;
74 }
75
76 @Override
77 public String securityGroupId() {
78 return securityGroupId;
79 }
80
81 @Override
82 public String direction() {
83 return direction;
84 }
85
86 @Override
87 public String etherType() {
88 return etherType;
89 }
90
91 @Override
92 public Integer portRangeMax() {
93 return portRangeMax;
94 }
95
96 @Override
97 public Integer portRangeMin() {
98 return portRangeMin;
99 }
100
101 @Override
102 public String protocol() {
103 return protocol;
104 }
105
106 @Override
107 public IpPrefix remoteIpPrefix() {
108 return remoteIpPrefix;
109 }
110
111 @Override
112 public String remoteGroupId() {
113 return remoteGroupId;
114 }
115
116 @Override
117 public boolean equals(Object o) {
118 if (this == o) {
119 return true;
120 }
121 if (o == null || getClass() != o.getClass()) {
122 return false;
123 }
124 DefaultKubevirtSecurityGroupRule that = (DefaultKubevirtSecurityGroupRule) o;
125 return id.equals(that.id) && securityGroupId.equals(that.securityGroupId) &&
126 direction.equals(that.direction) &&
127 Objects.equals(etherType, that.etherType) &&
128 Objects.equals(portRangeMax, that.portRangeMax) &&
129 Objects.equals(portRangeMin, that.portRangeMin) &&
130 Objects.equals(protocol, that.protocol) &&
131 Objects.equals(remoteIpPrefix, that.remoteIpPrefix) &&
132 Objects.equals(remoteGroupId, that.remoteGroupId);
133 }
134
135 @Override
136 public int hashCode() {
137 return Objects.hash(id, securityGroupId, direction, etherType, portRangeMax,
138 portRangeMin, protocol, remoteIpPrefix, remoteGroupId);
139 }
140
141 @Override
142 public String toString() {
143 return MoreObjects.toStringHelper(this)
144 .add("id", id)
145 .add("securityGroupId", securityGroupId)
146 .add("direction", direction)
147 .add("etherType", etherType)
148 .add("portRangeMax", portRangeMax)
149 .add("portRangeMin", portRangeMin)
150 .add("protocol", protocol)
151 .add("remoteIpPrefix", remoteIpPrefix)
152 .add("remoteGroupId", remoteGroupId)
153 .toString();
154 }
155
156 /**
157 * Returns new builder instance.
158 *
159 * @return kubevirt security group rule builder
160 */
161 public static Builder builder() {
162 return new Builder();
163 }
164
165 public static final class Builder implements KubevirtSecurityGroupRule.Builder {
166
167 private String id;
168 private String securityGroupId;
169 private String direction;
170 private String etherType;
171 private Integer portRangeMax;
172 private Integer portRangeMin;
173 private String protocol;
174 private IpPrefix remoteIpPrefix;
175 private String remoteGroupId;
176
177 @Override
178 public KubevirtSecurityGroupRule build() {
179 checkArgument(id != null, NOT_NULL_MSG, "id");
180 checkArgument(securityGroupId != null, NOT_NULL_MSG, "securityGroupId");
181 checkArgument(direction != null, NOT_NULL_MSG, "direction");
182
183 return new DefaultKubevirtSecurityGroupRule(id, securityGroupId,
184 direction, etherType, portRangeMax, portRangeMin, protocol,
185 remoteIpPrefix, remoteGroupId);
186 }
187
188 @Override
189 public Builder id(String id) {
190 this.id = id;
191 return this;
192 }
193
194 @Override
195 public Builder securityGroupId(String securityGroupId) {
196 this.securityGroupId = securityGroupId;
197 return this;
198 }
199
200 @Override
201 public Builder direction(String direction) {
202 this.direction = direction;
203 return this;
204 }
205
206 @Override
207 public Builder etherType(String etherType) {
208 this.etherType = etherType;
209 return this;
210 }
211
212 @Override
213 public Builder portRangeMax(Integer portRangeMax) {
214 this.portRangeMax = portRangeMax;
215 return this;
216 }
217
218 @Override
219 public Builder portRangeMin(Integer portRangeMin) {
220 this.portRangeMin = portRangeMin;
221 return this;
222 }
223
224 @Override
225 public Builder protocol(String protocol) {
226 this.protocol = protocol;
227 return this;
228 }
229
230 @Override
231 public Builder remoteIpPrefix(IpPrefix remoteIpPrefix) {
232 this.remoteIpPrefix = remoteIpPrefix;
233 return this;
234 }
235
236 @Override
237 public Builder remoteGroupId(String remoteGroupId) {
238 this.remoteGroupId = remoteGroupId;
239 return this;
240 }
241 }
242}