blob: b3c1038f5a5a4bf022505ecca2e9d2598796a5ab [file] [log] [blame]
Jian Li10a20702016-02-01 16:39:51 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
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
Jian Li92b0d2e2016-05-04 19:58:46 -070018import com.fasterxml.jackson.databind.node.ArrayNode;
Jian Li10a20702016-02-01 16:39:51 -080019import com.fasterxml.jackson.databind.node.ObjectNode;
Jian Li1aa07822016-04-19 17:58:02 -070020import com.google.common.base.Strings;
Jian Li10a20702016-02-01 16:39:51 -080021import com.google.common.collect.ImmutableSet;
Jian Li1aa07822016-04-19 17:58:02 -070022import com.google.common.collect.Maps;
Jian Li92b0d2e2016-05-04 19:58:46 -070023import com.google.common.collect.Sets;
Jian Li1aa07822016-04-19 17:58:02 -070024import org.apache.commons.lang.ArrayUtils;
25import org.apache.commons.lang3.StringUtils;
Jian Li1aa07822016-04-19 17:58:02 -070026import org.onosproject.cluster.ClusterService;
Jian Li89eeccd2016-05-06 02:10:33 -070027import org.onosproject.cluster.NodeId;
Jian Li1aa07822016-04-19 17:58:02 -070028import org.onosproject.cpman.ControlLoadSnapshot;
29import org.onosproject.cpman.ControlMetricType;
30import org.onosproject.cpman.ControlPlaneMonitorService;
31import org.onosproject.net.DeviceId;
Jian Li92b0d2e2016-05-04 19:58:46 -070032import org.onosproject.net.device.DeviceService;
Jian Li10a20702016-02-01 16:39:51 -080033import org.onosproject.ui.RequestHandler;
34import org.onosproject.ui.UiMessageHandler;
Jian Li1aa07822016-04-19 17:58:02 -070035import org.onosproject.ui.chart.ChartModel;
36import org.onosproject.ui.chart.ChartRequestHandler;
Jian Li10a20702016-02-01 16:39:51 -080037
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -070038import java.time.Instant;
39import java.time.LocalDateTime;
40import java.time.format.DateTimeFormatter;
Jian Li10a20702016-02-01 16:39:51 -080041import java.util.Collection;
Jian Li1aa07822016-04-19 17:58:02 -070042import java.util.Map;
43import java.util.Optional;
Jian Lid5be69f2016-04-25 18:11:52 -070044import java.util.Set;
Jian Li1aa07822016-04-19 17:58:02 -070045import java.util.concurrent.TimeUnit;
Jian Lid5be69f2016-04-25 18:11:52 -070046import java.util.stream.LongStream;
Jian Li1aa07822016-04-19 17:58:02 -070047
48import static org.onosproject.cpman.ControlResource.CONTROL_MESSAGE_METRICS;
49import static org.onosproject.cpman.ControlResource.Type.CONTROL_MESSAGE;
Jian Li10a20702016-02-01 16:39:51 -080050
51/**
Jian Lid5be69f2016-04-25 18:11:52 -070052 * Message handler for control plane monitoring view related messages.
Jian Li10a20702016-02-01 16:39:51 -080053 */
54public class CpmanViewMessageHandler extends UiMessageHandler {
55
56 private static final String CPMAN_DATA_REQ = "cpmanDataRequest";
57 private static final String CPMAN_DATA_RESP = "cpmanDataResponse";
Jian Li1aa07822016-04-19 17:58:02 -070058 private static final String CPMANS = "cpmans";
Jian Li10a20702016-02-01 16:39:51 -080059
Jian Li92b0d2e2016-05-04 19:58:46 -070060 private static final String DEVICE_IDS = "deviceIds";
61
Jian Lid5be69f2016-04-25 18:11:52 -070062 // TODO: we assume that server side always returns 20 data points
Jian Li92b0d2e2016-05-04 19:58:46 -070063 // to feed 20 minutes time slots, later this should make to be configurable
Jian Lid5be69f2016-04-25 18:11:52 -070064 private static final int NUM_OF_DATA_POINTS = 20;
Jian Li1aa07822016-04-19 17:58:02 -070065
66 private static final int MILLI_CONV_UNIT = 1000;
Jian Li10a20702016-02-01 16:39:51 -080067
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -070068 private static final DateTimeFormatter TIME_FORMAT = DateTimeFormatter.ISO_LOCAL_TIME;
Jian Li72f3f102016-04-28 12:40:57 -070069
Jian Lid5be69f2016-04-25 18:11:52 -070070 private long timestamp = 0L;
71
Jian Li10a20702016-02-01 16:39:51 -080072 @Override
73 protected Collection<RequestHandler> createRequestHandlers() {
74 return ImmutableSet.of(
Jian Li1aa07822016-04-19 17:58:02 -070075 new ControlMessageRequest()
Jian Li10a20702016-02-01 16:39:51 -080076 );
77 }
78
Jian Li1aa07822016-04-19 17:58:02 -070079 private final class ControlMessageRequest extends ChartRequestHandler {
Jian Li10a20702016-02-01 16:39:51 -080080
Jian Li1aa07822016-04-19 17:58:02 -070081 private ControlMessageRequest() {
82 super(CPMAN_DATA_REQ, CPMAN_DATA_RESP, CPMANS);
Jian Li10a20702016-02-01 16:39:51 -080083 }
84
85 @Override
Jian Li1aa07822016-04-19 17:58:02 -070086 protected String[] getSeries() {
87 return CONTROL_MESSAGE_METRICS.stream().map(type ->
88 StringUtils.lowerCase(type.name())).toArray(String[]::new);
89 }
Jian Li10a20702016-02-01 16:39:51 -080090
Jian Li1aa07822016-04-19 17:58:02 -070091 @Override
92 protected void populateChart(ChartModel cm, ObjectNode payload) {
93 String uri = string(payload, "devId");
Jian Lid5be69f2016-04-25 18:11:52 -070094 ControlPlaneMonitorService cpms = get(ControlPlaneMonitorService.class);
95 ClusterService cs = get(ClusterService.class);
Jian Li92b0d2e2016-05-04 19:58:46 -070096 DeviceService ds = get(DeviceService.class);
Jian Li89eeccd2016-05-06 02:10:33 -070097 NodeId localNodeId = cs.getLocalNode().id();
Jian Li92b0d2e2016-05-04 19:58:46 -070098
Jian Li1aa07822016-04-19 17:58:02 -070099 if (!Strings.isNullOrEmpty(uri)) {
Jian Li1aa07822016-04-19 17:58:02 -0700100 DeviceId deviceId = DeviceId.deviceId(uri);
Jian Li89eeccd2016-05-06 02:10:33 -0700101 if (cpms.availableResourcesSync(localNodeId, CONTROL_MESSAGE).contains(deviceId.toString())) {
Jian Lid5be69f2016-04-25 18:11:52 -0700102 Map<ControlMetricType, Long[]> data = generateMatrix(cpms, cs, deviceId);
Sean Condon436c60a2021-01-01 14:23:29 +0000103 @SuppressWarnings("FromTemporalAccessor")
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -0700104 LocalDateTime ldt = LocalDateTime.from(Instant.ofEpochMilli(timestamp * MILLI_CONV_UNIT));
Jian Li1aa07822016-04-19 17:58:02 -0700105
Jian Lid5be69f2016-04-25 18:11:52 -0700106 populateMetrics(cm, data, ldt, NUM_OF_DATA_POINTS);
Jian Li92b0d2e2016-05-04 19:58:46 -0700107
108 Set<DeviceId> deviceIds = Sets.newHashSet();
109 ds.getAvailableDevices().forEach(device -> deviceIds.add(device.id()));
110 attachDeviceList(cm, deviceIds);
Jian Li1aa07822016-04-19 17:58:02 -0700111 }
112 } else {
Jian Li89eeccd2016-05-06 02:10:33 -0700113 Set<String> deviceIds = cpms.availableResourcesSync(localNodeId, CONTROL_MESSAGE);
Jian Lid5be69f2016-04-25 18:11:52 -0700114 for (String deviceId : deviceIds) {
115 Map<ControlMetricType, Long> data =
116 populateDeviceMetrics(cpms, cs, DeviceId.deviceId(deviceId));
Jian Li72f3f102016-04-28 12:40:57 -0700117 Map<String, Object> local = Maps.newHashMap();
Jian Lid5be69f2016-04-25 18:11:52 -0700118 for (ControlMetricType cmt : CONTROL_MESSAGE_METRICS) {
119 local.put(StringUtils.lowerCase(cmt.name()), data.get(cmt));
120 }
Jian Li72f3f102016-04-28 12:40:57 -0700121
122 local.put(LABEL, deviceId);
123 populateMetric(cm.addDataPoint(deviceId), local);
Jian Lid5be69f2016-04-25 18:11:52 -0700124 }
Jian Li1aa07822016-04-19 17:58:02 -0700125 }
126 }
127
Jian Lid5be69f2016-04-25 18:11:52 -0700128 private Map<ControlMetricType, Long> populateDeviceMetrics(ControlPlaneMonitorService cpms,
129 ClusterService cs, DeviceId deviceId) {
130 Map<ControlMetricType, Long> data = Maps.newHashMap();
131 for (ControlMetricType cmt : CONTROL_MESSAGE_METRICS) {
Jian Lib1548ee2016-05-11 09:49:57 -0700132 ControlLoadSnapshot cls = cpms.getLoadSync(cs.getLocalNode().id(),
133 cmt, NUM_OF_DATA_POINTS, TimeUnit.MINUTES, Optional.of(deviceId));
134 data.put(cmt, Math.round(LongStream.of(cls.recent()).average().getAsDouble()));
135 timestamp = cls.time();
Jian Lid5be69f2016-04-25 18:11:52 -0700136 }
137 return data;
138 }
Jian Li1aa07822016-04-19 17:58:02 -0700139
Jian Lid5be69f2016-04-25 18:11:52 -0700140 private Map<ControlMetricType, Long[]> generateMatrix(ControlPlaneMonitorService cpms,
141 ClusterService cs, DeviceId deviceId) {
142 Map<ControlMetricType, Long[]> data = Maps.newHashMap();
143 for (ControlMetricType cmt : CONTROL_MESSAGE_METRICS) {
Jian Lif248aa22016-05-09 10:33:02 -0700144 ControlLoadSnapshot cls = cpms.getLoadSync(cs.getLocalNode().id(),
145 cmt, NUM_OF_DATA_POINTS, TimeUnit.MINUTES, Optional.of(deviceId));
Jian Lid5be69f2016-04-25 18:11:52 -0700146
Jian Lif248aa22016-05-09 10:33:02 -0700147 // TODO: in some cases, the number of returned data set is
148 // less than what we expected (expected -1)
149 // As a workaround, we simply fill the slot with 0 values,
150 // such a bug should be fixed with updated RRD4J lib...
151 data.put(cmt, ArrayUtils.toObject(fillData(cls.recent(), NUM_OF_DATA_POINTS)));
152 timestamp = cls.time();
Jian Lid5be69f2016-04-25 18:11:52 -0700153 }
154 return data;
155 }
156
157 private long[] fillData(long[] origin, int expected) {
158 if (origin.length == expected) {
159 return origin;
160 } else {
161 long[] filled = new long[expected];
162 for (int i = 0; i < expected; i++) {
163 if (i == 0) {
164 filled[i] = 0;
165 } else {
166 filled[i] = origin[i - 1];
167 }
168 }
169 return filled;
170 }
171 }
172
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -0700173 // FIXME using local time in timestamps likely to be sign of problem
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
Ray Milkey3717e602018-02-01 13:49:47 -0800183 String calculated = time.minusMinutes((long) numOfDp - i).format(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) {
Jian Li92b0d2e2016-05-04 19:58:46 -0700192 data.forEach(dataPoint::data);
193 }
194
195 private void attachDeviceList(ChartModel cm, Set<DeviceId> deviceIds) {
196 ArrayNode array = arrayNode();
197 deviceIds.forEach(id -> array.add(id.toString()));
198 cm.addAnnotation(DEVICE_IDS, array);
Jian Li10a20702016-02-01 16:39:51 -0800199 }
200 }
201}