Simon Hunt | 5f6dbf8 | 2016-03-30 08:53:33 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Simon Hunt | 5f6dbf8 | 2016-03-30 08:53:33 -0700 | [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.ui.model.topo; |
| 18 | |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 19 | import org.onosproject.cluster.NodeId; |
| 20 | |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 21 | import java.util.ArrayList; |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 22 | import java.util.HashMap; |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 23 | import java.util.List; |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 24 | import java.util.Map; |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 25 | |
Simon Hunt | 5f6dbf8 | 2016-03-30 08:53:33 -0700 | [diff] [blame] | 26 | /** |
| 27 | * Encapsulates the notion of the ONOS cluster. |
| 28 | */ |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 29 | class UiCluster extends UiElement { |
| 30 | |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame^] | 31 | private static final String DEFAULT_CLUSTER_ID = "CLUSTER-0"; |
| 32 | |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 33 | private final List<UiClusterMember> members = new ArrayList<>(); |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 34 | private final Map<NodeId, UiClusterMember> lookup = new HashMap<>(); |
| 35 | |
| 36 | @Override |
| 37 | public String toString() { |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame^] | 38 | return String.valueOf(size()) + "-member cluster"; |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 39 | } |
Simon Hunt | 23fb135 | 2016-04-11 12:15:19 -0700 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Removes all cluster members. |
| 43 | */ |
| 44 | void clear() { |
| 45 | members.clear(); |
| 46 | } |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 47 | |
| 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 Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame^] | 70 | * 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 Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 80 | * 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 Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame^] | 87 | |
| 88 | @Override |
| 89 | public String idAsString() { |
| 90 | return DEFAULT_CLUSTER_ID; |
| 91 | } |
Simon Hunt | 5f6dbf8 | 2016-03-30 08:53:33 -0700 | [diff] [blame] | 92 | } |