Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
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; |
| 22 | import org.apache.felix.scr.annotations.Activate; |
| 23 | import org.apache.felix.scr.annotations.Component; |
| 24 | import org.apache.felix.scr.annotations.Deactivate; |
| 25 | import org.apache.felix.scr.annotations.Reference; |
| 26 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 27 | import org.apache.felix.scr.annotations.Service; |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 28 | import org.onlab.osgi.ServiceDirectory; |
| 29 | import org.onosproject.intentperf.IntentPerfCollector.Sample; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 30 | import org.onosproject.ui.RequestHandler; |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 31 | import org.onosproject.ui.UiConnection; |
| 32 | import org.onosproject.ui.UiExtension; |
| 33 | import org.onosproject.ui.UiExtensionService; |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 34 | import org.onosproject.ui.UiMessageHandler; |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 35 | import org.onosproject.ui.UiView; |
| 36 | |
| 37 | import java.util.Collection; |
| 38 | import java.util.HashSet; |
| 39 | import java.util.List; |
| 40 | import java.util.Set; |
| 41 | |
| 42 | import static java.util.Collections.synchronizedSet; |
Thomas Vachuska | 8b91f4f | 2015-04-23 17:55:36 -0700 | [diff] [blame] | 43 | import static org.onosproject.ui.UiView.Category.OTHER; |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * Mechanism to stream data to the GUI. |
| 47 | */ |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 48 | @Component(immediate = true, enabled = true) |
| 49 | @Service(value = IntentPerfUi.class) |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 50 | public class IntentPerfUi { |
| 51 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 52 | private static final String INTENT_PERF_START = "intentPerfStart"; |
| 53 | private static final String INTENT_PERF_STOP = "intentPerfStop"; |
| 54 | |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 55 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 56 | protected UiExtensionService uiExtensionService; |
| 57 | |
| 58 | private final Set<StreamingControl> handlers = synchronizedSet(new HashSet<>()); |
| 59 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 60 | private List<UiView> views = ImmutableList.of( |
| 61 | new UiView(OTHER, "intentPerf", "Intent Performance") |
| 62 | ); |
| 63 | |
| 64 | private UiExtension uiExtension = |
Simon Hunt | e05cae4 | 2015-07-23 17:35:24 -0700 | [diff] [blame] | 65 | new UiExtension.Builder(getClass().getClassLoader(), views) |
| 66 | .messageHandlerFactory(this::newHandlers) |
| 67 | .build(); |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 68 | |
Thomas Vachuska | b967ebf | 2015-03-28 15:19:30 -0700 | [diff] [blame] | 69 | private IntentPerfCollector collector; |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 70 | |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 71 | @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 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 | * Binds the sample collector. |
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 | * @param collector list of headers for future samples |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 96 | */ |
Thomas Vachuska | b967ebf | 2015-03-28 15:19:30 -0700 | [diff] [blame] | 97 | public void setCollector(IntentPerfCollector collector) { |
| 98 | this.collector = collector; |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 101 | // Creates and returns session specific message handler. |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 102 | private Collection<UiMessageHandler> newHandlers() { |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 103 | return ImmutableList.of(new StreamingControl()); |
| 104 | } |
| 105 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 106 | |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 107 | // UI Message handlers for turning on/off reporting to a session. |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 108 | private class StreamingControl extends UiMessageHandler { |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 109 | |
| 110 | private boolean streamingEnabled = false; |
| 111 | |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 112 | @Override |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 113 | protected Collection<RequestHandler> createRequestHandlers() { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 114 | return ImmutableSet.of( |
| 115 | new IntentPerfStart(), |
| 116 | new IntentPerfStop() |
| 117 | ); |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 120 | @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 Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 133 | if (streamingEnabled) { |
Thomas Vachuska | b967ebf | 2015-03-28 15:19:30 -0700 | [diff] [blame] | 134 | connection().sendMessage("intentPerfSample", 0, sampleNode(sample)); |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 135 | } |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 136 | } |
Thomas Vachuska | b967ebf | 2015-03-28 15:19:30 -0700 | [diff] [blame] | 137 | |
Thomas Vachuska | b967ebf | 2015-03-28 15:19:30 -0700 | [diff] [blame] | 138 | |
| 139 | private ObjectNode sampleNode(Sample sample) { |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 140 | ObjectNode sampleNode = objectNode(); |
| 141 | ArrayNode an = arrayNode(); |
Thomas Vachuska | b967ebf | 2015-03-28 15:19:30 -0700 | [diff] [blame] | 142 | 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 Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 151 | // ====================================================================== |
| 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'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | } |