Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame^] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.k8snode.api; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import org.onlab.packet.IpAddress; |
| 21 | import org.onosproject.net.DeviceId; |
| 22 | |
| 23 | import java.util.HashSet; |
| 24 | import java.util.Objects; |
| 25 | import java.util.Set; |
| 26 | |
| 27 | import static com.google.common.base.Preconditions.checkArgument; |
| 28 | |
| 29 | /** |
| 30 | * Representation of a kubernetes host. |
| 31 | */ |
| 32 | public class DefaultK8sHost implements K8sHost { |
| 33 | |
| 34 | private final IpAddress hostIp; |
| 35 | private final Set<String> nodeNames; |
| 36 | private final K8sHostState state; |
| 37 | |
| 38 | private static final String NOT_NULL_MSG = "Host % cannot be null"; |
| 39 | |
| 40 | private static final String OVSDB = "ovsdb:"; |
| 41 | |
| 42 | /** |
| 43 | * A default constructor of kubernetes host. |
| 44 | * |
| 45 | * @param hostIp host IP address |
| 46 | * @param nodeNames node names |
| 47 | * @param state host state |
| 48 | */ |
| 49 | protected DefaultK8sHost(IpAddress hostIp, Set<String> nodeNames, |
| 50 | K8sHostState state) { |
| 51 | this.hostIp = hostIp; |
| 52 | this.nodeNames = nodeNames; |
| 53 | this.state = state; |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public IpAddress hostIp() { |
| 58 | return hostIp; |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public Set<String> nodeNames() { |
| 63 | return ImmutableSet.copyOf(nodeNames); |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public K8sHostState state() { |
| 68 | return state; |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public DeviceId ovsdb() { |
| 73 | return DeviceId.deviceId(OVSDB + hostIp.toString()); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public K8sHost updateState(K8sHostState newState) { |
| 78 | return new Builder() |
| 79 | .hostIp(hostIp) |
| 80 | .nodeNames(nodeNames) |
| 81 | .state(newState) |
| 82 | .build(); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public K8sHost updateNodeNames(Set<String> nodeNames) { |
| 87 | return new Builder() |
| 88 | .hostIp(hostIp) |
| 89 | .nodeNames(nodeNames) |
| 90 | .state(state) |
| 91 | .build(); |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public boolean equals(Object o) { |
| 96 | if (this == o) { |
| 97 | return true; |
| 98 | } |
| 99 | if (o == null || getClass() != o.getClass()) { |
| 100 | return false; |
| 101 | } |
| 102 | DefaultK8sHost that = (DefaultK8sHost) o; |
| 103 | return Objects.equals(hostIp, that.hostIp) && |
| 104 | Objects.equals(nodeNames, that.nodeNames) && |
| 105 | state == that.state; |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public int hashCode() { |
| 110 | return Objects.hash(hostIp, nodeNames, state); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public String toString() { |
| 115 | return MoreObjects.toStringHelper(this) |
| 116 | .add("hostIp", hostIp) |
| 117 | .add("nodeNames", nodeNames) |
| 118 | .add("state", state) |
| 119 | .toString(); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Returns new builder instance. |
| 124 | * |
| 125 | * @return kubernetes host builder |
| 126 | */ |
| 127 | public static Builder builder() { |
| 128 | return new Builder(); |
| 129 | } |
| 130 | |
| 131 | public static final class Builder implements K8sHost.Builder { |
| 132 | |
| 133 | private IpAddress hostIp; |
| 134 | private Set<String> nodeNames; |
| 135 | private K8sHostState state; |
| 136 | |
| 137 | // private constructor not intended to use from external |
| 138 | private Builder() { |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public K8sHost build() { |
| 143 | checkArgument(hostIp != null, NOT_NULL_MSG, "hostIp"); |
| 144 | checkArgument(state != null, NOT_NULL_MSG, "state"); |
| 145 | |
| 146 | if (nodeNames == null) { |
| 147 | nodeNames = new HashSet<>(); |
| 148 | } |
| 149 | |
| 150 | return new DefaultK8sHost(hostIp, nodeNames, state); |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public Builder hostIp(IpAddress hostIp) { |
| 155 | this.hostIp = hostIp; |
| 156 | return this; |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public Builder nodeNames(Set<String> nodeNames) { |
| 161 | this.nodeNames = nodeNames; |
| 162 | return this; |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | public Builder state(K8sHostState state) { |
| 167 | this.state = state; |
| 168 | return this; |
| 169 | } |
| 170 | } |
| 171 | } |