blob: f50ed3b4460585e9fd0b75f940c41fd7d883b288 [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;
Thomas Vachuska95aadff2015-03-26 11:45:41 -070039import java.util.Random;
Brian O'Connora468e902015-03-18 16:43:49 -070040import java.util.Set;
Thomas Vachuska95aadff2015-03-26 11:45:41 -070041import java.util.TimerTask;
Brian O'Connora468e902015-03-18 16:43:49 -070042
43import static java.util.Collections.synchronizedSet;
44
45/**
46 * Mechanism to stream data to the GUI.
47 */
Thomas Vachuska95aadff2015-03-26 11:45:41 -070048@Component(immediate = true, enabled = true)
49@Service(value = IntentPerfUi.class)
Brian O'Connora468e902015-03-18 16:43:49 -070050public class IntentPerfUi {
51
52 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
53 protected UiExtensionService uiExtensionService;
54
55 private final Set<StreamingControl> handlers = synchronizedSet(new HashSet<>());
56
57 private List<UiView> views = ImmutableList.of(new UiView("intentPerf", "Intent Performance"));
58 private UiExtension uiExtension = new UiExtension(views, this::newHandlers,
59 getClass().getClassLoader());
60
Thomas Vachuska95aadff2015-03-26 11:45:41 -070061 private List<String> headers = ImmutableList.of("One", "Two", "Three", "Four", "Five");
62
63 private Random random = new Random();
64 private TimerTask task;
65
Brian O'Connora468e902015-03-18 16:43:49 -070066 @Activate
67 protected void activate() {
68 uiExtensionService.register(uiExtension);
Thomas Vachuska95aadff2015-03-26 11:45:41 -070069// task = new TimerTask() {
70// @Override
71// public void run() {
72// Sample sample = new Sample(System.currentTimeMillis(), headers.size());
73// for (int i = 0; i < headers.size(); i++) {
74// sample.data[i] = 25_000 + random.nextInt(20_000) - 5_000;
75// }
76// reportSample(sample);
77// }
78// };
79// SharedExecutors.getTimer().scheduleAtFixedRate(task, 1000, 1000);
Brian O'Connora468e902015-03-18 16:43:49 -070080 }
81
82 @Deactivate
83 protected void deactivate() {
84 uiExtensionService.unregister(uiExtension);
85 }
86
87 /**
88 * Reports a single sample of performance data.
89 *
90 * @param sample performance sample
91 */
92 public void reportSample(Sample sample) {
93 synchronized (handlers) {
94 handlers.forEach(h -> h.send(sample));
95 }
96 }
97
Thomas Vachuska95aadff2015-03-26 11:45:41 -070098 /**
99 * Sets the headers for the subsequently reported samples.
100 *
101 * @param headers list of headers for future samples
102 */
103 public void setHeaders(List<String> headers) {
104 this.headers = headers;
105 }
106
Brian O'Connora468e902015-03-18 16:43:49 -0700107 // Creates and returns session specific message handler.
108 private Collection<UiMessageHandler> newHandlers() {
109 return ImmutableList.of(new StreamingControl());
110 }
111
112 // UI Message handlers for turning on/off reporting to a session.
113 private class StreamingControl extends UiMessageHandler {
114
115 private boolean streamingEnabled = false;
116
117 protected StreamingControl() {
118 super(ImmutableSet.of("intentPerfStart", "intentPerfStop"));
119 }
120
121 @Override
122 public void process(ObjectNode message) {
Thomas Vachuska95aadff2015-03-26 11:45:41 -0700123 streamingEnabled = message.path("event").asText("unknown").equals("intentPerfStart");
124 if (streamingEnabled) {
125 sendHeaders();
126 }
127 }
128
129 private void sendHeaders() {
130 ArrayNode an = mapper.createArrayNode();
131 for (String header : headers) {
132 an.add(header);
133 }
134
135 ObjectNode sn = mapper.createObjectNode();
136 sn.set("headers", an);
137
138 connection().sendMessage("intentPerfHeaders", 0, sn);
Brian O'Connora468e902015-03-18 16:43:49 -0700139 }
140
141 @Override
142 public void init(UiConnection connection, ServiceDirectory directory) {
143 super.init(connection, directory);
144 handlers.add(this);
145 }
146
147 @Override
148 public void destroy() {
149 super.destroy();
150 handlers.remove(this);
151 }
152
153 private void send(Sample sample) {
Thomas Vachuska95aadff2015-03-26 11:45:41 -0700154 if (streamingEnabled) {
155 ArrayNode an = mapper.createArrayNode();
156 for (double d : sample.data) {
157 an.add(d);
158 }
159
160 ObjectNode sn = mapper.createObjectNode();
161 sn.put("time", sample.time);
162 sn.set("data", an);
163
164 connection().sendMessage("intentPerfSample", 0, sn);
165 }
Brian O'Connora468e902015-03-18 16:43:49 -0700166 }
167 }
168
169}