blob: 7118ee1bf7b825097ab858db7366549cf2fae579 [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 =
65 new UiExtension(views, this::newHandlers, getClass().getClassLoader());
Brian O'Connora468e902015-03-18 16:43:49 -070066
Thomas Vachuskab967ebf2015-03-28 15:19:30 -070067 private IntentPerfCollector collector;
Thomas Vachuska95aadff2015-03-26 11:45:41 -070068
Brian O'Connora468e902015-03-18 16:43:49 -070069 @Activate
70 protected void activate() {
71 uiExtensionService.register(uiExtension);
72 }
73
74 @Deactivate
75 protected void deactivate() {
76 uiExtensionService.unregister(uiExtension);
77 }
78
79 /**
80 * Reports a single sample of performance data.
81 *
82 * @param sample performance sample
83 */
84 public void reportSample(Sample sample) {
85 synchronized (handlers) {
86 handlers.forEach(h -> h.send(sample));
87 }
88 }
89
Thomas Vachuska95aadff2015-03-26 11:45:41 -070090 /**
Thomas Vachuskab967ebf2015-03-28 15:19:30 -070091 * Binds the sample collector.
Thomas Vachuska95aadff2015-03-26 11:45:41 -070092 *
Thomas Vachuskab967ebf2015-03-28 15:19:30 -070093 * @param collector list of headers for future samples
Thomas Vachuska95aadff2015-03-26 11:45:41 -070094 */
Thomas Vachuskab967ebf2015-03-28 15:19:30 -070095 public void setCollector(IntentPerfCollector collector) {
96 this.collector = collector;
Thomas Vachuska95aadff2015-03-26 11:45:41 -070097 }
98
Brian O'Connora468e902015-03-18 16:43:49 -070099 // Creates and returns session specific message handler.
Simon Hunta0ddb022015-05-01 09:53:01 -0700100 private Collection<UiMessageHandler> newHandlers() {
Brian O'Connora468e902015-03-18 16:43:49 -0700101 return ImmutableList.of(new StreamingControl());
102 }
103
Simon Huntd2747a02015-04-30 22:41:16 -0700104
Brian O'Connora468e902015-03-18 16:43:49 -0700105 // UI Message handlers for turning on/off reporting to a session.
Simon Hunta0ddb022015-05-01 09:53:01 -0700106 private class StreamingControl extends UiMessageHandler {
Brian O'Connora468e902015-03-18 16:43:49 -0700107
108 private boolean streamingEnabled = false;
109
Brian O'Connora468e902015-03-18 16:43:49 -0700110 @Override
Simon Huntd2747a02015-04-30 22:41:16 -0700111 protected Collection<RequestHandler> getHandlers() {
112 return ImmutableSet.of(
113 new IntentPerfStart(),
114 new IntentPerfStop()
115 );
Thomas Vachuska95aadff2015-03-26 11:45:41 -0700116 }
117
Brian O'Connora468e902015-03-18 16:43:49 -0700118 @Override
119 public void init(UiConnection connection, ServiceDirectory directory) {
120 super.init(connection, directory);
121 handlers.add(this);
122 }
123
124 @Override
125 public void destroy() {
126 super.destroy();
127 handlers.remove(this);
128 }
129
130 private void send(Sample sample) {
Thomas Vachuska95aadff2015-03-26 11:45:41 -0700131 if (streamingEnabled) {
Thomas Vachuskab967ebf2015-03-28 15:19:30 -0700132 connection().sendMessage("intentPerfSample", 0, sampleNode(sample));
Thomas Vachuska95aadff2015-03-26 11:45:41 -0700133 }
Brian O'Connora468e902015-03-18 16:43:49 -0700134 }
Thomas Vachuskab967ebf2015-03-28 15:19:30 -0700135
Thomas Vachuskab967ebf2015-03-28 15:19:30 -0700136
137 private ObjectNode sampleNode(Sample sample) {
138 ObjectNode sampleNode = mapper.createObjectNode();
139 ArrayNode an = mapper.createArrayNode();
140 sampleNode.put("time", sample.time);
141 sampleNode.set("data", an);
142
143 for (double d : sample.data) {
144 an.add(d);
145 }
146 return sampleNode;
147 }
148
Simon Huntd2747a02015-04-30 22:41:16 -0700149 // ======================================================================
150
151 private final class IntentPerfStart extends RequestHandler {
152
153 private IntentPerfStart() {
154 super(INTENT_PERF_START);
155 }
156
157 @Override
158 public void process(long sid, ObjectNode payload) {
159 streamingEnabled = true;
160 sendInitData();
161 }
162
163 private void sendInitData() {
164 ObjectNode rootNode = MAPPER.createObjectNode();
165 ArrayNode an = MAPPER.createArrayNode();
166 ArrayNode sn = MAPPER.createArrayNode();
167 rootNode.set("headers", an);
168 rootNode.set("samples", sn);
169
170 collector.getSampleHeaders().forEach(an::add);
171 collector.getSamples().forEach(s -> sn.add(sampleNode(s)));
172 sendMessage("intentPerfInit", 0, rootNode);
173 }
174 }
175
176 // ======================================================================
177
178 private final class IntentPerfStop extends RequestHandler {
179
180 private IntentPerfStop() {
181 super(INTENT_PERF_STOP);
182 }
183
184 @Override
185 public void process(long sid, ObjectNode payload) {
186 streamingEnabled = false;
187 }
188 }
189
Brian O'Connora468e902015-03-18 16:43:49 -0700190 }
191
192}