blob: 40de28ce85d052f60a6d998bb0e622367b0a99c6 [file] [log] [blame]
Simon Hunt5f6dbf82016-03-30 08:53:33 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Simon Hunt5f6dbf82016-03-30 08:53:33 -07003 *
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.ui.model.topo;
18
Simon Hunt338a3b42016-04-14 09:43:52 -070019import org.onosproject.cluster.NodeId;
20
Simon Hunt23fb1352016-04-11 12:15:19 -070021import java.util.ArrayList;
Simon Hunt338a3b42016-04-14 09:43:52 -070022import java.util.HashMap;
Simon Hunt23fb1352016-04-11 12:15:19 -070023import java.util.List;
Simon Hunt338a3b42016-04-14 09:43:52 -070024import java.util.Map;
Simon Hunt23fb1352016-04-11 12:15:19 -070025
Simon Hunt5f6dbf82016-03-30 08:53:33 -070026/**
27 * Encapsulates the notion of the ONOS cluster.
28 */
Simon Hunt23fb1352016-04-11 12:15:19 -070029class UiCluster extends UiElement {
30
Simon Hunt642bc452016-05-04 19:34:45 -070031 private static final String DEFAULT_CLUSTER_ID = "CLUSTER-0";
32
Simon Hunt23fb1352016-04-11 12:15:19 -070033 private final List<UiClusterMember> members = new ArrayList<>();
Simon Hunt338a3b42016-04-14 09:43:52 -070034 private final Map<NodeId, UiClusterMember> lookup = new HashMap<>();
35
36 @Override
37 public String toString() {
Simon Hunt642bc452016-05-04 19:34:45 -070038 return String.valueOf(size()) + "-member cluster";
Simon Hunt338a3b42016-04-14 09:43:52 -070039 }
Simon Hunt23fb1352016-04-11 12:15:19 -070040
41 /**
42 * Removes all cluster members.
43 */
44 void clear() {
45 members.clear();
46 }
Simon Hunt338a3b42016-04-14 09:43:52 -070047
48 /**
49 * Returns the cluster member with the given identifier, or null if no
50 * such member exists.
51 *
52 * @param id identifier of member to find
53 * @return corresponding member
54 */
55 public UiClusterMember find(NodeId id) {
56 return lookup.get(id);
57 }
58
59 /**
60 * Adds the given member to the cluster.
61 *
62 * @param member member to add
63 */
64 public void add(UiClusterMember member) {
65 members.add(member);
66 lookup.put(member.id(), member);
67 }
68
69 /**
Simon Hunt642bc452016-05-04 19:34:45 -070070 * Removes the given member from the cluster.
71 *
72 * @param member member to remove
73 */
74 public void remove(UiClusterMember member) {
75 members.remove(member);
76 lookup.remove(member.id());
77 }
78
79 /**
Simon Hunt338a3b42016-04-14 09:43:52 -070080 * Returns the number of members in the cluster.
81 *
82 * @return number of members
83 */
84 public int size() {
85 return members.size();
86 }
Simon Hunt642bc452016-05-04 19:34:45 -070087
88 @Override
89 public String idAsString() {
90 return DEFAULT_CLUSTER_ID;
91 }
Simon Hunt5f6dbf82016-03-30 08:53:33 -070092}