blob: 2f1a2e2a42a7f708dc04abc0231c79020705c4b3 [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;
Simon Hunt1002cd82015-04-23 14:44:03 -070031import org.onosproject.ui.UiViewHidden;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080032import org.slf4j.Logger;
33import org.slf4j.LoggerFactory;
34
35import java.util.List;
36import java.util.Map;
Thomas Vachuska9ed335b2015-04-14 12:07:47 -070037import java.util.Set;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080038
39import static com.google.common.collect.ImmutableList.of;
40import static java.util.stream.Collectors.toSet;
41
42/**
43 * Manages the user interface extensions.
44 */
45@Component(immediate = true)
46@Service
Thomas Vachuska9ed335b2015-04-14 12:07:47 -070047public class UiExtensionManager implements UiExtensionService, SpriteService {
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080048
49 private final Logger log = LoggerFactory.getLogger(getClass());
50
51 // List of all extensions
52 private final List<UiExtension> extensions = Lists.newArrayList();
53
54 // Map of views to extensions
55 private final Map<String, UiExtension> views = Maps.newHashMap();
56
57 // Core views & core extension
Thomas Vachuska3553b302015-03-07 14:49:43 -080058 private final UiExtension core = createCoreExtension();
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080059
Thomas Vachuska3553b302015-03-07 14:49:43 -080060
61 // Creates core UI extension
62 private static UiExtension createCoreExtension() {
Simon Hunt1002cd82015-04-23 14:44:03 -070063 List<UiView> coreViews = of(new UiView("topo", "Topology"),
Thomas Vachuskaef646762015-03-17 15:19:03 -070064 new UiView("device", "Devices"),
Simon Hunt1002cd82015-04-23 14:44:03 -070065 new UiViewHidden("flow"),
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070066 new UiView("link", "Links"),
Thomas Vachuska583bc632015-04-14 10:10:57 -070067 new UiView("host", "Hosts"),
68 new UiView("intent", "Intents"),
69 new UiView("app", "Applications"),
Simon Hunt07ee46e2015-04-17 09:59:20 -070070 new UiView("cluster", "Cluster Nodes"));
Simon Hunt1002cd82015-04-23 14:44:03 -070071
Simon Hunt4c7edd32015-03-11 10:42:53 -070072 UiMessageHandlerFactory messageHandlerFactory =
73 () -> ImmutableList.of(
Thomas Vachuskae586b792015-03-26 13:59:38 -070074 new TopologyViewMessageHandler(),
75 new DeviceViewMessageHandler(),
Thomas Vachuska583bc632015-04-14 10:10:57 -070076 new LinkViewMessageHandler(),
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070077 new HostViewMessageHandler(),
Bri Prebilic Cole384e8dc2015-04-13 15:51:14 -070078 new IntentViewMessageHandler(),
Thomas Vachuska583bc632015-04-14 10:10:57 -070079 new ApplicationViewMessageHandler(),
80 new ClusterViewMessageHandler()
Simon Hunt4c7edd32015-03-11 10:42:53 -070081 );
Simon Hunt1002cd82015-04-23 14:44:03 -070082
Thomas Vachuska3553b302015-03-07 14:49:43 -080083 return new UiExtension(coreViews, messageHandlerFactory, "core",
84 UiExtensionManager.class.getClassLoader());
85 }
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080086
87 @Activate
88 public void activate() {
89 register(core);
90 log.info("Started");
91 }
92
93 @Deactivate
94 public void deactivate() {
95 unregister(core);
96 log.info("Stopped");
97 }
98
99 @Override
100 public synchronized void register(UiExtension extension) {
101 if (!extensions.contains(extension)) {
102 extensions.add(extension);
103 for (UiView view : extension.views()) {
104 views.put(view.id(), extension);
105 }
106 }
107 }
108
109 @Override
110 public synchronized void unregister(UiExtension extension) {
111 extensions.remove(extension);
112 extension.views().stream().map(UiView::id).collect(toSet()).forEach(views::remove);
113 }
114
115 @Override
116 public synchronized List<UiExtension> getExtensions() {
117 return ImmutableList.copyOf(extensions);
118 }
119
120 @Override
121 public synchronized UiExtension getViewExtension(String viewId) {
122 return views.get(viewId);
123 }
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700124
125
126 // Provisional tracking of sprite definitions
127 private Map<String, JsonNode> sprites = Maps.newHashMap();
128
129 @Override
130 public Set<String> getNames() {
131 return ImmutableSet.copyOf(sprites.keySet());
132 }
133
134 @Override
135 public void put(String name, JsonNode spriteData) {
Simon Huntfd8c7d72015-04-14 17:53:37 -0700136 log.info("Registered sprite definition [{}]", name);
Thomas Vachuska9ed335b2015-04-14 12:07:47 -0700137 sprites.put(name, spriteData);
138 }
139
140 @Override
141 public JsonNode get(String name) {
142 return sprites.get(name);
143 }
144
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800145}