blob: de1e04c576f822d9df94bfd239745a880621056c [file] [log] [blame]
Thomas Vachuska48448082016-02-19 22:14:54 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Thomas Vachuska48448082016-02-19 22:14:54 -08003 *
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
17package org.onosproject.net.region;
18
19import com.google.common.base.MoreObjects;
20import com.google.common.collect.ImmutableList;
21import org.onosproject.cluster.NodeId;
Simon Hunt53612212016-12-04 17:19:52 -080022import org.onosproject.net.AbstractAnnotated;
23import org.onosproject.net.Annotations;
Thomas Vachuska48448082016-02-19 22:14:54 -080024
25import java.util.List;
26import java.util.Objects;
27import java.util.Set;
28
Jordan Halterman0d89ea32017-06-13 10:42:36 -070029import static com.google.common.base.Preconditions.checkArgument;
30
Thomas Vachuska48448082016-02-19 22:14:54 -080031/**
32 * Default implementation of a region.
33 */
Simon Hunt53612212016-12-04 17:19:52 -080034public final class DefaultRegion extends AbstractAnnotated implements Region {
Thomas Vachuska48448082016-02-19 22:14:54 -080035
Jordan Halterman0d89ea32017-06-13 10:42:36 -070036 private static final int NAME_MAX_LENGTH = 1024;
37
Thomas Vachuska48448082016-02-19 22:14:54 -080038 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 Hunt53612212016-12-04 17:19:52 -080049 * @param annots annotations
Thomas Vachuska48448082016-02-19 22:14:54 -080050 * @param masters list of sets of cluster node identifiers; in order of mastership
51 */
Simon Hunt53612212016-12-04 17:19:52 -080052 public DefaultRegion(RegionId id, String name, Type type,
53 Annotations annots, List<Set<NodeId>> masters) {
54 super(annots);
Thomas Vachuska48448082016-02-19 22:14:54 -080055 this.id = id;
56 this.name = name;
57 this.type = type;
58 this.masters = masters != null ? ImmutableList.copyOf(masters) : ImmutableList.of();
Jordan Halterman0d89ea32017-06-13 10:42:36 -070059 if (name != null) {
60 checkArgument(name.length() <= NAME_MAX_LENGTH, "name exceeds maximum length " + NAME_MAX_LENGTH);
61 }
Thomas Vachuska48448082016-02-19 22:14:54 -080062 }
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}