Jian Li | 19f2526 | 2018-07-03 22:37:12 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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.openstackvtap.impl; |
| 17 | |
| 18 | import com.google.common.base.Objects; |
| 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import com.google.common.collect.Sets; |
| 21 | import org.onosproject.net.AbstractDescription; |
| 22 | import org.onosproject.net.DefaultAnnotations; |
| 23 | import org.onosproject.net.DeviceId; |
| 24 | import org.onosproject.net.SparseAnnotations; |
| 25 | import org.onosproject.openstackvtap.api.OpenstackVtap; |
| 26 | import org.onosproject.openstackvtap.api.OpenstackVtapCriterion; |
| 27 | import org.onosproject.openstackvtap.api.OpenstackVtapId; |
| 28 | |
| 29 | import java.util.Set; |
| 30 | |
| 31 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 32 | import static com.google.common.base.Preconditions.checkArgument; |
| 33 | |
| 34 | /** |
| 35 | * Default implementation of an immutable openstack vTap. |
| 36 | */ |
| 37 | public final class DefaultOpenstackVtap extends AbstractDescription implements OpenstackVtap { |
| 38 | |
| 39 | private final OpenstackVtapId id; |
| 40 | private final Type type; |
| 41 | private final OpenstackVtapCriterion vTapCriterion; |
| 42 | private final Set<DeviceId> txDeviceIds; |
| 43 | private final Set<DeviceId> rxDeviceIds; |
| 44 | |
| 45 | // private constructor not intended to use from external |
| 46 | private DefaultOpenstackVtap(OpenstackVtapId id, Type type, |
| 47 | OpenstackVtapCriterion vTapCriterion, |
| 48 | Set<DeviceId> txDeviceIds, Set<DeviceId> rxDeviceIds, |
| 49 | SparseAnnotations... annotations) { |
| 50 | super(annotations); |
| 51 | this.id = id; |
| 52 | this.type = type; |
| 53 | this.vTapCriterion = vTapCriterion; |
| 54 | this.txDeviceIds = txDeviceIds; |
| 55 | this.rxDeviceIds = rxDeviceIds; |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public OpenstackVtapId id() { |
| 60 | return id; |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public Type type() { |
| 65 | return type; |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public OpenstackVtapCriterion vTapCriterion() { |
| 70 | return vTapCriterion; |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public Set<DeviceId> txDeviceIds() { |
| 75 | return ImmutableSet.copyOf(txDeviceIds); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public Set<DeviceId> rxDeviceIds() { |
| 80 | return ImmutableSet.copyOf(rxDeviceIds); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public String toString() { |
| 85 | return toStringHelper(this) |
| 86 | .add("id", id) |
| 87 | .add("type", type) |
| 88 | .add("vTapCriterion", vTapCriterion) |
| 89 | .add("txDeviceIds", txDeviceIds) |
| 90 | .add("rxDeviceIds", rxDeviceIds) |
| 91 | .toString(); |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public int hashCode() { |
Jian Li | d18f2b0 | 2018-07-04 02:26:45 +0900 | [diff] [blame] | 96 | return Objects.hashCode(id, type, vTapCriterion, txDeviceIds, rxDeviceIds); |
Jian Li | 19f2526 | 2018-07-03 22:37:12 +0900 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | @Override |
Jian Li | d18f2b0 | 2018-07-04 02:26:45 +0900 | [diff] [blame] | 100 | public boolean equals(Object o) { |
| 101 | if (this == o) { |
| 102 | return true; |
| 103 | } |
| 104 | if (o == null || getClass() != o.getClass()) { |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | DefaultOpenstackVtap that = (DefaultOpenstackVtap) o; |
| 109 | return Objects.equal(this.id, that.id) |
Jian Li | 19f2526 | 2018-07-03 22:37:12 +0900 | [diff] [blame] | 110 | && Objects.equal(this.type, that.type) |
| 111 | && Objects.equal(this.vTapCriterion, that.vTapCriterion) |
| 112 | && Objects.equal(this.txDeviceIds, that.txDeviceIds) |
| 113 | && Objects.equal(this.rxDeviceIds, that.rxDeviceIds); |
Jian Li | 19f2526 | 2018-07-03 22:37:12 +0900 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Creates a new default openstack vTap builder. |
| 118 | * |
| 119 | * @return default openstack vTap builder |
| 120 | */ |
| 121 | public static Builder builder() { |
| 122 | return new Builder(); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Builder for DefaultOpenstackVtap object. |
| 127 | */ |
| 128 | public static class Builder implements OpenstackVtap.Builder { |
| 129 | private static final SparseAnnotations EMPTY = DefaultAnnotations.builder().build(); |
| 130 | |
| 131 | private OpenstackVtapId id; |
| 132 | private Type type; |
| 133 | private OpenstackVtapCriterion vTapCriterion; |
| 134 | private Set<DeviceId> txDeviceIds; |
| 135 | private Set<DeviceId> rxDeviceIds; |
| 136 | private SparseAnnotations annotations = EMPTY; |
| 137 | |
| 138 | // private constructor not intended to use from external |
| 139 | Builder() { |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public Builder id(OpenstackVtapId id) { |
| 144 | this.id = id; |
| 145 | return this; |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public Builder type(Type type) { |
| 150 | this.type = type; |
| 151 | return this; |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public Builder vTapCriterion(OpenstackVtapCriterion vTapCriterion) { |
| 156 | this.vTapCriterion = vTapCriterion; |
| 157 | return this; |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public Builder txDeviceIds(Set<DeviceId> txDeviceIds) { |
| 162 | if (txDeviceIds != null) { |
| 163 | this.txDeviceIds = ImmutableSet.copyOf(txDeviceIds); |
| 164 | } else { |
| 165 | this.txDeviceIds = Sets.newHashSet(); |
| 166 | } |
| 167 | return this; |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public Builder rxDeviceIds(Set<DeviceId> rxDeviceIds) { |
| 172 | if (rxDeviceIds != null) { |
| 173 | this.rxDeviceIds = ImmutableSet.copyOf(rxDeviceIds); |
| 174 | } else { |
| 175 | this.rxDeviceIds = Sets.newHashSet(); |
| 176 | } |
| 177 | return this; |
| 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public Builder annotations(SparseAnnotations... annotations) { |
| 182 | checkArgument(annotations.length <= 1, |
| 183 | "Only one set of annotations is expected"); |
| 184 | this.annotations = annotations.length == 1 ? annotations[0] : EMPTY; |
| 185 | return this; |
| 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public DefaultOpenstackVtap build() { |
| 190 | return new DefaultOpenstackVtap(id, type, vTapCriterion, |
| 191 | txDeviceIds, rxDeviceIds, annotations); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | } |