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; |
| 30 | import org.onosproject.net.device.DeviceService; |
Jian Li | 10a2070 | 2016-02-01 16:39:51 -0800 | [diff] [blame] | 31 | import org.onosproject.ui.RequestHandler; |
| 32 | import org.onosproject.ui.UiMessageHandler; |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame^] | 33 | import org.onosproject.ui.chart.ChartModel; |
| 34 | import org.onosproject.ui.chart.ChartRequestHandler; |
| 35 | import org.slf4j.Logger; |
| 36 | import org.slf4j.LoggerFactory; |
Jian Li | 10a2070 | 2016-02-01 16:39:51 -0800 | [diff] [blame] | 37 | |
| 38 | import java.util.Collection; |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame^] | 39 | import java.util.Map; |
| 40 | import java.util.Optional; |
| 41 | import java.util.concurrent.ExecutionException; |
| 42 | import java.util.concurrent.TimeUnit; |
| 43 | |
| 44 | import static org.onosproject.cpman.ControlResource.CONTROL_MESSAGE_METRICS; |
| 45 | import static org.onosproject.cpman.ControlResource.Type.CONTROL_MESSAGE; |
Jian Li | 10a2070 | 2016-02-01 16:39:51 -0800 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * CpmanViewMessageHandler class implementation. |
| 49 | */ |
| 50 | public class CpmanViewMessageHandler extends UiMessageHandler { |
| 51 | |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame^] | 52 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 53 | |
Jian Li | 10a2070 | 2016-02-01 16:39:51 -0800 | [diff] [blame] | 54 | private static final String CPMAN_DATA_REQ = "cpmanDataRequest"; |
| 55 | private static final String CPMAN_DATA_RESP = "cpmanDataResponse"; |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame^] | 56 | private static final String CPMANS = "cpmans"; |
Jian Li | 10a2070 | 2016-02-01 16:39:51 -0800 | [diff] [blame] | 57 | |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame^] | 58 | // TODO: we assume that server side always returns 60 data points |
| 59 | // to feed 1 hour time slots, later this should make to be configurable |
| 60 | private static final int NUM_OF_DATA_POINTS = 60; |
| 61 | |
| 62 | private static final int MILLI_CONV_UNIT = 1000; |
Jian Li | 10a2070 | 2016-02-01 16:39:51 -0800 | [diff] [blame] | 63 | |
| 64 | @Override |
| 65 | protected Collection<RequestHandler> createRequestHandlers() { |
| 66 | return ImmutableSet.of( |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame^] | 67 | new ControlMessageRequest() |
Jian Li | 10a2070 | 2016-02-01 16:39:51 -0800 | [diff] [blame] | 68 | ); |
| 69 | } |
| 70 | |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame^] | 71 | private final class ControlMessageRequest extends ChartRequestHandler { |
Jian Li | 10a2070 | 2016-02-01 16:39:51 -0800 | [diff] [blame] | 72 | |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame^] | 73 | private ControlMessageRequest() { |
| 74 | super(CPMAN_DATA_REQ, CPMAN_DATA_RESP, CPMANS); |
Jian Li | 10a2070 | 2016-02-01 16:39:51 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | @Override |
Jian Li | 1aa0782 | 2016-04-19 17:58:02 -0700 | [diff] [blame^] | 78 | protected String[] getSeries() { |
| 79 | return CONTROL_MESSAGE_METRICS.stream().map(type -> |
| 80 | StringUtils.lowerCase(type.name())).toArray(String[]::new); |
| 81 | } |
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 | @Override |
| 84 | protected void populateChart(ChartModel cm, ObjectNode payload) { |
| 85 | String uri = string(payload, "devId"); |
| 86 | if (!Strings.isNullOrEmpty(uri)) { |
| 87 | Map<ControlMetricType, Long[]> data = Maps.newHashMap(); |
| 88 | DeviceId deviceId = DeviceId.deviceId(uri); |
| 89 | ClusterService cs = get(ClusterService.class); |
| 90 | ControlPlaneMonitorService cpms = get(ControlPlaneMonitorService.class); |
| 91 | |
| 92 | if (cpms.availableResources(CONTROL_MESSAGE).contains(deviceId.toString())) { |
| 93 | LocalDateTime ldt = null; |
| 94 | |
| 95 | try { |
| 96 | for (ControlMetricType cmt : CONTROL_MESSAGE_METRICS) { |
| 97 | ControlLoadSnapshot cls = cpms.getLoad(cs.getLocalNode().id(), |
| 98 | cmt, NUM_OF_DATA_POINTS, TimeUnit.MINUTES, |
| 99 | Optional.of(deviceId)).get(); |
| 100 | data.put(cmt, ArrayUtils.toObject(cls.recent())); |
| 101 | if (ldt == null) { |
| 102 | ldt = new LocalDateTime(cls.time() * MILLI_CONV_UNIT); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | for (int i = 0; i < NUM_OF_DATA_POINTS; i++) { |
| 107 | Map<String, Long> local = Maps.newHashMap(); |
| 108 | for (ControlMetricType cmt : CONTROL_MESSAGE_METRICS) { |
| 109 | local.put(StringUtils.lowerCase(cmt.name()), data.get(cmt)[i]); |
| 110 | } |
| 111 | |
| 112 | local.put(LABEL, ldt.minusMinutes(NUM_OF_DATA_POINTS - i).toDateTime().getMillis()); |
| 113 | |
| 114 | populateMetric(cm.addDataPoint(ldt.minusMinutes(NUM_OF_DATA_POINTS - i) |
| 115 | .toDateTime().getMillis()), local); |
| 116 | } |
| 117 | |
| 118 | } catch (InterruptedException | ExecutionException e) { |
| 119 | log.warn(e.getMessage()); |
| 120 | } |
| 121 | } |
| 122 | } else { |
| 123 | DeviceService ds = get(DeviceService.class); |
| 124 | ds.getAvailableDevices(); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | private void populateAllDevs(ChartModel.DataPoint dataPoint, Map<String, Long> data) { |
| 129 | |
| 130 | } |
| 131 | |
| 132 | private void populateMetric(ChartModel.DataPoint dataPoint, |
| 133 | Map<String, Long> data) { |
| 134 | data.forEach((k, v) -> dataPoint.data(k, v.doubleValue())); |
Jian Li | 10a2070 | 2016-02-01 16:39:51 -0800 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | } |