blob: 28e345c303a92233fcc8546607069dcbc914aa50 [file] [log] [blame]
Simon Huntf679c4e2016-04-01 17:02:24 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Simon Huntf679c4e2016-04-01 17:02:24 -07003 *
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
17package org.onosproject.ui.impl.topo;
18
Thomas Vachuska4d66d0a2016-04-15 15:48:13 -070019import com.google.common.collect.ImmutableSet;
Simon Huntf679c4e2016-04-01 17:02:24 -070020import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
Thomas Vachuska4d66d0a2016-04-15 15:48:13 -070023import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
Simon Huntf679c4e2016-04-01 17:02:24 -070025import org.apache.felix.scr.annotations.Service;
Thomas Vachuska4d66d0a2016-04-15 15:48:13 -070026import org.onlab.util.KryoNamespace;
27import org.onosproject.store.serializers.KryoNamespaces;
28import org.onosproject.store.service.ConsistentMap;
29import org.onosproject.store.service.Serializer;
30import org.onosproject.store.service.StorageService;
Simon Huntf679c4e2016-04-01 17:02:24 -070031import org.onosproject.ui.UiTopoLayoutService;
32import org.onosproject.ui.model.topo.UiTopoLayout;
Thomas Vachuska4d66d0a2016-04-15 15:48:13 -070033import org.onosproject.ui.model.topo.UiTopoLayoutId;
Simon Huntf679c4e2016-04-01 17:02:24 -070034import org.slf4j.Logger;
35import org.slf4j.LoggerFactory;
36
Simon Huntb1ce2602016-07-23 14:04:31 -070037import java.util.Collections;
Thomas Vachuska4d66d0a2016-04-15 15:48:13 -070038import java.util.Map;
Thomas Vachuska92b016b2016-05-20 11:37:57 -070039import java.util.Objects;
Thomas Vachuska4d66d0a2016-04-15 15:48:13 -070040import java.util.Set;
Thomas Vachuska92b016b2016-05-20 11:37:57 -070041import java.util.stream.Collectors;
42
43import static com.google.common.base.Preconditions.checkNotNull;
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -070044import static org.onosproject.ui.model.topo.UiTopoLayoutId.DEFAULT_ID;
Simon Huntf679c4e2016-04-01 17:02:24 -070045
46/**
47 * Manages the user interface topology layouts.
48 * Note that these layouts are persisted and distributed across the cluster.
49 */
50@Component(immediate = true)
51@Service
52public class UiTopoLayoutManager implements UiTopoLayoutService {
53
Simon Huntf679c4e2016-04-01 17:02:24 -070054 private final Logger log = LoggerFactory.getLogger(getClass());
55
Thomas Vachuska92b016b2016-05-20 11:37:57 -070056 private static final String ID_NULL = "Layout ID cannot be null";
57 private static final String LAYOUT_NULL = "Layout cannot be null";
58
Thomas Vachuska4d66d0a2016-04-15 15:48:13 -070059 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
60 protected StorageService storageService;
61
62 private ConsistentMap<UiTopoLayoutId, UiTopoLayout> layouts;
63 private Map<UiTopoLayoutId, UiTopoLayout> layoutMap;
64
Simon Huntf679c4e2016-04-01 17:02:24 -070065 @Activate
66 public void activate() {
Thomas Vachuska4d66d0a2016-04-15 15:48:13 -070067 KryoNamespace.Builder kryoBuilder = new KryoNamespace.Builder()
68 .register(KryoNamespaces.API)
Thomas Vachuska92b016b2016-05-20 11:37:57 -070069 .register(UiTopoLayoutId.class)
Thomas Vachuska4d66d0a2016-04-15 15:48:13 -070070 .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 Vachuska92b016b2016-05-20 11:37:57 -070079 // Create and add the default layout, if needed.
80 layoutMap.computeIfAbsent(DEFAULT_ID, k -> new UiTopoLayout(k, null, null));
81
Simon Huntf679c4e2016-04-01 17:02:24 -070082 log.info("Started");
83 }
84
85 @Deactivate
86 public void deactivate() {
Simon Huntf679c4e2016-04-01 17:02:24 -070087 log.info("Stopped");
88 }
89
90
91 @Override
Thomas Vachuska92b016b2016-05-20 11:37:57 -070092 public UiTopoLayout getRootLayout() {
93 return getLayout(DEFAULT_ID);
94 }
95
96 @Override
Thomas Vachuska4d66d0a2016-04-15 15:48:13 -070097 public Set<UiTopoLayout> getLayouts() {
98 return ImmutableSet.copyOf(layoutMap.values());
Simon Huntf679c4e2016-04-01 17:02:24 -070099 }
100
101 @Override
102 public boolean addLayout(UiTopoLayout layout) {
Thomas Vachuska92b016b2016-05-20 11:37:57 -0700103 checkNotNull(layout, LAYOUT_NULL);
Thomas Vachuska4d66d0a2016-04-15 15:48:13 -0700104 return layouts.put(layout.id(), layout) == null;
105 }
106
107 @Override
108 public UiTopoLayout getLayout(UiTopoLayoutId layoutId) {
Thomas Vachuska92b016b2016-05-20 11:37:57 -0700109 checkNotNull(layoutId, ID_NULL);
Thomas Vachuska4d66d0a2016-04-15 15:48:13 -0700110 return layoutMap.get(layoutId);
Simon Huntf679c4e2016-04-01 17:02:24 -0700111 }
112
113 @Override
Simon Huntb1ce2602016-07-23 14:04:31 -0700114 public Set<UiTopoLayout> getPeers(UiTopoLayoutId layoutId) {
115 checkNotNull(layoutId, ID_NULL);
116 UiTopoLayout layout = layoutMap.get(layoutId);
117 if (layout == null) {
118 return Collections.emptySet();
119 }
120
121 UiTopoLayoutId parentId = layout.parent();
122 return layoutMap.values().stream()
123 .filter(l -> !Objects.equals(l.id(), layoutId) &&
124 Objects.equals(l.parent(), parentId))
125 .collect(Collectors.toSet());
126 }
127
128 @Override
Thomas Vachuska92b016b2016-05-20 11:37:57 -0700129 public Set<UiTopoLayout> getChildren(UiTopoLayoutId layoutId) {
130 checkNotNull(layoutId, ID_NULL);
131 return layoutMap.values().stream()
132 .filter(l -> Objects.equals(l.parent(), layoutId))
133 .collect(Collectors.toSet());
134 }
135
136 @Override
Simon Huntf679c4e2016-04-01 17:02:24 -0700137 public boolean removeLayout(UiTopoLayout layout) {
Thomas Vachuska92b016b2016-05-20 11:37:57 -0700138 checkNotNull(layout, LAYOUT_NULL);
Thomas Vachuska4d66d0a2016-04-15 15:48:13 -0700139 return layouts.remove(layout.id()) != null;
Simon Huntf679c4e2016-04-01 17:02:24 -0700140 }
Thomas Vachuska4d66d0a2016-04-15 15:48:13 -0700141
Simon Huntf679c4e2016-04-01 17:02:24 -0700142}