blob: bc581216dd918e59ca3eea8c9820b1424e356a0a [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;
38import java.util.Set;
Simon Huntf679c4e2016-04-01 17:02:24 -070039
40/**
41 * Manages the user interface topology layouts.
42 * Note that these layouts are persisted and distributed across the cluster.
43 */
44@Component(immediate = true)
45@Service
46public class UiTopoLayoutManager implements UiTopoLayoutService {
47
Simon Huntf679c4e2016-04-01 17:02:24 -070048 private final Logger log = LoggerFactory.getLogger(getClass());
49
Thomas Vachuska4d66d0a2016-04-15 15:48:13 -070050 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
51 protected StorageService storageService;
52
53 private ConsistentMap<UiTopoLayoutId, UiTopoLayout> layouts;
54 private Map<UiTopoLayoutId, UiTopoLayout> layoutMap;
55
Simon Huntf679c4e2016-04-01 17:02:24 -070056 @Activate
57 public void activate() {
Thomas Vachuska4d66d0a2016-04-15 15:48:13 -070058 KryoNamespace.Builder kryoBuilder = new KryoNamespace.Builder()
59 .register(KryoNamespaces.API)
60 .register(UiTopoLayout.class);
61
62 layouts = storageService.<UiTopoLayoutId, UiTopoLayout>consistentMapBuilder()
63 .withSerializer(Serializer.using(kryoBuilder.build()))
64 .withName("onos-topo-layouts")
65 .withRelaxedReadConsistency()
66 .build();
67 layoutMap = layouts.asJavaMap();
68
Simon Huntf679c4e2016-04-01 17:02:24 -070069 log.info("Started");
70 }
71
72 @Deactivate
73 public void deactivate() {
Simon Huntf679c4e2016-04-01 17:02:24 -070074 log.info("Stopped");
75 }
76
77
78 @Override
Thomas Vachuska4d66d0a2016-04-15 15:48:13 -070079 public Set<UiTopoLayout> getLayouts() {
80 return ImmutableSet.copyOf(layoutMap.values());
Simon Huntf679c4e2016-04-01 17:02:24 -070081 }
82
83 @Override
84 public boolean addLayout(UiTopoLayout layout) {
Thomas Vachuska4d66d0a2016-04-15 15:48:13 -070085 return layouts.put(layout.id(), layout) == null;
86 }
87
88 @Override
89 public UiTopoLayout getLayout(UiTopoLayoutId layoutId) {
90 return layoutMap.get(layoutId);
Simon Huntf679c4e2016-04-01 17:02:24 -070091 }
92
93 @Override
94 public boolean removeLayout(UiTopoLayout layout) {
Thomas Vachuska4d66d0a2016-04-15 15:48:13 -070095 return layouts.remove(layout.id()) != null;
Simon Huntf679c4e2016-04-01 17:02:24 -070096 }
Thomas Vachuska4d66d0a2016-04-15 15:48:13 -070097
Simon Huntf679c4e2016-04-01 17:02:24 -070098}