blob: a44ead570bcf54591207402aec690747923a3526 [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;
Simon Huntd2747a02015-04-30 22:41:16 -070030import org.onosproject.ui.RequestHandler;
Brian O'Connora468e902015-03-18 16:43:49 -070031import org.onosproject.ui.UiConnection;
32import org.onosproject.ui.UiExtension;
33import org.onosproject.ui.UiExtensionService;
Simon Hunta0ddb022015-05-01 09:53:01 -070034import org.onosproject.ui.UiMessageHandler;
Brian O'Connora468e902015-03-18 16:43:49 -070035import org.onosproject.ui.UiView;
36
37import java.util.Collection;
38import java.util.HashSet;
39import java.util.List;
40import java.util.Set;
41
42import static java.util.Collections.synchronizedSet;
Thomas Vachuska8b91f4f2015-04-23 17:55:36 -070043import static org.onosproject.ui.UiView.Category.OTHER;
Brian O'Connora468e902015-03-18 16:43:49 -070044
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
Simon Huntd2747a02015-04-30 22:41:16 -070052 private static final String INTENT_PERF_START = "intentPerfStart";
53 private static final String INTENT_PERF_STOP = "intentPerfStop";
54
Brian O'Connora468e902015-03-18 16:43:49 -070055 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
56 protected UiExtensionService uiExtensionService;
57
58 private final Set<StreamingControl> handlers = synchronizedSet(new HashSet<>());
59
Simon Huntd2747a02015-04-30 22:41:16 -070060 private List<UiView> views = ImmutableList.of(
61 new UiView(OTHER, "intentPerf", "Intent Performance")
62 );
63
64 private UiExtension uiExtension =
Simon Hunte05cae42015-07-23 17:35:24 -070065 new UiExtension.Builder(getClass().getClassLoader(), views)
66 .messageHandlerFactory(this::newHandlers)
67 .build();
Brian O'Connora468e902015-03-18 16:43:49 -070068
Thomas Vachuskab967ebf2015-03-28 15:19:30 -070069 private IntentPerfCollector collector;
Thomas Vachuska95aadff2015-03-26 11:45:41 -070070
Brian O'Connora468e902015-03-18 16:43:49 -070071 @Activate
72 protected void activate() {
73 uiExtensionService.register(uiExtension);
74 }
75
76 @Deactivate
77 protected void deactivate() {
78 uiExtensionService.unregister(uiExtension);
79 }
80
81 /**
82 * Reports a single sample of performance data.
83 *
84 * @param sample performance sample
85 */
86 public void reportSample(Sample sample) {
87 synchronized (handlers) {
88 handlers.forEach(h -> h.send(sample));
89 }
90 }
91
Thomas Vachuska95aadff2015-03-26 11:45:41 -070092 /**
Thomas Vachuskab967ebf2015-03-28 15:19:30 -070093 * Binds the sample collector.
Thomas Vachuska95aadff2015-03-26 11:45:41 -070094 *
Thomas Vachuskab967ebf2015-03-28 15:19:30 -070095 * @param collector list of headers for future samples
Thomas Vachuska95aadff2015-03-26 11:45:41 -070096 */
Thomas Vachuskab967ebf2015-03-28 15:19:30 -070097 public void setCollector(IntentPerfCollector collector) {
98 this.collector = collector;
Thomas Vachuska95aadff2015-03-26 11:45:41 -070099 }
100
Brian O'Connora468e902015-03-18 16:43:49 -0700101 // Creates and returns session specific message handler.
Simon Hunta0ddb022015-05-01 09:53:01 -0700102 private Collection<UiMessageHandler> newHandlers() {
Brian O'Connora468e902015-03-18 16:43:49 -0700103 return ImmutableList.of(new StreamingControl());
104 }
105
Simon Huntd2747a02015-04-30 22:41:16 -0700106
Brian O'Connora468e902015-03-18 16:43:49 -0700107 // UI Message handlers for turning on/off reporting to a session.
Simon Hunta0ddb022015-05-01 09:53:01 -0700108 private class StreamingControl extends UiMessageHandler {
Brian O'Connora468e902015-03-18 16:43:49 -0700109
110 private boolean streamingEnabled = false;
111
Brian O'Connora468e902015-03-18 16:43:49 -0700112 @Override
Simon Huntda580882015-05-12 20:58:18 -0700113 protected Collection<RequestHandler> createRequestHandlers() {
Simon Huntd2747a02015-04-30 22:41:16 -0700114 return ImmutableSet.of(
115 new IntentPerfStart(),
116 new IntentPerfStop()
117 );
Thomas Vachuska95aadff2015-03-26 11:45:41 -0700118 }
119
Brian O'Connora468e902015-03-18 16:43:49 -0700120 @Override
121 public void init(UiConnection connection, ServiceDirectory directory) {
122 super.init(connection, directory);
123 handlers.add(this);
124 }
125
126 @Override
127 public void destroy() {
128 super.destroy();
129 handlers.remove(this);
130 }
131
132 private void send(Sample sample) {
Thomas Vachuska95aadff2015-03-26 11:45:41 -0700133 if (streamingEnabled) {
Thomas Vachuskab967ebf2015-03-28 15:19:30 -0700134 connection().sendMessage("intentPerfSample", 0, sampleNode(sample));
Thomas Vachuska95aadff2015-03-26 11:45:41 -0700135 }
Brian O'Connora468e902015-03-18 16:43:49 -0700136 }
Thomas Vachuskab967ebf2015-03-28 15:19:30 -0700137
Thomas Vachuskab967ebf2015-03-28 15:19:30 -0700138
139 private ObjectNode sampleNode(Sample sample) {
Simon Huntda580882015-05-12 20:58:18 -0700140 ObjectNode sampleNode = objectNode();
141 ArrayNode an = arrayNode();
Thomas Vachuskab967ebf2015-03-28 15:19:30 -0700142 sampleNode.put("time", sample.time);
143 sampleNode.set("data", an);
144
145 for (double d : sample.data) {
146 an.add(d);
147 }
148 return sampleNode;
149 }
150
Simon Huntd2747a02015-04-30 22:41:16 -0700151 // ======================================================================
152
153 private final class IntentPerfStart extends RequestHandler {
154
155 private IntentPerfStart() {
156 super(INTENT_PERF_START);
157 }
158
159 @Override
160 public void process(long sid, ObjectNode payload) {
161 streamingEnabled = true;
162 sendInitData();
163 }
164
165 private void sendInitData() {
166 ObjectNode rootNode = MAPPER.createObjectNode();
167 ArrayNode an = MAPPER.createArrayNode();
168 ArrayNode sn = MAPPER.createArrayNode();
169 rootNode.set("headers", an);
170 rootNode.set("samples", sn);
171
172 collector.getSampleHeaders().forEach(an::add);
173 collector.getSamples().forEach(s -> sn.add(sampleNode(s)));
174 sendMessage("intentPerfInit", 0, rootNode);
175 }
176 }
177
178 // ======================================================================
179
180 private final class IntentPerfStop extends RequestHandler {
181
182 private IntentPerfStop() {
183 super(INTENT_PERF_STOP);
184 }
185
186 @Override
187 public void process(long sid, ObjectNode payload) {
188 streamingEnabled = false;
189 }
190 }
191
Brian O'Connora468e902015-03-18 16:43:49 -0700192 }
193
194}