blob: 1f31672ba4d42cc5ebb1b2c5a8a17bbfdbd0884e [file] [log] [blame]
Brian O'Connora468e902015-03-18 16:43:49 -07001/*
2 * Copyright 2015 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 */
16package org.onosproject.intentperf;
17
Thomas Vachuska95aadff2015-03-26 11:45:41 -070018import com.fasterxml.jackson.databind.node.ArrayNode;
Brian O'Connora468e902015-03-18 16:43:49 -070019import com.fasterxml.jackson.databind.node.ObjectNode;
20import com.google.common.collect.ImmutableList;
21import com.google.common.collect.ImmutableSet;
22import org.apache.felix.scr.annotations.Activate;
23import org.apache.felix.scr.annotations.Component;
24import org.apache.felix.scr.annotations.Deactivate;
25import org.apache.felix.scr.annotations.Reference;
26import org.apache.felix.scr.annotations.ReferenceCardinality;
Thomas Vachuska95aadff2015-03-26 11:45:41 -070027import org.apache.felix.scr.annotations.Service;
Brian O'Connora468e902015-03-18 16:43:49 -070028import org.onlab.osgi.ServiceDirectory;
29import org.onosproject.intentperf.IntentPerfCollector.Sample;
30import org.onosproject.ui.UiConnection;
31import org.onosproject.ui.UiExtension;
32import org.onosproject.ui.UiExtensionService;
33import org.onosproject.ui.UiMessageHandler;
34import org.onosproject.ui.UiView;
35
36import java.util.Collection;
37import java.util.HashSet;
38import java.util.List;
39import java.util.Set;
40
41import static java.util.Collections.synchronizedSet;
Thomas Vachuska8b91f4f2015-04-23 17:55:36 -070042import static org.onosproject.ui.UiView.Category.OTHER;
Brian O'Connora468e902015-03-18 16:43:49 -070043
44/**
45 * Mechanism to stream data to the GUI.
46 */
Thomas Vachuska95aadff2015-03-26 11:45:41 -070047@Component(immediate = true, enabled = true)
48@Service(value = IntentPerfUi.class)
Brian O'Connora468e902015-03-18 16:43:49 -070049public class IntentPerfUi {
50
51 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
52 protected UiExtensionService uiExtensionService;
53
54 private final Set<StreamingControl> handlers = synchronizedSet(new HashSet<>());
55
Thomas Vachuska8b91f4f2015-04-23 17:55:36 -070056 private List<UiView> views = ImmutableList.of(new UiView(OTHER, "intentPerf", "Intent Performance"));
Brian O'Connora468e902015-03-18 16:43:49 -070057 private UiExtension uiExtension = new UiExtension(views, this::newHandlers,
58 getClass().getClassLoader());
59
Thomas Vachuskab967ebf2015-03-28 15:19:30 -070060 private IntentPerfCollector collector;
Thomas Vachuska95aadff2015-03-26 11:45:41 -070061
Brian O'Connora468e902015-03-18 16:43:49 -070062 @Activate
63 protected void activate() {
64 uiExtensionService.register(uiExtension);
65 }
66
67 @Deactivate
68 protected void deactivate() {
69 uiExtensionService.unregister(uiExtension);
70 }
71
72 /**
73 * Reports a single sample of performance data.
74 *
75 * @param sample performance sample
76 */
77 public void reportSample(Sample sample) {
78 synchronized (handlers) {
79 handlers.forEach(h -> h.send(sample));
80 }
81 }
82
Thomas Vachuska95aadff2015-03-26 11:45:41 -070083 /**
Thomas Vachuskab967ebf2015-03-28 15:19:30 -070084 * Binds the sample collector.
Thomas Vachuska95aadff2015-03-26 11:45:41 -070085 *
Thomas Vachuskab967ebf2015-03-28 15:19:30 -070086 * @param collector list of headers for future samples
Thomas Vachuska95aadff2015-03-26 11:45:41 -070087 */
Thomas Vachuskab967ebf2015-03-28 15:19:30 -070088 public void setCollector(IntentPerfCollector collector) {
89 this.collector = collector;
Thomas Vachuska95aadff2015-03-26 11:45:41 -070090 }
91
Brian O'Connora468e902015-03-18 16:43:49 -070092 // Creates and returns session specific message handler.
93 private Collection<UiMessageHandler> newHandlers() {
94 return ImmutableList.of(new StreamingControl());
95 }
96
97 // UI Message handlers for turning on/off reporting to a session.
98 private class StreamingControl extends UiMessageHandler {
99
100 private boolean streamingEnabled = false;
101
102 protected StreamingControl() {
103 super(ImmutableSet.of("intentPerfStart", "intentPerfStop"));
104 }
105
106 @Override
107 public void process(ObjectNode message) {
Thomas Vachuska95aadff2015-03-26 11:45:41 -0700108 streamingEnabled = message.path("event").asText("unknown").equals("intentPerfStart");
109 if (streamingEnabled) {
Thomas Vachuskab967ebf2015-03-28 15:19:30 -0700110 sendInitData();
Thomas Vachuska95aadff2015-03-26 11:45:41 -0700111 }
112 }
113
Brian O'Connora468e902015-03-18 16:43:49 -0700114 @Override
115 public void init(UiConnection connection, ServiceDirectory directory) {
116 super.init(connection, directory);
117 handlers.add(this);
118 }
119
120 @Override
121 public void destroy() {
122 super.destroy();
123 handlers.remove(this);
124 }
125
126 private void send(Sample sample) {
Thomas Vachuska95aadff2015-03-26 11:45:41 -0700127 if (streamingEnabled) {
Thomas Vachuskab967ebf2015-03-28 15:19:30 -0700128 connection().sendMessage("intentPerfSample", 0, sampleNode(sample));
Thomas Vachuska95aadff2015-03-26 11:45:41 -0700129 }
Brian O'Connora468e902015-03-18 16:43:49 -0700130 }
Thomas Vachuskab967ebf2015-03-28 15:19:30 -0700131
132 private void sendInitData() {
133 ObjectNode rootNode = mapper.createObjectNode();
134 ArrayNode an = mapper.createArrayNode();
135 ArrayNode sn = mapper.createArrayNode();
136 rootNode.set("headers", an);
137 rootNode.set("samples", sn);
138
139 collector.getSampleHeaders().forEach(an::add);
140 collector.getSamples().forEach(s -> sn.add(sampleNode(s)));
141 connection().sendMessage("intentPerfInit", 0, rootNode);
142 }
143
144 private ObjectNode sampleNode(Sample sample) {
145 ObjectNode sampleNode = mapper.createObjectNode();
146 ArrayNode an = mapper.createArrayNode();
147 sampleNode.put("time", sample.time);
148 sampleNode.set("data", an);
149
150 for (double d : sample.data) {
151 an.add(d);
152 }
153 return sampleNode;
154 }
155
Brian O'Connora468e902015-03-18 16:43:49 -0700156 }
157
158}