blob: 94be29eefb6297fecb7fdb2cceef411150ed5027 [file] [log] [blame]
Jian Lie2a04ce2020-07-01 19:07:02 +09001/*
2 * Copyright 2020-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.k8snode.api;
17
18import com.google.common.base.MoreObjects;
19import com.google.common.collect.ImmutableSet;
20import org.onlab.packet.IpAddress;
21
22import java.util.Objects;
23import java.util.Set;
24
25import static com.google.common.base.Preconditions.checkArgument;
26
27/**
28 * Default implementation of host to nodes mapping info.
29 */
30public final class DefaultHostNodesInfo implements HostNodesInfo {
31
32 private static final String NOT_NULL_MSG = "HostNodesInfo % cannot be null";
33
34 private final IpAddress hostIp;
35 private final Set<String> nodes;
36
37 private DefaultHostNodesInfo(IpAddress hostIp, Set<String> nodes) {
38 this.hostIp = hostIp;
39 this.nodes = ImmutableSet.copyOf(nodes);
40 }
41
42 @Override
43 public IpAddress hostIp() {
44 return hostIp;
45 }
46
47 @Override
48 public Set<String> nodes() {
49 return ImmutableSet.copyOf(nodes);
50 }
51
52 @Override
53 public boolean equals(Object o) {
54 if (this == o) {
55 return true;
56 }
57 if (o == null || getClass() != o.getClass()) {
58 return false;
59 }
60 DefaultHostNodesInfo that = (DefaultHostNodesInfo) o;
61 return Objects.equals(hostIp, that.hostIp) &&
62 Objects.equals(nodes, that.nodes);
63 }
64
65 @Override
66 public int hashCode() {
67 return Objects.hash(hostIp, nodes);
68 }
69
70 @Override
71 public String toString() {
72 return MoreObjects.toStringHelper(this)
73 .add("hostIp", hostIp)
74 .add("nodes", nodes)
75 .toString();
76 }
77
78 /**
79 * Returns new builder instance.
80 *
81 * @return HostNodesInfo builder
82 */
83 public static Builder builder() {
84 return new Builder();
85 }
86
87 public static final class Builder implements HostNodesInfo.Builder {
88
89 private IpAddress hostIp;
90 private Set<String> nodes;
91
92 @Override
93 public HostNodesInfo build() {
94 checkArgument(hostIp != null, NOT_NULL_MSG, "Host IP address");
95 if (nodes == null) {
96 nodes = ImmutableSet.of();
97 }
98
99 return new DefaultHostNodesInfo(hostIp, nodes);
100 }
101
102 @Override
103 public HostNodesInfo.Builder hostIp(IpAddress hostIp) {
104 this.hostIp = hostIp;
105 return this;
106 }
107
108 @Override
109 public HostNodesInfo.Builder nodes(Set<String> nodes) {
110 this.nodes = nodes;
111 return this;
112 }
113 }
114}