blob: a597f248017f8a4caceb8c06d2bb8283fd7d0caf [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
Thomas Vachuska4d66d0a2016-04-15 15:48:13 -070037import java.util.Map;
Thomas Vachuska92b016b2016-05-20 11:37:57 -070038import java.util.Objects;
Thomas Vachuska4d66d0a2016-04-15 15:48:13 -070039import java.util.Set;
Thomas Vachuska92b016b2016-05-20 11:37:57 -070040import java.util.stream.Collectors;
41
42import static com.google.common.base.Preconditions.checkNotNull;
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -070043import static org.onosproject.ui.model.topo.UiTopoLayoutId.DEFAULT_ID;
Simon Huntf679c4e2016-04-01 17:02:24 -070044
45/**
46 * Manages the user interface topology layouts.
47 * Note that these layouts are persisted and distributed across the cluster.
48 */
49@Component(immediate = true)
50@Service
51public class UiTopoLayoutManager implements UiTopoLayoutService {
52
Simon Huntf679c4e2016-04-01 17:02:24 -070053 private final Logger log = LoggerFactory.getLogger(getClass());
54
Thomas Vachuska92b016b2016-05-20 11:37:57 -070055 private static final String ID_NULL = "Layout ID cannot be null";
56 private static final String LAYOUT_NULL = "Layout cannot be null";
57
Thomas Vachuska4d66d0a2016-04-15 15:48:13 -070058 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
59 protected StorageService storageService;
60
61 private ConsistentMap<UiTopoLayoutId, UiTopoLayout> layouts;
62 private Map<UiTopoLayoutId, UiTopoLayout> layoutMap;
63
Simon Huntf679c4e2016-04-01 17:02:24 -070064 @Activate
65 public void activate() {
Thomas Vachuska4d66d0a2016-04-15 15:48:13 -070066 KryoNamespace.Builder kryoBuilder = new KryoNamespace.Builder()
67 .register(KryoNamespaces.API)
Thomas Vachuska92b016b2016-05-20 11:37:57 -070068 .register(UiTopoLayoutId.class)
Thomas Vachuska4d66d0a2016-04-15 15:48:13 -070069 .register(UiTopoLayout.class);
70
71 layouts = storageService.<UiTopoLayoutId, UiTopoLayout>consistentMapBuilder()
72 .withSerializer(Serializer.using(kryoBuilder.build()))
73 .withName("onos-topo-layouts")
74 .withRelaxedReadConsistency()
75 .build();
76 layoutMap = layouts.asJavaMap();
77
Thomas Vachuska92b016b2016-05-20 11:37:57 -070078 // Create and add the default layout, if needed.
79 layoutMap.computeIfAbsent(DEFAULT_ID, k -> new UiTopoLayout(k, null, null));
80
Simon Huntf679c4e2016-04-01 17:02:24 -070081 log.info("Started");
82 }
83
84 @Deactivate
85 public void deactivate() {
Simon Huntf679c4e2016-04-01 17:02:24 -070086 log.info("Stopped");
87 }
88
89
90 @Override
Thomas Vachuska92b016b2016-05-20 11:37:57 -070091 public UiTopoLayout getRootLayout() {
92 return getLayout(DEFAULT_ID);
93 }
94
95 @Override
Thomas Vachuska4d66d0a2016-04-15 15:48:13 -070096 public Set<UiTopoLayout> getLayouts() {
97 return ImmutableSet.copyOf(layoutMap.values());
Simon Huntf679c4e2016-04-01 17:02:24 -070098 }
99
100 @Override
101 public boolean addLayout(UiTopoLayout layout) {
Thomas Vachuska92b016b2016-05-20 11:37:57 -0700102 checkNotNull(layout, LAYOUT_NULL);
Thomas Vachuska4d66d0a2016-04-15 15:48:13 -0700103 return layouts.put(layout.id(), layout) == null;
104 }
105
106 @Override
107 public UiTopoLayout getLayout(UiTopoLayoutId layoutId) {
Thomas Vachuska92b016b2016-05-20 11:37:57 -0700108 checkNotNull(layoutId, ID_NULL);
Thomas Vachuska4d66d0a2016-04-15 15:48:13 -0700109 return layoutMap.get(layoutId);
Simon Huntf679c4e2016-04-01 17:02:24 -0700110 }
111
112 @Override
Thomas Vachuska92b016b2016-05-20 11:37:57 -0700113 public Set<UiTopoLayout> getChildren(UiTopoLayoutId layoutId) {
114 checkNotNull(layoutId, ID_NULL);
115 return layoutMap.values().stream()
116 .filter(l -> Objects.equals(l.parent(), layoutId))
117 .collect(Collectors.toSet());
118 }
119
120 @Override
Simon Huntf679c4e2016-04-01 17:02:24 -0700121 public boolean removeLayout(UiTopoLayout layout) {
Thomas Vachuska92b016b2016-05-20 11:37:57 -0700122 checkNotNull(layout, LAYOUT_NULL);
Thomas Vachuska4d66d0a2016-04-15 15:48:13 -0700123 return layouts.remove(layout.id()) != null;
Simon Huntf679c4e2016-04-01 17:02:24 -0700124 }
Thomas Vachuska4d66d0a2016-04-15 15:48:13 -0700125
Simon Huntf679c4e2016-04-01 17:02:24 -0700126}