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