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 | |
| 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.ImmutableSet; |
| 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 26 | import org.onlab.osgi.ServiceDirectory; |
| 27 | import org.onosproject.intentperf.IntentPerfCollector.Sample; |
| 28 | import org.onosproject.ui.UiConnection; |
| 29 | import org.onosproject.ui.UiExtension; |
| 30 | import org.onosproject.ui.UiExtensionService; |
| 31 | import org.onosproject.ui.UiMessageHandler; |
| 32 | import org.onosproject.ui.UiView; |
| 33 | |
| 34 | import java.util.Collection; |
| 35 | import java.util.HashSet; |
| 36 | import java.util.List; |
| 37 | import java.util.Set; |
| 38 | |
| 39 | import static java.util.Collections.synchronizedSet; |
| 40 | |
| 41 | /** |
| 42 | * Mechanism to stream data to the GUI. |
| 43 | */ |
| 44 | @Component(immediate = true, enabled = false) |
| 45 | public class IntentPerfUi { |
| 46 | |
| 47 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 48 | protected UiExtensionService uiExtensionService; |
| 49 | |
| 50 | private final Set<StreamingControl> handlers = synchronizedSet(new HashSet<>()); |
| 51 | |
| 52 | private List<UiView> views = ImmutableList.of(new UiView("intentPerf", "Intent Performance")); |
| 53 | private UiExtension uiExtension = new UiExtension(views, this::newHandlers, |
| 54 | getClass().getClassLoader()); |
| 55 | |
| 56 | @Activate |
| 57 | protected void activate() { |
| 58 | uiExtensionService.register(uiExtension); |
| 59 | } |
| 60 | |
| 61 | @Deactivate |
| 62 | protected void deactivate() { |
| 63 | uiExtensionService.unregister(uiExtension); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Reports a single sample of performance data. |
| 68 | * |
| 69 | * @param sample performance sample |
| 70 | */ |
| 71 | public void reportSample(Sample sample) { |
| 72 | synchronized (handlers) { |
| 73 | handlers.forEach(h -> h.send(sample)); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // Creates and returns session specific message handler. |
| 78 | private Collection<UiMessageHandler> newHandlers() { |
| 79 | return ImmutableList.of(new StreamingControl()); |
| 80 | } |
| 81 | |
| 82 | // UI Message handlers for turning on/off reporting to a session. |
| 83 | private class StreamingControl extends UiMessageHandler { |
| 84 | |
| 85 | private boolean streamingEnabled = false; |
| 86 | |
| 87 | protected StreamingControl() { |
| 88 | super(ImmutableSet.of("intentPerfStart", "intentPerfStop")); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public void process(ObjectNode message) { |
| 93 | streamingEnabled = message.path("event").asText("unknown").equals("initPerfStart"); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public void init(UiConnection connection, ServiceDirectory directory) { |
| 98 | super.init(connection, directory); |
| 99 | handlers.add(this); |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public void destroy() { |
| 104 | super.destroy(); |
| 105 | handlers.remove(this); |
| 106 | } |
| 107 | |
| 108 | private void send(Sample sample) { |
| 109 | // FIXME: finish this |
| 110 | ObjectNode sn = mapper.createObjectNode() |
| 111 | .put("time", sample.time); |
| 112 | connection().sendMessage("intentPerf", 0, sn); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | } |