blob: 5a24b4a053fd13b1dca032b844b34a6accf11bf9 [file] [log] [blame]
Thomas Vachuska2b4de872021-03-30 16:31:34 -07001/*
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 */
16package org.onosproject.ui.impl;
17
18import org.onosproject.net.EdgeLink;
19import org.onosproject.net.Host;
20import org.onosproject.net.Link;
21import org.onosproject.net.host.HostService;
22import org.onosproject.net.link.LinkService;
23import org.onosproject.ui.UiExtensionService;
24import org.onosproject.ui.UiTopoHighlighter;
25import org.onosproject.ui.UiTopoHighlighterFactory;
26import org.onosproject.ui.topo.BaseLink;
27import org.onosproject.ui.topo.BaseLinkMap;
28import org.onosproject.ui.topo.Highlights;
29import org.onosproject.ui.topo.HostHighlight;
30import org.onosproject.ui.topo.LinkHighlight;
31import org.onosproject.ui.topo.Mod;
32import org.onosproject.ui.topo.NodeBadge;
33import org.osgi.service.component.annotations.Activate;
34import org.osgi.service.component.annotations.Component;
35import org.osgi.service.component.annotations.Deactivate;
36import org.osgi.service.component.annotations.Reference;
37import org.osgi.service.component.annotations.ReferenceCardinality;
38
39import static org.onosproject.net.DefaultEdgeLink.createEdgeLinks;
40import static org.onosproject.ui.topo.NodeBadge.text;
41
Charles Chan4c02a942021-04-11 23:07:40 -070042@Component(enabled = false)
Thomas Vachuska2b4de872021-03-30 16:31:34 -070043public 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}