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; |
Thomas Vachuska | 4d66d0a | 2016-04-15 15:48:13 -0700 | [diff] [blame] | 26 | import org.onlab.util.KryoNamespace; |
Simon Hunt | 4f4ffc3 | 2016-08-03 18:30:47 -0700 | [diff] [blame] | 27 | import org.onosproject.net.region.RegionId; |
Thomas Vachuska | 4d66d0a | 2016-04-15 15:48:13 -0700 | [diff] [blame] | 28 | import org.onosproject.store.serializers.KryoNamespaces; |
| 29 | import org.onosproject.store.service.ConsistentMap; |
| 30 | import org.onosproject.store.service.Serializer; |
| 31 | import org.onosproject.store.service.StorageService; |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 32 | import org.onosproject.ui.UiTopoLayoutService; |
Simon Hunt | 4f4ffc3 | 2016-08-03 18:30:47 -0700 | [diff] [blame] | 33 | import org.onosproject.ui.model.topo.UiRegion; |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 34 | import org.onosproject.ui.model.topo.UiTopoLayout; |
Thomas Vachuska | 4d66d0a | 2016-04-15 15:48:13 -0700 | [diff] [blame] | 35 | import org.onosproject.ui.model.topo.UiTopoLayoutId; |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 36 | import org.slf4j.Logger; |
| 37 | import org.slf4j.LoggerFactory; |
| 38 | |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 39 | import java.util.Collections; |
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 | |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 57 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 58 | |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 59 | private static final String ID_NULL = "Layout ID cannot be null"; |
| 60 | private static final String LAYOUT_NULL = "Layout cannot be null"; |
| 61 | |
Thomas Vachuska | 4d66d0a | 2016-04-15 15:48:13 -0700 | [diff] [blame] | 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 63 | protected StorageService storageService; |
| 64 | |
| 65 | private ConsistentMap<UiTopoLayoutId, UiTopoLayout> layouts; |
| 66 | private Map<UiTopoLayoutId, UiTopoLayout> layoutMap; |
| 67 | |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 68 | @Activate |
| 69 | public void activate() { |
Thomas Vachuska | 4d66d0a | 2016-04-15 15:48:13 -0700 | [diff] [blame] | 70 | KryoNamespace.Builder kryoBuilder = new KryoNamespace.Builder() |
| 71 | .register(KryoNamespaces.API) |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 72 | .register(UiTopoLayoutId.class) |
Thomas Vachuska | 4d66d0a | 2016-04-15 15:48:13 -0700 | [diff] [blame] | 73 | .register(UiTopoLayout.class); |
| 74 | |
| 75 | layouts = storageService.<UiTopoLayoutId, UiTopoLayout>consistentMapBuilder() |
| 76 | .withSerializer(Serializer.using(kryoBuilder.build())) |
| 77 | .withName("onos-topo-layouts") |
| 78 | .withRelaxedReadConsistency() |
| 79 | .build(); |
| 80 | layoutMap = layouts.asJavaMap(); |
| 81 | |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 82 | // Create and add the default layout, if needed. |
| 83 | layoutMap.computeIfAbsent(DEFAULT_ID, k -> new UiTopoLayout(k, null, null)); |
| 84 | |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 85 | log.info("Started"); |
| 86 | } |
| 87 | |
| 88 | @Deactivate |
| 89 | public void deactivate() { |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 90 | log.info("Stopped"); |
| 91 | } |
| 92 | |
| 93 | |
| 94 | @Override |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 95 | public UiTopoLayout getRootLayout() { |
| 96 | return getLayout(DEFAULT_ID); |
| 97 | } |
| 98 | |
| 99 | @Override |
Thomas Vachuska | 4d66d0a | 2016-04-15 15:48:13 -0700 | [diff] [blame] | 100 | public Set<UiTopoLayout> getLayouts() { |
| 101 | return ImmutableSet.copyOf(layoutMap.values()); |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public boolean addLayout(UiTopoLayout layout) { |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 106 | checkNotNull(layout, LAYOUT_NULL); |
Thomas Vachuska | 4d66d0a | 2016-04-15 15:48:13 -0700 | [diff] [blame] | 107 | return layouts.put(layout.id(), layout) == null; |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public UiTopoLayout getLayout(UiTopoLayoutId layoutId) { |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 112 | checkNotNull(layoutId, ID_NULL); |
Thomas Vachuska | 4d66d0a | 2016-04-15 15:48:13 -0700 | [diff] [blame] | 113 | return layoutMap.get(layoutId); |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | @Override |
Simon Hunt | 4f4ffc3 | 2016-08-03 18:30:47 -0700 | [diff] [blame] | 117 | public UiTopoLayout getLayout(RegionId regionId) { |
| 118 | if (regionId == null || regionId.equals(UiRegion.NULL_ID)) { |
| 119 | return getRootLayout(); |
| 120 | } |
| 121 | |
| 122 | List<UiTopoLayout> matchingLayouts = layoutMap.values().stream() |
| 123 | .filter(l -> Objects.equals(regionId, l.regionId())) |
| 124 | .collect(Collectors.toList()); |
| 125 | return matchingLayouts.isEmpty() ? null : matchingLayouts.get(0); |
| 126 | } |
| 127 | |
| 128 | @Override |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 129 | public Set<UiTopoLayout> getPeerLayouts(UiTopoLayoutId layoutId) { |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 130 | checkNotNull(layoutId, ID_NULL); |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 131 | |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 132 | UiTopoLayout layout = layoutMap.get(layoutId); |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 133 | if (layout == null || layout.isRoot()) { |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 134 | return Collections.emptySet(); |
| 135 | } |
| 136 | |
| 137 | UiTopoLayoutId parentId = layout.parent(); |
| 138 | return layoutMap.values().stream() |
Simon Hunt | e43cccebf | 2016-09-01 17:01:58 -0700 | [diff] [blame] | 139 | // 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] | 140 | .filter(l -> !Objects.equals(l.id(), layoutId) && |
Simon Hunt | e43cccebf | 2016-09-01 17:01:58 -0700 | [diff] [blame] | 141 | !Objects.equals(l.id(), UiTopoLayoutId.DEFAULT_ID) && |
Simon Hunt | b1ce260 | 2016-07-23 14:04:31 -0700 | [diff] [blame] | 142 | Objects.equals(l.parent(), parentId)) |
| 143 | .collect(Collectors.toSet()); |
| 144 | } |
| 145 | |
| 146 | @Override |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 147 | public Set<UiTopoLayout> getChildren(UiTopoLayoutId layoutId) { |
| 148 | checkNotNull(layoutId, ID_NULL); |
| 149 | return layoutMap.values().stream() |
Simon Hunt | 9818919 | 2016-07-29 19:02:27 -0700 | [diff] [blame] | 150 | .filter(l -> !l.isRoot() && Objects.equals(l.parent(), layoutId)) |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 151 | .collect(Collectors.toSet()); |
| 152 | } |
| 153 | |
| 154 | @Override |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 155 | public boolean removeLayout(UiTopoLayout layout) { |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame] | 156 | checkNotNull(layout, LAYOUT_NULL); |
Thomas Vachuska | 4d66d0a | 2016-04-15 15:48:13 -0700 | [diff] [blame] | 157 | return layouts.remove(layout.id()) != null; |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 158 | } |
Thomas Vachuska | 4d66d0a | 2016-04-15 15:48:13 -0700 | [diff] [blame] | 159 | |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 160 | } |