blob: 42451f0d3aaccc4455060c8cab65eb7cafa0f20d [file] [log] [blame]
Simon Hunte05cae42015-07-23 17:35:24 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Simon Hunte05cae42015-07-23 17:35:24 -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.
15 *
16 */
17
18package org.onosproject.ui.impl;
19
20import org.onosproject.ui.UiTopoOverlay;
21
22import java.util.HashMap;
23import java.util.Map;
24
Simon Hunt0af1ec32015-07-24 12:17:55 -070025import static com.google.common.base.Strings.isNullOrEmpty;
26
Simon Hunte05cae42015-07-23 17:35:24 -070027/**
28 * A cache of {@link org.onosproject.ui.UiTopoOverlay}'s that were registered
29 * at the time the UI connection was established.
30 */
31public class TopoOverlayCache {
32
Simon Huntd2862c32015-08-24 17:41:51 -070033 private static final String EMPTY = "";
Simon Hunt0af1ec32015-07-24 12:17:55 -070034 private static final UiTopoOverlay NONE = new NullOverlay();
35
Simon Hunte05cae42015-07-23 17:35:24 -070036 private final Map<String, UiTopoOverlay> overlays = new HashMap<>();
Simon Hunt0af1ec32015-07-24 12:17:55 -070037 private UiTopoOverlay current = NONE;
38
39 public TopoOverlayCache() {
40 overlays.put(null, NONE);
41 }
Simon Hunte05cae42015-07-23 17:35:24 -070042
43 /**
44 * Adds a topology overlay to the cache.
45 *
46 * @param overlay a topology overlay
47 */
48 public void add(UiTopoOverlay overlay) {
49 overlays.put(overlay.id(), overlay);
50 }
51
52 /**
53 * Invoked when the cache is no longer needed.
54 */
55 public void destroy() {
56 overlays.clear();
57 }
58
59 /**
60 * Switching currently selected overlay.
61 *
62 * @param deact identity of overlay to deactivate
63 * @param act identity of overlay to activate
64 */
65 public void switchOverlay(String deact, String act) {
66 UiTopoOverlay toDeactivate = getOverlay(deact);
67 UiTopoOverlay toActivate = getOverlay(act);
Simon Hunt0af1ec32015-07-24 12:17:55 -070068
69 toDeactivate.deactivate();
70 current = toActivate;
71 current.activate();
Simon Hunte05cae42015-07-23 17:35:24 -070072 }
73
74 private UiTopoOverlay getOverlay(String id) {
Simon Hunt0af1ec32015-07-24 12:17:55 -070075 return isNullOrEmpty(id) ? NONE : overlays.get(id);
76 }
77
Simon Huntb745ca62015-07-28 15:37:11 -070078 /**
79 * Returns the current overlay instance.
80 * Note that this method always returns a reference; when there is no
81 * overlay selected the "NULL" overlay instance is returned.
82 *
83 * @return the current overlay
84 */
Simon Hunt0af1ec32015-07-24 12:17:55 -070085 public UiTopoOverlay currentOverlay() {
86 return current;
Simon Hunte05cae42015-07-23 17:35:24 -070087 }
88
89 /**
Simon Huntb745ca62015-07-28 15:37:11 -070090 * Returns the number of overlays in the cache. Remember that this
91 * includes the "NULL" overlay, representing "no overlay selected".
Simon Hunte05cae42015-07-23 17:35:24 -070092 *
93 * @return number of overlays
94 */
95 public int size() {
96 return overlays.size();
97 }
Simon Hunt0af1ec32015-07-24 12:17:55 -070098
Simon Huntd2862c32015-08-24 17:41:51 -070099 /**
100 * Returns true if the identifier of the currently active overlay
101 * matches the given parameter.
102 *
103 * @param overlayId overlay identifier
104 * @return true if this matches the ID of currently active overlay
105 */
106 public boolean isActive(String overlayId) {
107 return currentOverlay().id().equals(overlayId);
108 }
Simon Hunt0af1ec32015-07-24 12:17:55 -0700109
Simon Huntb745ca62015-07-28 15:37:11 -0700110 // overlay instance representing "no overlay selected"
Simon Hunt0af1ec32015-07-24 12:17:55 -0700111 private static class NullOverlay extends UiTopoOverlay {
112 public NullOverlay() {
Simon Huntd2862c32015-08-24 17:41:51 -0700113 super(EMPTY);
Simon Hunt0af1ec32015-07-24 12:17:55 -0700114 }
Simon Hunt0af1ec32015-07-24 12:17:55 -0700115 }
Simon Hunte05cae42015-07-23 17:35:24 -0700116}