Committed by
Gerrit Code Review
Implementing the UiLayoutManager.
Change-Id: I0a3424f7e3b13a3c18e668a5eed5151755bce4f9
Showing
6 changed files
with
206 additions
and
21 deletions
... | @@ -16,8 +16,9 @@ | ... | @@ -16,8 +16,9 @@ |
16 | package org.onosproject.ui; | 16 | package org.onosproject.ui; |
17 | 17 | ||
18 | import org.onosproject.ui.model.topo.UiTopoLayout; | 18 | import org.onosproject.ui.model.topo.UiTopoLayout; |
19 | +import org.onosproject.ui.model.topo.UiTopoLayoutId; | ||
19 | 20 | ||
20 | -import java.util.List; | 21 | +import java.util.Set; |
21 | 22 | ||
22 | /** | 23 | /** |
23 | * Service for managing {@link UiTopoLayout} instances. | 24 | * Service for managing {@link UiTopoLayout} instances. |
... | @@ -25,20 +26,28 @@ import java.util.List; | ... | @@ -25,20 +26,28 @@ import java.util.List; |
25 | public interface UiTopoLayoutService { | 26 | public interface UiTopoLayoutService { |
26 | 27 | ||
27 | /** | 28 | /** |
28 | - * Returns the list of available layouts. | 29 | + * Returns the set of available layouts. |
29 | * | 30 | * |
30 | - * @return available layouts | 31 | + * @return set of available layouts |
31 | */ | 32 | */ |
32 | - List<UiTopoLayout> getLayouts(); | 33 | + Set<UiTopoLayout> getLayouts(); |
33 | 34 | ||
34 | /** | 35 | /** |
35 | - * Adds a layout to the system. | 36 | + * Adds a layout to the system or updates an existing one. |
36 | * | 37 | * |
37 | - * @param layout the layout to add | 38 | + * @param layout the layout to add or update |
38 | * @return an indication of success | 39 | * @return an indication of success |
39 | */ | 40 | */ |
40 | boolean addLayout(UiTopoLayout layout); | 41 | boolean addLayout(UiTopoLayout layout); |
41 | 42 | ||
43 | + | ||
44 | + /** | ||
45 | + * Returns the layout with the specified identifier. | ||
46 | + * @param layoutId layout identifier | ||
47 | + * @return layout or null if no such layout is found | ||
48 | + */ | ||
49 | + UiTopoLayout getLayout(UiTopoLayoutId layoutId); | ||
50 | + | ||
42 | /** | 51 | /** |
43 | * Removes a layout from the system. | 52 | * Removes a layout from the system. |
44 | * | 53 | * | ... | ... |
... | @@ -24,6 +24,37 @@ import org.onosproject.net.region.Region; | ... | @@ -24,6 +24,37 @@ import org.onosproject.net.region.Region; |
24 | */ | 24 | */ |
25 | public class UiTopoLayout { | 25 | public class UiTopoLayout { |
26 | 26 | ||
27 | - private Region backingRegion; | 27 | + private final UiTopoLayoutId id; |
28 | + private final Region region; | ||
28 | 29 | ||
30 | + /** | ||
31 | + * Created a new UI topology layout. | ||
32 | + * | ||
33 | + * @param id layout identifier | ||
34 | + * @param region backing region | ||
35 | + */ | ||
36 | + public UiTopoLayout(UiTopoLayoutId id, Region region) { | ||
37 | + this.id = id; | ||
38 | + this.region = region; | ||
39 | + } | ||
40 | + | ||
41 | + /** | ||
42 | + * Returns the UI layout identifier. | ||
43 | + * | ||
44 | + * @return identifier of the layout | ||
45 | + */ | ||
46 | + public UiTopoLayoutId id() { | ||
47 | + return id; | ||
48 | + } | ||
49 | + | ||
50 | + /** | ||
51 | + * Returns the backing region with which this layout is associated. | ||
52 | + * | ||
53 | + * @return backing region | ||
54 | + */ | ||
55 | + public Region region() { | ||
56 | + return region; | ||
57 | + } | ||
58 | + | ||
59 | + // TODO: additional properties pertinent to the layout | ||
29 | } | 60 | } | ... | ... |
1 | +/* | ||
2 | + * Copyright 2016 Open Networking Laboratory | ||
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.model.topo; | ||
18 | + | ||
19 | +import org.onlab.util.Identifier; | ||
20 | + | ||
21 | +/** | ||
22 | + * Identifier of a topology layout. | ||
23 | + */ | ||
24 | +public final class UiTopoLayoutId extends Identifier<String> { | ||
25 | + | ||
26 | + // For serialization | ||
27 | + private UiTopoLayoutId() { | ||
28 | + } | ||
29 | + | ||
30 | + private UiTopoLayoutId(String value) { | ||
31 | + super(value); | ||
32 | + } | ||
33 | + | ||
34 | + /** | ||
35 | + * Returns the layout identifier created from the specified value. | ||
36 | + * | ||
37 | + * @param value string value | ||
38 | + * @return layout identifier | ||
39 | + */ | ||
40 | + public static UiTopoLayoutId layoutId(String value) { | ||
41 | + return new UiTopoLayoutId(value); | ||
42 | + } | ||
43 | +} |
... | @@ -189,7 +189,6 @@ public class UiExtensionManager | ... | @@ -189,7 +189,6 @@ public class UiExtensionManager |
189 | @Deactivate | 189 | @Deactivate |
190 | public void deactivate() { | 190 | public void deactivate() { |
191 | prefs.removeListener(prefsListener); | 191 | prefs.removeListener(prefsListener); |
192 | - prefs.destroy(); | ||
193 | UiWebSocketServlet.closeAll(); | 192 | UiWebSocketServlet.closeAll(); |
194 | unregister(core); | 193 | unregister(core); |
195 | log.info("Stopped"); | 194 | log.info("Stopped"); | ... | ... |
... | @@ -16,16 +16,26 @@ | ... | @@ -16,16 +16,26 @@ |
16 | 16 | ||
17 | package org.onosproject.ui.impl.topo; | 17 | package org.onosproject.ui.impl.topo; |
18 | 18 | ||
19 | +import com.google.common.collect.ImmutableSet; | ||
19 | import org.apache.felix.scr.annotations.Activate; | 20 | import org.apache.felix.scr.annotations.Activate; |
20 | import org.apache.felix.scr.annotations.Component; | 21 | import org.apache.felix.scr.annotations.Component; |
21 | import org.apache.felix.scr.annotations.Deactivate; | 22 | import org.apache.felix.scr.annotations.Deactivate; |
23 | +import org.apache.felix.scr.annotations.Reference; | ||
24 | +import org.apache.felix.scr.annotations.ReferenceCardinality; | ||
22 | import org.apache.felix.scr.annotations.Service; | 25 | import org.apache.felix.scr.annotations.Service; |
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; | ||
23 | import org.onosproject.ui.UiTopoLayoutService; | 31 | import org.onosproject.ui.UiTopoLayoutService; |
24 | import org.onosproject.ui.model.topo.UiTopoLayout; | 32 | import org.onosproject.ui.model.topo.UiTopoLayout; |
33 | +import org.onosproject.ui.model.topo.UiTopoLayoutId; | ||
25 | import org.slf4j.Logger; | 34 | import org.slf4j.Logger; |
26 | import org.slf4j.LoggerFactory; | 35 | import org.slf4j.LoggerFactory; |
27 | 36 | ||
28 | -import java.util.List; | 37 | +import java.util.Map; |
38 | +import java.util.Set; | ||
29 | 39 | ||
30 | /** | 40 | /** |
31 | * Manages the user interface topology layouts. | 41 | * Manages the user interface topology layouts. |
... | @@ -35,39 +45,54 @@ import java.util.List; | ... | @@ -35,39 +45,54 @@ import java.util.List; |
35 | @Service | 45 | @Service |
36 | public class UiTopoLayoutManager implements UiTopoLayoutService { | 46 | public class UiTopoLayoutManager implements UiTopoLayoutService { |
37 | 47 | ||
38 | -// private static final ClassLoader CL = | ||
39 | -// UiTopoLayoutManager.class.getClassLoader(); | ||
40 | - | ||
41 | private final Logger log = LoggerFactory.getLogger(getClass()); | 48 | private final Logger log = LoggerFactory.getLogger(getClass()); |
42 | 49 | ||
50 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
51 | + protected StorageService storageService; | ||
52 | + | ||
53 | + private ConsistentMap<UiTopoLayoutId, UiTopoLayout> layouts; | ||
54 | + private Map<UiTopoLayoutId, UiTopoLayout> layoutMap; | ||
55 | + | ||
43 | @Activate | 56 | @Activate |
44 | public void activate() { | 57 | public void activate() { |
45 | - // TODO: implement starting stuff | 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 | + | ||
46 | log.info("Started"); | 69 | log.info("Started"); |
47 | } | 70 | } |
48 | 71 | ||
49 | @Deactivate | 72 | @Deactivate |
50 | public void deactivate() { | 73 | public void deactivate() { |
51 | - // TODO: implement stopping stuff | ||
52 | log.info("Stopped"); | 74 | log.info("Stopped"); |
53 | } | 75 | } |
54 | 76 | ||
55 | 77 | ||
56 | @Override | 78 | @Override |
57 | - public List<UiTopoLayout> getLayouts() { | 79 | + public Set<UiTopoLayout> getLayouts() { |
58 | - // TODO: implement | 80 | + return ImmutableSet.copyOf(layoutMap.values()); |
59 | - return null; | ||
60 | } | 81 | } |
61 | 82 | ||
62 | @Override | 83 | @Override |
63 | public boolean addLayout(UiTopoLayout layout) { | 84 | public boolean addLayout(UiTopoLayout layout) { |
64 | - // TODO: implement | 85 | + return layouts.put(layout.id(), layout) == null; |
65 | - return false; | 86 | + } |
87 | + | ||
88 | + @Override | ||
89 | + public UiTopoLayout getLayout(UiTopoLayoutId layoutId) { | ||
90 | + return layoutMap.get(layoutId); | ||
66 | } | 91 | } |
67 | 92 | ||
68 | @Override | 93 | @Override |
69 | public boolean removeLayout(UiTopoLayout layout) { | 94 | public boolean removeLayout(UiTopoLayout layout) { |
70 | - // TODO: implement | 95 | + return layouts.remove(layout.id()) != null; |
71 | - return false; | ||
72 | } | 96 | } |
97 | + | ||
73 | } | 98 | } | ... | ... |
1 | +/* | ||
2 | + * Copyright 2016 Open Networking Laboratory | ||
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 | + | ||
19 | +import org.junit.After; | ||
20 | +import org.junit.Before; | ||
21 | +import org.junit.Test; | ||
22 | +import org.onosproject.net.region.DefaultRegion; | ||
23 | +import org.onosproject.net.region.Region; | ||
24 | +import org.onosproject.net.region.RegionId; | ||
25 | +import org.onosproject.store.service.TestStorageService; | ||
26 | +import org.onosproject.ui.UiTopoLayoutService; | ||
27 | +import org.onosproject.ui.model.topo.UiTopoLayout; | ||
28 | +import org.onosproject.ui.model.topo.UiTopoLayoutId; | ||
29 | + | ||
30 | +import static org.junit.Assert.*; | ||
31 | + | ||
32 | +/** | ||
33 | + * Suite of unit tests for the UI topology layout manager. | ||
34 | + */ | ||
35 | +public class UiTopoLayoutManagerTest { | ||
36 | + | ||
37 | + private UiTopoLayoutService svc; | ||
38 | + private UiTopoLayoutManager mgr; | ||
39 | + | ||
40 | + private static final UiTopoLayout L1 = | ||
41 | + new UiTopoLayout(UiTopoLayoutId.layoutId("l1"), | ||
42 | + new DefaultRegion(RegionId.regionId("r1"), "R1", | ||
43 | + Region.Type.CAMPUS, null)); | ||
44 | + private static final UiTopoLayout L2 = | ||
45 | + new UiTopoLayout(UiTopoLayoutId.layoutId("l2"), | ||
46 | + new DefaultRegion(RegionId.regionId("r2"), "R2", | ||
47 | + Region.Type.CAMPUS, null)); | ||
48 | + | ||
49 | + @Before | ||
50 | + public void setUp() { | ||
51 | + mgr = new UiTopoLayoutManager(); | ||
52 | + svc = mgr; | ||
53 | + mgr.storageService = new TestStorageService(); | ||
54 | + mgr.activate(); | ||
55 | + } | ||
56 | + | ||
57 | + @After | ||
58 | + public void tearDown() { | ||
59 | + mgr.deactivate(); | ||
60 | + mgr.storageService = null; | ||
61 | + } | ||
62 | + | ||
63 | + @Test | ||
64 | + public void basics() { | ||
65 | + assertTrue("should be no layout", svc.getLayouts().isEmpty()); | ||
66 | + svc.addLayout(L1); | ||
67 | + svc.addLayout(L2); | ||
68 | + assertEquals("incorrect number of layouts", 2, svc.getLayouts().size()); | ||
69 | + assertEquals("incorrect layout", L1.id(), svc.getLayout(L1.id()).id()); | ||
70 | + assertEquals("incorrect layout", L2.id(), svc.getLayout(L2.id()).id()); | ||
71 | + svc.removeLayout(L1); | ||
72 | + assertEquals("incorrect number of layouts", 1, svc.getLayouts().size()); | ||
73 | + assertNull("layout should be gone", svc.getLayout(L1.id())); | ||
74 | + assertEquals("incorrect layout", L2.id(), svc.getLayout(L2.id()).id()); | ||
75 | + } | ||
76 | + | ||
77 | + | ||
78 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment