Thomas Vachuska | 2b4de87 | 2021-03-30 16:31:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021-present Open Networking Foundation |
| 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 | package org.onosproject.ui.impl; |
| 17 | |
| 18 | import org.onosproject.net.EdgeLink; |
| 19 | import org.onosproject.net.Host; |
| 20 | import org.onosproject.net.Link; |
| 21 | import org.onosproject.net.host.HostService; |
| 22 | import org.onosproject.net.link.LinkService; |
| 23 | import org.onosproject.ui.UiExtensionService; |
| 24 | import org.onosproject.ui.UiTopoHighlighter; |
| 25 | import org.onosproject.ui.UiTopoHighlighterFactory; |
| 26 | import org.onosproject.ui.topo.BaseLink; |
| 27 | import org.onosproject.ui.topo.BaseLinkMap; |
| 28 | import org.onosproject.ui.topo.Highlights; |
| 29 | import org.onosproject.ui.topo.HostHighlight; |
| 30 | import org.onosproject.ui.topo.LinkHighlight; |
| 31 | import org.onosproject.ui.topo.Mod; |
| 32 | import org.onosproject.ui.topo.NodeBadge; |
| 33 | import org.osgi.service.component.annotations.Activate; |
| 34 | import org.osgi.service.component.annotations.Component; |
| 35 | import org.osgi.service.component.annotations.Deactivate; |
| 36 | import org.osgi.service.component.annotations.Reference; |
| 37 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 38 | |
| 39 | import static org.onosproject.net.DefaultEdgeLink.createEdgeLinks; |
| 40 | import static org.onosproject.ui.topo.NodeBadge.text; |
| 41 | |
Charles Chan | 4c02a94 | 2021-04-11 23:07:40 -0700 | [diff] [blame] | 42 | @Component(enabled = false) |
Thomas Vachuska | 2b4de87 | 2021-03-30 16:31:34 -0700 | [diff] [blame] | 43 | public class SampleHighlighterFactory { |
| 44 | |
| 45 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 46 | protected UiExtensionService uiExtensionService; |
| 47 | |
| 48 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 49 | protected LinkService linkService; |
| 50 | |
| 51 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 52 | protected HostService hostService; |
| 53 | |
| 54 | private UiTopoHighlighterFactory foo = () -> |
| 55 | new TestHighlighter("foo", new Mod("style=\"stroke: #0ff; stroke-width: 10px;\"")); |
| 56 | private UiTopoHighlighterFactory bar = () -> |
| 57 | new TestHighlighter("bar", new Mod("style=\"stroke: #f0f; stroke-width: 4px; stroke-dasharray: 5 2;\"")); |
| 58 | |
| 59 | @Activate |
| 60 | protected void activate() { |
| 61 | uiExtensionService.register(foo); |
| 62 | uiExtensionService.register(bar); |
| 63 | } |
| 64 | |
| 65 | @Deactivate |
| 66 | protected void deactivate() { |
| 67 | uiExtensionService.unregister(foo); |
| 68 | uiExtensionService.unregister(bar); |
| 69 | } |
| 70 | |
| 71 | private final class TestHighlighter implements UiTopoHighlighter { |
| 72 | |
| 73 | private final String name; |
| 74 | private final Mod mod; |
| 75 | |
| 76 | private TestHighlighter(String name, Mod mod) { |
| 77 | this.name = name; |
| 78 | this.mod = mod; |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public String name() { |
| 83 | return name; |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public Highlights createHighlights() { |
| 88 | Highlights highlights = new Highlights(); |
| 89 | BaseLinkMap linkMap = new BaseLinkMap(); |
| 90 | |
| 91 | // Create a map of base bi-links from the set of active links first. |
| 92 | for (Link link : linkService.getActiveLinks()) { |
| 93 | linkMap.add(link); |
| 94 | } |
| 95 | |
| 96 | for (Host host : hostService.getHosts()) { |
| 97 | for (EdgeLink link : createEdgeLinks(host, false)) { |
| 98 | linkMap.add(link); |
| 99 | } |
| 100 | |
| 101 | // Also add a host badge for kicks. |
| 102 | HostHighlight hostHighlight = new HostHighlight(host.id().toString()); |
| 103 | hostHighlight.setBadge(text(NodeBadge.Status.WARN, name)); |
| 104 | highlights.add(hostHighlight); |
| 105 | } |
| 106 | |
| 107 | // Now scan through the links and annotate them with desired highlights |
| 108 | for (BaseLink link : linkMap.biLinks()) { |
| 109 | highlights.add(new LinkHighlight(link.linkId(), LinkHighlight.Flavor.PRIMARY_HIGHLIGHT) |
| 110 | .addMod(mod).setLabel(name + "-" + link.one().src().port())); |
| 111 | } |
| 112 | |
| 113 | return highlights; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | } |