blob: f7690e81089bb4864a58bde31010b1ffbba29f55 [file] [log] [blame]
Simon Hunte05cae42015-07-23 17:35:24 -07001/*
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.
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 Hunt0af1ec32015-07-24 12:17:55 -070033 private static final UiTopoOverlay NONE = new NullOverlay();
34
Simon Hunte05cae42015-07-23 17:35:24 -070035 private final Map<String, UiTopoOverlay> overlays = new HashMap<>();
Simon Hunt0af1ec32015-07-24 12:17:55 -070036 private UiTopoOverlay current = NONE;
37
38 public TopoOverlayCache() {
39 overlays.put(null, NONE);
40 }
Simon Hunte05cae42015-07-23 17:35:24 -070041
42 /**
43 * Adds a topology overlay to the cache.
44 *
45 * @param overlay a topology overlay
46 */
47 public void add(UiTopoOverlay overlay) {
48 overlays.put(overlay.id(), overlay);
49 }
50
51 /**
52 * Invoked when the cache is no longer needed.
53 */
54 public void destroy() {
55 overlays.clear();
56 }
57
58 /**
59 * Switching currently selected overlay.
60 *
61 * @param deact identity of overlay to deactivate
62 * @param act identity of overlay to activate
63 */
64 public void switchOverlay(String deact, String act) {
65 UiTopoOverlay toDeactivate = getOverlay(deact);
66 UiTopoOverlay toActivate = getOverlay(act);
Simon Hunt0af1ec32015-07-24 12:17:55 -070067
68 toDeactivate.deactivate();
69 current = toActivate;
70 current.activate();
Simon Hunte05cae42015-07-23 17:35:24 -070071 }
72
73 private UiTopoOverlay getOverlay(String id) {
Simon Hunt0af1ec32015-07-24 12:17:55 -070074 return isNullOrEmpty(id) ? NONE : overlays.get(id);
75 }
76
Simon Huntb745ca62015-07-28 15:37:11 -070077 /**
78 * Returns the current overlay instance.
79 * Note that this method always returns a reference; when there is no
80 * overlay selected the "NULL" overlay instance is returned.
81 *
82 * @return the current overlay
83 */
Simon Hunt0af1ec32015-07-24 12:17:55 -070084 public UiTopoOverlay currentOverlay() {
85 return current;
Simon Hunte05cae42015-07-23 17:35:24 -070086 }
87
88 /**
Simon Huntb745ca62015-07-28 15:37:11 -070089 * Returns the number of overlays in the cache. Remember that this
90 * includes the "NULL" overlay, representing "no overlay selected".
Simon Hunte05cae42015-07-23 17:35:24 -070091 *
92 * @return number of overlays
93 */
94 public int size() {
95 return overlays.size();
96 }
Simon Hunt0af1ec32015-07-24 12:17:55 -070097
98
Simon Huntb745ca62015-07-28 15:37:11 -070099 // overlay instance representing "no overlay selected"
Simon Hunt0af1ec32015-07-24 12:17:55 -0700100 private static class NullOverlay extends UiTopoOverlay {
101 public NullOverlay() {
102 super(null);
103 }
104
Simon Huntb745ca62015-07-28 15:37:11 -0700105 // override activate and deactivate, so no log messages are written
Simon Hunt0af1ec32015-07-24 12:17:55 -0700106 @Override
107 public void activate() {
108 }
109
110 @Override
111 public void deactivate() {
112 }
Simon Hunt0af1ec32015-07-24 12:17:55 -0700113 }
Simon Hunte05cae42015-07-23 17:35:24 -0700114}