Jian Li
Committed by Gerrit Code Review

[ONOS-3504] Initial implementation of control message aggregation

This commit implements control message collection feature in
OpenFlow message provider.

Change-Id: I2a3ed2e5edbe1f39b503bb74a10259026b806513
1 +<?xml version="1.0" encoding="UTF-8"?>
2 +<!--
3 + ~ Copyright 2016 Open Networking Laboratory
4 + ~
5 + ~ Licensed under the Apache License, Version 2.0 (the "License");
6 + ~ you may not use this file except in compliance with the License.
7 + ~ You may obtain a copy of the License at
8 + ~
9 + ~ http://www.apache.org/licenses/LICENSE-2.0
10 + ~
11 + ~ Unless required by applicable law or agreed to in writing, software
12 + ~ distributed under the License is distributed on an "AS IS" BASIS,
13 + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 + ~ See the License for the specific language governing permissions and
15 + ~ limitations under the License.
16 + -->
17 +<app name="org.onosproject.openflow-message" origin="ON.Lab" version="${project.version}"
18 + category="default" url="http://onosproject.org"
19 + featuresRepo="mvn:${project.groupId}/${project.artifactId}/${project.version}/xml/features"
20 + features="${project.artifactId}">
21 + <description>${project.description}</description>
22 + <artifact>mvn:${project.groupId}/onos-of-provider-message/${project.version}</artifact>
23 +</app>
1 +<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2 +<!--
3 + ~ Copyright 2016 Open Networking Laboratory
4 + ~
5 + ~ Licensed under the Apache License, Version 2.0 (the "License");
6 + ~ you may not use this file except in compliance with the License.
7 + ~ You may obtain a copy of the License at
8 + ~
9 + ~ http://www.apache.org/licenses/LICENSE-2.0
10 + ~
11 + ~ Unless required by applicable law or agreed to in writing, software
12 + ~ distributed under the License is distributed on an "AS IS" BASIS,
13 + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 + ~ See the License for the specific language governing permissions and
15 + ~ limitations under the License.
16 + -->
17 +<features xmlns="http://karaf.apache.org/xmlns/features/v1.2.0" name="${project.artifactId}-${project.version}">
18 + <feature name="${project.artifactId}" version="${project.version}"
19 + description="${project.description}">
20 + <feature>onos-api</feature>
21 + <bundle>mvn:${project.groupId}/onos-of-provider-message/${project.version}</bundle>
22 + </feature>
23 +</features>
1 +/*
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 +
17 +package org.onosproject.provider.of.message.impl;
18 +
19 +import com.codahale.metrics.Meter;
20 +import com.google.common.collect.ImmutableSet;
21 +import com.google.common.collect.Maps;
22 +import org.onlab.metrics.MetricsComponent;
23 +import org.onlab.metrics.MetricsFeature;
24 +import org.onlab.metrics.MetricsService;
25 +import org.onosproject.cpman.ControlMessage;
26 +import org.onosproject.cpman.DefaultControlMessage;
27 +import org.onosproject.cpman.message.ControlMessageProviderService;
28 +import org.onosproject.net.DeviceId;
29 +import org.projectfloodlight.openflow.protocol.OFMessage;
30 +import org.projectfloodlight.openflow.protocol.OFType;
31 +
32 +import java.util.ArrayList;
33 +import java.util.Collection;
34 +import java.util.Collections;
35 +import java.util.Map;
36 +import java.util.Set;
37 +
38 +import static org.onosproject.provider.of.message.impl.OpenFlowControlMessageMapper.lookupControlMessageType;
39 +
40 +/**
41 + * Collects the OpenFlow messages and aggregates using MetricsService.
42 + */
43 +public class OpenFlowControlMessageAggregator implements Runnable {
44 +
45 + private static final Set<OFType> OF_TYPE_SET =
46 + ImmutableSet.of(OFType.PACKET_IN, OFType.PACKET_OUT, OFType.FLOW_MOD,
47 + OFType.FLOW_REMOVED, OFType.STATS_REQUEST, OFType.STATS_REPLY);
48 +
49 + private final Map<OFType, Meter> rateMeterMap = Maps.newHashMap();
50 + private final Map<OFType, Meter> countMeterMap = Maps.newHashMap();
51 +
52 + private final DeviceId deviceId;
53 + private final ControlMessageProviderService providerService;
54 +
55 + private static final String RATE_NAME = "rate";
56 + private static final String COUNT_NAME = "count";
57 +
58 + private Collection<ControlMessage> controlMessages = new ArrayList<>();
59 +
60 + // TODO: this needs to be configurable
61 + private static final int EXECUTE_PERIOD_IN_SECOND = 60;
62 +
63 + /**
64 + * Generates an OpenFlow message aggregator instance.
65 + * The instance is for aggregating a specific OpenFlow message
66 + * type of an OpenFlow switch.
67 + *
68 + * @param metricsService metrics service reference object
69 + * @param providerService control message provider service reference object
70 + * @param deviceId device identification
71 + */
72 + public OpenFlowControlMessageAggregator(MetricsService metricsService,
73 + ControlMessageProviderService providerService,
74 + DeviceId deviceId) {
75 + MetricsComponent mc = metricsService.registerComponent(deviceId.toString());
76 +
77 + OF_TYPE_SET.forEach(type -> {
78 + MetricsFeature metricsFeature = mc.registerFeature(type.toString());
79 + Meter rateMeter = metricsService.createMeter(mc, metricsFeature, RATE_NAME);
80 + Meter countMeter = metricsService.createMeter(mc, metricsFeature, COUNT_NAME);
81 + rateMeterMap.put(type, rateMeter);
82 + countMeterMap.put(type, countMeter);
83 + });
84 +
85 + this.deviceId = deviceId;
86 + this.providerService = providerService;
87 + }
88 +
89 + /**
90 + * Increments the meter rate by n, and the meter count by 1.
91 + *
92 + * @param msg OpenFlow message
93 + */
94 + public void increment(OFMessage msg) {
95 + rateMeterMap.get(msg.getType()).mark(msg.toString().length());
96 + countMeterMap.get(msg.getType()).mark(1);
97 + }
98 +
99 + @Override
100 + public void run() {
101 + // update 1 minute statistic information of all control messages
102 + OF_TYPE_SET.forEach(type -> controlMessages.add(
103 + new DefaultControlMessage(lookupControlMessageType(type),
104 + getLoad(type), getRate(type), getCount(type),
105 + System.currentTimeMillis())));
106 + providerService.updateStatsInfo(deviceId,
107 + Collections.unmodifiableCollection(controlMessages));
108 + }
109 +
110 + /**
111 + * Returns the average load value.
112 + *
113 + * @param type OpenFlow message type
114 + * @return load value
115 + */
116 + private long getLoad(OFType type) {
117 + return (long) rateMeterMap.get(type).getOneMinuteRate() /
118 + (long) countMeterMap.get(type).getOneMinuteRate();
119 + }
120 +
121 + /**
122 + * Returns the average meter rate within recent 1 minute.
123 + *
124 + * @param type OpenFlow message type
125 + * @return rate value
126 + */
127 + private long getRate(OFType type) {
128 + return (long) rateMeterMap.get(type).getOneMinuteRate();
129 + }
130 +
131 + /**
132 + * Returns the average meter count within recent 1 minute.
133 + *
134 + * @param type OpenFlow message type
135 + * @return count value
136 + */
137 + private long getCount(OFType type) {
138 + return (long) countMeterMap.get(type).getOneMinuteRate() *
139 + EXECUTE_PERIOD_IN_SECOND;
140 + }
141 +}
1 +/*
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 +package org.onosproject.provider.of.message.impl;
17 +
18 +import com.google.common.collect.BiMap;
19 +import com.google.common.collect.EnumHashBiMap;
20 +import org.onosproject.cpman.ControlMessage;
21 +import org.projectfloodlight.openflow.protocol.OFType;
22 +
23 +import static org.projectfloodlight.openflow.protocol.OFType.*;
24 +import static org.onosproject.cpman.ControlMessage.Type.*;
25 +
26 +/**
27 + * Collection of helper methods to convert protocol agnostic control message to
28 + * messages used in OpenFlow specification.
29 + */
30 +public final class OpenFlowControlMessageMapper {
31 +
32 + // prohibit instantiation
33 + private OpenFlowControlMessageMapper() {
34 + }
35 +
36 + private static final BiMap<OFType, ControlMessage.Type> MESSAGE_TYPE =
37 + EnumHashBiMap.create(OFType.class);
38 +
39 + static {
40 + // key is OpenFlow specific OFType
41 + // value is protocol agnostic ControlMessage.Type
42 + MESSAGE_TYPE.put(PACKET_IN, INCOMING_PACKET);
43 + MESSAGE_TYPE.put(PACKET_OUT, OUTGOING_PACKET);
44 + MESSAGE_TYPE.put(FLOW_MOD, FLOW_MOD_PACKET);
45 + MESSAGE_TYPE.put(FLOW_REMOVED, FLOW_REMOVED_PACKET);
46 + MESSAGE_TYPE.put(STATS_REQUEST, REQUEST_PACKET);
47 + MESSAGE_TYPE.put(STATS_REPLY, REPLY_PACKET);
48 + }
49 +
50 + /**
51 + * Looks up the specified input value to the corresponding value with the specified map.
52 + *
53 + * @param map bidirectional mapping
54 + * @param input input type
55 + * @param cls class of output value
56 + * @param <I> type of input value
57 + * @param <O> type of output value
58 + * @return the corresponding value stored in the specified map
59 + */
60 + private static <I, O> O lookup(BiMap<I, O> map, I input, Class<O> cls) {
61 + if (!map.containsKey(input)) {
62 + throw new RuntimeException(
63 + String.format("No mapping found for %s when converting to %s",
64 + input, cls.getName()));
65 + }
66 + return map.get(input);
67 + }
68 +
69 + /**
70 + * Looks up the corresponding {@link ControlMessage.Type} instance
71 + * from the specified OFType value for OpenFlow message type.
72 + *
73 + * @param type OpenFlow message type
74 + * @return protocol agnostic control message type
75 + */
76 + public static ControlMessage.Type lookupControlMessageType(OFType type) {
77 + return lookup(MESSAGE_TYPE, type, ControlMessage.Type.class);
78 + }
79 +
80 + /**
81 + * Looks up the corresponding {@link OFType} instance from the specified
82 + * ControlMetricType value.
83 + *
84 + * @param type control message type
85 + * @return OpenFlow specific message type
86 + */
87 + public static OFType lookupOFType(ControlMessage.Type type) {
88 + return lookup(MESSAGE_TYPE.inverse(), type, OFType.class);
89 + }
90 +}
...@@ -15,19 +15,39 @@ ...@@ -15,19 +15,39 @@
15 */ 15 */
16 package org.onosproject.provider.of.message.impl; 16 package org.onosproject.provider.of.message.impl;
17 17
18 +import com.google.common.collect.Maps;
18 import org.apache.felix.scr.annotations.Activate; 19 import org.apache.felix.scr.annotations.Activate;
19 import org.apache.felix.scr.annotations.Component; 20 import org.apache.felix.scr.annotations.Component;
20 import org.apache.felix.scr.annotations.Deactivate; 21 import org.apache.felix.scr.annotations.Deactivate;
21 import org.apache.felix.scr.annotations.Reference; 22 import org.apache.felix.scr.annotations.Reference;
22 import org.apache.felix.scr.annotations.ReferenceCardinality; 23 import org.apache.felix.scr.annotations.ReferenceCardinality;
24 +import org.onlab.metrics.MetricsService;
23 import org.onosproject.cpman.message.ControlMessageProvider; 25 import org.onosproject.cpman.message.ControlMessageProvider;
24 import org.onosproject.cpman.message.ControlMessageProviderRegistry; 26 import org.onosproject.cpman.message.ControlMessageProviderRegistry;
25 import org.onosproject.cpman.message.ControlMessageProviderService; 27 import org.onosproject.cpman.message.ControlMessageProviderService;
28 +import org.onosproject.net.DeviceId;
26 import org.onosproject.net.provider.AbstractProvider; 29 import org.onosproject.net.provider.AbstractProvider;
27 import org.onosproject.net.provider.ProviderId; 30 import org.onosproject.net.provider.ProviderId;
31 +import org.onosproject.openflow.controller.Dpid;
32 +import org.onosproject.openflow.controller.OpenFlowController;
33 +import org.onosproject.openflow.controller.OpenFlowEventListener;
34 +import org.onosproject.openflow.controller.OpenFlowSwitch;
35 +import org.onosproject.openflow.controller.OpenFlowSwitchListener;
36 +import org.onosproject.openflow.controller.RoleState;
37 +import org.projectfloodlight.openflow.protocol.OFMessage;
38 +import org.projectfloodlight.openflow.protocol.OFPortStatus;
28 import org.slf4j.Logger; 39 import org.slf4j.Logger;
29 40
41 +import java.util.HashMap;
42 +import java.util.concurrent.Executors;
43 +import java.util.concurrent.ScheduledExecutorService;
44 +import java.util.concurrent.ScheduledFuture;
45 +import java.util.concurrent.TimeUnit;
46 +
47 +import static org.onosproject.net.DeviceId.deviceId;
48 +import static org.onosproject.openflow.controller.Dpid.uri;
30 import static org.slf4j.LoggerFactory.getLogger; 49 import static org.slf4j.LoggerFactory.getLogger;
50 +import static org.onlab.util.Tools.groupedThreads;
31 51
32 /** 52 /**
33 * Provider which uses an OpenFlow controller to collect control message. 53 * Provider which uses an OpenFlow controller to collect control message.
...@@ -36,13 +56,34 @@ import static org.slf4j.LoggerFactory.getLogger; ...@@ -36,13 +56,34 @@ import static org.slf4j.LoggerFactory.getLogger;
36 public class OpenFlowControlMessageProvider extends AbstractProvider 56 public class OpenFlowControlMessageProvider extends AbstractProvider
37 implements ControlMessageProvider { 57 implements ControlMessageProvider {
38 58
39 - private static final Logger LOG = getLogger(OpenFlowControlMessageProvider.class); 59 + private final Logger log = getLogger(getClass());
40 60
41 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 61 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
42 protected ControlMessageProviderRegistry providerRegistry; 62 protected ControlMessageProviderRegistry providerRegistry;
43 63
64 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
65 + protected OpenFlowController controller;
66 +
67 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
68 + protected MetricsService metricsService;
69 +
44 private ControlMessageProviderService providerService; 70 private ControlMessageProviderService providerService;
45 71
72 + private final InternalDeviceProvider listener = new InternalDeviceProvider();
73 +
74 + private final InternalIncomingMessageProvider inMsgListener =
75 + new InternalIncomingMessageProvider();
76 +
77 + private final InternalOutgoingMessageProvider outMsgListener =
78 + new InternalOutgoingMessageProvider();
79 +
80 + private HashMap<Dpid, OpenFlowControlMessageAggregator> aggregators = Maps.newHashMap();
81 + private ScheduledExecutorService executor;
82 + private static final int AGGR_INIT_DELAY = 1;
83 + private static final int AGGR_PERIOD = 1;
84 + private static final TimeUnit AGGR_TIME_UNIT = TimeUnit.MINUTES;
85 + private HashMap<Dpid, ScheduledFuture<?>> executorResults = Maps.newHashMap();
86 +
46 /** 87 /**
47 * Creates a provider with the supplier identifier. 88 * Creates a provider with the supplier identifier.
48 */ 89 */
...@@ -53,13 +94,131 @@ public class OpenFlowControlMessageProvider extends AbstractProvider ...@@ -53,13 +94,131 @@ public class OpenFlowControlMessageProvider extends AbstractProvider
53 @Activate 94 @Activate
54 protected void activate() { 95 protected void activate() {
55 providerService = providerRegistry.register(this); 96 providerService = providerRegistry.register(this);
56 - LOG.info("Started"); 97 +
98 + // listens all OpenFlow device related events
99 + controller.addListener(listener);
100 +
101 + // listens all OpenFlow incoming message events
102 + controller.addEventListener(inMsgListener);
103 + controller.monitorAllEvents(true);
104 +
105 + // listens all OpenFlow outgoing message events
106 + controller.getSwitches().forEach(sw -> sw.addEventListener(outMsgListener));
107 +
108 + executor = Executors.newSingleThreadScheduledExecutor(
109 + groupedThreads("onos/provider", "aggregator"));
110 +
111 + connectInitialDevices();
112 + log.info("Started");
57 } 113 }
58 114
59 @Deactivate 115 @Deactivate
60 protected void deactivate() { 116 protected void deactivate() {
117 + controller.removeListener(listener);
61 providerRegistry.unregister(this); 118 providerRegistry.unregister(this);
62 providerService = null; 119 providerService = null;
63 - LOG.info("Stopped"); 120 +
121 + // stops listening all OpenFlow incoming message events
122 + controller.monitorAllEvents(false);
123 + controller.removeEventListener(inMsgListener);
124 +
125 + // stops listening all OpenFlow outgoing message events
126 + controller.getSwitches().forEach(sw -> sw.removeEventListener(outMsgListener));
127 +
128 + log.info("Stopped");
129 + }
130 +
131 + private void connectInitialDevices() {
132 + for (OpenFlowSwitch sw: controller.getSwitches()) {
133 + try {
134 + listener.switchAdded(new Dpid(sw.getId()));
135 + } catch (Exception e) {
136 + log.warn("Failed initially adding {} : {}", sw.getStringId(), e.getMessage());
137 + log.debug("Error details:", e);
138 + }
139 + }
140 + }
141 +
142 + /**
143 + * A listener for OpenFlow switch event.
144 + */
145 + private class InternalDeviceProvider implements OpenFlowSwitchListener {
146 +
147 + @Override
148 + public void switchAdded(Dpid dpid) {
149 + if (providerService == null) {
150 + return;
151 + }
152 +
153 + OpenFlowSwitch sw = controller.getSwitch(dpid);
154 + if (sw != null) {
155 + // start to monitor the outgoing control messages
156 + sw.addEventListener(outMsgListener);
157 + }
158 +
159 + DeviceId deviceId = deviceId(uri(dpid));
160 + OpenFlowControlMessageAggregator ofcma =
161 + new OpenFlowControlMessageAggregator(metricsService,
162 + providerService, deviceId);
163 + ScheduledFuture result = executor.scheduleAtFixedRate(ofcma,
164 + AGGR_INIT_DELAY, AGGR_PERIOD, AGGR_TIME_UNIT);
165 + aggregators.put(dpid, ofcma);
166 + executorResults.put(dpid, result);
167 + }
168 +
169 + @Override
170 + public void switchRemoved(Dpid dpid) {
171 + if (providerService == null) {
172 + return;
173 + }
174 +
175 + OpenFlowSwitch sw = controller.getSwitch(dpid);
176 + if (sw != null) {
177 + // stop monitoring the outgoing control messages
178 + sw.removeEventListener(outMsgListener);
179 + }
180 +
181 + // removes the aggregator when switch is removed
182 + // this also stops the aggregator from running
183 + OpenFlowControlMessageAggregator aggregator = aggregators.remove(dpid);
184 + if (aggregator != null) {
185 + executorResults.get(dpid).cancel(true);
186 + executorResults.remove(dpid);
187 + }
188 + }
189 +
190 + @Override
191 + public void switchChanged(Dpid dpid) {
192 + }
193 +
194 + @Override
195 + public void portChanged(Dpid dpid, OFPortStatus status) {
196 + }
197 +
198 + @Override
199 + public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) {
200 + }
201 + }
202 +
203 + /**
204 + * A listener for incoming OpenFlow messages.
205 + */
206 + private class InternalIncomingMessageProvider implements OpenFlowEventListener {
207 +
208 + @Override
209 + public void handleMessage(Dpid dpid, OFMessage msg) {
210 + aggregators.get(dpid).increment(msg);
211 + }
212 + }
213 +
214 + /**
215 + * A listener for outgoing OpenFlow messages.
216 + */
217 + private class InternalOutgoingMessageProvider implements OpenFlowEventListener {
218 +
219 + @Override
220 + public void handleMessage(Dpid dpid, OFMessage msg) {
221 + aggregators.get(dpid).increment(msg);
222 + }
64 } 223 }
65 } 224 }
......