blob: 5a1141992697b1419352b015433ab8c26253232a [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;
Jian Li10a20702016-02-01 16:39:51 -080030import org.onosproject.ui.RequestHandler;
31import org.onosproject.ui.UiMessageHandler;
Jian Li1aa07822016-04-19 17:58:02 -070032import org.onosproject.ui.chart.ChartModel;
33import org.onosproject.ui.chart.ChartRequestHandler;
34import org.slf4j.Logger;
35import org.slf4j.LoggerFactory;
Jian Li10a20702016-02-01 16:39:51 -080036
37import java.util.Collection;
Jian Li1aa07822016-04-19 17:58:02 -070038import java.util.Map;
39import java.util.Optional;
Jian Lid5be69f2016-04-25 18:11:52 -070040import java.util.Set;
Jian Li1aa07822016-04-19 17:58:02 -070041import java.util.concurrent.ExecutionException;
42import java.util.concurrent.TimeUnit;
Jian Lid5be69f2016-04-25 18:11:52 -070043import java.util.stream.LongStream;
Jian Li1aa07822016-04-19 17:58:02 -070044
45import static org.onosproject.cpman.ControlResource.CONTROL_MESSAGE_METRICS;
46import static org.onosproject.cpman.ControlResource.Type.CONTROL_MESSAGE;
Jian Li10a20702016-02-01 16:39:51 -080047
48/**
Jian Lid5be69f2016-04-25 18:11:52 -070049 * Message handler for control plane monitoring view related messages.
Jian Li10a20702016-02-01 16:39:51 -080050 */
51public class CpmanViewMessageHandler extends UiMessageHandler {
52
Jian Li1aa07822016-04-19 17:58:02 -070053 private final Logger log = LoggerFactory.getLogger(getClass());
54
Jian Li10a20702016-02-01 16:39:51 -080055 private static final String CPMAN_DATA_REQ = "cpmanDataRequest";
56 private static final String CPMAN_DATA_RESP = "cpmanDataResponse";
Jian Li1aa07822016-04-19 17:58:02 -070057 private static final String CPMANS = "cpmans";
Jian Li10a20702016-02-01 16:39:51 -080058
Jian Lid5be69f2016-04-25 18:11:52 -070059 // TODO: we assume that server side always returns 20 data points
Jian Li1aa07822016-04-19 17:58:02 -070060 // to feed 1 hour time slots, later this should make to be configurable
Jian Lid5be69f2016-04-25 18:11:52 -070061 private static final int NUM_OF_DATA_POINTS = 20;
Jian Li1aa07822016-04-19 17:58:02 -070062
63 private static final int MILLI_CONV_UNIT = 1000;
Jian Li10a20702016-02-01 16:39:51 -080064
Jian Li72f3f102016-04-28 12:40:57 -070065 private static final String TIME_FORMAT = "HH:mm";
66
Jian Lid5be69f2016-04-25 18:11:52 -070067 private long timestamp = 0L;
68
Jian Li10a20702016-02-01 16:39:51 -080069 @Override
70 protected Collection<RequestHandler> createRequestHandlers() {
71 return ImmutableSet.of(
Jian Li1aa07822016-04-19 17:58:02 -070072 new ControlMessageRequest()
Jian Li10a20702016-02-01 16:39:51 -080073 );
74 }
75
Jian Li1aa07822016-04-19 17:58:02 -070076 private final class ControlMessageRequest extends ChartRequestHandler {
Jian Li10a20702016-02-01 16:39:51 -080077
Jian Li1aa07822016-04-19 17:58:02 -070078 private ControlMessageRequest() {
79 super(CPMAN_DATA_REQ, CPMAN_DATA_RESP, CPMANS);
Jian Li10a20702016-02-01 16:39:51 -080080 }
81
82 @Override
Jian Li1aa07822016-04-19 17:58:02 -070083 protected String[] getSeries() {
84 return CONTROL_MESSAGE_METRICS.stream().map(type ->
85 StringUtils.lowerCase(type.name())).toArray(String[]::new);
86 }
Jian Li10a20702016-02-01 16:39:51 -080087
Jian Li1aa07822016-04-19 17:58:02 -070088 @Override
89 protected void populateChart(ChartModel cm, ObjectNode payload) {
90 String uri = string(payload, "devId");
Jian Lid5be69f2016-04-25 18:11:52 -070091 ControlPlaneMonitorService cpms = get(ControlPlaneMonitorService.class);
92 ClusterService cs = get(ClusterService.class);
Jian Li1aa07822016-04-19 17:58:02 -070093 if (!Strings.isNullOrEmpty(uri)) {
Jian Li1aa07822016-04-19 17:58:02 -070094 DeviceId deviceId = DeviceId.deviceId(uri);
Jian Li1aa07822016-04-19 17:58:02 -070095 if (cpms.availableResources(CONTROL_MESSAGE).contains(deviceId.toString())) {
Jian Lid5be69f2016-04-25 18:11:52 -070096 Map<ControlMetricType, Long[]> data = generateMatrix(cpms, cs, deviceId);
97 LocalDateTime ldt = new LocalDateTime(timestamp * MILLI_CONV_UNIT);
Jian Li1aa07822016-04-19 17:58:02 -070098
Jian Lid5be69f2016-04-25 18:11:52 -070099 populateMetrics(cm, data, ldt, NUM_OF_DATA_POINTS);
Jian Li1aa07822016-04-19 17:58:02 -0700100 }
101 } else {
Jian Lid5be69f2016-04-25 18:11:52 -0700102 Set<String> deviceIds = cpms.availableResources(CONTROL_MESSAGE);
103 for (String deviceId : deviceIds) {
104 Map<ControlMetricType, Long> data =
105 populateDeviceMetrics(cpms, cs, DeviceId.deviceId(deviceId));
Jian Li72f3f102016-04-28 12:40:57 -0700106 Map<String, Object> local = Maps.newHashMap();
Jian Lid5be69f2016-04-25 18:11:52 -0700107 for (ControlMetricType cmt : CONTROL_MESSAGE_METRICS) {
108 local.put(StringUtils.lowerCase(cmt.name()), data.get(cmt));
109 }
Jian Li72f3f102016-04-28 12:40:57 -0700110
111 local.put(LABEL, deviceId);
112 populateMetric(cm.addDataPoint(deviceId), local);
Jian Lid5be69f2016-04-25 18:11:52 -0700113 }
Jian Li1aa07822016-04-19 17:58:02 -0700114 }
115 }
116
Jian Lid5be69f2016-04-25 18:11:52 -0700117 private Map<ControlMetricType, Long> populateDeviceMetrics(ControlPlaneMonitorService cpms,
118 ClusterService cs, DeviceId deviceId) {
119 Map<ControlMetricType, Long> data = Maps.newHashMap();
120 for (ControlMetricType cmt : CONTROL_MESSAGE_METRICS) {
121 ControlLoadSnapshot cls;
122 try {
123 cls = cpms.getLoad(cs.getLocalNode().id(),
124 cmt, NUM_OF_DATA_POINTS, TimeUnit.MINUTES,
125 Optional.of(deviceId)).get();
126 data.put(cmt, Math.round(LongStream.of(cls.recent()).average().getAsDouble()));
127 timestamp = cls.time();
128 } catch (InterruptedException | ExecutionException e) {
129 log.warn(e.getMessage());
130 }
131 }
132 return data;
133 }
Jian Li1aa07822016-04-19 17:58:02 -0700134
Jian Lid5be69f2016-04-25 18:11:52 -0700135 private Map<ControlMetricType, Long[]> generateMatrix(ControlPlaneMonitorService cpms,
136 ClusterService cs, DeviceId deviceId) {
137 Map<ControlMetricType, Long[]> data = Maps.newHashMap();
138 for (ControlMetricType cmt : CONTROL_MESSAGE_METRICS) {
139 ControlLoadSnapshot cls;
140 try {
141 cls = cpms.getLoad(cs.getLocalNode().id(),
142 cmt, NUM_OF_DATA_POINTS, TimeUnit.MINUTES,
143 Optional.of(deviceId)).get();
144
145 // TODO: in some cases, the number of returned dataset is
146 // less than what we expected (expected -1)
147 // As a workaround, we simply fill the slot with 0 values,
148 // such a bug should be fixed with updated RRD4J lib...
149 data.put(cmt, ArrayUtils.toObject(fillData(cls.recent(), NUM_OF_DATA_POINTS)));
150 timestamp = cls.time();
151 } catch (InterruptedException | ExecutionException e) {
152 log.warn(e.getMessage());
153 }
154 }
155 return data;
156 }
157
158 private long[] fillData(long[] origin, int expected) {
159 if (origin.length == expected) {
160 return origin;
161 } else {
162 long[] filled = new long[expected];
163 for (int i = 0; i < expected; i++) {
164 if (i == 0) {
165 filled[i] = 0;
166 } else {
167 filled[i] = origin[i - 1];
168 }
169 }
170 return filled;
171 }
172 }
173
Jian Li72f3f102016-04-28 12:40:57 -0700174 private void populateMetrics(ChartModel cm,
175 Map<ControlMetricType, Long[]> data,
176 LocalDateTime time, int numOfDp) {
Jian Lid5be69f2016-04-25 18:11:52 -0700177 for (int i = 0; i < numOfDp; i++) {
Jian Li72f3f102016-04-28 12:40:57 -0700178 Map<String, Object> local = Maps.newHashMap();
Jian Lid5be69f2016-04-25 18:11:52 -0700179 for (ControlMetricType cmt : CONTROL_MESSAGE_METRICS) {
180 local.put(StringUtils.lowerCase(cmt.name()), data.get(cmt)[i]);
181 }
182
Jian Li72f3f102016-04-28 12:40:57 -0700183 String calculated = time.minusMinutes(numOfDp - i).toString(TIME_FORMAT);
Jian Lid5be69f2016-04-25 18:11:52 -0700184
Jian Li72f3f102016-04-28 12:40:57 -0700185 local.put(LABEL, calculated);
186 populateMetric(cm.addDataPoint(calculated), local);
Jian Lid5be69f2016-04-25 18:11:52 -0700187 }
Jian Li1aa07822016-04-19 17:58:02 -0700188 }
189
190 private void populateMetric(ChartModel.DataPoint dataPoint,
Jian Li72f3f102016-04-28 12:40:57 -0700191 Map<String, Object> data) {
192 data.forEach((k, v) -> dataPoint.data(k, v));
Jian Li10a20702016-02-01 16:39:51 -0800193 }
194 }
195}