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