blob: a0cacc8563932674d9465ec6188363cc4e4149b6 [file] [log] [blame]
Jian Li1d13c262016-02-09 14:58:28 -08001/*
2 * Copyright 2016 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
17package org.onosproject.provider.of.message.impl;
18
19import com.codahale.metrics.Meter;
20import com.google.common.collect.ImmutableSet;
21import com.google.common.collect.Maps;
Jian Li72b9b122016-02-11 15:58:51 -080022import com.google.common.collect.Sets;
Jian Li1d13c262016-02-09 14:58:28 -080023import org.onlab.metrics.MetricsComponent;
24import org.onlab.metrics.MetricsFeature;
25import org.onlab.metrics.MetricsService;
26import org.onosproject.cpman.ControlMessage;
27import org.onosproject.cpman.DefaultControlMessage;
28import org.onosproject.cpman.message.ControlMessageProviderService;
29import org.onosproject.net.DeviceId;
30import org.projectfloodlight.openflow.protocol.OFMessage;
31import org.projectfloodlight.openflow.protocol.OFType;
Jian Li7d9d4392016-02-11 11:41:26 -080032import org.slf4j.Logger;
Jian Li1d13c262016-02-09 14:58:28 -080033
Jian Li1d13c262016-02-09 14:58:28 -080034import java.util.Map;
35import java.util.Set;
36
37import static org.onosproject.provider.of.message.impl.OpenFlowControlMessageMapper.lookupControlMessageType;
Jian Li7d9d4392016-02-11 11:41:26 -080038import static org.slf4j.LoggerFactory.getLogger;
Jian Li1d13c262016-02-09 14:58:28 -080039
40/**
41 * Collects the OpenFlow messages and aggregates using MetricsService.
42 */
43public class OpenFlowControlMessageAggregator implements Runnable {
44
Jian Li7d9d4392016-02-11 11:41:26 -080045 private final Logger log = getLogger(getClass());
46
Jian Li1d13c262016-02-09 14:58:28 -080047 private static final Set<OFType> OF_TYPE_SET =
48 ImmutableSet.of(OFType.PACKET_IN, OFType.PACKET_OUT, OFType.FLOW_MOD,
49 OFType.FLOW_REMOVED, OFType.STATS_REQUEST, OFType.STATS_REPLY);
50
51 private final Map<OFType, Meter> rateMeterMap = Maps.newHashMap();
52 private final Map<OFType, Meter> countMeterMap = Maps.newHashMap();
53
54 private final DeviceId deviceId;
55 private final ControlMessageProviderService providerService;
56
57 private static final String RATE_NAME = "rate";
58 private static final String COUNT_NAME = "count";
59
Jian Li72b9b122016-02-11 15:58:51 -080060 private Set<ControlMessage> controlMessages = Sets.newConcurrentHashSet();
Jian Li1d13c262016-02-09 14:58:28 -080061
62 // TODO: this needs to be configurable
63 private static final int EXECUTE_PERIOD_IN_SECOND = 60;
64
65 /**
66 * Generates an OpenFlow message aggregator instance.
67 * The instance is for aggregating a specific OpenFlow message
68 * type of an OpenFlow switch.
69 *
70 * @param metricsService metrics service reference object
71 * @param providerService control message provider service reference object
72 * @param deviceId device identification
73 */
74 public OpenFlowControlMessageAggregator(MetricsService metricsService,
75 ControlMessageProviderService providerService,
76 DeviceId deviceId) {
77 MetricsComponent mc = metricsService.registerComponent(deviceId.toString());
78
79 OF_TYPE_SET.forEach(type -> {
80 MetricsFeature metricsFeature = mc.registerFeature(type.toString());
81 Meter rateMeter = metricsService.createMeter(mc, metricsFeature, RATE_NAME);
82 Meter countMeter = metricsService.createMeter(mc, metricsFeature, COUNT_NAME);
83 rateMeterMap.put(type, rateMeter);
84 countMeterMap.put(type, countMeter);
85 });
86
87 this.deviceId = deviceId;
88 this.providerService = providerService;
89 }
90
91 /**
92 * Increments the meter rate by n, and the meter count by 1.
93 *
94 * @param msg OpenFlow message
95 */
96 public void increment(OFMessage msg) {
97 rateMeterMap.get(msg.getType()).mark(msg.toString().length());
98 countMeterMap.get(msg.getType()).mark(1);
99 }
100
101 @Override
102 public void run() {
103 // update 1 minute statistic information of all control messages
104 OF_TYPE_SET.forEach(type -> controlMessages.add(
105 new DefaultControlMessage(lookupControlMessageType(type),
Jian Li72b9b122016-02-11 15:58:51 -0800106 deviceId, getLoad(type), getRate(type), getCount(type),
Jian Li1d13c262016-02-09 14:58:28 -0800107 System.currentTimeMillis())));
Jian Li7d9d4392016-02-11 11:41:26 -0800108 log.debug("sent aggregated control message");
Jian Li72b9b122016-02-11 15:58:51 -0800109 providerService.updateStatsInfo(deviceId, ImmutableSet.copyOf(controlMessages));
Jian Li7d9d4392016-02-11 11:41:26 -0800110 controlMessages.clear();
Jian Li1d13c262016-02-09 14:58:28 -0800111 }
112
113 /**
114 * Returns the average load value.
115 *
116 * @param type OpenFlow message type
117 * @return load value
118 */
119 private long getLoad(OFType type) {
Jian Li7d9d4392016-02-11 11:41:26 -0800120 if (countMeterMap.get(type).getOneMinuteRate() == 0D) {
121 return 0L;
122 }
Jian Li72b9b122016-02-11 15:58:51 -0800123 return (long) (rateMeterMap.get(type).getOneMinuteRate() /
124 countMeterMap.get(type).getOneMinuteRate());
Jian Li1d13c262016-02-09 14:58:28 -0800125 }
126
127 /**
128 * Returns the average meter rate within recent 1 minute.
129 *
130 * @param type OpenFlow message type
131 * @return rate value
132 */
133 private long getRate(OFType type) {
134 return (long) rateMeterMap.get(type).getOneMinuteRate();
135 }
136
137 /**
138 * Returns the average meter count within recent 1 minute.
139 *
140 * @param type OpenFlow message type
141 * @return count value
142 */
143 private long getCount(OFType type) {
Jian Lidaf55ea2016-04-04 20:38:30 -0700144 return (long) (countMeterMap.get(type).getOneMinuteRate()
145 * EXECUTE_PERIOD_IN_SECOND);
Jian Li1d13c262016-02-09 14:58:28 -0800146 }
147}