blob: bfebdaea2a57beccc345fd6827e9ca0e48f32179 [file] [log] [blame]
Simon Hunta17fa672015-08-19 18:42:22 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Simon Hunta17fa672015-08-19 18:42:22 -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.
Simon Hunta17fa672015-08-19 18:42:22 -070015 */
16
17package org.onosproject.ui.topo;
18
Andrea Campanella2dc91dc2015-12-07 12:17:02 -080019import com.google.common.base.Preconditions;
20
Simon Hunt94f7dae2015-08-26 17:40:59 -070021import java.util.Collection;
Simon Hunta17fa672015-08-19 18:42:22 -070022import java.util.Collections;
Simon Hunt94f7dae2015-08-26 17:40:59 -070023import java.util.HashMap;
24import java.util.Map;
Simon Hunta17fa672015-08-19 18:42:22 -070025
Simon Hunt743a8492015-08-25 16:18:19 -070026import static com.google.common.base.Preconditions.checkNotNull;
27
Simon Hunta17fa672015-08-19 18:42:22 -070028/**
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 */
33public class Highlights {
34
Simon Hunt743a8492015-08-25 16:18:19 -070035 private static final String EMPTY = "";
36 private static final String MIN = "min";
37 private static final String MAX = "max";
38
Andrea Campanella2dc91dc2015-12-07 12:17:02 -080039
Simon Hunt743a8492015-08-25 16:18:19 -070040 /**
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 Campanella2dc91dc2015-12-07 12:17:02 -080049
Simon Hunt743a8492015-08-25 16:18:19 -070050 Amount(String str) {
51 s = str;
52 }
53
54 @Override
55 public String toString() {
56 return s;
57 }
58 }
59
Simon Hunt94f7dae2015-08-26 17:40:59 -070060 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 Hunta17fa672015-08-19 18:42:22 -070063
Simon Hunt743a8492015-08-25 16:18:19 -070064 private Amount subdueLevel = Amount.ZERO;
Andrea Campanella2dc91dc2015-12-07 12:17:02 -080065 private int delayMs = 0;
Simon Hunta17fa672015-08-19 18:42:22 -070066
Andrea Campanella2dc91dc2015-12-07 12:17:02 -080067 //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 Hunt743a8492015-08-25 16:18:19 -070089
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 Hunt94f7dae2015-08-26 17:40:59 -070097 devices.put(dh.elementId(), dh);
Simon Hunta17fa672015-08-19 18:42:22 -070098 return this;
99 }
100
Simon Hunt743a8492015-08-25 16:18:19 -0700101 /**
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 Hunt94f7dae2015-08-26 17:40:59 -0700108 hosts.put(hh.elementId(), hh);
Simon Hunta17fa672015-08-19 18:42:22 -0700109 return this;
110 }
111
Simon Hunt743a8492015-08-25 16:18:19 -0700112 /**
113 * Adds highlighting information for a link.
114 *
115 * @param lh link highlight
116 * @return self, for chaining
117 */
Simon Hunta17fa672015-08-19 18:42:22 -0700118 public Highlights add(LinkHighlight lh) {
Simon Hunt94f7dae2015-08-26 17:40:59 -0700119 links.put(lh.elementId(), lh);
Simon Hunta17fa672015-08-19 18:42:22 -0700120 return this;
121 }
122
Simon Hunt743a8492015-08-25 16:18:19 -0700123 /**
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 Hunta17fa672015-08-19 18:42:22 -0700134
Simon Hunt743a8492015-08-25 16:18:19 -0700135 /**
Simon Hunt94f7dae2015-08-26 17:40:59 -0700136 * Returns the collection of device highlights.
Simon Hunt743a8492015-08-25 16:18:19 -0700137 *
138 * @return device highlights
139 */
Simon Hunt94f7dae2015-08-26 17:40:59 -0700140 public Collection<DeviceHighlight> devices() {
141 return Collections.unmodifiableCollection(devices.values());
Simon Hunta17fa672015-08-19 18:42:22 -0700142 }
143
Simon Hunt743a8492015-08-25 16:18:19 -0700144 /**
Simon Hunt94f7dae2015-08-26 17:40:59 -0700145 * Returns the collection of host highlights.
Simon Hunt743a8492015-08-25 16:18:19 -0700146 *
147 * @return host highlights
148 */
Simon Hunt94f7dae2015-08-26 17:40:59 -0700149 public Collection<HostHighlight> hosts() {
150 return Collections.unmodifiableCollection(hosts.values());
Simon Hunta17fa672015-08-19 18:42:22 -0700151 }
152
Simon Hunt743a8492015-08-25 16:18:19 -0700153 /**
Simon Hunt94f7dae2015-08-26 17:40:59 -0700154 * Returns the collection of link highlights.
Simon Hunt743a8492015-08-25 16:18:19 -0700155 *
156 * @return link highlights
157 */
Simon Hunt94f7dae2015-08-26 17:40:59 -0700158 public Collection<LinkHighlight> links() {
159 return Collections.unmodifiableCollection(links.values());
Simon Hunta17fa672015-08-19 18:42:22 -0700160 }
Simon Hunt743a8492015-08-25 16:18:19 -0700161
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 Hunt94f7dae2015-08-26 17:40:59 -0700171
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 Campanella2dc91dc2015-12-07 12:17:02 -0800216
Simon Hunta17fa672015-08-19 18:42:22 -0700217}