blob: d4139f3d99be5a8ef343088350f4cf86d4311299 [file] [log] [blame]
Jian Li0967cd72015-11-25 17:38:48 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Jian Li0967cd72015-11-25 17:38:48 -08003 *
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 Li6b86a762016-01-29 09:30:40 -080016package org.onosproject.cpman.impl;
Jian Li0967cd72015-11-25 17:38:48 -080017
Jian Li0967cd72015-11-25 17:38:48 -080018import org.apache.felix.scr.annotations.Activate;
Jian Lic132c112016-01-28 20:27:34 -080019import org.apache.felix.scr.annotations.Component;
Jian Li0967cd72015-11-25 17:38:48 -080020import org.apache.felix.scr.annotations.Deactivate;
Jian Li60804322015-12-02 14:46:31 -080021import org.apache.felix.scr.annotations.Reference;
22import org.apache.felix.scr.annotations.ReferenceCardinality;
Jian Li60804322015-12-02 14:46:31 -080023import org.onosproject.core.ApplicationId;
24import org.onosproject.core.CoreService;
Jian Li72b9b122016-02-11 15:58:51 -080025import org.onosproject.cpman.ControlMessage;
26import org.onosproject.cpman.ControlMetric;
27import org.onosproject.cpman.ControlPlaneMonitorService;
28import org.onosproject.cpman.MetricValue;
29import org.onosproject.cpman.message.ControlMessageEvent;
30import org.onosproject.cpman.message.ControlMessageListener;
31import org.onosproject.cpman.message.ControlMessageService;
Jian Li0967cd72015-11-25 17:38:48 -080032import org.slf4j.Logger;
Jian Lic132c112016-01-28 20:27:34 -080033import org.slf4j.LoggerFactory;
Jian Li0967cd72015-11-25 17:38:48 -080034
Jian Li72b9b122016-02-11 15:58:51 -080035import java.util.Optional;
36import java.util.Set;
37
38import static org.onosproject.cpman.message.ControlMessageEvent.Type.STATS_UPDATE;
39
Jian Li0967cd72015-11-25 17:38:48 -080040/**
Jian Li6b86a762016-01-29 09:30:40 -080041 * Skeletal control plane management component.
Jian Li0967cd72015-11-25 17:38:48 -080042 */
Jian Li60804322015-12-02 14:46:31 -080043@Component(immediate = true)
Jian Li0967cd72015-11-25 17:38:48 -080044public class ControlPlaneManager {
45
Jian Lic132c112016-01-28 20:27:34 -080046 private final Logger log = LoggerFactory.getLogger(getClass());
Jian Li60804322015-12-02 14:46:31 -080047
48 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
49 protected CoreService coreService;
50
51 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Jian Li72b9b122016-02-11 15:58:51 -080052 protected ControlMessageService messageService;
53
54 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
55 protected ControlPlaneMonitorService monitorService;
56
57 private final ControlMessageListener messageListener =
58 new InternalControlMessageListener();
Jian Lic132c112016-01-28 20:27:34 -080059
60 private ApplicationId appId;
Jian Li0967cd72015-11-25 17:38:48 -080061
62 @Activate
Jian Lic132c112016-01-28 20:27:34 -080063 protected void activate() {
Jian Li60804322015-12-02 14:46:31 -080064 appId = coreService.registerApplication("org.onosproject.cpman");
Jian Li72b9b122016-02-11 15:58:51 -080065 messageService.addListener(messageListener);
Jian Li60804322015-12-02 14:46:31 -080066 log.info("Started");
Jian Li0967cd72015-11-25 17:38:48 -080067 }
68
69 @Deactivate
Jian Lic132c112016-01-28 20:27:34 -080070 protected void deactivate() {
Jian Li72b9b122016-02-11 15:58:51 -080071 messageService.removeListener(messageListener);
Jian Li60804322015-12-02 14:46:31 -080072 log.info("Stopped");
Jian Li0967cd72015-11-25 17:38:48 -080073 }
Jian Li72b9b122016-02-11 15:58:51 -080074
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)) {
Jian Li1aa07822016-04-19 17:58:02 -070084 controlMessages.forEach(c ->
Jian Li72b9b122016-02-11 15:58:51 -080085 monitorService.updateMetric(getControlMetric(c), 1,
Jian Li1aa07822016-04-19 17:58:02 -070086 Optional.of(c.deviceId()))
87 );
Jian Li72b9b122016-02-11 15:58:51 -080088 }
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 Li6b86a762016-01-29 09:30:40 -0800101}