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; |
| 27 | import org.onosproject.store.serializers.KryoNamespaces; |
| 28 | import org.onosproject.store.service.ConsistentMap; |
| 29 | import org.onosproject.store.service.Serializer; |
| 30 | import org.onosproject.store.service.StorageService; |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 31 | import org.onosproject.ui.UiTopoLayoutService; |
| 32 | import org.onosproject.ui.model.topo.UiTopoLayout; |
Thomas Vachuska | 4d66d0a | 2016-04-15 15:48:13 -0700 | [diff] [blame] | 33 | import org.onosproject.ui.model.topo.UiTopoLayoutId; |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 34 | import org.slf4j.Logger; |
| 35 | import org.slf4j.LoggerFactory; |
| 36 | |
Thomas Vachuska | 4d66d0a | 2016-04-15 15:48:13 -0700 | [diff] [blame] | 37 | import java.util.Map; |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame^] | 38 | import java.util.Objects; |
Thomas Vachuska | 4d66d0a | 2016-04-15 15:48:13 -0700 | [diff] [blame] | 39 | import java.util.Set; |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame^] | 40 | import java.util.stream.Collectors; |
| 41 | |
| 42 | import static com.google.common.base.Preconditions.checkNotNull; |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Manages the user interface topology layouts. |
| 46 | * Note that these layouts are persisted and distributed across the cluster. |
| 47 | */ |
| 48 | @Component(immediate = true) |
| 49 | @Service |
| 50 | public class UiTopoLayoutManager implements UiTopoLayoutService { |
| 51 | |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 52 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 53 | |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame^] | 54 | private static final String ID_NULL = "Layout ID cannot be null"; |
| 55 | private static final String LAYOUT_NULL = "Layout cannot be null"; |
| 56 | |
| 57 | private static final UiTopoLayoutId DEFAULT_ID = UiTopoLayoutId.layoutId("_default_"); |
| 58 | |
Thomas Vachuska | 4d66d0a | 2016-04-15 15:48:13 -0700 | [diff] [blame] | 59 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 60 | protected StorageService storageService; |
| 61 | |
| 62 | private ConsistentMap<UiTopoLayoutId, UiTopoLayout> layouts; |
| 63 | private Map<UiTopoLayoutId, UiTopoLayout> layoutMap; |
| 64 | |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 65 | @Activate |
| 66 | public void activate() { |
Thomas Vachuska | 4d66d0a | 2016-04-15 15:48:13 -0700 | [diff] [blame] | 67 | KryoNamespace.Builder kryoBuilder = new KryoNamespace.Builder() |
| 68 | .register(KryoNamespaces.API) |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame^] | 69 | .register(UiTopoLayoutId.class) |
Thomas Vachuska | 4d66d0a | 2016-04-15 15:48:13 -0700 | [diff] [blame] | 70 | .register(UiTopoLayout.class); |
| 71 | |
| 72 | layouts = storageService.<UiTopoLayoutId, UiTopoLayout>consistentMapBuilder() |
| 73 | .withSerializer(Serializer.using(kryoBuilder.build())) |
| 74 | .withName("onos-topo-layouts") |
| 75 | .withRelaxedReadConsistency() |
| 76 | .build(); |
| 77 | layoutMap = layouts.asJavaMap(); |
| 78 | |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame^] | 79 | // Create and add the default layout, if needed. |
| 80 | layoutMap.computeIfAbsent(DEFAULT_ID, k -> new UiTopoLayout(k, null, null)); |
| 81 | |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 82 | log.info("Started"); |
| 83 | } |
| 84 | |
| 85 | @Deactivate |
| 86 | public void deactivate() { |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 87 | log.info("Stopped"); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | @Override |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame^] | 92 | public UiTopoLayout getRootLayout() { |
| 93 | return getLayout(DEFAULT_ID); |
| 94 | } |
| 95 | |
| 96 | @Override |
Thomas Vachuska | 4d66d0a | 2016-04-15 15:48:13 -0700 | [diff] [blame] | 97 | public Set<UiTopoLayout> getLayouts() { |
| 98 | return ImmutableSet.copyOf(layoutMap.values()); |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public boolean addLayout(UiTopoLayout layout) { |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame^] | 103 | checkNotNull(layout, LAYOUT_NULL); |
Thomas Vachuska | 4d66d0a | 2016-04-15 15:48:13 -0700 | [diff] [blame] | 104 | return layouts.put(layout.id(), layout) == null; |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public UiTopoLayout getLayout(UiTopoLayoutId layoutId) { |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame^] | 109 | checkNotNull(layoutId, ID_NULL); |
Thomas Vachuska | 4d66d0a | 2016-04-15 15:48:13 -0700 | [diff] [blame] | 110 | return layoutMap.get(layoutId); |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | @Override |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame^] | 114 | public Set<UiTopoLayout> getChildren(UiTopoLayoutId layoutId) { |
| 115 | checkNotNull(layoutId, ID_NULL); |
| 116 | return layoutMap.values().stream() |
| 117 | .filter(l -> Objects.equals(l.parent(), layoutId)) |
| 118 | .collect(Collectors.toSet()); |
| 119 | } |
| 120 | |
| 121 | @Override |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 122 | public boolean removeLayout(UiTopoLayout layout) { |
Thomas Vachuska | 92b016b | 2016-05-20 11:37:57 -0700 | [diff] [blame^] | 123 | checkNotNull(layout, LAYOUT_NULL); |
Thomas Vachuska | 4d66d0a | 2016-04-15 15:48:13 -0700 | [diff] [blame] | 124 | return layouts.remove(layout.id()) != null; |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 125 | } |
Thomas Vachuska | 4d66d0a | 2016-04-15 15:48:13 -0700 | [diff] [blame] | 126 | |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 127 | } |