blob: d30fe4eee74a7e78451965b7bc42f30eade9d8a5 [file] [log] [blame]
Thomas Vachuska7648d662015-03-16 11:58:30 -07001/*
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 */
16package org.onosproject.intentperf;
17
18import com.fasterxml.jackson.databind.node.ObjectNode;
19import com.google.common.collect.ImmutableList;
20import com.google.common.collect.ImmutableSet;
21import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
24import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
26import org.onlab.osgi.ServiceDirectory;
27import org.onosproject.intentperf.IntentPerfCollector.Sample;
28import org.onosproject.ui.UiConnection;
29import org.onosproject.ui.UiExtension;
30import org.onosproject.ui.UiExtensionService;
31import org.onosproject.ui.UiMessageHandler;
32import org.onosproject.ui.UiView;
33
34import java.util.Collection;
35import java.util.HashSet;
36import java.util.List;
37import java.util.Set;
38
39import static java.util.Collections.synchronizedSet;
40
41/**
42 * Mechanism to stream data to the GUI.
43 */
44@Component(immediate = true, enabled = false)
45public 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}