Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | |
| 18 | package org.onosproject.ui.topo; |
| 19 | |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame^] | 20 | import java.util.Collection; |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 21 | import java.util.Collections; |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame^] | 22 | import java.util.HashMap; |
| 23 | import java.util.Map; |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 24 | |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 25 | import static com.google.common.base.Preconditions.checkNotNull; |
| 26 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 27 | /** |
| 28 | * Encapsulates highlights to be applied to the topology view, such as |
| 29 | * highlighting links, displaying link labels, perhaps even decorating |
| 30 | * nodes with badges, etc. |
| 31 | */ |
| 32 | public class Highlights { |
| 33 | |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 34 | private static final String EMPTY = ""; |
| 35 | private static final String MIN = "min"; |
| 36 | private static final String MAX = "max"; |
| 37 | |
| 38 | /** |
| 39 | * A notion of amount. |
| 40 | */ |
| 41 | public enum Amount { |
| 42 | ZERO(EMPTY), |
| 43 | MINIMALLY(MIN), |
| 44 | MAXIMALLY(MAX); |
| 45 | |
| 46 | private final String s; |
| 47 | Amount(String str) { |
| 48 | s = str; |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public String toString() { |
| 53 | return s; |
| 54 | } |
| 55 | } |
| 56 | |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame^] | 57 | private final Map<String, DeviceHighlight> devices = new HashMap<>(); |
| 58 | private final Map<String, HostHighlight> hosts = new HashMap<>(); |
| 59 | private final Map<String, LinkHighlight> links = new HashMap<>(); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 60 | |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 61 | private Amount subdueLevel = Amount.ZERO; |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 62 | |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 63 | |
| 64 | /** |
| 65 | * Adds highlighting information for a device. |
| 66 | * |
| 67 | * @param dh device highlight |
| 68 | * @return self, for chaining |
| 69 | */ |
| 70 | public Highlights add(DeviceHighlight dh) { |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame^] | 71 | devices.put(dh.elementId(), dh); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 72 | return this; |
| 73 | } |
| 74 | |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 75 | /** |
| 76 | * Adds highlighting information for a host. |
| 77 | * |
| 78 | * @param hh host highlight |
| 79 | * @return self, for chaining |
| 80 | */ |
| 81 | public Highlights add(HostHighlight hh) { |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame^] | 82 | hosts.put(hh.elementId(), hh); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 83 | return this; |
| 84 | } |
| 85 | |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 86 | /** |
| 87 | * Adds highlighting information for a link. |
| 88 | * |
| 89 | * @param lh link highlight |
| 90 | * @return self, for chaining |
| 91 | */ |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 92 | public Highlights add(LinkHighlight lh) { |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame^] | 93 | links.put(lh.elementId(), lh); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 94 | return this; |
| 95 | } |
| 96 | |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 97 | /** |
| 98 | * Marks the amount by which all other elements (devices, hosts, links) |
| 99 | * not explicitly referenced here will be "subdued" visually. |
| 100 | * |
| 101 | * @param amount amount to subdue other elements |
| 102 | * @return self, for chaining |
| 103 | */ |
| 104 | public Highlights subdueAllElse(Amount amount) { |
| 105 | subdueLevel = checkNotNull(amount); |
| 106 | return this; |
| 107 | } |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 108 | |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 109 | /** |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame^] | 110 | * Returns the collection of device highlights. |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 111 | * |
| 112 | * @return device highlights |
| 113 | */ |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame^] | 114 | public Collection<DeviceHighlight> devices() { |
| 115 | return Collections.unmodifiableCollection(devices.values()); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 118 | /** |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame^] | 119 | * Returns the collection of host highlights. |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 120 | * |
| 121 | * @return host highlights |
| 122 | */ |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame^] | 123 | public Collection<HostHighlight> hosts() { |
| 124 | return Collections.unmodifiableCollection(hosts.values()); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 127 | /** |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame^] | 128 | * Returns the collection of link highlights. |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 129 | * |
| 130 | * @return link highlights |
| 131 | */ |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame^] | 132 | public Collection<LinkHighlight> links() { |
| 133 | return Collections.unmodifiableCollection(links.values()); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 134 | } |
Simon Hunt | 743a849 | 2015-08-25 16:18:19 -0700 | [diff] [blame] | 135 | |
| 136 | /** |
| 137 | * Returns the amount by which all other elements not explicitly |
| 138 | * referenced here should be "subdued". |
| 139 | * |
| 140 | * @return amount to subdue other elements |
| 141 | */ |
| 142 | public Amount subdueLevel() { |
| 143 | return subdueLevel; |
| 144 | } |
Simon Hunt | 94f7dae | 2015-08-26 17:40:59 -0700 | [diff] [blame^] | 145 | |
| 146 | /** |
| 147 | * Returns the node highlight (device or host) for the given element |
| 148 | * identifier, or null if no match. |
| 149 | * |
| 150 | * @param id element identifier |
| 151 | * @return corresponding node highlight |
| 152 | */ |
| 153 | public NodeHighlight getNode(String id) { |
| 154 | NodeHighlight nh = devices.get(id); |
| 155 | return nh != null ? nh : hosts.get(id); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Returns the device highlight for the given device identifier, |
| 160 | * or null if no match. |
| 161 | * |
| 162 | * @param id device identifier |
| 163 | * @return corresponding device highlight |
| 164 | */ |
| 165 | public DeviceHighlight getDevice(String id) { |
| 166 | return devices.get(id); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Returns the host highlight for the given host identifier, |
| 171 | * or null if no match. |
| 172 | * |
| 173 | * @param id host identifier |
| 174 | * @return corresponding host highlight |
| 175 | */ |
| 176 | public HostHighlight getHost(String id) { |
| 177 | return hosts.get(id); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Returns the link highlight for the given link identifier, |
| 182 | * or null if no match. |
| 183 | * |
| 184 | * @param id link identifier |
| 185 | * @return corresponding link highlight |
| 186 | */ |
| 187 | public LinkHighlight getLink(String id) { |
| 188 | return links.get(id); |
| 189 | } |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 190 | } |