blob: 66138e720920d718f7859da993264845752280cc [file] [log] [blame]
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -08001/*
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 */
16package org.onosproject.ui.impl;
17
Thomas Vachuska9ed335b2015-04-14 12:07:47 -070018import com.fasterxml.jackson.databind.JsonNode;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080019import com.google.common.collect.ImmutableList;
Thomas Vachuska9ed335b2015-04-14 12:07:47 -070020import com.google.common.collect.ImmutableSet;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080021import com.google.common.collect.Lists;
22import com.google.common.collect.Maps;
23import org.apache.felix.scr.annotations.Activate;
24import org.apache.felix.scr.annotations.Component;
25import org.apache.felix.scr.annotations.Deactivate;
26import org.apache.felix.scr.annotations.Service;
27import org.onosproject.ui.UiExtension;
28import org.onosproject.ui.UiExtensionService;
Thomas Vachuska3553b302015-03-07 14:49:43 -080029import org.onosproject.ui.UiMessageHandlerFactory;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080030import org.onosproject.ui.UiView;
31import org.slf4j.Logger;
32import org.slf4j.LoggerFactory;
33
34import java.util.List;
35import java.util.Map;
Thomas Vachuska9ed335b2015-04-14 12:07:47 -070036import java.util.Set;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080037
38import static com.google.common.collect.ImmutableList.of;
39import static java.util.stream.Collectors.toSet;
40
41/**
42 * Manages the user interface extensions.
43 */
44@Component(immediate = true)
45@Service
Thomas Vachuska9ed335b2015-04-14 12:07:47 -070046public class UiExtensionManager implements UiExtensionService, SpriteService {
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080047
48 private final Logger log = LoggerFactory.getLogger(getClass());
49
50 // List of all extensions
51 private final List<UiExtension> extensions = Lists.newArrayList();
52
53 // Map of views to extensions
54 private final Map<String, UiExtension> views = Maps.newHashMap();
55
56 // Core views & core extension
Thomas Vachuska3553b302015-03-07 14:49:43 -080057 private final UiExtension core = createCoreExtension();
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080058
Thomas Vachuska3553b302015-03-07 14:49:43 -080059
60 // Creates core UI extension
61 private static UiExtension createCoreExtension() {
Thomas Vachuskaef646762015-03-17 15:19:03 -070062 List<UiView> coreViews = of(new UiView("topo", "Topology View"),
63 new UiView("device", "Devices"),
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070064 new UiView("link", "Links"),
Thomas Vachuska583bc632015-04-14 10:10:57 -070065 new UiView("host", "Hosts"),
66 new UiView("intent", "Intents"),
67 new UiView("app", "Applications"),
68 new UiView("cluster", "Cluster Nodes"),
Thomas Vachuskaef646762015-03-17 15:19:03 -070069 new UiView("sample", "Sample"));
Simon Hunt4c7edd32015-03-11 10:42:53 -070070 UiMessageHandlerFactory messageHandlerFactory =
71 () -> ImmutableList.of(
Thomas Vachuskae586b792015-03-26 13:59:38 -070072 new TopologyViewMessageHandler(),
73 new DeviceViewMessageHandler(),
Thomas Vachuska583bc632015-04-14 10:10:57 -070074 new LinkViewMessageHandler(),
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070075 new HostViewMessageHandler(),
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070076 new IntentViewMessageHandler(),
Thomas Vachuska583bc632015-04-14 10:10:57 -070077 new ApplicationViewMessageHandler(),
78 new ClusterViewMessageHandler()
Simon Hunt4c7edd32015-03-11 10:42:53 -070079 );
Thomas Vachuska3553b302015-03-07 14:49:43 -080080 return new UiExtension(coreViews, messageHandlerFactory, "core",
81 UiExtensionManager.class.getClassLoader());
82 }
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080083
84 @Activate
85 public void activate() {
86 register(core);
87 log.info("Started");
88 }
89
90 @Deactivate
91 public void deactivate() {
92 unregister(core);
93 log.info("Stopped");
94 }
95
96 @Override
97 public synchronized void register(UiExtension extension) {
98 if (!extensions.contains(extension)) {
99 extensions.add(extension);
100 for (UiView view : extension.views()) {
101 views.put(view.id(), extension);
102 }
103 }
104 }
105
106 @Override
107 public synchronized void unregister(UiExtension extension) {
108 extensions.remove(extension);
109 extension.views().stream().map(UiView::id).collect(toSet()).forEach(views::remove);
110 }
111
112 @Override
113 public synchronized List<UiExtension> getExtensions() {
114 return ImmutableList.copyOf(extensions);
115 }
116
117 @Override
118 public synchronized UiExtension getViewExtension(String viewId) {
119 return views.get(viewId);
120 }
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700121
122
123 // Provisional tracking of sprite definitions
124 private Map<String, JsonNode> sprites = Maps.newHashMap();
125
126 @Override
127 public Set<String> getNames() {
128 return ImmutableSet.copyOf(sprites.keySet());
129 }
130
131 @Override
132 public void put(String name, JsonNode spriteData) {
133 log.info("Registered sprite definition {}", name);
134 sprites.put(name, spriteData);
135 }
136
137 @Override
138 public JsonNode get(String name) {
139 return sprites.get(name);
140 }
141
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800142}