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