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; |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 39 | import java.util.Random; |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 40 | import java.util.Set; |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 41 | import java.util.TimerTask; |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 42 | |
| 43 | import static java.util.Collections.synchronizedSet; |
| 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 | |
| 52 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 53 | protected UiExtensionService uiExtensionService; |
| 54 | |
| 55 | private final Set<StreamingControl> handlers = synchronizedSet(new HashSet<>()); |
| 56 | |
| 57 | private List<UiView> views = ImmutableList.of(new UiView("intentPerf", "Intent Performance")); |
| 58 | private UiExtension uiExtension = new UiExtension(views, this::newHandlers, |
| 59 | getClass().getClassLoader()); |
| 60 | |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 61 | private List<String> headers = ImmutableList.of("One", "Two", "Three", "Four", "Five"); |
| 62 | |
| 63 | private Random random = new Random(); |
| 64 | private TimerTask task; |
| 65 | |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 66 | @Activate |
| 67 | protected void activate() { |
| 68 | uiExtensionService.register(uiExtension); |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 69 | // task = new TimerTask() { |
| 70 | // @Override |
| 71 | // public void run() { |
| 72 | // Sample sample = new Sample(System.currentTimeMillis(), headers.size()); |
| 73 | // for (int i = 0; i < headers.size(); i++) { |
| 74 | // sample.data[i] = 25_000 + random.nextInt(20_000) - 5_000; |
| 75 | // } |
| 76 | // reportSample(sample); |
| 77 | // } |
| 78 | // }; |
| 79 | // SharedExecutors.getTimer().scheduleAtFixedRate(task, 1000, 1000); |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @Deactivate |
| 83 | protected void deactivate() { |
| 84 | uiExtensionService.unregister(uiExtension); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Reports a single sample of performance data. |
| 89 | * |
| 90 | * @param sample performance sample |
| 91 | */ |
| 92 | public void reportSample(Sample sample) { |
| 93 | synchronized (handlers) { |
| 94 | handlers.forEach(h -> h.send(sample)); |
| 95 | } |
| 96 | } |
| 97 | |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 98 | /** |
| 99 | * Sets the headers for the subsequently reported samples. |
| 100 | * |
| 101 | * @param headers list of headers for future samples |
| 102 | */ |
| 103 | public void setHeaders(List<String> headers) { |
| 104 | this.headers = headers; |
| 105 | } |
| 106 | |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 107 | // Creates and returns session specific message handler. |
| 108 | private Collection<UiMessageHandler> newHandlers() { |
| 109 | return ImmutableList.of(new StreamingControl()); |
| 110 | } |
| 111 | |
| 112 | // UI Message handlers for turning on/off reporting to a session. |
| 113 | private class StreamingControl extends UiMessageHandler { |
| 114 | |
| 115 | private boolean streamingEnabled = false; |
| 116 | |
| 117 | protected StreamingControl() { |
| 118 | super(ImmutableSet.of("intentPerfStart", "intentPerfStop")); |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public void process(ObjectNode message) { |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 123 | streamingEnabled = message.path("event").asText("unknown").equals("intentPerfStart"); |
| 124 | if (streamingEnabled) { |
| 125 | sendHeaders(); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | private void sendHeaders() { |
| 130 | ArrayNode an = mapper.createArrayNode(); |
| 131 | for (String header : headers) { |
| 132 | an.add(header); |
| 133 | } |
| 134 | |
| 135 | ObjectNode sn = mapper.createObjectNode(); |
| 136 | sn.set("headers", an); |
| 137 | |
| 138 | connection().sendMessage("intentPerfHeaders", 0, sn); |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public void init(UiConnection connection, ServiceDirectory directory) { |
| 143 | super.init(connection, directory); |
| 144 | handlers.add(this); |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public void destroy() { |
| 149 | super.destroy(); |
| 150 | handlers.remove(this); |
| 151 | } |
| 152 | |
| 153 | private void send(Sample sample) { |
Thomas Vachuska | 95aadff | 2015-03-26 11:45:41 -0700 | [diff] [blame] | 154 | if (streamingEnabled) { |
| 155 | ArrayNode an = mapper.createArrayNode(); |
| 156 | for (double d : sample.data) { |
| 157 | an.add(d); |
| 158 | } |
| 159 | |
| 160 | ObjectNode sn = mapper.createObjectNode(); |
| 161 | sn.put("time", sample.time); |
| 162 | sn.set("data", an); |
| 163 | |
| 164 | connection().sendMessage("intentPerfSample", 0, sn); |
| 165 | } |
Brian O'Connor | a468e90 | 2015-03-18 16:43:49 -0700 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
| 169 | } |