Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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; |
| 17 | |
| 18 | import org.onlab.metrics.MetricsService; |
| 19 | import org.onosproject.net.DeviceId; |
| 20 | import org.onosproject.net.device.DeviceService; |
| 21 | |
| 22 | import java.util.HashSet; |
| 23 | import java.util.Map; |
| 24 | import java.util.Optional; |
| 25 | import java.util.Set; |
| 26 | import java.util.concurrent.ConcurrentHashMap; |
| 27 | |
| 28 | /** |
| 29 | * Singleton class to provide various control plane metrics to other components. |
| 30 | */ |
| 31 | public final class ControlMetricsFactory { |
| 32 | private static volatile ControlMetricsFactory uniqueInstance; |
| 33 | |
| 34 | private MetricsService metricsService; |
| 35 | private boolean enableMonitor = false; |
| 36 | |
| 37 | // define a set of MetricsAggregators |
| 38 | private MetricsAggregator cpuInfoMetric; |
| 39 | private MetricsAggregator memoryInfoMetric; |
| 40 | private Map<DeviceId, MetricsAggregator> inboundPacketMetrics; |
| 41 | private Map<DeviceId, MetricsAggregator> outboundPacketMetrics; |
| 42 | private Map<DeviceId, MetricsAggregator> flowmodPacketMetrics; |
| 43 | private Map<DeviceId, MetricsAggregator> flowrmvPacketMetrics; |
| 44 | private Map<DeviceId, MetricsAggregator> requestPacketMetrics; |
| 45 | private Map<DeviceId, MetricsAggregator> replyPacketMetrics; |
| 46 | private Set<DeviceId> deviceIds = new HashSet<>(); |
| 47 | |
| 48 | private ControlMetricsFactory(MetricsService metricsService, DeviceService deviceService) { |
| 49 | this.metricsService = metricsService; |
| 50 | registerMetrics(); |
| 51 | |
| 52 | deviceService.getDevices().forEach(d->deviceIds.add(d.id())); |
| 53 | |
| 54 | addAllDeviceMetrics(deviceIds); |
| 55 | } |
| 56 | |
| 57 | public static ControlMetricsFactory getInstance(MetricsService metricsService, |
| 58 | DeviceService deviceService) { |
| 59 | if (uniqueInstance == null) { |
| 60 | synchronized (ControlMetricsFactory.class) { |
| 61 | if (uniqueInstance == null) { |
| 62 | uniqueInstance = new ControlMetricsFactory(metricsService, deviceService); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | return uniqueInstance; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Adds control metrics of a new device. |
| 71 | * |
| 72 | * @param deviceId {@link org.onosproject.net.DeviceId} |
| 73 | */ |
| 74 | public void addMetricsByDeviceId(DeviceId deviceId) { |
| 75 | MetricsAggregator inbound = new MetricsAggregator(metricsService, |
| 76 | ControlMetricType.INBOUND_PACKET, Optional.of(deviceId)); |
| 77 | MetricsAggregator outbound = new MetricsAggregator(metricsService, |
| 78 | ControlMetricType.OUTBOUND_PACKET, Optional.of(deviceId)); |
| 79 | MetricsAggregator flowmod = new MetricsAggregator(metricsService, |
| 80 | ControlMetricType.FLOW_MOD_PACKET, Optional.of(deviceId)); |
| 81 | MetricsAggregator flowrmv = new MetricsAggregator(metricsService, |
| 82 | ControlMetricType.FLOW_REMOVED_PACKET, Optional.of(deviceId)); |
| 83 | MetricsAggregator request = new MetricsAggregator(metricsService, |
| 84 | ControlMetricType.REQUEST_PACKET, Optional.of(deviceId)); |
| 85 | MetricsAggregator reply = new MetricsAggregator(metricsService, |
| 86 | ControlMetricType.REPLY_PACKET, Optional.of(deviceId)); |
| 87 | |
| 88 | inboundPacketMetrics.putIfAbsent(deviceId, inbound); |
| 89 | outboundPacketMetrics.putIfAbsent(deviceId, outbound); |
| 90 | flowmodPacketMetrics.putIfAbsent(deviceId, flowmod); |
| 91 | flowrmvPacketMetrics.putIfAbsent(deviceId, flowrmv); |
| 92 | requestPacketMetrics.putIfAbsent(deviceId, request); |
| 93 | replyPacketMetrics.putIfAbsent(deviceId, reply); |
| 94 | |
| 95 | deviceIds.add(deviceId); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Removes control metrics of an existing device. |
| 100 | * |
| 101 | * @param deviceId {@link org.onosproject.net.DeviceId} |
| 102 | */ |
| 103 | public void removeMetricsByDeviceId(DeviceId deviceId) { |
| 104 | inboundPacketMetrics.remove(deviceId); |
| 105 | outboundPacketMetrics.remove(deviceId); |
| 106 | flowmodPacketMetrics.remove(deviceId); |
| 107 | flowrmvPacketMetrics.remove(deviceId); |
| 108 | requestPacketMetrics.remove(deviceId); |
| 109 | replyPacketMetrics.remove(deviceId); |
| 110 | |
| 111 | deviceIds.remove(deviceId); |
| 112 | } |
| 113 | |
| 114 | public Set<DeviceId> getDeviceIds() { |
| 115 | return this.deviceIds; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Adds control metrics for all devices. |
| 120 | * |
| 121 | * @param deviceIds a set of deviceIds |
| 122 | */ |
| 123 | public void addAllDeviceMetrics(Set<DeviceId> deviceIds) { |
| 124 | deviceIds.forEach(v -> addMetricsByDeviceId(v)); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Returns monitoring status. |
| 129 | * |
| 130 | * @return monitoring status |
| 131 | */ |
| 132 | public boolean isMonitor() { |
| 133 | return this.enableMonitor; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Enable control plane monitoring. |
| 138 | */ |
| 139 | protected void startMonitor() { |
| 140 | this.enableMonitor = true; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Disable control plane monitoring. |
| 145 | */ |
| 146 | protected void stopMonitor() { |
| 147 | this.enableMonitor = false; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Registers new control metrics. |
| 152 | */ |
| 153 | protected void registerMetrics() { |
| 154 | cpuInfoMetric = new MetricsAggregator(metricsService, |
| 155 | ControlMetricType.CPU_INFO, Optional.ofNullable(null)); |
| 156 | memoryInfoMetric = new MetricsAggregator(metricsService, |
| 157 | ControlMetricType.MEMORY_INFO, Optional.ofNullable(null)); |
| 158 | |
| 159 | inboundPacketMetrics = new ConcurrentHashMap<>(); |
| 160 | outboundPacketMetrics = new ConcurrentHashMap<>(); |
| 161 | flowmodPacketMetrics = new ConcurrentHashMap<>(); |
| 162 | flowrmvPacketMetrics = new ConcurrentHashMap<>(); |
| 163 | requestPacketMetrics = new ConcurrentHashMap<>(); |
| 164 | replyPacketMetrics = new ConcurrentHashMap<>(); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Unregisters all control metrics. |
| 169 | */ |
| 170 | protected void unregisterMetrics() { |
| 171 | cpuInfoMetric.removeMetrics(); |
| 172 | memoryInfoMetric.removeMetrics(); |
| 173 | |
| 174 | inboundPacketMetrics.clear(); |
| 175 | outboundPacketMetrics.clear(); |
| 176 | flowmodPacketMetrics.clear(); |
| 177 | flowrmvPacketMetrics.clear(); |
| 178 | requestPacketMetrics.clear(); |
| 179 | replyPacketMetrics.clear(); |
| 180 | } |
| 181 | |
| 182 | public MetricsAggregator cpuInfoMetric() { |
| 183 | return cpuInfoMetric; |
| 184 | } |
| 185 | |
| 186 | public MetricsAggregator memoryInfoMetric() { |
| 187 | return memoryInfoMetric; |
| 188 | } |
| 189 | |
| 190 | public Map<DeviceId, MetricsAggregator> inboundPacketMetrics() { |
| 191 | return inboundPacketMetrics; |
| 192 | } |
| 193 | |
| 194 | public Map<DeviceId, MetricsAggregator> outboundPacketMetrics() { |
| 195 | return outboundPacketMetrics; |
| 196 | } |
| 197 | |
| 198 | public Map<DeviceId, MetricsAggregator> flowmodPacketMetrics() { |
| 199 | return flowmodPacketMetrics; |
| 200 | } |
| 201 | |
| 202 | public Map<DeviceId, MetricsAggregator> flowrmvPacketMetrics() { |
| 203 | return flowrmvPacketMetrics; |
| 204 | } |
| 205 | |
| 206 | public Map<DeviceId, MetricsAggregator> requestPacketMetrics() { |
| 207 | return requestPacketMetrics; |
| 208 | } |
| 209 | |
| 210 | public Map<DeviceId, MetricsAggregator> replyPacketMetrics() { |
| 211 | return replyPacketMetrics; |
| 212 | } |
| 213 | |
| 214 | public MetricsAggregator inboundPacketMetrics(DeviceId deviceId) { |
| 215 | return inboundPacketMetrics.get(deviceId); |
| 216 | } |
| 217 | |
| 218 | public MetricsAggregator outboundPacketMetrics(DeviceId deviceId) { |
| 219 | return outboundPacketMetrics.get(deviceId); |
| 220 | } |
| 221 | |
| 222 | public MetricsAggregator flowmodPacketMetrics(DeviceId deviceId) { |
| 223 | return flowmodPacketMetrics.get(deviceId); |
| 224 | } |
| 225 | |
| 226 | public MetricsAggregator flowrmvPacketMetrics(DeviceId deviceId) { |
| 227 | return flowrmvPacketMetrics.get(deviceId); |
| 228 | } |
| 229 | |
| 230 | public MetricsAggregator requestPacketMetrics(DeviceId deviceId) { |
| 231 | return requestPacketMetrics.get(deviceId); |
| 232 | } |
| 233 | |
| 234 | public MetricsAggregator replyPacketMetrics(DeviceId deviceId) { |
| 235 | return replyPacketMetrics.get(deviceId); |
| 236 | } |
| 237 | } |