blob: e606275a3a2e21d60373a5ff0f83077f0d00f140 [file] [log] [blame]
Jian Li70c32de2021-02-26 18:15:20 +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 com.google.common.collect.ImmutableMap;
20import com.google.common.collect.ImmutableSet;
21import org.onlab.packet.IpAddress;
22
23import java.util.HashMap;
24import java.util.HashSet;
25import java.util.Map;
26import java.util.Objects;
27import java.util.Set;
28
29import static com.google.common.base.Preconditions.checkArgument;
30
31/**
32 * Default implementation class of kubevirt router.
33 */
34public final class DefaultKubevirtRouter implements KubevirtRouter {
35
36 private static final String NOT_NULL_MSG = "Router % cannot be null";
37
38 private final String name;
39 private final String description;
40 private final boolean enableSnat;
41 private final Set<String> internal;
42 private final Map<IpAddress, String> external;
43 private final KubevirtPeerRouter peerRouter;
44
45 /**
46 * A default constructor.
47 *
48 * @param name router name
49 * @param description router description
50 * @param enableSnat snat use indicator
51 * @param internal internal networks
52 * @param external external network
53 * @param peerRouter external peer router
54 */
55 public DefaultKubevirtRouter(String name, String description, boolean enableSnat,
56 Set<String> internal,
57 Map<IpAddress, String> external,
58 KubevirtPeerRouter peerRouter) {
59 this.name = name;
60 this.description = description;
61 this.enableSnat = enableSnat;
62 this.internal = internal;
63 this.external = external;
64 this.peerRouter = peerRouter;
65 }
66
67 @Override
68 public String name() {
69 return name;
70 }
71
72 @Override
73 public String description() {
74 return description;
75 }
76
77 @Override
78 public boolean enableSnat() {
79 return enableSnat;
80 }
81
82 @Override
83 public Set<String> internal() {
84 return ImmutableSet.copyOf(internal);
85 }
86
87 @Override
88 public Map<IpAddress, String> external() {
89 return ImmutableMap.copyOf(external);
90 }
91
92 @Override
93 public KubevirtPeerRouter peerRouter() {
94 return peerRouter;
95 }
96
97 @Override
98 public boolean equals(Object o) {
99 if (this == o) {
100 return true;
101 }
102 if (o == null || getClass() != o.getClass()) {
103 return false;
104 }
105 DefaultKubevirtRouter that = (DefaultKubevirtRouter) o;
106 return enableSnat == that.enableSnat && name.equals(that.name) &&
107 description.equals(that.description) && internal.equals(that.internal) &&
108 external.equals(that.external) && peerRouter.equals(that.peerRouter);
109 }
110
111 @Override
112 public int hashCode() {
113 return Objects.hash(name, description, enableSnat, internal, external, peerRouter);
114 }
115
116 @Override
117 public String toString() {
118 return MoreObjects.toStringHelper(this)
119 .add("name", name)
120 .add("description", description)
121 .add("enableSnat", enableSnat)
122 .add("internal", internal)
123 .add("external", external)
124 .add("peerRouter", peerRouter)
125 .toString();
126 }
127
128 /**
129 * Returns new builder instance.
130 *
131 * @return kubevirt router builder
132 */
133 public static Builder builder() {
134 return new Builder();
135 }
136
137 public static final class Builder implements KubevirtRouter.Builder {
138
139 private String name;
140 private String description;
141 private boolean enableSnat;
142 private Set<String> internal;
143 private Map<IpAddress, String> external;
144 private KubevirtPeerRouter peerRouter;
145
146 @Override
147 public KubevirtRouter build() {
148 checkArgument(name != null, NOT_NULL_MSG, "name");
149
150 return new DefaultKubevirtRouter(name, description, enableSnat,
151 internal, external, peerRouter);
152 }
153
154 @Override
155 public Builder name(String name) {
156 this.name = name;
157 return this;
158 }
159
160 @Override
161 public Builder description(String description) {
162 this.description = description;
163 return this;
164 }
165
166 @Override
167 public Builder enableSnat(boolean flag) {
168 this.enableSnat = flag;
169 return this;
170 }
171
172 @Override
173 public Builder internal(Set<String> internal) {
174 this.internal = Objects.requireNonNullElseGet(internal, HashSet::new);
175 return this;
176 }
177
178 @Override
179 public Builder external(Map<IpAddress, String> external) {
180 this.external = Objects.requireNonNullElseGet(external, HashMap::new);
181 return this;
182 }
183
184 @Override
185 public Builder peerRouter(KubevirtPeerRouter router) {
186 this.peerRouter = router;
187 return this;
188 }
189 }
190}