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