Jian Li | 0967cd7 | 2015-11-25 17:38:48 -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 | |
Jian Li | 0967cd7 | 2015-11-25 17:38:48 -0800 | [diff] [blame] | 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Deactivate; |
| 20 | import org.apache.felix.scr.annotations.Modified; |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame^] | 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | import org.onlab.metrics.MetricsService; |
| 25 | import org.onosproject.core.ApplicationId; |
| 26 | import org.onosproject.core.CoreService; |
| 27 | import org.onosproject.net.device.DeviceService; |
Jian Li | 0967cd7 | 2015-11-25 17:38:48 -0800 | [diff] [blame] | 28 | import org.slf4j.Logger; |
| 29 | |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame^] | 30 | import java.util.HashSet; |
| 31 | import java.util.Optional; |
| 32 | import java.util.Set; |
| 33 | |
Jian Li | 0967cd7 | 2015-11-25 17:38:48 -0800 | [diff] [blame] | 34 | import static org.slf4j.LoggerFactory.getLogger; |
| 35 | |
| 36 | /** |
| 37 | * Control plane management application. |
| 38 | */ |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame^] | 39 | @Component(immediate = true) |
Jian Li | 0967cd7 | 2015-11-25 17:38:48 -0800 | [diff] [blame] | 40 | public class ControlPlaneManager { |
| 41 | |
| 42 | private final Logger log = getLogger(getClass()); |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame^] | 43 | private Set<ControlMetricsObserver> controlMetricsObservers = new HashSet<>(); |
| 44 | private ControlMetricsObserver cpObserver; |
| 45 | |
| 46 | private ApplicationId appId; |
| 47 | |
| 48 | private ControlMetricsFactory cmf; |
| 49 | |
| 50 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 51 | protected DeviceService deviceService; |
| 52 | |
| 53 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 54 | protected CoreService coreService; |
| 55 | |
| 56 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 57 | protected MetricsService metricsService; |
Jian Li | 0967cd7 | 2015-11-25 17:38:48 -0800 | [diff] [blame] | 58 | |
| 59 | @Activate |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame^] | 60 | public void activate() { |
| 61 | appId = coreService.registerApplication("org.onosproject.cpman"); |
Jian Li | 0967cd7 | 2015-11-25 17:38:48 -0800 | [diff] [blame] | 62 | |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame^] | 63 | cmf = ControlMetricsFactory.getInstance(metricsService, deviceService); |
| 64 | // currently disable monitoring by default |
| 65 | // cmf.startMonitor(); |
| 66 | |
| 67 | registerObserver(); |
| 68 | |
| 69 | log.info("Started"); |
Jian Li | 0967cd7 | 2015-11-25 17:38:48 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | @Deactivate |
| 73 | public void deactivate() { |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame^] | 74 | unregisterObserver(); |
| 75 | cmf.stopMonitor(); |
| 76 | log.info("Stopped"); |
Jian Li | 0967cd7 | 2015-11-25 17:38:48 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Modified |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame^] | 80 | public void modified() { |
Jian Li | 0967cd7 | 2015-11-25 17:38:48 -0800 | [diff] [blame] | 81 | } |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame^] | 82 | |
| 83 | private void registerObserver() { |
| 84 | cpObserver = new DefaultControlMetricsObserver(); |
| 85 | this.addControlMetricsObserver(cpObserver); |
| 86 | } |
| 87 | |
| 88 | private void unregisterObserver() { |
| 89 | this.removeControlMetricsObserver(cpObserver); |
| 90 | } |
| 91 | |
| 92 | private void executeMonitorTask() { |
| 93 | |
| 94 | // TODO: execute monitoring task with 1 minute period |
| 95 | if (cmf.isMonitor()) { |
| 96 | controlMetricsObservers.forEach(observer -> { |
| 97 | |
| 98 | // try to feed the CPU and memory stats |
| 99 | observer.feedMetrics(cmf.cpuInfoMetric(), Optional.ofNullable(null)); |
| 100 | observer.feedMetrics(cmf.memoryInfoMetric(), Optional.ofNullable(null)); |
| 101 | |
| 102 | // try to feed the control message stats |
| 103 | cmf.getDeviceIds().forEach(v -> { |
| 104 | observer.feedMetrics(cmf.inboundPacketMetrics(v), Optional.of(v)); |
| 105 | observer.feedMetrics(cmf.outboundPacketMetrics(v), Optional.of(v)); |
| 106 | observer.feedMetrics(cmf.flowmodPacketMetrics(v), Optional.of(v)); |
| 107 | observer.feedMetrics(cmf.flowrmvPacketMetrics(v), Optional.of(v)); |
| 108 | observer.feedMetrics(cmf.requestPacketMetrics(v), Optional.of(v)); |
| 109 | observer.feedMetrics(cmf.replyPacketMetrics(v), Optional.of(v)); |
| 110 | }); |
| 111 | }); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Adds a new control metrics observer. |
| 117 | * |
| 118 | * @param cmObserver control metric observer instance |
| 119 | */ |
| 120 | public void addControlMetricsObserver(ControlMetricsObserver cmObserver) { |
| 121 | controlMetricsObservers.add(cmObserver); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Removes an existing control metrics observer. |
| 126 | * |
| 127 | * @param cmObserver control metric observer instance |
| 128 | */ |
| 129 | public void removeControlMetricsObserver(ControlMetricsObserver cmObserver) { |
| 130 | controlMetricsObservers.remove(cmObserver); |
| 131 | } |
| 132 | } |