Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 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; |
| 30 | import org.onosproject.ui.UiConnection; |
| 31 | import org.onosproject.ui.UiExtension; |
| 32 | import org.onosproject.ui.UiExtensionService; |
| 33 | import org.onosproject.ui.UiMessageHandler; |
| 34 | import org.onosproject.ui.UiView; |
| 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; |
| 42 | |
| 43 | /** |
| 44 | * Mechanism to stream data to the GUI. |
| 45 | */ |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 46 | @Component(immediate = true, enabled = true) |
| 47 | @Service(value = IntentPerfUi.class) |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 48 | public class IntentPerfUi { |
| 49 | |
| 50 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 51 | protected UiExtensionService uiExtensionService; |
| 52 | |
| 53 | private final Set<StreamingControl> handlers = synchronizedSet(new HashSet<>()); |
| 54 | |
| 55 | private List<UiView> views = ImmutableList.of(new UiView("intentPerf", "Intent Performance")); |
| 56 | private UiExtension uiExtension = new UiExtension(views, this::newHandlers, |
| 57 | getClass().getClassLoader()); |
| 58 | |
Thomas Vachuska | b967ebf | 2015-03-28 15:19:30 -0700 | [diff] [blame^] | 59 | private IntentPerfCollector collector; |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 60 | |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 61 | @Activate |
| 62 | protected void activate() { |
| 63 | uiExtensionService.register(uiExtension); |
| 64 | } |
| 65 | |
| 66 | @Deactivate |
| 67 | protected void deactivate() { |
| 68 | uiExtensionService.unregister(uiExtension); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Reports a single sample of performance data. |
| 73 | * |
| 74 | * @param sample performance sample |
| 75 | */ |
| 76 | public void reportSample(Sample sample) { |
| 77 | synchronized (handlers) { |
| 78 | handlers.forEach(h -> h.send(sample)); |
| 79 | } |
| 80 | } |
| 81 | |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 82 | /** |
Thomas Vachuska | b967ebf | 2015-03-28 15:19:30 -0700 | [diff] [blame^] | 83 | * Binds the sample collector. |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 84 | * |
Thomas Vachuska | b967ebf | 2015-03-28 15:19:30 -0700 | [diff] [blame^] | 85 | * @param collector list of headers for future samples |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 86 | */ |
Thomas Vachuska | b967ebf | 2015-03-28 15:19:30 -0700 | [diff] [blame^] | 87 | public void setCollector(IntentPerfCollector collector) { |
| 88 | this.collector = collector; |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 91 | // Creates and returns session specific message handler. |
| 92 | private Collection<UiMessageHandler> newHandlers() { |
| 93 | return ImmutableList.of(new StreamingControl()); |
| 94 | } |
| 95 | |
| 96 | // UI Message handlers for turning on/off reporting to a session. |
| 97 | private class StreamingControl extends UiMessageHandler { |
| 98 | |
| 99 | private boolean streamingEnabled = false; |
| 100 | |
| 101 | protected StreamingControl() { |
| 102 | super(ImmutableSet.of("intentPerfStart", "intentPerfStop")); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public void process(ObjectNode message) { |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 107 | streamingEnabled = message.path("event").asText("unknown").equals("intentPerfStart"); |
| 108 | if (streamingEnabled) { |
Thomas Vachuska | b967ebf | 2015-03-28 15:19:30 -0700 | [diff] [blame^] | 109 | sendInitData(); |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 113 | @Override |
| 114 | public void init(UiConnection connection, ServiceDirectory directory) { |
| 115 | super.init(connection, directory); |
| 116 | handlers.add(this); |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public void destroy() { |
| 121 | super.destroy(); |
| 122 | handlers.remove(this); |
| 123 | } |
| 124 | |
| 125 | private void send(Sample sample) { |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 126 | if (streamingEnabled) { |
Thomas Vachuska | b967ebf | 2015-03-28 15:19:30 -0700 | [diff] [blame^] | 127 | connection().sendMessage("intentPerfSample", 0, sampleNode(sample)); |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 128 | } |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 129 | } |
Thomas Vachuska | b967ebf | 2015-03-28 15:19:30 -0700 | [diff] [blame^] | 130 | |
| 131 | private void sendInitData() { |
| 132 | ObjectNode rootNode = mapper.createObjectNode(); |
| 133 | ArrayNode an = mapper.createArrayNode(); |
| 134 | ArrayNode sn = mapper.createArrayNode(); |
| 135 | rootNode.set("headers", an); |
| 136 | rootNode.set("samples", sn); |
| 137 | |
| 138 | collector.getSampleHeaders().forEach(an::add); |
| 139 | collector.getSamples().forEach(s -> sn.add(sampleNode(s))); |
| 140 | connection().sendMessage("intentPerfInit", 0, rootNode); |
| 141 | } |
| 142 | |
| 143 | private ObjectNode sampleNode(Sample sample) { |
| 144 | ObjectNode sampleNode = mapper.createObjectNode(); |
| 145 | ArrayNode an = mapper.createArrayNode(); |
| 146 | sampleNode.put("time", sample.time); |
| 147 | sampleNode.set("data", an); |
| 148 | |
| 149 | for (double d : sample.data) { |
| 150 | an.add(d); |
| 151 | } |
| 152 | return sampleNode; |
| 153 | } |
| 154 | |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | } |