blob: 5d7132d5f9823607e929cc5164f870ac905676c4 [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;
42
43/**
44 * Mechanism to stream data to the GUI.
45 */
Thomas Vachuska95aadff2015-03-26 11:45:41 -070046@Component(immediate = true, enabled = true)
47@Service(value = IntentPerfUi.class)
Brian O'Connora468e902015-03-18 16:43:49 -070048public class IntentPerfUi {
49
50 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
51 protected UiExtensionService uiExtensionService;
52
53 private final Set<StreamingControl> handlers = synchronizedSet(new HashSet<>());
54
55 private List<UiView> views = ImmutableList.of(new UiView("intentPerf", "Intent Performance"));
56 private UiExtension uiExtension = new UiExtension(views, this::newHandlers,
57 getClass().getClassLoader());
58
Thomas Vachuskab967ebf2015-03-28 15:19:30 -070059 private IntentPerfCollector collector;
Thomas Vachuska95aadff2015-03-26 11:45:41 -070060
Brian O'Connora468e902015-03-18 16:43:49 -070061 @Activate
62 protected void activate() {
63 uiExtensionService.register(uiExtension);
64 }
65
66 @Deactivate
67 protected void deactivate() {
68 uiExtensionService.unregister(uiExtension);
69 }
70
71 /**
72 * Reports a single sample of performance data.
73 *
74 * @param sample performance sample
75 */
76 public void reportSample(Sample sample) {
77 synchronized (handlers) {
78 handlers.forEach(h -> h.send(sample));
79 }
80 }
81
Thomas Vachuska95aadff2015-03-26 11:45:41 -070082 /**
Thomas Vachuskab967ebf2015-03-28 15:19:30 -070083 * Binds the sample collector.
Thomas Vachuska95aadff2015-03-26 11:45:41 -070084 *
Thomas Vachuskab967ebf2015-03-28 15:19:30 -070085 * @param collector list of headers for future samples
Thomas Vachuska95aadff2015-03-26 11:45:41 -070086 */
Thomas Vachuskab967ebf2015-03-28 15:19:30 -070087 public void setCollector(IntentPerfCollector collector) {
88 this.collector = collector;
Thomas Vachuska95aadff2015-03-26 11:45:41 -070089 }
90
Brian O'Connora468e902015-03-18 16:43:49 -070091 // Creates and returns session specific message handler.
92 private Collection<UiMessageHandler> newHandlers() {
93 return ImmutableList.of(new StreamingControl());
94 }
95
96 // UI Message handlers for turning on/off reporting to a session.
97 private class StreamingControl extends UiMessageHandler {
98
99 private boolean streamingEnabled = false;
100
101 protected StreamingControl() {
102 super(ImmutableSet.of("intentPerfStart", "intentPerfStop"));
103 }
104
105 @Override
106 public void process(ObjectNode message) {
Thomas Vachuska95aadff2015-03-26 11:45:41 -0700107 streamingEnabled = message.path("event").asText("unknown").equals("intentPerfStart");
108 if (streamingEnabled) {
Thomas Vachuskab967ebf2015-03-28 15:19:30 -0700109 sendInitData();
Thomas Vachuska95aadff2015-03-26 11:45:41 -0700110 }
111 }
112
Brian O'Connora468e902015-03-18 16:43:49 -0700113 @Override
114 public void init(UiConnection connection, ServiceDirectory directory) {
115 super.init(connection, directory);
116 handlers.add(this);
117 }
118
119 @Override
120 public void destroy() {
121 super.destroy();
122 handlers.remove(this);
123 }
124
125 private void send(Sample sample) {
Thomas Vachuska95aadff2015-03-26 11:45:41 -0700126 if (streamingEnabled) {
Thomas Vachuskab967ebf2015-03-28 15:19:30 -0700127 connection().sendMessage("intentPerfSample", 0, sampleNode(sample));
Thomas Vachuska95aadff2015-03-26 11:45:41 -0700128 }
Brian O'Connora468e902015-03-18 16:43:49 -0700129 }
Thomas Vachuskab967ebf2015-03-28 15:19:30 -0700130
131 private void sendInitData() {
132 ObjectNode rootNode = mapper.createObjectNode();
133 ArrayNode an = mapper.createArrayNode();
134 ArrayNode sn = mapper.createArrayNode();
135 rootNode.set("headers", an);
136 rootNode.set("samples", sn);
137
138 collector.getSampleHeaders().forEach(an::add);
139 collector.getSamples().forEach(s -> sn.add(sampleNode(s)));
140 connection().sendMessage("intentPerfInit", 0, rootNode);
141 }
142
143 private ObjectNode sampleNode(Sample sample) {
144 ObjectNode sampleNode = mapper.createObjectNode();
145 ArrayNode an = mapper.createArrayNode();
146 sampleNode.put("time", sample.time);
147 sampleNode.set("data", an);
148
149 for (double d : sample.data) {
150 an.add(d);
151 }
152 return sampleNode;
153 }
154
Brian O'Connora468e902015-03-18 16:43:49 -0700155 }
156
157}