blob: 13b94ce807698faa7c5a1f5145a04b44d6f5fed3 [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 com.google.common.collect.ImmutableSet;
20
21import java.util.Objects;
22import java.util.Set;
23
24import static com.google.common.base.Preconditions.checkArgument;
25
26public final class DefaultKubevirtInstance implements KubevirtInstance {
27
28 private static final String NOT_NULL_MSG = "Instance % cannot be null";
29
30 private final String uid;
31 private final String name;
32 private final Set<KubevirtPort> ports;
33
34 /**
35 * Default constructor.
36 *
37 * @param uid UID
38 * @param name name of instance
39 * @param ports set of ports associated with the instance
40 */
41 public DefaultKubevirtInstance(String uid, String name, Set<KubevirtPort> ports) {
42 this.uid = uid;
43 this.name = name;
44 this.ports = ports;
45 }
46
47 @Override
48 public String uid() {
49 return uid;
50 }
51
52 @Override
53 public String name() {
54 return name;
55 }
56
57 @Override
58 public Set<KubevirtPort> ports() {
59 return ImmutableSet.copyOf(ports);
60 }
61
62 @Override
63 public boolean equals(Object o) {
64 if (this == o) {
65 return true;
66 }
67 if (o == null || getClass() != o.getClass()) {
68 return false;
69 }
70 DefaultKubevirtInstance that = (DefaultKubevirtInstance) o;
71 return uid.equals(that.uid) && name.equals(that.name) && ports.equals(that.ports);
72 }
73
74 @Override
75 public int hashCode() {
76 return Objects.hash(uid, name, ports);
77 }
78
79 @Override
80 public String toString() {
81 return MoreObjects.toStringHelper(this)
82 .add("uid", uid)
83 .add("name", name)
84 .add("ports", ports)
85 .toString();
86 }
87
88 /**
89 * Returns new builder instance.
90 *
91 * @return kubevirt port builder
92 */
93 public static Builder builder() {
94 return new Builder();
95 }
96
97 /**
98 * Default builder implementation.
99 */
100 public static final class Builder implements KubevirtInstance.Builder {
101
102 private String uid;
103 private String name;
104 private Set<KubevirtPort> ports;
105
106 @Override
107 public KubevirtInstance build() {
108 checkArgument(uid != null, NOT_NULL_MSG, "UID");
109 checkArgument(name != null, NOT_NULL_MSG, "name");
110 checkArgument(ports != null, NOT_NULL_MSG, "ports");
111
112 return new DefaultKubevirtInstance(uid, name, ports);
113 }
114
115 @Override
116 public Builder uid(String uid) {
117 this.uid = uid;
118 return this;
119 }
120
121 @Override
122 public Builder name(String name) {
123 this.name = name;
124 return this;
125 }
126
127 @Override
128 public Builder ports(Set<KubevirtPort> ports) {
129 this.ports = ports;
130 return this;
131 }
132 }
133}