Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 1 | /* |
Jian Li | e044d1a | 2016-01-25 09:01:20 -0800 | [diff] [blame] | 2 | * Copyright 2015-2016 Open Networking Laboratory |
Jian Li | 6080432 | 2015-12-02 14:46:31 -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 | */ |
Jian Li | 6b86a76 | 2016-01-29 09:30:40 -0800 | [diff] [blame] | 16 | package org.onosproject.cpman.impl; |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 17 | |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 19 | import com.google.common.collect.Maps; |
| 20 | import com.google.common.collect.Sets; |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Activate; |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Component; |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Deactivate; |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Service; |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 27 | import org.onosproject.cluster.ClusterService; |
| 28 | import org.onosproject.cluster.ControllerNode; |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 29 | import org.onosproject.cluster.NodeId; |
Jian Li | 6b86a76 | 2016-01-29 09:30:40 -0800 | [diff] [blame] | 30 | import org.onosproject.cpman.ControlLoad; |
| 31 | import org.onosproject.cpman.ControlMetric; |
| 32 | import org.onosproject.cpman.ControlMetricType; |
| 33 | import org.onosproject.cpman.ControlPlaneMonitorService; |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 34 | import org.onosproject.cpman.MetricsDatabase; |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 35 | import org.onosproject.net.DeviceId; |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 36 | import org.slf4j.Logger; |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 37 | import org.slf4j.LoggerFactory; |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 38 | |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 39 | import java.util.Map; |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 40 | import java.util.Optional; |
Jian Li | c5cb4a1 | 2016-02-03 23:24:42 -0800 | [diff] [blame] | 41 | import java.util.Set; |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 42 | import java.util.stream.Collectors; |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 43 | |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 44 | import static org.onosproject.cpman.ControlResource.*; |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Control plane monitoring service class. |
| 48 | */ |
| 49 | @Component(immediate = true) |
| 50 | @Service |
| 51 | public class ControlPlaneMonitor implements ControlPlaneMonitorService { |
| 52 | |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 53 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 54 | private MetricsDatabase cpuMetrics; |
| 55 | private MetricsDatabase memoryMetrics; |
| 56 | private Map<DeviceId, MetricsDatabase> controlMessageMap; |
| 57 | private Map<String, MetricsDatabase> diskMetricsMap; |
| 58 | private Map<String, MetricsDatabase> networkMetricsMap; |
| 59 | |
| 60 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 61 | protected ClusterService clusterService; |
| 62 | |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 63 | private static final Set RESOURCE_TYPE_SET = |
| 64 | ImmutableSet.of(Type.CONTROL_MESSAGE, Type.DISK, Type.NETWORK); |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 65 | |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 66 | private Map<ControlMetricType, Double> cpuBuf; |
| 67 | private Map<ControlMetricType, Double> memoryBuf; |
| 68 | private Map<String, Map<ControlMetricType, Double>> diskBuf; |
| 69 | private Map<String, Map<ControlMetricType, Double>> networkBuf; |
| 70 | private Map<DeviceId, Map<ControlMetricType, Double>> ctrlMsgBuf; |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 71 | |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 72 | private Map<Type, Set<String>> availableResourceMap; |
| 73 | private Set<DeviceId> availableDeviceIdSet; |
| 74 | |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 75 | @Activate |
| 76 | public void activate() { |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 77 | cpuMetrics = genMDbBuilder(Type.CPU, CPU_METRICS); |
| 78 | memoryMetrics = genMDbBuilder(Type.MEMORY, MEMORY_METRICS); |
| 79 | controlMessageMap = Maps.newConcurrentMap(); |
| 80 | diskMetricsMap = Maps.newConcurrentMap(); |
| 81 | networkMetricsMap = Maps.newConcurrentMap(); |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 82 | |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 83 | cpuBuf = Maps.newConcurrentMap(); |
| 84 | memoryBuf = Maps.newConcurrentMap(); |
| 85 | diskBuf = Maps.newConcurrentMap(); |
| 86 | networkBuf = Maps.newConcurrentMap(); |
| 87 | ctrlMsgBuf = Maps.newConcurrentMap(); |
| 88 | |
| 89 | availableResourceMap = Maps.newConcurrentMap(); |
| 90 | availableDeviceIdSet = Sets.newConcurrentHashSet(); |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 91 | |
| 92 | log.info("Started"); |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | @Deactivate |
| 96 | public void deactivate() { |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 97 | |
| 98 | // TODO: need to handle the mdb close. |
| 99 | cpuBuf.clear(); |
| 100 | memoryBuf.clear(); |
| 101 | diskBuf.clear(); |
| 102 | networkBuf.clear(); |
| 103 | ctrlMsgBuf.clear(); |
| 104 | |
| 105 | log.info("Stopped"); |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 106 | } |
| 107 | |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 108 | @Override |
Jian Li | c5cb4a1 | 2016-02-03 23:24:42 -0800 | [diff] [blame] | 109 | public void updateMetric(ControlMetric cm, int updateIntervalInMinutes, |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 110 | Optional<DeviceId> deviceId) { |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 111 | if (deviceId.isPresent()) { |
Jian Li | 4614890 | 2016-01-29 13:33:50 -0800 | [diff] [blame] | 112 | |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 113 | // insert a new device entry if we cannot find any |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 114 | ctrlMsgBuf.putIfAbsent(deviceId.get(), Maps.newConcurrentMap()); |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 115 | |
| 116 | // update control message metrics |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 117 | if (CONTROL_MESSAGE_METRICS.contains(cm.metricType())) { |
| 118 | |
| 119 | if (!availableDeviceIdSet.contains(deviceId.get())) { |
| 120 | availableDeviceIdSet.add(deviceId.get()); |
| 121 | } |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 122 | |
| 123 | // we will accumulate the metric value into buffer first |
| 124 | ctrlMsgBuf.get(deviceId.get()).putIfAbsent(cm.metricType(), |
| 125 | (double) cm.metricValue().getLoad()); |
| 126 | |
| 127 | // if buffer contains all control message metrics, |
| 128 | // we simply set and update the values into MetricsDatabase. |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 129 | if (ctrlMsgBuf.get(deviceId.get()).keySet() |
| 130 | .containsAll(CONTROL_MESSAGE_METRICS)) { |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 131 | updateControlMessages(ctrlMsgBuf.get(deviceId.get()), deviceId.get()); |
| 132 | ctrlMsgBuf.get(deviceId.get()).clear(); |
| 133 | } |
| 134 | } |
| 135 | } else { |
| 136 | |
| 137 | // update cpu metrics |
| 138 | if (CPU_METRICS.contains(cm.metricType())) { |
| 139 | cpuBuf.putIfAbsent(cm.metricType(), |
| 140 | (double) cm.metricValue().getLoad()); |
| 141 | if (cpuBuf.keySet().containsAll(CPU_METRICS)) { |
| 142 | cpuMetrics.updateMetrics(convertMap(cpuBuf)); |
| 143 | cpuBuf.clear(); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | // update memory metrics |
| 148 | if (MEMORY_METRICS.contains(cm.metricType())) { |
| 149 | memoryBuf.putIfAbsent(cm.metricType(), |
| 150 | (double) cm.metricValue().getLoad()); |
| 151 | if (memoryBuf.keySet().containsAll(MEMORY_METRICS)) { |
| 152 | memoryMetrics.updateMetrics(convertMap(memoryBuf)); |
| 153 | memoryBuf.clear(); |
| 154 | } |
| 155 | } |
| 156 | } |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | @Override |
Jian Li | c5cb4a1 | 2016-02-03 23:24:42 -0800 | [diff] [blame] | 160 | public void updateMetric(ControlMetric cm, int updateIntervalInMinutes, |
Jian Li | e044d1a | 2016-01-25 09:01:20 -0800 | [diff] [blame] | 161 | String resourceName) { |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 162 | // update disk metrics |
| 163 | if (DISK_METRICS.contains(cm.metricType())) { |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 164 | diskBuf.putIfAbsent(resourceName, Maps.newConcurrentMap()); |
| 165 | |
| 166 | availableResourceMap.putIfAbsent(Type.DISK, Sets.newHashSet()); |
| 167 | availableResourceMap.computeIfPresent(Type.DISK, (k, v) -> { |
| 168 | v.add(resourceName); |
| 169 | return v; |
| 170 | }); |
| 171 | |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 172 | diskBuf.get(resourceName).putIfAbsent(cm.metricType(), |
| 173 | (double) cm.metricValue().getLoad()); |
| 174 | if (diskBuf.get(resourceName).keySet().containsAll(DISK_METRICS)) { |
| 175 | updateDiskMetrics(diskBuf.get(resourceName), resourceName); |
| 176 | diskBuf.clear(); |
| 177 | } |
| 178 | } |
Jian Li | e044d1a | 2016-01-25 09:01:20 -0800 | [diff] [blame] | 179 | |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 180 | // update network metrics |
| 181 | if (NETWORK_METRICS.contains(cm.metricType())) { |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 182 | networkBuf.putIfAbsent(resourceName, Maps.newConcurrentMap()); |
| 183 | |
| 184 | availableResourceMap.putIfAbsent(Type.NETWORK, Sets.newHashSet()); |
| 185 | availableResourceMap.computeIfPresent(Type.NETWORK, (k, v) -> { |
| 186 | v.add(resourceName); |
| 187 | return v; |
| 188 | }); |
| 189 | |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 190 | networkBuf.get(resourceName).putIfAbsent(cm.metricType(), |
| 191 | (double) cm.metricValue().getLoad()); |
| 192 | if (networkBuf.get(resourceName).keySet().containsAll(NETWORK_METRICS)) { |
| 193 | updateNetworkMetrics(networkBuf.get(resourceName), resourceName); |
| 194 | networkBuf.clear(); |
| 195 | } |
| 196 | } |
Jian Li | e044d1a | 2016-01-25 09:01:20 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | @Override |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 200 | public ControlLoad getLoad(NodeId nodeId, ControlMetricType type, |
| 201 | Optional<DeviceId> deviceId) { |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 202 | ControllerNode node = clusterService.getNode(nodeId); |
| 203 | if (clusterService.getLocalNode().equals(node)) { |
| 204 | |
| 205 | if (deviceId.isPresent()) { |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 206 | if (CONTROL_MESSAGE_METRICS.contains(type) && |
| 207 | availableDeviceIdSet.contains(deviceId.get())) { |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 208 | return new DefaultControlLoad(controlMessageMap.get(deviceId.get()), type); |
| 209 | } |
| 210 | } else { |
| 211 | // returns controlLoad of CPU metrics |
| 212 | if (CPU_METRICS.contains(type)) { |
| 213 | return new DefaultControlLoad(cpuMetrics, type); |
| 214 | } |
| 215 | |
| 216 | // returns memoryLoad of memory metrics |
| 217 | if (MEMORY_METRICS.contains(type)) { |
| 218 | return new DefaultControlLoad(memoryMetrics, type); |
| 219 | } |
| 220 | } |
| 221 | } else { |
| 222 | // TODO: currently only query the metrics of local node |
| 223 | return null; |
| 224 | } |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 225 | return null; |
| 226 | } |
| 227 | |
| 228 | @Override |
| 229 | public ControlLoad getLoad(NodeId nodeId, ControlMetricType type, |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 230 | String resourceName) { |
| 231 | if (clusterService.getLocalNode().id().equals(nodeId)) { |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 232 | if (DISK_METRICS.contains(type) && |
| 233 | availableResources(Type.DISK).contains(resourceName)) { |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 234 | return new DefaultControlLoad(diskMetricsMap.get(resourceName), type); |
| 235 | } |
| 236 | |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 237 | if (NETWORK_METRICS.contains(type) && |
| 238 | availableResources(Type.NETWORK).contains(resourceName)) { |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 239 | return new DefaultControlLoad(networkMetricsMap.get(resourceName), type); |
| 240 | } |
| 241 | } else { |
| 242 | // TODO: currently only query the metrics of local node |
| 243 | return null; |
| 244 | } |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 245 | return null; |
| 246 | } |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 247 | |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 248 | @Override |
| 249 | public Set<String> availableResources(Type resourceType) { |
| 250 | if (RESOURCE_TYPE_SET.contains(resourceType)) { |
| 251 | if (Type.CONTROL_MESSAGE.equals(resourceType)) { |
| 252 | return availableDeviceIdSet.stream().map(id -> |
| 253 | id.toString()).collect(Collectors.toSet()); |
| 254 | } else { |
| 255 | return availableResourceMap.get(resourceType); |
| 256 | } |
| 257 | } |
| 258 | return null; |
| 259 | } |
| 260 | |
| 261 | private MetricsDatabase genMDbBuilder(Type resourceType, |
Jian Li | c5cb4a1 | 2016-02-03 23:24:42 -0800 | [diff] [blame] | 262 | Set<ControlMetricType> metricTypes) { |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 263 | MetricsDatabase.Builder builder = new DefaultMetricsDatabase.Builder(); |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 264 | builder.withMetricName(resourceType.toString()); |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 265 | metricTypes.forEach(type -> builder.addMetricType(type.toString())); |
| 266 | return builder.build(); |
| 267 | } |
| 268 | |
| 269 | private void updateNetworkMetrics(Map<ControlMetricType, Double> metricMap, |
| 270 | String resName) { |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 271 | networkMetricsMap.putIfAbsent(resName, |
| 272 | genMDbBuilder(Type.NETWORK, NETWORK_METRICS)); |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 273 | networkMetricsMap.get(resName).updateMetrics(convertMap(metricMap)); |
| 274 | } |
| 275 | |
| 276 | private void updateDiskMetrics(Map<ControlMetricType, Double> metricMap, |
| 277 | String resName) { |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 278 | diskMetricsMap.putIfAbsent(resName, genMDbBuilder(Type.DISK, DISK_METRICS)); |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 279 | diskMetricsMap.get(resName).updateMetrics(convertMap(metricMap)); |
| 280 | } |
| 281 | |
| 282 | private void updateControlMessages(Map<ControlMetricType, Double> metricMap, |
| 283 | DeviceId devId) { |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 284 | controlMessageMap.putIfAbsent(devId, |
| 285 | genMDbBuilder(Type.CONTROL_MESSAGE, CONTROL_MESSAGE_METRICS)); |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 286 | controlMessageMap.get(devId).updateMetrics(convertMap(metricMap)); |
| 287 | } |
| 288 | |
| 289 | private Map convertMap(Map<ControlMetricType, Double> map) { |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 290 | Map newMap = Maps.newConcurrentMap(); |
Jian Li | 7d180c5 | 2016-02-01 21:53:08 -0800 | [diff] [blame] | 291 | map.forEach((k, v) -> newMap.putIfAbsent(k.toString(), v)); |
| 292 | return newMap; |
| 293 | } |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 294 | } |