Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.intentperf; |
| 17 | |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import com.google.common.collect.ImmutableList; |
| 21 | import com.google.common.collect.ImmutableSet; |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 22 | import org.onlab.osgi.ServiceDirectory; |
| 23 | import org.onosproject.intentperf.IntentPerfCollector.Sample; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 24 | import org.onosproject.ui.RequestHandler; |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 25 | import org.onosproject.ui.UiConnection; |
| 26 | import org.onosproject.ui.UiExtension; |
| 27 | import org.onosproject.ui.UiExtensionService; |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 28 | import org.onosproject.ui.UiMessageHandler; |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 29 | import org.onosproject.ui.UiView; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 30 | import org.osgi.service.component.annotations.Activate; |
| 31 | import org.osgi.service.component.annotations.Component; |
| 32 | import org.osgi.service.component.annotations.Deactivate; |
| 33 | import org.osgi.service.component.annotations.Reference; |
| 34 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 35 | |
| 36 | import java.util.Collection; |
| 37 | import java.util.HashSet; |
| 38 | import java.util.List; |
| 39 | import java.util.Set; |
| 40 | |
| 41 | import static java.util.Collections.synchronizedSet; |
Thomas Vachuska | 8b91f4f | 2015-04-23 17:55:36 -0700 | [diff] [blame] | 42 | import static org.onosproject.ui.UiView.Category.OTHER; |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Mechanism to stream data to the GUI. |
| 46 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 47 | @Component(immediate = true, service = IntentPerfUi.class) |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 48 | public class IntentPerfUi { |
| 49 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 50 | private static final String INTENT_PERF_START = "intentPerfStart"; |
| 51 | private static final String INTENT_PERF_STOP = "intentPerfStop"; |
| 52 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 53 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 54 | protected UiExtensionService uiExtensionService; |
| 55 | |
| 56 | private final Set<StreamingControl> handlers = synchronizedSet(new HashSet<>()); |
| 57 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 58 | private List<UiView> views = ImmutableList.of( |
| 59 | new UiView(OTHER, "intentPerf", "Intent Performance") |
| 60 | ); |
| 61 | |
| 62 | private UiExtension uiExtension = |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 63 | new UiExtension.Builder(getClass().getClassLoader(), views) |
| 64 | .messageHandlerFactory(this::newHandlers) |
| 65 | .build(); |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 66 | |
Thomas Vachuska | b967ebf | 2015-03-28 15:19:30 -0700 | [diff] [blame] | 67 | private IntentPerfCollector collector; |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 68 | |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 69 | @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 Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 90 | /** |
Thomas Vachuska | b967ebf | 2015-03-28 15:19:30 -0700 | [diff] [blame] | 91 | * Binds the sample collector. |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 92 | * |
Thomas Vachuska | b967ebf | 2015-03-28 15:19:30 -0700 | [diff] [blame] | 93 | * @param collector list of headers for future samples |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 94 | */ |
Thomas Vachuska | b967ebf | 2015-03-28 15:19:30 -0700 | [diff] [blame] | 95 | public void setCollector(IntentPerfCollector collector) { |
| 96 | this.collector = collector; |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 99 | // Creates and returns session specific message handler. |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 100 | private Collection<UiMessageHandler> newHandlers() { |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 101 | return ImmutableList.of(new StreamingControl()); |
| 102 | } |
| 103 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 104 | |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 105 | // UI Message handlers for turning on/off reporting to a session. |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 106 | private class StreamingControl extends UiMessageHandler { |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 107 | |
| 108 | private boolean streamingEnabled = false; |
| 109 | |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 110 | @Override |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 111 | protected Collection<RequestHandler> createRequestHandlers() { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 112 | return ImmutableSet.of( |
| 113 | new IntentPerfStart(), |
| 114 | new IntentPerfStop() |
| 115 | ); |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 118 | @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 Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 131 | if (streamingEnabled) { |
Simon Hunt | c536818 | 2017-01-10 13:32:04 -0800 | [diff] [blame] | 132 | connection().sendMessage("intentPerfSample", sampleNode(sample)); |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 133 | } |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 134 | } |
Thomas Vachuska | b967ebf | 2015-03-28 15:19:30 -0700 | [diff] [blame] | 135 | |
Thomas Vachuska | b967ebf | 2015-03-28 15:19:30 -0700 | [diff] [blame] | 136 | |
| 137 | private ObjectNode sampleNode(Sample sample) { |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 138 | ObjectNode sampleNode = objectNode(); |
| 139 | ArrayNode an = arrayNode(); |
Thomas Vachuska | b967ebf | 2015-03-28 15:19:30 -0700 | [diff] [blame] | 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 Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 149 | // ====================================================================== |
| 150 | |
| 151 | private final class IntentPerfStart extends RequestHandler { |
| 152 | |
| 153 | private IntentPerfStart() { |
| 154 | super(INTENT_PERF_START); |
| 155 | } |
| 156 | |
| 157 | @Override |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 158 | public void process(ObjectNode payload) { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 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))); |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 172 | sendMessage("intentPerfInit", rootNode); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 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 |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 185 | public void process(ObjectNode payload) { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 186 | streamingEnabled = false; |
| 187 | } |
| 188 | } |
| 189 | |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | } |