blob: 9263aa84ab84cff48c4cc66a8ab0130ca6ebfa5f [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;
26import org.joda.time.LocalDateTime;
27import org.onosproject.cluster.ClusterService;
Jian Li89eeccd2016-05-06 02:10:33 -070028import org.onosproject.cluster.NodeId;
Jian Li1aa07822016-04-19 17:58:02 -070029import org.onosproject.cpman.ControlLoadSnapshot;
30import org.onosproject.cpman.ControlMetricType;
31import org.onosproject.cpman.ControlPlaneMonitorService;
32import org.onosproject.net.DeviceId;
Jian Li92b0d2e2016-05-04 19:58:46 -070033import org.onosproject.net.device.DeviceService;
Jian Li10a20702016-02-01 16:39:51 -080034import org.onosproject.ui.RequestHandler;
35import org.onosproject.ui.UiMessageHandler;
Jian Li1aa07822016-04-19 17:58:02 -070036import org.onosproject.ui.chart.ChartModel;
37import org.onosproject.ui.chart.ChartRequestHandler;
Jian Li10a20702016-02-01 16:39:51 -080038
39import java.util.Collection;
Jian Li1aa07822016-04-19 17:58:02 -070040import java.util.Map;
41import java.util.Optional;
Jian Lid5be69f2016-04-25 18:11:52 -070042import java.util.Set;
Jian Li1aa07822016-04-19 17:58:02 -070043import java.util.concurrent.TimeUnit;
Jian Lid5be69f2016-04-25 18:11:52 -070044import java.util.stream.LongStream;
Jian Li1aa07822016-04-19 17:58:02 -070045
46import static org.onosproject.cpman.ControlResource.CONTROL_MESSAGE_METRICS;
47import static org.onosproject.cpman.ControlResource.Type.CONTROL_MESSAGE;
Jian Li10a20702016-02-01 16:39:51 -080048
49/**
Jian Lid5be69f2016-04-25 18:11:52 -070050 * Message handler for control plane monitoring view related messages.
Jian Li10a20702016-02-01 16:39:51 -080051 */
52public class CpmanViewMessageHandler extends UiMessageHandler {
53
54 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 Li92b0d2e2016-05-04 19:58:46 -070058 private static final String DEVICE_IDS = "deviceIds";
59
Jian Lid5be69f2016-04-25 18:11:52 -070060 // TODO: we assume that server side always returns 20 data points
Jian Li92b0d2e2016-05-04 19:58:46 -070061 // to feed 20 minutes time slots, later this should make to be configurable
Jian Lid5be69f2016-04-25 18:11:52 -070062 private static final int NUM_OF_DATA_POINTS = 20;
Jian Li1aa07822016-04-19 17:58:02 -070063
64 private static final int MILLI_CONV_UNIT = 1000;
Jian Li10a20702016-02-01 16:39:51 -080065
Jian Li72f3f102016-04-28 12:40:57 -070066 private static final String TIME_FORMAT = "HH:mm";
67
Jian Lid5be69f2016-04-25 18:11:52 -070068 private long timestamp = 0L;
69
Jian Li10a20702016-02-01 16:39:51 -080070 @Override
71 protected Collection<RequestHandler> createRequestHandlers() {
72 return ImmutableSet.of(
Jian Li1aa07822016-04-19 17:58:02 -070073 new ControlMessageRequest()
Jian Li10a20702016-02-01 16:39:51 -080074 );
75 }
76
Jian Li1aa07822016-04-19 17:58:02 -070077 private final class ControlMessageRequest extends ChartRequestHandler {
Jian Li10a20702016-02-01 16:39:51 -080078
Jian Li1aa07822016-04-19 17:58:02 -070079 private ControlMessageRequest() {
80 super(CPMAN_DATA_REQ, CPMAN_DATA_RESP, CPMANS);
Jian Li10a20702016-02-01 16:39:51 -080081 }
82
83 @Override
Jian Li1aa07822016-04-19 17:58:02 -070084 protected String[] getSeries() {
85 return CONTROL_MESSAGE_METRICS.stream().map(type ->
86 StringUtils.lowerCase(type.name())).toArray(String[]::new);
87 }
Jian Li10a20702016-02-01 16:39:51 -080088
Jian Li1aa07822016-04-19 17:58:02 -070089 @Override
90 protected void populateChart(ChartModel cm, ObjectNode payload) {
91 String uri = string(payload, "devId");
Jian Lid5be69f2016-04-25 18:11:52 -070092 ControlPlaneMonitorService cpms = get(ControlPlaneMonitorService.class);
93 ClusterService cs = get(ClusterService.class);
Jian Li92b0d2e2016-05-04 19:58:46 -070094 DeviceService ds = get(DeviceService.class);
Jian Li89eeccd2016-05-06 02:10:33 -070095 NodeId localNodeId = cs.getLocalNode().id();
Jian Li92b0d2e2016-05-04 19:58:46 -070096
Jian Li1aa07822016-04-19 17:58:02 -070097 if (!Strings.isNullOrEmpty(uri)) {
Jian Li1aa07822016-04-19 17:58:02 -070098 DeviceId deviceId = DeviceId.deviceId(uri);
Jian Li89eeccd2016-05-06 02:10:33 -070099 if (cpms.availableResourcesSync(localNodeId, CONTROL_MESSAGE).contains(deviceId.toString())) {
Jian Lid5be69f2016-04-25 18:11:52 -0700100 Map<ControlMetricType, Long[]> data = generateMatrix(cpms, cs, deviceId);
101 LocalDateTime ldt = new LocalDateTime(timestamp * MILLI_CONV_UNIT);
Jian Li1aa07822016-04-19 17:58:02 -0700102
Jian Lid5be69f2016-04-25 18:11:52 -0700103 populateMetrics(cm, data, ldt, NUM_OF_DATA_POINTS);
Jian Li92b0d2e2016-05-04 19:58:46 -0700104
105 Set<DeviceId> deviceIds = Sets.newHashSet();
106 ds.getAvailableDevices().forEach(device -> deviceIds.add(device.id()));
107 attachDeviceList(cm, deviceIds);
Jian Li1aa07822016-04-19 17:58:02 -0700108 }
109 } else {
Jian Li89eeccd2016-05-06 02:10:33 -0700110 Set<String> deviceIds = cpms.availableResourcesSync(localNodeId, CONTROL_MESSAGE);
Jian Lid5be69f2016-04-25 18:11:52 -0700111 for (String deviceId : deviceIds) {
112 Map<ControlMetricType, Long> data =
113 populateDeviceMetrics(cpms, cs, DeviceId.deviceId(deviceId));
Jian Li72f3f102016-04-28 12:40:57 -0700114 Map<String, Object> local = Maps.newHashMap();
Jian Lid5be69f2016-04-25 18:11:52 -0700115 for (ControlMetricType cmt : CONTROL_MESSAGE_METRICS) {
116 local.put(StringUtils.lowerCase(cmt.name()), data.get(cmt));
117 }
Jian Li72f3f102016-04-28 12:40:57 -0700118
119 local.put(LABEL, deviceId);
120 populateMetric(cm.addDataPoint(deviceId), local);
Jian Lid5be69f2016-04-25 18:11:52 -0700121 }
Jian Li1aa07822016-04-19 17:58:02 -0700122 }
123 }
124
Jian Lid5be69f2016-04-25 18:11:52 -0700125 private Map<ControlMetricType, Long> populateDeviceMetrics(ControlPlaneMonitorService cpms,
126 ClusterService cs, DeviceId deviceId) {
127 Map<ControlMetricType, Long> data = Maps.newHashMap();
128 for (ControlMetricType cmt : CONTROL_MESSAGE_METRICS) {
Jian Lib1548ee2016-05-11 09:49:57 -0700129 ControlLoadSnapshot cls = cpms.getLoadSync(cs.getLocalNode().id(),
130 cmt, NUM_OF_DATA_POINTS, TimeUnit.MINUTES, Optional.of(deviceId));
131 data.put(cmt, Math.round(LongStream.of(cls.recent()).average().getAsDouble()));
132 timestamp = cls.time();
Jian Lid5be69f2016-04-25 18:11:52 -0700133 }
134 return data;
135 }
Jian Li1aa07822016-04-19 17:58:02 -0700136
Jian Lid5be69f2016-04-25 18:11:52 -0700137 private Map<ControlMetricType, Long[]> generateMatrix(ControlPlaneMonitorService cpms,
138 ClusterService cs, DeviceId deviceId) {
139 Map<ControlMetricType, Long[]> data = Maps.newHashMap();
140 for (ControlMetricType cmt : CONTROL_MESSAGE_METRICS) {
Jian Lif248aa22016-05-09 10:33:02 -0700141 ControlLoadSnapshot cls = cpms.getLoadSync(cs.getLocalNode().id(),
142 cmt, NUM_OF_DATA_POINTS, TimeUnit.MINUTES, Optional.of(deviceId));
Jian Lid5be69f2016-04-25 18:11:52 -0700143
Jian Lif248aa22016-05-09 10:33:02 -0700144 // TODO: in some cases, the number of returned data set is
145 // less than what we expected (expected -1)
146 // As a workaround, we simply fill the slot with 0 values,
147 // such a bug should be fixed with updated RRD4J lib...
148 data.put(cmt, ArrayUtils.toObject(fillData(cls.recent(), NUM_OF_DATA_POINTS)));
149 timestamp = cls.time();
Jian Lid5be69f2016-04-25 18:11:52 -0700150 }
151 return data;
152 }
153
154 private long[] fillData(long[] origin, int expected) {
155 if (origin.length == expected) {
156 return origin;
157 } else {
158 long[] filled = new long[expected];
159 for (int i = 0; i < expected; i++) {
160 if (i == 0) {
161 filled[i] = 0;
162 } else {
163 filled[i] = origin[i - 1];
164 }
165 }
166 return filled;
167 }
168 }
169
Jian Li72f3f102016-04-28 12:40:57 -0700170 private void populateMetrics(ChartModel cm,
171 Map<ControlMetricType, Long[]> data,
172 LocalDateTime time, int numOfDp) {
Jian Lid5be69f2016-04-25 18:11:52 -0700173 for (int i = 0; i < numOfDp; i++) {
Jian Li72f3f102016-04-28 12:40:57 -0700174 Map<String, Object> local = Maps.newHashMap();
Jian Lid5be69f2016-04-25 18:11:52 -0700175 for (ControlMetricType cmt : CONTROL_MESSAGE_METRICS) {
176 local.put(StringUtils.lowerCase(cmt.name()), data.get(cmt)[i]);
177 }
178
Jian Li72f3f102016-04-28 12:40:57 -0700179 String calculated = time.minusMinutes(numOfDp - i).toString(TIME_FORMAT);
Jian Lid5be69f2016-04-25 18:11:52 -0700180
Jian Li72f3f102016-04-28 12:40:57 -0700181 local.put(LABEL, calculated);
182 populateMetric(cm.addDataPoint(calculated), local);
Jian Lid5be69f2016-04-25 18:11:52 -0700183 }
Jian Li1aa07822016-04-19 17:58:02 -0700184 }
185
186 private void populateMetric(ChartModel.DataPoint dataPoint,
Jian Li72f3f102016-04-28 12:40:57 -0700187 Map<String, Object> data) {
Jian Li92b0d2e2016-05-04 19:58:46 -0700188 data.forEach(dataPoint::data);
189 }
190
191 private void attachDeviceList(ChartModel cm, Set<DeviceId> deviceIds) {
192 ArrayNode array = arrayNode();
193 deviceIds.forEach(id -> array.add(id.toString()));
194 cm.addAnnotation(DEVICE_IDS, array);
Jian Li10a20702016-02-01 16:39:51 -0800195 }
196 }
197}