blob: 2d4e28f028b475c7233e463c7f81351fae5efc01 [file] [log] [blame]
Jian Li10a20702016-02-01 16:39:51 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Jian Li10a20702016-02-01 16:39:51 -08003 *
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.cpman.gui;
17
18import com.fasterxml.jackson.databind.node.ObjectNode;
Jian Li1aa07822016-04-19 17:58:02 -070019import com.google.common.base.Strings;
Jian Li10a20702016-02-01 16:39:51 -080020import com.google.common.collect.ImmutableSet;
Jian Li1aa07822016-04-19 17:58:02 -070021import com.google.common.collect.Maps;
22import org.apache.commons.lang.ArrayUtils;
23import org.apache.commons.lang3.StringUtils;
24import org.joda.time.LocalDateTime;
25import org.onosproject.cluster.ClusterService;
26import org.onosproject.cpman.ControlLoadSnapshot;
27import org.onosproject.cpman.ControlMetricType;
28import org.onosproject.cpman.ControlPlaneMonitorService;
29import org.onosproject.net.DeviceId;
30import org.onosproject.net.device.DeviceService;
Jian Li10a20702016-02-01 16:39:51 -080031import org.onosproject.ui.RequestHandler;
32import org.onosproject.ui.UiMessageHandler;
Jian Li1aa07822016-04-19 17:58:02 -070033import org.onosproject.ui.chart.ChartModel;
34import org.onosproject.ui.chart.ChartRequestHandler;
35import org.slf4j.Logger;
36import org.slf4j.LoggerFactory;
Jian Li10a20702016-02-01 16:39:51 -080037
38import java.util.Collection;
Jian Li1aa07822016-04-19 17:58:02 -070039import java.util.Map;
40import java.util.Optional;
41import java.util.concurrent.ExecutionException;
42import java.util.concurrent.TimeUnit;
43
44import static org.onosproject.cpman.ControlResource.CONTROL_MESSAGE_METRICS;
45import static org.onosproject.cpman.ControlResource.Type.CONTROL_MESSAGE;
Jian Li10a20702016-02-01 16:39:51 -080046
47/**
48 * CpmanViewMessageHandler class implementation.
49 */
50public class CpmanViewMessageHandler extends UiMessageHandler {
51
Jian Li1aa07822016-04-19 17:58:02 -070052 private final Logger log = LoggerFactory.getLogger(getClass());
53
Jian Li10a20702016-02-01 16:39:51 -080054 private static final String CPMAN_DATA_REQ = "cpmanDataRequest";
55 private static final String CPMAN_DATA_RESP = "cpmanDataResponse";
Jian Li1aa07822016-04-19 17:58:02 -070056 private static final String CPMANS = "cpmans";
Jian Li10a20702016-02-01 16:39:51 -080057
Jian Li1aa07822016-04-19 17:58:02 -070058 // TODO: we assume that server side always returns 60 data points
59 // to feed 1 hour time slots, later this should make to be configurable
60 private static final int NUM_OF_DATA_POINTS = 60;
61
62 private static final int MILLI_CONV_UNIT = 1000;
Jian Li10a20702016-02-01 16:39:51 -080063
64 @Override
65 protected Collection<RequestHandler> createRequestHandlers() {
66 return ImmutableSet.of(
Jian Li1aa07822016-04-19 17:58:02 -070067 new ControlMessageRequest()
Jian Li10a20702016-02-01 16:39:51 -080068 );
69 }
70
Jian Li1aa07822016-04-19 17:58:02 -070071 private final class ControlMessageRequest extends ChartRequestHandler {
Jian Li10a20702016-02-01 16:39:51 -080072
Jian Li1aa07822016-04-19 17:58:02 -070073 private ControlMessageRequest() {
74 super(CPMAN_DATA_REQ, CPMAN_DATA_RESP, CPMANS);
Jian Li10a20702016-02-01 16:39:51 -080075 }
76
77 @Override
Jian Li1aa07822016-04-19 17:58:02 -070078 protected String[] getSeries() {
79 return CONTROL_MESSAGE_METRICS.stream().map(type ->
80 StringUtils.lowerCase(type.name())).toArray(String[]::new);
81 }
Jian Li10a20702016-02-01 16:39:51 -080082
Jian Li1aa07822016-04-19 17:58:02 -070083 @Override
84 protected void populateChart(ChartModel cm, ObjectNode payload) {
85 String uri = string(payload, "devId");
86 if (!Strings.isNullOrEmpty(uri)) {
87 Map<ControlMetricType, Long[]> data = Maps.newHashMap();
88 DeviceId deviceId = DeviceId.deviceId(uri);
89 ClusterService cs = get(ClusterService.class);
90 ControlPlaneMonitorService cpms = get(ControlPlaneMonitorService.class);
91
92 if (cpms.availableResources(CONTROL_MESSAGE).contains(deviceId.toString())) {
93 LocalDateTime ldt = null;
94
95 try {
96 for (ControlMetricType cmt : CONTROL_MESSAGE_METRICS) {
97 ControlLoadSnapshot cls = cpms.getLoad(cs.getLocalNode().id(),
98 cmt, NUM_OF_DATA_POINTS, TimeUnit.MINUTES,
99 Optional.of(deviceId)).get();
100 data.put(cmt, ArrayUtils.toObject(cls.recent()));
101 if (ldt == null) {
102 ldt = new LocalDateTime(cls.time() * MILLI_CONV_UNIT);
103 }
104 }
105
106 for (int i = 0; i < NUM_OF_DATA_POINTS; i++) {
107 Map<String, Long> local = Maps.newHashMap();
108 for (ControlMetricType cmt : CONTROL_MESSAGE_METRICS) {
109 local.put(StringUtils.lowerCase(cmt.name()), data.get(cmt)[i]);
110 }
111
112 local.put(LABEL, ldt.minusMinutes(NUM_OF_DATA_POINTS - i).toDateTime().getMillis());
113
114 populateMetric(cm.addDataPoint(ldt.minusMinutes(NUM_OF_DATA_POINTS - i)
115 .toDateTime().getMillis()), local);
116 }
117
118 } catch (InterruptedException | ExecutionException e) {
119 log.warn(e.getMessage());
120 }
121 }
122 } else {
123 DeviceService ds = get(DeviceService.class);
124 ds.getAvailableDevices();
125 }
126 }
127
128 private void populateAllDevs(ChartModel.DataPoint dataPoint, Map<String, Long> data) {
129
130 }
131
132 private void populateMetric(ChartModel.DataPoint dataPoint,
133 Map<String, Long> data) {
134 data.forEach((k, v) -> dataPoint.data(k, v.doubleValue()));
Jian Li10a20702016-02-01 16:39:51 -0800135 }
136 }
137}