blob: b95aeafad2fc2d13e4a8223f06cc233a171f5b80 [file] [log] [blame]
Simon Hunt22c35df2017-04-26 17:28:42 -07001/*
2 * Copyright 2017-present 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
18package org.onosproject.ui.impl.topo;
19
20import org.onosproject.ui.UiTopo2Overlay;
21import org.slf4j.Logger;
22import org.slf4j.LoggerFactory;
23
24import java.util.Collection;
25import java.util.HashMap;
26import java.util.Map;
27
28import static com.google.common.base.Strings.isNullOrEmpty;
29
30/**
31 * A cache of {@link org.onosproject.ui.UiTopo2Overlay}'s that were
32 * registered at the time the UI connection was established.
33 * <p>
34 * Note, for now, this is a simplified version which will only cache
35 * a single overlay. At some future point, this should be expanded to mirror
36 * the behavior of {@link org.onosproject.ui.impl.TopoOverlayCache}.
37 */
38public class Topo2OverlayCache {
39
40 private final Logger log = LoggerFactory.getLogger(getClass());
41
42 private static final String EMPTY = "";
43 private static final String NO_OVERLAY = "No Overlay";
44 private static final String UNKNOWN = "unknown";
45
46 private static final UiTopo2Overlay NONE = new NullOverlay();
47
48 private final Map<String, UiTopo2Overlay> overlays = new HashMap<>();
49 private UiTopo2Overlay current = null;
50
51 /**
52 * Constructs the overlay cache.
53 */
54 public Topo2OverlayCache() {
55 overlays.put(null, NONE);
56 }
57
58 /**
59 * Adds a topology-2 overlay to the cache.
60 *
61 * @param overlay a topology-2 overlay
62 */
63 public void add(UiTopo2Overlay overlay) {
64 overlays.put(overlay.id(), overlay);
65 log.warn("added overlay: " + overlay);
66 }
67
68 /**
69 * Invoked when the cache is no longer needed.
70 */
71 public void destroy() {
72 overlays.clear();
73 }
74
75 /**
76 * Switching currently selected overlay.
77 *
78 * @param deact identity of overlay to deactivate
79 * @param act identity of overlay to activate
80 */
81 public void switchOverlay(String deact, String act) {
82 UiTopo2Overlay toDeactivate = getOverlay(deact);
83 UiTopo2Overlay toActivate = getOverlay(act);
84
85 toDeactivate.deactivate();
86 current = toActivate;
87 current.activate();
88 }
89
90 private UiTopo2Overlay getOverlay(String id) {
91 return isNullOrEmpty(id) ? NONE : overlays.get(id);
92 }
93
94 /**
95 * Returns the current overlay instance.
96 * Note that this method always returns a reference; when there is no
97 * overlay selected the "NULL" overlay instance is returned.
98 *
99 * @return the current overlay
100 */
101 public UiTopo2Overlay currentOverlay() {
102 return current;
103 }
104
105 /**
106 * Returns the number of overlays in the cache. Remember that this
107 * includes the "NULL" overlay, representing "no overlay selected".
108 *
109 * @return number of overlays
110 */
111 public int size() {
112 return overlays.size();
113 }
114
115 /**
116 * Returns true if the identifier of the currently active overlay
117 * matches the given parameter.
118 *
119 * @param overlayId overlay identifier
120 * @return true if this matches the ID of currently active overlay
121 */
122 public boolean isActive(String overlayId) {
123 return currentOverlay().id().equals(overlayId);
124 }
125
126 /**
127 * Returns the collection of registered overlays.
128 *
129 * @return registered overlays
130 */
131 public Collection<UiTopo2Overlay> list() {
132 return overlays.values();
133 }
134
135 // overlay instance representing "no overlay selected"
136 private static class NullOverlay extends UiTopo2Overlay {
137 NullOverlay() {
Simon Hunt6cc86452017-04-27 17:46:22 -0700138 super(EMPTY);
Simon Hunt22c35df2017-04-26 17:28:42 -0700139 }
140 }
141}