Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.net.region; |
| 18 | |
| 19 | import com.google.common.base.MoreObjects; |
| 20 | import com.google.common.collect.ImmutableList; |
| 21 | import org.onosproject.cluster.NodeId; |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 22 | import org.onosproject.net.AbstractAnnotated; |
| 23 | import org.onosproject.net.Annotations; |
Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 24 | |
| 25 | import java.util.List; |
| 26 | import java.util.Objects; |
| 27 | import java.util.Set; |
| 28 | |
Jordan Halterman | 0d89ea3 | 2017-06-13 10:42:36 -0700 | [diff] [blame] | 29 | import static com.google.common.base.Preconditions.checkArgument; |
| 30 | |
Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 31 | /** |
| 32 | * Default implementation of a region. |
| 33 | */ |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 34 | public final class DefaultRegion extends AbstractAnnotated implements Region { |
Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 35 | |
Jordan Halterman | 0d89ea3 | 2017-06-13 10:42:36 -0700 | [diff] [blame] | 36 | private static final int NAME_MAX_LENGTH = 1024; |
| 37 | |
Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 38 | private final RegionId id; |
| 39 | private final String name; |
| 40 | private final Type type; |
| 41 | private final List<Set<NodeId>> masters; |
| 42 | |
| 43 | /** |
| 44 | * Creates a region using the supplied information. |
| 45 | * |
| 46 | * @param id region identifier |
| 47 | * @param name friendly name |
| 48 | * @param type region type |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 49 | * @param annots annotations |
Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 50 | * @param masters list of sets of cluster node identifiers; in order of mastership |
| 51 | */ |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 52 | public DefaultRegion(RegionId id, String name, Type type, |
| 53 | Annotations annots, List<Set<NodeId>> masters) { |
| 54 | super(annots); |
Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 55 | this.id = id; |
| 56 | this.name = name; |
| 57 | this.type = type; |
| 58 | this.masters = masters != null ? ImmutableList.copyOf(masters) : ImmutableList.of(); |
Jordan Halterman | 0d89ea3 | 2017-06-13 10:42:36 -0700 | [diff] [blame] | 59 | if (name != null) { |
| 60 | checkArgument(name.length() <= NAME_MAX_LENGTH, "name exceeds maximum length " + NAME_MAX_LENGTH); |
| 61 | } |
Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public RegionId id() { |
| 66 | return id; |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public String name() { |
| 71 | return name; |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public Type type() { |
| 76 | return type; |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public List<Set<NodeId>> masters() { |
| 81 | return masters; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public int hashCode() { |
| 86 | return Objects.hash(id, name, type, masters); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public boolean equals(Object obj) { |
| 91 | if (this == obj) { |
| 92 | return true; |
| 93 | } |
| 94 | if (obj instanceof DefaultRegion) { |
| 95 | final DefaultRegion that = (DefaultRegion) obj; |
| 96 | return Objects.equals(this.id, that.id) |
| 97 | && Objects.equals(this.name, that.name) |
| 98 | && Objects.equals(this.type, that.type) |
| 99 | && Objects.equals(this.masters, that.masters); |
| 100 | } |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public String toString() { |
| 106 | return MoreObjects.toStringHelper(this) |
| 107 | .add("id", id) |
| 108 | .add("name", name) |
| 109 | .add("type", type) |
| 110 | .add("masters", masters) |
| 111 | .toString(); |
| 112 | } |
| 113 | |
| 114 | } |