Jian Li | 1c10cf2 | 2021-03-05 01:32:04 +0900 | [diff] [blame^] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.kubevirtnetworking.api; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | |
| 20 | import java.util.HashSet; |
| 21 | import java.util.Objects; |
| 22 | import java.util.Set; |
| 23 | |
| 24 | import static com.google.common.base.Preconditions.checkArgument; |
| 25 | |
| 26 | /** |
| 27 | * Default implementation class of kubevirt security group. |
| 28 | */ |
| 29 | public final class DefaultKubevirtSecurityGroup implements KubevirtSecurityGroup { |
| 30 | |
| 31 | private static final String NOT_NULL_MSG = "Security Group % cannot be null"; |
| 32 | |
| 33 | private final String id; |
| 34 | private final String name; |
| 35 | private final String description; |
| 36 | private final Set<KubevirtSecurityGroupRule> rules; |
| 37 | |
| 38 | /** |
| 39 | * A default constructor. |
| 40 | * |
| 41 | * @param id security group identifier |
| 42 | * @param name security group name |
| 43 | * @param description security group description |
| 44 | * @param rules security group rules |
| 45 | */ |
| 46 | public DefaultKubevirtSecurityGroup(String id, String name, String description, |
| 47 | Set<KubevirtSecurityGroupRule> rules) { |
| 48 | this.id = id; |
| 49 | this.name = name; |
| 50 | this.description = description; |
| 51 | this.rules = rules; |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public String id() { |
| 56 | return id; |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public String name() { |
| 61 | return name; |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public String description() { |
| 66 | return description; |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public Set<KubevirtSecurityGroupRule> rules() { |
| 71 | return Objects.requireNonNullElseGet(rules, HashSet::new); |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public KubevirtSecurityGroup updateRules(Set<KubevirtSecurityGroupRule> updatedRules) { |
| 76 | return new Builder() |
| 77 | .id(id) |
| 78 | .name(name) |
| 79 | .description(description) |
| 80 | .rules(updatedRules) |
| 81 | .build(); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public boolean equals(Object o) { |
| 86 | if (this == o) { |
| 87 | return true; |
| 88 | } |
| 89 | if (o == null || getClass() != o.getClass()) { |
| 90 | return false; |
| 91 | } |
| 92 | DefaultKubevirtSecurityGroup that = (DefaultKubevirtSecurityGroup) o; |
| 93 | return id.equals(that.id) && name.equals(that.name) && |
| 94 | Objects.equals(description, that.description) && |
| 95 | Objects.equals(rules, that.rules); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public int hashCode() { |
| 100 | return Objects.hash(id, name, description, rules); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public String toString() { |
| 105 | return MoreObjects.toStringHelper(this) |
| 106 | .add("id", id) |
| 107 | .add("name", name) |
| 108 | .add("description", description) |
| 109 | .add("rules", rules) |
| 110 | .toString(); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Returns new builder instance. |
| 115 | * |
| 116 | * @return kubevirt security group builder |
| 117 | */ |
| 118 | public static Builder builder() { |
| 119 | return new Builder(); |
| 120 | } |
| 121 | |
| 122 | public static final class Builder implements KubevirtSecurityGroup.Builder { |
| 123 | |
| 124 | private String id; |
| 125 | private String name; |
| 126 | private String description; |
| 127 | private Set<KubevirtSecurityGroupRule> rules; |
| 128 | |
| 129 | @Override |
| 130 | public KubevirtSecurityGroup build() { |
| 131 | checkArgument(id != null, NOT_NULL_MSG, "id"); |
| 132 | checkArgument(name != null, NOT_NULL_MSG, "name"); |
| 133 | |
| 134 | return new DefaultKubevirtSecurityGroup(id, name, description, rules); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public Builder id(String id) { |
| 139 | this.id = id; |
| 140 | return this; |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public Builder name(String name) { |
| 145 | this.name = name; |
| 146 | return this; |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public Builder description(String description) { |
| 151 | this.description = description; |
| 152 | return this; |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public Builder rules(Set<KubevirtSecurityGroupRule> rules) { |
| 157 | this.rules = rules; |
| 158 | return this; |
| 159 | } |
| 160 | } |
| 161 | } |