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