Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 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 | package org.onosproject.ui.impl.topo; |
| 18 | |
Thomas Vachuska | 4d66d0a | 2016-04-15 15:48:13 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
Simon Hunt | d0fa284 | 2016-10-24 18:04:05 -0700 | [diff] [blame] | 20 | import org.onosproject.net.config.NetworkConfigEvent; |
| 21 | import org.onosproject.net.config.NetworkConfigListener; |
| 22 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 23 | import org.onosproject.net.config.basics.BasicUiTopoLayoutConfig; |
Simon Hunt | 4f4ffc3 | 2016-08-03 18:30:47 -0700 | [diff] [blame] | 24 | import org.onosproject.net.region.RegionId; |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 25 | import org.onosproject.ui.UiTopoLayoutService; |
Simon Hunt | 4f4ffc3 | 2016-08-03 18:30:47 -0700 | [diff] [blame] | 26 | import org.onosproject.ui.model.topo.UiRegion; |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 27 | import org.onosproject.ui.model.topo.UiTopoLayout; |
Thomas Vachuska | 4d66d0a | 2016-04-15 15:48:13 -0700 | [diff] [blame] | 28 | import org.onosproject.ui.model.topo.UiTopoLayoutId; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 29 | import org.osgi.service.component.annotations.Activate; |
| 30 | import org.osgi.service.component.annotations.Component; |
| 31 | import org.osgi.service.component.annotations.Deactivate; |
| 32 | import org.osgi.service.component.annotations.Reference; |
| 33 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 34 | import org.slf4j.Logger; |
| 35 | import org.slf4j.LoggerFactory; |
| 36 | |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 37 | import java.util.Collections; |
Simon Hunt | d0fa284 | 2016-10-24 18:04:05 -0700 | [diff] [blame] | 38 | import java.util.HashMap; |
Simon Hunt | 4f4ffc3 | 2016-08-03 18:30:47 -0700 | [diff] [blame] | 39 | import java.util.List; |
Thomas Vachuska | 4d66d0a | 2016-04-15 15:48:13 -0700 | [diff] [blame] | 40 | import java.util.Map; |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 41 | import java.util.Objects; |
Thomas Vachuska | 4d66d0a | 2016-04-15 15:48:13 -0700 | [diff] [blame] | 42 | import java.util.Set; |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 43 | import java.util.stream.Collectors; |
| 44 | |
| 45 | import static com.google.common.base.Preconditions.checkNotNull; |
Thomas Vachuska | eb851cd | 2016-07-21 15:41:05 -0700 | [diff] [blame] | 46 | import static org.onosproject.ui.model.topo.UiTopoLayoutId.DEFAULT_ID; |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * Manages the user interface topology layouts. |
| 50 | * Note that these layouts are persisted and distributed across the cluster. |
| 51 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 52 | @Component(immediate = true, service = UiTopoLayoutService.class) |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 53 | public class UiTopoLayoutManager implements UiTopoLayoutService { |
| 54 | |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 55 | private static final String ID_NULL = "Layout ID cannot be null"; |
| 56 | private static final String LAYOUT_NULL = "Layout cannot be null"; |
| 57 | |
Simon Hunt | d0fa284 | 2016-10-24 18:04:05 -0700 | [diff] [blame] | 58 | private final Logger log = LoggerFactory.getLogger(getClass()); |
Thomas Vachuska | 4d66d0a | 2016-04-15 15:48:13 -0700 | [diff] [blame] | 59 | |
Simon Hunt | d0fa284 | 2016-10-24 18:04:05 -0700 | [diff] [blame] | 60 | private final InternalConfigListener cfgListener = new InternalConfigListener(); |
| 61 | private final Map<UiTopoLayoutId, UiTopoLayout> layoutMap = new HashMap<>(); |
| 62 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 63 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Simon Hunt | d0fa284 | 2016-10-24 18:04:05 -0700 | [diff] [blame] | 64 | protected NetworkConfigRegistry cfgService; |
| 65 | |
Thomas Vachuska | 4d66d0a | 2016-04-15 15:48:13 -0700 | [diff] [blame] | 66 | |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 67 | @Activate |
| 68 | public void activate() { |
Thomas Vachuska | 4d66d0a | 2016-04-15 15:48:13 -0700 | [diff] [blame] | 69 | |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 70 | // Create and add the default layout, if needed. |
Simon Hunt | d0fa284 | 2016-10-24 18:04:05 -0700 | [diff] [blame] | 71 | layoutMap.computeIfAbsent(DEFAULT_ID, UiTopoLayout::new); |
| 72 | |
| 73 | cfgService.addListener(cfgListener); |
| 74 | cfgListener.initAllConfigs(); |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 75 | |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 76 | log.info("Started"); |
| 77 | } |
| 78 | |
| 79 | @Deactivate |
| 80 | public void deactivate() { |
Simon Hunt | d0fa284 | 2016-10-24 18:04:05 -0700 | [diff] [blame] | 81 | cfgService.removeListener(cfgListener); |
| 82 | |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 83 | log.info("Stopped"); |
| 84 | } |
| 85 | |
| 86 | |
| 87 | @Override |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 88 | public UiTopoLayout getRootLayout() { |
| 89 | return getLayout(DEFAULT_ID); |
| 90 | } |
| 91 | |
| 92 | @Override |
Thomas Vachuska | 4d66d0a | 2016-04-15 15:48:13 -0700 | [diff] [blame] | 93 | public Set<UiTopoLayout> getLayouts() { |
| 94 | return ImmutableSet.copyOf(layoutMap.values()); |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public boolean addLayout(UiTopoLayout layout) { |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 99 | checkNotNull(layout, LAYOUT_NULL); |
Simon Hunt | d0fa284 | 2016-10-24 18:04:05 -0700 | [diff] [blame] | 100 | return layoutMap.put(layout.id(), layout) == null; |
Thomas Vachuska | 4d66d0a | 2016-04-15 15:48:13 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public UiTopoLayout getLayout(UiTopoLayoutId layoutId) { |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 105 | checkNotNull(layoutId, ID_NULL); |
Thomas Vachuska | 4d66d0a | 2016-04-15 15:48:13 -0700 | [diff] [blame] | 106 | return layoutMap.get(layoutId); |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | @Override |
Simon Hunt | 4f4ffc3 | 2016-08-03 18:30:47 -0700 | [diff] [blame] | 110 | public UiTopoLayout getLayout(RegionId regionId) { |
| 111 | if (regionId == null || regionId.equals(UiRegion.NULL_ID)) { |
| 112 | return getRootLayout(); |
| 113 | } |
| 114 | |
| 115 | List<UiTopoLayout> matchingLayouts = layoutMap.values().stream() |
| 116 | .filter(l -> Objects.equals(regionId, l.regionId())) |
| 117 | .collect(Collectors.toList()); |
| 118 | return matchingLayouts.isEmpty() ? null : matchingLayouts.get(0); |
| 119 | } |
| 120 | |
| 121 | @Override |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 122 | public Set<UiTopoLayout> getPeerLayouts(UiTopoLayoutId layoutId) { |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 123 | checkNotNull(layoutId, ID_NULL); |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 124 | |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 125 | UiTopoLayout layout = layoutMap.get(layoutId); |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 126 | if (layout == null || layout.isRoot()) { |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 127 | return Collections.emptySet(); |
| 128 | } |
| 129 | |
| 130 | UiTopoLayoutId parentId = layout.parent(); |
| 131 | return layoutMap.values().stream() |
Simon Hunt | e43cccebf | 2016-09-01 17:01:58 -0700 | [diff] [blame] | 132 | // all layouts who are NOT me (or root) and who share my parent... |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 133 | .filter(l -> !Objects.equals(l.id(), layoutId) && |
Simon Hunt | e43cccebf | 2016-09-01 17:01:58 -0700 | [diff] [blame] | 134 | !Objects.equals(l.id(), UiTopoLayoutId.DEFAULT_ID) && |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 135 | Objects.equals(l.parent(), parentId)) |
| 136 | .collect(Collectors.toSet()); |
| 137 | } |
| 138 | |
| 139 | @Override |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 140 | public Set<UiTopoLayout> getChildren(UiTopoLayoutId layoutId) { |
| 141 | checkNotNull(layoutId, ID_NULL); |
| 142 | return layoutMap.values().stream() |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 143 | .filter(l -> !l.isRoot() && Objects.equals(l.parent(), layoutId)) |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 144 | .collect(Collectors.toSet()); |
| 145 | } |
| 146 | |
| 147 | @Override |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 148 | public boolean removeLayout(UiTopoLayout layout) { |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 149 | checkNotNull(layout, LAYOUT_NULL); |
Simon Hunt | d0fa284 | 2016-10-24 18:04:05 -0700 | [diff] [blame] | 150 | return layoutMap.remove(layout.id()) != null; |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 151 | } |
Thomas Vachuska | 4d66d0a | 2016-04-15 15:48:13 -0700 | [diff] [blame] | 152 | |
Simon Hunt | d0fa284 | 2016-10-24 18:04:05 -0700 | [diff] [blame] | 153 | /* |
| 154 | * Listens for changes to layout configs, updating instances as necessary |
| 155 | */ |
| 156 | private class InternalConfigListener implements NetworkConfigListener { |
| 157 | |
| 158 | // look up the current config by layout ID and apply it |
| 159 | private void updateLayoutConfig(UiTopoLayoutId id) { |
| 160 | BasicUiTopoLayoutConfig cfg = |
| 161 | cfgService.getConfig(id, BasicUiTopoLayoutConfig.class); |
| 162 | |
| 163 | log.info("Updating Layout via config... {}: {}", id, cfg); |
| 164 | |
| 165 | UiTopoLayout layout = layoutMap.get(id); |
| 166 | |
| 167 | // NOTE: if a value is null, then that null-ness should be set |
| 168 | // TODO: add setters on UiTopoLayout and implement... |
| 169 | // layout |
| 170 | // .region(cfg.region()) |
| 171 | // .parent(cfg.parent()) |
| 172 | // .geomap(cfg.geomap()) |
| 173 | // .sprites(cfg.sprites()) |
| 174 | // .scale(cfg.scale()) |
| 175 | // .offsetX(cfg.offsetX()) |
| 176 | // .offsetY(cfg.offsetY()); |
| 177 | } |
| 178 | |
| 179 | private void initAllConfigs() { |
| 180 | log.info("Initializing layout configurations..."); |
| 181 | layoutMap.keySet().forEach(this::updateLayoutConfig); |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public void event(NetworkConfigEvent event) { |
| 186 | UiTopoLayoutId id = (UiTopoLayoutId) event.subject(); |
| 187 | updateLayoutConfig(id); |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public boolean isRelevant(NetworkConfigEvent event) { |
| 192 | return (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED || |
| 193 | event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) && |
| 194 | event.configClass().equals(BasicUiTopoLayoutConfig.class); |
| 195 | } |
| 196 | } |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 197 | } |