blob: 8003933fab5dec4d7c0e2d050ca485f4fe9999b6 [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 Lid5be69f2016-04-25 18:11:52 -070065 private long timestamp = 0L;
66
Jian Li10a20702016-02-01 16:39:51 -080067 @Override
68 protected Collection<RequestHandler> createRequestHandlers() {
69 return ImmutableSet.of(
Jian Li1aa07822016-04-19 17:58:02 -070070 new ControlMessageRequest()
Jian Li10a20702016-02-01 16:39:51 -080071 );
72 }
73
Jian Li1aa07822016-04-19 17:58:02 -070074 private final class ControlMessageRequest extends ChartRequestHandler {
Jian Li10a20702016-02-01 16:39:51 -080075
Jian Li1aa07822016-04-19 17:58:02 -070076 private ControlMessageRequest() {
77 super(CPMAN_DATA_REQ, CPMAN_DATA_RESP, CPMANS);
Jian Li10a20702016-02-01 16:39:51 -080078 }
79
80 @Override
Jian Li1aa07822016-04-19 17:58:02 -070081 protected String[] getSeries() {
82 return CONTROL_MESSAGE_METRICS.stream().map(type ->
83 StringUtils.lowerCase(type.name())).toArray(String[]::new);
84 }
Jian Li10a20702016-02-01 16:39:51 -080085
Jian Li1aa07822016-04-19 17:58:02 -070086 @Override
87 protected void populateChart(ChartModel cm, ObjectNode payload) {
88 String uri = string(payload, "devId");
Jian Lid5be69f2016-04-25 18:11:52 -070089 ControlPlaneMonitorService cpms = get(ControlPlaneMonitorService.class);
90 ClusterService cs = get(ClusterService.class);
Jian Li1aa07822016-04-19 17:58:02 -070091 if (!Strings.isNullOrEmpty(uri)) {
Jian Li1aa07822016-04-19 17:58:02 -070092 DeviceId deviceId = DeviceId.deviceId(uri);
Jian Li1aa07822016-04-19 17:58:02 -070093 if (cpms.availableResources(CONTROL_MESSAGE).contains(deviceId.toString())) {
Jian Lid5be69f2016-04-25 18:11:52 -070094 Map<ControlMetricType, Long[]> data = generateMatrix(cpms, cs, deviceId);
95 LocalDateTime ldt = new LocalDateTime(timestamp * MILLI_CONV_UNIT);
Jian Li1aa07822016-04-19 17:58:02 -070096
Jian Lid5be69f2016-04-25 18:11:52 -070097 populateMetrics(cm, data, ldt, NUM_OF_DATA_POINTS);
Jian Li1aa07822016-04-19 17:58:02 -070098 }
99 } else {
Jian Lid5be69f2016-04-25 18:11:52 -0700100 Set<String> deviceIds = cpms.availableResources(CONTROL_MESSAGE);
101 for (String deviceId : deviceIds) {
102 Map<ControlMetricType, Long> data =
103 populateDeviceMetrics(cpms, cs, DeviceId.deviceId(deviceId));
104 Map<String, Long> local = Maps.newHashMap();
105 for (ControlMetricType cmt : CONTROL_MESSAGE_METRICS) {
106 local.put(StringUtils.lowerCase(cmt.name()), data.get(cmt));
107 }
108 // TODO: need to find a way to present device id using long type
109 String shortId = StringUtils.substring(deviceId,
110 deviceId.length() - 2, deviceId.length());
111 local.put(LABEL, Long.valueOf(shortId));
112 populateMetric(cm.addDataPoint(Long.valueOf(shortId)), local);
113 }
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
174 private void populateMetrics(ChartModel cm, Map<ControlMetricType,
175 Long[]> data, LocalDateTime time, int numOfDp) {
176 for (int i = 0; i < numOfDp; i++) {
177 Map<String, Long> local = Maps.newHashMap();
178 for (ControlMetricType cmt : CONTROL_MESSAGE_METRICS) {
179 local.put(StringUtils.lowerCase(cmt.name()), data.get(cmt)[i]);
180 }
181
182 local.put(LABEL, time.minusMinutes(numOfDp - i).toDateTime().getMillis());
183
184 populateMetric(cm.addDataPoint(time.minusMinutes(numOfDp - i)
185 .toDateTime().getMillis()), local);
186 }
Jian Li1aa07822016-04-19 17:58:02 -0700187 }
188
189 private void populateMetric(ChartModel.DataPoint dataPoint,
190 Map<String, Long> data) {
191 data.forEach((k, v) -> dataPoint.data(k, v.doubleValue()));
Jian Li10a20702016-02-01 16:39:51 -0800192 }
193 }
194}