Jian Li | 0967cd7 | 2015-11-25 17:38:48 -0800 | [diff] [blame] | 1 | /* |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 2 | * Copyright 2016 Open Networking Laboratory |
Jian Li | 0967cd7 | 2015-11-25 17:38:48 -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 | 0967cd7 | 2015-11-25 17:38:48 -0800 | [diff] [blame] | 17 | |
Jian Li | 0967cd7 | 2015-11-25 17:38:48 -0800 | [diff] [blame] | 18 | import org.apache.felix.scr.annotations.Activate; |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 19 | import org.apache.felix.scr.annotations.Component; |
Jian Li | 0967cd7 | 2015-11-25 17:38:48 -0800 | [diff] [blame] | 20 | import org.apache.felix.scr.annotations.Deactivate; |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 23 | import org.onosproject.core.ApplicationId; |
| 24 | import org.onosproject.core.CoreService; |
Jian Li | 72b9b12 | 2016-02-11 15:58:51 -0800 | [diff] [blame] | 25 | import org.onosproject.cpman.ControlMessage; |
| 26 | import org.onosproject.cpman.ControlMetric; |
| 27 | import org.onosproject.cpman.ControlPlaneMonitorService; |
| 28 | import org.onosproject.cpman.MetricValue; |
| 29 | import org.onosproject.cpman.message.ControlMessageEvent; |
| 30 | import org.onosproject.cpman.message.ControlMessageListener; |
| 31 | import org.onosproject.cpman.message.ControlMessageService; |
Jian Li | 0967cd7 | 2015-11-25 17:38:48 -0800 | [diff] [blame] | 32 | import org.slf4j.Logger; |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 33 | import org.slf4j.LoggerFactory; |
Jian Li | 0967cd7 | 2015-11-25 17:38:48 -0800 | [diff] [blame] | 34 | |
Jian Li | 72b9b12 | 2016-02-11 15:58:51 -0800 | [diff] [blame] | 35 | import java.util.Optional; |
| 36 | import java.util.Set; |
| 37 | |
| 38 | import static org.onosproject.cpman.message.ControlMessageEvent.Type.STATS_UPDATE; |
| 39 | |
Jian Li | 0967cd7 | 2015-11-25 17:38:48 -0800 | [diff] [blame] | 40 | /** |
Jian Li | 6b86a76 | 2016-01-29 09:30:40 -0800 | [diff] [blame] | 41 | * Skeletal control plane management component. |
Jian Li | 0967cd7 | 2015-11-25 17:38:48 -0800 | [diff] [blame] | 42 | */ |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 43 | @Component(immediate = true) |
Jian Li | 0967cd7 | 2015-11-25 17:38:48 -0800 | [diff] [blame] | 44 | public class ControlPlaneManager { |
| 45 | |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 46 | private final Logger log = LoggerFactory.getLogger(getClass()); |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 47 | |
| 48 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 49 | protected CoreService coreService; |
| 50 | |
| 51 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jian Li | 72b9b12 | 2016-02-11 15:58:51 -0800 | [diff] [blame] | 52 | protected ControlMessageService messageService; |
| 53 | |
| 54 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 55 | protected ControlPlaneMonitorService monitorService; |
| 56 | |
| 57 | private final ControlMessageListener messageListener = |
| 58 | new InternalControlMessageListener(); |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 59 | |
| 60 | private ApplicationId appId; |
Jian Li | 0967cd7 | 2015-11-25 17:38:48 -0800 | [diff] [blame] | 61 | |
| 62 | @Activate |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 63 | protected void activate() { |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 64 | appId = coreService.registerApplication("org.onosproject.cpman"); |
Jian Li | 72b9b12 | 2016-02-11 15:58:51 -0800 | [diff] [blame] | 65 | messageService.addListener(messageListener); |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 66 | log.info("Started"); |
Jian Li | 0967cd7 | 2015-11-25 17:38:48 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | @Deactivate |
Jian Li | c132c11 | 2016-01-28 20:27:34 -0800 | [diff] [blame] | 70 | protected void deactivate() { |
Jian Li | 72b9b12 | 2016-02-11 15:58:51 -0800 | [diff] [blame] | 71 | messageService.removeListener(messageListener); |
Jian Li | 6080432 | 2015-12-02 14:46:31 -0800 | [diff] [blame] | 72 | log.info("Stopped"); |
Jian Li | 0967cd7 | 2015-11-25 17:38:48 -0800 | [diff] [blame] | 73 | } |
Jian Li | 72b9b12 | 2016-02-11 15:58:51 -0800 | [diff] [blame] | 74 | |
| 75 | private class InternalControlMessageListener implements ControlMessageListener { |
| 76 | |
| 77 | @Override |
| 78 | public void event(ControlMessageEvent event) { |
| 79 | Set<ControlMessage> controlMessages = event.subject(); |
| 80 | |
| 81 | // TODO: this can be changed to switch-case if we have more than |
| 82 | // one event type |
| 83 | if (event.type().equals(STATS_UPDATE)) { |
| 84 | controlMessages.forEach(c -> { |
| 85 | monitorService.updateMetric(getControlMetric(c), 1, |
| 86 | Optional.of(c.deviceId())); |
| 87 | }); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | private ControlMetric getControlMetric(ControlMessage message) { |
| 93 | MetricValue mv = new MetricValue.Builder() |
| 94 | .load(message.load()) |
| 95 | .rate(message.rate()) |
| 96 | .count(message.count()) |
| 97 | .add(); |
| 98 | return new ControlMetric(ControlMessageMetricMapper |
| 99 | .lookupControlMetricType(message.type()), mv); |
| 100 | } |
Jian Li | 6b86a76 | 2016-01-29 09:30:40 -0800 | [diff] [blame] | 101 | } |