Jian Li | 10a2070 | 2016-02-01 16:39:51 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Jian Li | 10a2070 | 2016-02-01 16:39:51 -0800 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | package org.onosproject.cpman.gui; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 19 | import com.google.common.base.Strings; |
Jian Li | 10a2070 | 2016-02-01 16:39:51 -0800 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableSet; |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 21 | import com.google.common.collect.Maps; |
| 22 | import org.apache.commons.lang.ArrayUtils; |
| 23 | import org.apache.commons.lang3.StringUtils; |
| 24 | import org.joda.time.LocalDateTime; |
| 25 | import org.onosproject.cluster.ClusterService; |
| 26 | import org.onosproject.cpman.ControlLoadSnapshot; |
| 27 | import org.onosproject.cpman.ControlMetricType; |
| 28 | import org.onosproject.cpman.ControlPlaneMonitorService; |
| 29 | import org.onosproject.net.DeviceId; |
Jian Li | 10a2070 | 2016-02-01 16:39:51 -0800 | [diff] [blame] | 30 | import org.onosproject.ui.RequestHandler; |
| 31 | import org.onosproject.ui.UiMessageHandler; |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 32 | import org.onosproject.ui.chart.ChartModel; |
| 33 | import org.onosproject.ui.chart.ChartRequestHandler; |
| 34 | import org.slf4j.Logger; |
| 35 | import org.slf4j.LoggerFactory; |
Jian Li | 10a2070 | 2016-02-01 16:39:51 -0800 | [diff] [blame] | 36 | |
| 37 | import java.util.Collection; |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 38 | import java.util.Map; |
| 39 | import java.util.Optional; |
Jian Li | d5be69f | 2016-04-25 18:11:52 -0700 | [diff] [blame] | 40 | import java.util.Set; |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 41 | import java.util.concurrent.ExecutionException; |
| 42 | import java.util.concurrent.TimeUnit; |
Jian Li | d5be69f | 2016-04-25 18:11:52 -0700 | [diff] [blame] | 43 | import java.util.stream.LongStream; |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 44 | |
| 45 | import static org.onosproject.cpman.ControlResource.CONTROL_MESSAGE_METRICS; |
| 46 | import static org.onosproject.cpman.ControlResource.Type.CONTROL_MESSAGE; |
Jian Li | 10a2070 | 2016-02-01 16:39:51 -0800 | [diff] [blame] | 47 | |
| 48 | /** |
Jian Li | d5be69f | 2016-04-25 18:11:52 -0700 | [diff] [blame] | 49 | * Message handler for control plane monitoring view related messages. |
Jian Li | 10a2070 | 2016-02-01 16:39:51 -0800 | [diff] [blame] | 50 | */ |
| 51 | public class CpmanViewMessageHandler extends UiMessageHandler { |
| 52 | |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 53 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 54 | |
Jian Li | 10a2070 | 2016-02-01 16:39:51 -0800 | [diff] [blame] | 55 | private static final String CPMAN_DATA_REQ = "cpmanDataRequest"; |
| 56 | private static final String CPMAN_DATA_RESP = "cpmanDataResponse"; |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 57 | private static final String CPMANS = "cpmans"; |
Jian Li | 10a2070 | 2016-02-01 16:39:51 -0800 | [diff] [blame] | 58 | |
Jian Li | d5be69f | 2016-04-25 18:11:52 -0700 | [diff] [blame] | 59 | // TODO: we assume that server side always returns 20 data points |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 60 | // to feed 1 hour time slots, later this should make to be configurable |
Jian Li | d5be69f | 2016-04-25 18:11:52 -0700 | [diff] [blame] | 61 | private static final int NUM_OF_DATA_POINTS = 20; |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 62 | |
| 63 | private static final int MILLI_CONV_UNIT = 1000; |
Jian Li | 10a2070 | 2016-02-01 16:39:51 -0800 | [diff] [blame] | 64 | |
Jian Li | 72f3f10 | 2016-04-28 12:40:57 -0700 | [diff] [blame^] | 65 | private static final String TIME_FORMAT = "HH:mm"; |
| 66 | |
Jian Li | d5be69f | 2016-04-25 18:11:52 -0700 | [diff] [blame] | 67 | private long timestamp = 0L; |
| 68 | |
Jian Li | 10a2070 | 2016-02-01 16:39:51 -0800 | [diff] [blame] | 69 | @Override |
| 70 | protected Collection<RequestHandler> createRequestHandlers() { |
| 71 | return ImmutableSet.of( |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 72 | new ControlMessageRequest() |
Jian Li | 10a2070 | 2016-02-01 16:39:51 -0800 | [diff] [blame] | 73 | ); |
| 74 | } |
| 75 | |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 76 | private final class ControlMessageRequest extends ChartRequestHandler { |
Jian Li | 10a2070 | 2016-02-01 16:39:51 -0800 | [diff] [blame] | 77 | |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 78 | private ControlMessageRequest() { |
| 79 | super(CPMAN_DATA_REQ, CPMAN_DATA_RESP, CPMANS); |
Jian Li | 10a2070 | 2016-02-01 16:39:51 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @Override |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 83 | protected String[] getSeries() { |
| 84 | return CONTROL_MESSAGE_METRICS.stream().map(type -> |
| 85 | StringUtils.lowerCase(type.name())).toArray(String[]::new); |
| 86 | } |
Jian Li | 10a2070 | 2016-02-01 16:39:51 -0800 | [diff] [blame] | 87 | |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 88 | @Override |
| 89 | protected void populateChart(ChartModel cm, ObjectNode payload) { |
| 90 | String uri = string(payload, "devId"); |
Jian Li | d5be69f | 2016-04-25 18:11:52 -0700 | [diff] [blame] | 91 | ControlPlaneMonitorService cpms = get(ControlPlaneMonitorService.class); |
| 92 | ClusterService cs = get(ClusterService.class); |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 93 | if (!Strings.isNullOrEmpty(uri)) { |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 94 | DeviceId deviceId = DeviceId.deviceId(uri); |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 95 | if (cpms.availableResources(CONTROL_MESSAGE).contains(deviceId.toString())) { |
Jian Li | d5be69f | 2016-04-25 18:11:52 -0700 | [diff] [blame] | 96 | Map<ControlMetricType, Long[]> data = generateMatrix(cpms, cs, deviceId); |
| 97 | LocalDateTime ldt = new LocalDateTime(timestamp * MILLI_CONV_UNIT); |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 98 | |
Jian Li | d5be69f | 2016-04-25 18:11:52 -0700 | [diff] [blame] | 99 | populateMetrics(cm, data, ldt, NUM_OF_DATA_POINTS); |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 100 | } |
| 101 | } else { |
Jian Li | d5be69f | 2016-04-25 18:11:52 -0700 | [diff] [blame] | 102 | 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 Li | 72f3f10 | 2016-04-28 12:40:57 -0700 | [diff] [blame^] | 106 | Map<String, Object> local = Maps.newHashMap(); |
Jian Li | d5be69f | 2016-04-25 18:11:52 -0700 | [diff] [blame] | 107 | for (ControlMetricType cmt : CONTROL_MESSAGE_METRICS) { |
| 108 | local.put(StringUtils.lowerCase(cmt.name()), data.get(cmt)); |
| 109 | } |
Jian Li | 72f3f10 | 2016-04-28 12:40:57 -0700 | [diff] [blame^] | 110 | |
| 111 | local.put(LABEL, deviceId); |
| 112 | populateMetric(cm.addDataPoint(deviceId), local); |
Jian Li | d5be69f | 2016-04-25 18:11:52 -0700 | [diff] [blame] | 113 | } |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
Jian Li | d5be69f | 2016-04-25 18:11:52 -0700 | [diff] [blame] | 117 | 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 Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 134 | |
Jian Li | d5be69f | 2016-04-25 18:11:52 -0700 | [diff] [blame] | 135 | 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 Li | 72f3f10 | 2016-04-28 12:40:57 -0700 | [diff] [blame^] | 174 | private void populateMetrics(ChartModel cm, |
| 175 | Map<ControlMetricType, Long[]> data, |
| 176 | LocalDateTime time, int numOfDp) { |
Jian Li | d5be69f | 2016-04-25 18:11:52 -0700 | [diff] [blame] | 177 | for (int i = 0; i < numOfDp; i++) { |
Jian Li | 72f3f10 | 2016-04-28 12:40:57 -0700 | [diff] [blame^] | 178 | Map<String, Object> local = Maps.newHashMap(); |
Jian Li | d5be69f | 2016-04-25 18:11:52 -0700 | [diff] [blame] | 179 | for (ControlMetricType cmt : CONTROL_MESSAGE_METRICS) { |
| 180 | local.put(StringUtils.lowerCase(cmt.name()), data.get(cmt)[i]); |
| 181 | } |
| 182 | |
Jian Li | 72f3f10 | 2016-04-28 12:40:57 -0700 | [diff] [blame^] | 183 | String calculated = time.minusMinutes(numOfDp - i).toString(TIME_FORMAT); |
Jian Li | d5be69f | 2016-04-25 18:11:52 -0700 | [diff] [blame] | 184 | |
Jian Li | 72f3f10 | 2016-04-28 12:40:57 -0700 | [diff] [blame^] | 185 | local.put(LABEL, calculated); |
| 186 | populateMetric(cm.addDataPoint(calculated), local); |
Jian Li | d5be69f | 2016-04-25 18:11:52 -0700 | [diff] [blame] | 187 | } |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | private void populateMetric(ChartModel.DataPoint dataPoint, |
Jian Li | 72f3f10 | 2016-04-28 12:40:57 -0700 | [diff] [blame^] | 191 | Map<String, Object> data) { |
| 192 | data.forEach((k, v) -> dataPoint.data(k, v)); |
Jian Li | 10a2070 | 2016-02-01 16:39:51 -0800 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | } |