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 | |
Jian Li | 92b0d2e | 2016-05-04 19:58:46 -0700 | [diff] [blame^] | 18 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Jian Li | 10a2070 | 2016-02-01 16:39:51 -0800 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 20 | import com.google.common.base.Strings; |
Jian Li | 10a2070 | 2016-02-01 16:39:51 -0800 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableSet; |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 22 | import com.google.common.collect.Maps; |
Jian Li | 92b0d2e | 2016-05-04 19:58:46 -0700 | [diff] [blame^] | 23 | import com.google.common.collect.Sets; |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 24 | import org.apache.commons.lang.ArrayUtils; |
| 25 | import org.apache.commons.lang3.StringUtils; |
| 26 | import org.joda.time.LocalDateTime; |
| 27 | import org.onosproject.cluster.ClusterService; |
| 28 | import org.onosproject.cpman.ControlLoadSnapshot; |
| 29 | import org.onosproject.cpman.ControlMetricType; |
| 30 | import org.onosproject.cpman.ControlPlaneMonitorService; |
| 31 | import org.onosproject.net.DeviceId; |
Jian Li | 92b0d2e | 2016-05-04 19:58:46 -0700 | [diff] [blame^] | 32 | import org.onosproject.net.device.DeviceService; |
Jian Li | 10a2070 | 2016-02-01 16:39:51 -0800 | [diff] [blame] | 33 | import org.onosproject.ui.RequestHandler; |
| 34 | import org.onosproject.ui.UiMessageHandler; |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 35 | import org.onosproject.ui.chart.ChartModel; |
| 36 | import org.onosproject.ui.chart.ChartRequestHandler; |
| 37 | import org.slf4j.Logger; |
| 38 | import org.slf4j.LoggerFactory; |
Jian Li | 10a2070 | 2016-02-01 16:39:51 -0800 | [diff] [blame] | 39 | |
| 40 | import java.util.Collection; |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 41 | import java.util.Map; |
| 42 | import java.util.Optional; |
Jian Li | d5be69f | 2016-04-25 18:11:52 -0700 | [diff] [blame] | 43 | import java.util.Set; |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 44 | import java.util.concurrent.ExecutionException; |
| 45 | import java.util.concurrent.TimeUnit; |
Jian Li | d5be69f | 2016-04-25 18:11:52 -0700 | [diff] [blame] | 46 | import java.util.stream.LongStream; |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 47 | |
| 48 | import static org.onosproject.cpman.ControlResource.CONTROL_MESSAGE_METRICS; |
| 49 | import static org.onosproject.cpman.ControlResource.Type.CONTROL_MESSAGE; |
Jian Li | 10a2070 | 2016-02-01 16:39:51 -0800 | [diff] [blame] | 50 | |
| 51 | /** |
Jian Li | d5be69f | 2016-04-25 18:11:52 -0700 | [diff] [blame] | 52 | * Message handler for control plane monitoring view related messages. |
Jian Li | 10a2070 | 2016-02-01 16:39:51 -0800 | [diff] [blame] | 53 | */ |
| 54 | public class CpmanViewMessageHandler extends UiMessageHandler { |
| 55 | |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 56 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 57 | |
Jian Li | 10a2070 | 2016-02-01 16:39:51 -0800 | [diff] [blame] | 58 | private static final String CPMAN_DATA_REQ = "cpmanDataRequest"; |
| 59 | private static final String CPMAN_DATA_RESP = "cpmanDataResponse"; |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 60 | private static final String CPMANS = "cpmans"; |
Jian Li | 10a2070 | 2016-02-01 16:39:51 -0800 | [diff] [blame] | 61 | |
Jian Li | 92b0d2e | 2016-05-04 19:58:46 -0700 | [diff] [blame^] | 62 | private static final String DEVICE_IDS = "deviceIds"; |
| 63 | |
Jian Li | d5be69f | 2016-04-25 18:11:52 -0700 | [diff] [blame] | 64 | // TODO: we assume that server side always returns 20 data points |
Jian Li | 92b0d2e | 2016-05-04 19:58:46 -0700 | [diff] [blame^] | 65 | // to feed 20 minutes time slots, later this should make to be configurable |
Jian Li | d5be69f | 2016-04-25 18:11:52 -0700 | [diff] [blame] | 66 | private static final int NUM_OF_DATA_POINTS = 20; |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 67 | |
| 68 | private static final int MILLI_CONV_UNIT = 1000; |
Jian Li | 10a2070 | 2016-02-01 16:39:51 -0800 | [diff] [blame] | 69 | |
Jian Li | 72f3f10 | 2016-04-28 12:40:57 -0700 | [diff] [blame] | 70 | private static final String TIME_FORMAT = "HH:mm"; |
| 71 | |
Jian Li | d5be69f | 2016-04-25 18:11:52 -0700 | [diff] [blame] | 72 | private long timestamp = 0L; |
| 73 | |
Jian Li | 10a2070 | 2016-02-01 16:39:51 -0800 | [diff] [blame] | 74 | @Override |
| 75 | protected Collection<RequestHandler> createRequestHandlers() { |
| 76 | return ImmutableSet.of( |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 77 | new ControlMessageRequest() |
Jian Li | 10a2070 | 2016-02-01 16:39:51 -0800 | [diff] [blame] | 78 | ); |
| 79 | } |
| 80 | |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 81 | private final class ControlMessageRequest extends ChartRequestHandler { |
Jian Li | 10a2070 | 2016-02-01 16:39:51 -0800 | [diff] [blame] | 82 | |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 83 | private ControlMessageRequest() { |
| 84 | super(CPMAN_DATA_REQ, CPMAN_DATA_RESP, CPMANS); |
Jian Li | 10a2070 | 2016-02-01 16:39:51 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Override |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 88 | protected String[] getSeries() { |
| 89 | return CONTROL_MESSAGE_METRICS.stream().map(type -> |
| 90 | StringUtils.lowerCase(type.name())).toArray(String[]::new); |
| 91 | } |
Jian Li | 10a2070 | 2016-02-01 16:39:51 -0800 | [diff] [blame] | 92 | |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 93 | @Override |
| 94 | protected void populateChart(ChartModel cm, ObjectNode payload) { |
| 95 | String uri = string(payload, "devId"); |
Jian Li | d5be69f | 2016-04-25 18:11:52 -0700 | [diff] [blame] | 96 | ControlPlaneMonitorService cpms = get(ControlPlaneMonitorService.class); |
| 97 | ClusterService cs = get(ClusterService.class); |
Jian Li | 92b0d2e | 2016-05-04 19:58:46 -0700 | [diff] [blame^] | 98 | DeviceService ds = get(DeviceService.class); |
| 99 | |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 100 | if (!Strings.isNullOrEmpty(uri)) { |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 101 | DeviceId deviceId = DeviceId.deviceId(uri); |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 102 | if (cpms.availableResources(CONTROL_MESSAGE).contains(deviceId.toString())) { |
Jian Li | d5be69f | 2016-04-25 18:11:52 -0700 | [diff] [blame] | 103 | Map<ControlMetricType, Long[]> data = generateMatrix(cpms, cs, deviceId); |
| 104 | LocalDateTime ldt = new LocalDateTime(timestamp * MILLI_CONV_UNIT); |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 105 | |
Jian Li | d5be69f | 2016-04-25 18:11:52 -0700 | [diff] [blame] | 106 | populateMetrics(cm, data, ldt, NUM_OF_DATA_POINTS); |
Jian Li | 92b0d2e | 2016-05-04 19:58:46 -0700 | [diff] [blame^] | 107 | |
| 108 | Set<DeviceId> deviceIds = Sets.newHashSet(); |
| 109 | ds.getAvailableDevices().forEach(device -> deviceIds.add(device.id())); |
| 110 | attachDeviceList(cm, deviceIds); |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 111 | } |
| 112 | } else { |
Jian Li | d5be69f | 2016-04-25 18:11:52 -0700 | [diff] [blame] | 113 | Set<String> deviceIds = cpms.availableResources(CONTROL_MESSAGE); |
| 114 | for (String deviceId : deviceIds) { |
| 115 | Map<ControlMetricType, Long> data = |
| 116 | populateDeviceMetrics(cpms, cs, DeviceId.deviceId(deviceId)); |
Jian Li | 72f3f10 | 2016-04-28 12:40:57 -0700 | [diff] [blame] | 117 | Map<String, Object> local = Maps.newHashMap(); |
Jian Li | d5be69f | 2016-04-25 18:11:52 -0700 | [diff] [blame] | 118 | for (ControlMetricType cmt : CONTROL_MESSAGE_METRICS) { |
| 119 | local.put(StringUtils.lowerCase(cmt.name()), data.get(cmt)); |
| 120 | } |
Jian Li | 72f3f10 | 2016-04-28 12:40:57 -0700 | [diff] [blame] | 121 | |
| 122 | local.put(LABEL, deviceId); |
| 123 | populateMetric(cm.addDataPoint(deviceId), local); |
Jian Li | d5be69f | 2016-04-25 18:11:52 -0700 | [diff] [blame] | 124 | } |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
Jian Li | d5be69f | 2016-04-25 18:11:52 -0700 | [diff] [blame] | 128 | 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) { |
| 132 | ControlLoadSnapshot cls; |
| 133 | try { |
| 134 | cls = cpms.getLoad(cs.getLocalNode().id(), |
| 135 | cmt, NUM_OF_DATA_POINTS, TimeUnit.MINUTES, |
| 136 | Optional.of(deviceId)).get(); |
| 137 | data.put(cmt, Math.round(LongStream.of(cls.recent()).average().getAsDouble())); |
| 138 | timestamp = cls.time(); |
| 139 | } catch (InterruptedException | ExecutionException e) { |
| 140 | log.warn(e.getMessage()); |
| 141 | } |
| 142 | } |
| 143 | return data; |
| 144 | } |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 145 | |
Jian Li | d5be69f | 2016-04-25 18:11:52 -0700 | [diff] [blame] | 146 | private Map<ControlMetricType, Long[]> generateMatrix(ControlPlaneMonitorService cpms, |
| 147 | ClusterService cs, DeviceId deviceId) { |
| 148 | Map<ControlMetricType, Long[]> data = Maps.newHashMap(); |
| 149 | for (ControlMetricType cmt : CONTROL_MESSAGE_METRICS) { |
| 150 | ControlLoadSnapshot cls; |
| 151 | try { |
| 152 | cls = cpms.getLoad(cs.getLocalNode().id(), |
| 153 | cmt, NUM_OF_DATA_POINTS, TimeUnit.MINUTES, |
| 154 | Optional.of(deviceId)).get(); |
| 155 | |
| 156 | // TODO: in some cases, the number of returned dataset is |
| 157 | // less than what we expected (expected -1) |
| 158 | // As a workaround, we simply fill the slot with 0 values, |
| 159 | // such a bug should be fixed with updated RRD4J lib... |
| 160 | data.put(cmt, ArrayUtils.toObject(fillData(cls.recent(), NUM_OF_DATA_POINTS))); |
| 161 | timestamp = cls.time(); |
| 162 | } catch (InterruptedException | ExecutionException e) { |
| 163 | log.warn(e.getMessage()); |
| 164 | } |
| 165 | } |
| 166 | return data; |
| 167 | } |
| 168 | |
| 169 | private long[] fillData(long[] origin, int expected) { |
| 170 | if (origin.length == expected) { |
| 171 | return origin; |
| 172 | } else { |
| 173 | long[] filled = new long[expected]; |
| 174 | for (int i = 0; i < expected; i++) { |
| 175 | if (i == 0) { |
| 176 | filled[i] = 0; |
| 177 | } else { |
| 178 | filled[i] = origin[i - 1]; |
| 179 | } |
| 180 | } |
| 181 | return filled; |
| 182 | } |
| 183 | } |
| 184 | |
Jian Li | 72f3f10 | 2016-04-28 12:40:57 -0700 | [diff] [blame] | 185 | private void populateMetrics(ChartModel cm, |
| 186 | Map<ControlMetricType, Long[]> data, |
| 187 | LocalDateTime time, int numOfDp) { |
Jian Li | d5be69f | 2016-04-25 18:11:52 -0700 | [diff] [blame] | 188 | for (int i = 0; i < numOfDp; i++) { |
Jian Li | 72f3f10 | 2016-04-28 12:40:57 -0700 | [diff] [blame] | 189 | Map<String, Object> local = Maps.newHashMap(); |
Jian Li | d5be69f | 2016-04-25 18:11:52 -0700 | [diff] [blame] | 190 | for (ControlMetricType cmt : CONTROL_MESSAGE_METRICS) { |
| 191 | local.put(StringUtils.lowerCase(cmt.name()), data.get(cmt)[i]); |
| 192 | } |
| 193 | |
Jian Li | 72f3f10 | 2016-04-28 12:40:57 -0700 | [diff] [blame] | 194 | String calculated = time.minusMinutes(numOfDp - i).toString(TIME_FORMAT); |
Jian Li | d5be69f | 2016-04-25 18:11:52 -0700 | [diff] [blame] | 195 | |
Jian Li | 72f3f10 | 2016-04-28 12:40:57 -0700 | [diff] [blame] | 196 | local.put(LABEL, calculated); |
| 197 | populateMetric(cm.addDataPoint(calculated), local); |
Jian Li | d5be69f | 2016-04-25 18:11:52 -0700 | [diff] [blame] | 198 | } |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | private void populateMetric(ChartModel.DataPoint dataPoint, |
Jian Li | 72f3f10 | 2016-04-28 12:40:57 -0700 | [diff] [blame] | 202 | Map<String, Object> data) { |
Jian Li | 92b0d2e | 2016-05-04 19:58:46 -0700 | [diff] [blame^] | 203 | data.forEach(dataPoint::data); |
| 204 | } |
| 205 | |
| 206 | private void attachDeviceList(ChartModel cm, Set<DeviceId> deviceIds) { |
| 207 | ArrayNode array = arrayNode(); |
| 208 | deviceIds.forEach(id -> array.add(id.toString())); |
| 209 | cm.addAnnotation(DEVICE_IDS, array); |
Jian Li | 10a2070 | 2016-02-01 16:39:51 -0800 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | } |