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; |
| 38 | import java.util.Set; |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 39 | |
| 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 |
| 46 | public class UiTopoLayoutManager implements UiTopoLayoutService { |
| 47 | |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 48 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 49 | |
Thomas Vachuska | 4d66d0a | 2016-04-15 15:48:13 -0700 | [diff] [blame^] | 50 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 51 | protected StorageService storageService; |
| 52 | |
| 53 | private ConsistentMap<UiTopoLayoutId, UiTopoLayout> layouts; |
| 54 | private Map<UiTopoLayoutId, UiTopoLayout> layoutMap; |
| 55 | |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 56 | @Activate |
| 57 | public void activate() { |
Thomas Vachuska | 4d66d0a | 2016-04-15 15:48:13 -0700 | [diff] [blame^] | 58 | 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 Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 69 | log.info("Started"); |
| 70 | } |
| 71 | |
| 72 | @Deactivate |
| 73 | public void deactivate() { |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 74 | log.info("Stopped"); |
| 75 | } |
| 76 | |
| 77 | |
| 78 | @Override |
Thomas Vachuska | 4d66d0a | 2016-04-15 15:48:13 -0700 | [diff] [blame^] | 79 | public Set<UiTopoLayout> getLayouts() { |
| 80 | return ImmutableSet.copyOf(layoutMap.values()); |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public boolean addLayout(UiTopoLayout layout) { |
Thomas Vachuska | 4d66d0a | 2016-04-15 15:48:13 -0700 | [diff] [blame^] | 85 | return layouts.put(layout.id(), layout) == null; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public UiTopoLayout getLayout(UiTopoLayoutId layoutId) { |
| 90 | return layoutMap.get(layoutId); |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public boolean removeLayout(UiTopoLayout layout) { |
Thomas Vachuska | 4d66d0a | 2016-04-15 15:48:13 -0700 | [diff] [blame^] | 95 | return layouts.remove(layout.id()) != null; |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 96 | } |
Thomas Vachuska | 4d66d0a | 2016-04-15 15:48:13 -0700 | [diff] [blame^] | 97 | |
Simon Hunt | f679c4e | 2016-04-01 17:02:24 -0700 | [diff] [blame] | 98 | } |