Jian Li | e0e01c2 | 2016-02-09 14:02:49 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Jian Li | e0e01c2 | 2016-02-09 14:02:49 -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 | */ |
| 16 | package org.onosproject.provider.of.message.impl; |
| 17 | |
Jian Li | 1d13c26 | 2016-02-09 14:58:28 -0800 | [diff] [blame] | 18 | import com.google.common.collect.Maps; |
Jian Li | e0e01c2 | 2016-02-09 14:02:49 -0800 | [diff] [blame] | 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Jian Li | 1d13c26 | 2016-02-09 14:58:28 -0800 | [diff] [blame] | 24 | import org.onlab.metrics.MetricsService; |
Jian Li | a1d7f27 | 2016-03-28 17:21:47 -0700 | [diff] [blame] | 25 | import org.onlab.util.SharedScheduledExecutorService; |
Jian Li | 66f1526 | 2016-03-03 11:18:40 -0800 | [diff] [blame] | 26 | import org.onlab.util.SharedScheduledExecutors; |
Jian Li | e0e01c2 | 2016-02-09 14:02:49 -0800 | [diff] [blame] | 27 | import org.onosproject.cpman.message.ControlMessageProvider; |
| 28 | import org.onosproject.cpman.message.ControlMessageProviderRegistry; |
| 29 | import org.onosproject.cpman.message.ControlMessageProviderService; |
Jian Li | 1d13c26 | 2016-02-09 14:58:28 -0800 | [diff] [blame] | 30 | import org.onosproject.net.DeviceId; |
Jian Li | e0e01c2 | 2016-02-09 14:02:49 -0800 | [diff] [blame] | 31 | import org.onosproject.net.provider.AbstractProvider; |
| 32 | import org.onosproject.net.provider.ProviderId; |
Jian Li | 1d13c26 | 2016-02-09 14:58:28 -0800 | [diff] [blame] | 33 | import org.onosproject.openflow.controller.Dpid; |
| 34 | import org.onosproject.openflow.controller.OpenFlowController; |
Jian Li | a78cdb2 | 2016-04-21 13:03:58 -0700 | [diff] [blame] | 35 | import org.onosproject.openflow.controller.OpenFlowMessageListener; |
Jian Li | 1d13c26 | 2016-02-09 14:58:28 -0800 | [diff] [blame] | 36 | import org.onosproject.openflow.controller.OpenFlowSwitch; |
| 37 | import org.onosproject.openflow.controller.OpenFlowSwitchListener; |
| 38 | import org.onosproject.openflow.controller.RoleState; |
| 39 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 40 | import org.projectfloodlight.openflow.protocol.OFPortStatus; |
Jian Li | 7ceb7b0 | 2016-04-04 12:42:40 -0700 | [diff] [blame] | 41 | import org.projectfloodlight.openflow.protocol.OFType; |
Jian Li | e0e01c2 | 2016-02-09 14:02:49 -0800 | [diff] [blame] | 42 | import org.slf4j.Logger; |
| 43 | |
Jian Li | 1d13c26 | 2016-02-09 14:58:28 -0800 | [diff] [blame] | 44 | import java.util.HashMap; |
Jian Li | a78cdb2 | 2016-04-21 13:03:58 -0700 | [diff] [blame] | 45 | import java.util.List; |
Jian Li | 1d13c26 | 2016-02-09 14:58:28 -0800 | [diff] [blame] | 46 | import java.util.concurrent.ScheduledFuture; |
| 47 | import java.util.concurrent.TimeUnit; |
| 48 | |
| 49 | import static org.onosproject.net.DeviceId.deviceId; |
| 50 | import static org.onosproject.openflow.controller.Dpid.uri; |
Jian Li | e0e01c2 | 2016-02-09 14:02:49 -0800 | [diff] [blame] | 51 | import static org.slf4j.LoggerFactory.getLogger; |
| 52 | |
| 53 | /** |
| 54 | * Provider which uses an OpenFlow controller to collect control message. |
| 55 | */ |
| 56 | @Component(immediate = true) |
| 57 | public class OpenFlowControlMessageProvider extends AbstractProvider |
| 58 | implements ControlMessageProvider { |
| 59 | |
Jian Li | 1d13c26 | 2016-02-09 14:58:28 -0800 | [diff] [blame] | 60 | private final Logger log = getLogger(getClass()); |
Jian Li | e0e01c2 | 2016-02-09 14:02:49 -0800 | [diff] [blame] | 61 | |
| 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 63 | protected ControlMessageProviderRegistry providerRegistry; |
| 64 | |
Jian Li | 1d13c26 | 2016-02-09 14:58:28 -0800 | [diff] [blame] | 65 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 66 | protected OpenFlowController controller; |
| 67 | |
| 68 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 69 | protected MetricsService metricsService; |
| 70 | |
Jian Li | e0e01c2 | 2016-02-09 14:02:49 -0800 | [diff] [blame] | 71 | private ControlMessageProviderService providerService; |
| 72 | |
Jian Li | 1d13c26 | 2016-02-09 14:58:28 -0800 | [diff] [blame] | 73 | private final InternalDeviceProvider listener = new InternalDeviceProvider(); |
| 74 | |
Jian Li | a78cdb2 | 2016-04-21 13:03:58 -0700 | [diff] [blame] | 75 | private final InternalControlMessageListener messageListener = |
| 76 | new InternalControlMessageListener(); |
Jian Li | 1d13c26 | 2016-02-09 14:58:28 -0800 | [diff] [blame] | 77 | |
| 78 | private HashMap<Dpid, OpenFlowControlMessageAggregator> aggregators = Maps.newHashMap(); |
Jian Li | a1d7f27 | 2016-03-28 17:21:47 -0700 | [diff] [blame] | 79 | private SharedScheduledExecutorService executor; |
Jian Li | 1d13c26 | 2016-02-09 14:58:28 -0800 | [diff] [blame] | 80 | private static final int AGGR_INIT_DELAY = 1; |
| 81 | private static final int AGGR_PERIOD = 1; |
| 82 | private static final TimeUnit AGGR_TIME_UNIT = TimeUnit.MINUTES; |
| 83 | private HashMap<Dpid, ScheduledFuture<?>> executorResults = Maps.newHashMap(); |
| 84 | |
Jian Li | e0e01c2 | 2016-02-09 14:02:49 -0800 | [diff] [blame] | 85 | /** |
| 86 | * Creates a provider with the supplier identifier. |
| 87 | */ |
| 88 | public OpenFlowControlMessageProvider() { |
| 89 | super(new ProviderId("of", "org.onosproject.provider.openflow")); |
| 90 | } |
| 91 | |
| 92 | @Activate |
| 93 | protected void activate() { |
| 94 | providerService = providerRegistry.register(this); |
Jian Li | 1d13c26 | 2016-02-09 14:58:28 -0800 | [diff] [blame] | 95 | |
| 96 | // listens all OpenFlow device related events |
| 97 | controller.addListener(listener); |
| 98 | |
Jian Li | a78cdb2 | 2016-04-21 13:03:58 -0700 | [diff] [blame] | 99 | // listens all OpenFlow control message |
| 100 | controller.addMessageListener(messageListener); |
Jian Li | 1d13c26 | 2016-02-09 14:58:28 -0800 | [diff] [blame] | 101 | |
Jian Li | 66f1526 | 2016-03-03 11:18:40 -0800 | [diff] [blame] | 102 | executor = SharedScheduledExecutors.getSingleThreadExecutor(); |
Jian Li | 1d13c26 | 2016-02-09 14:58:28 -0800 | [diff] [blame] | 103 | |
| 104 | connectInitialDevices(); |
| 105 | log.info("Started"); |
Jian Li | e0e01c2 | 2016-02-09 14:02:49 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | @Deactivate |
| 109 | protected void deactivate() { |
Kavitha Alagesan | 47b80fa | 2016-11-10 16:11:32 +0530 | [diff] [blame] | 110 | disconnectDevices(); |
Jian Li | 1d13c26 | 2016-02-09 14:58:28 -0800 | [diff] [blame] | 111 | controller.removeListener(listener); |
Jian Li | e0e01c2 | 2016-02-09 14:02:49 -0800 | [diff] [blame] | 112 | providerRegistry.unregister(this); |
| 113 | providerService = null; |
Jian Li | 1d13c26 | 2016-02-09 14:58:28 -0800 | [diff] [blame] | 114 | |
Jian Li | a78cdb2 | 2016-04-21 13:03:58 -0700 | [diff] [blame] | 115 | // stops listening all OpenFlow control message events |
| 116 | controller.removeMessageListener(messageListener); |
Jian Li | 1d13c26 | 2016-02-09 14:58:28 -0800 | [diff] [blame] | 117 | |
| 118 | log.info("Stopped"); |
| 119 | } |
| 120 | |
| 121 | private void connectInitialDevices() { |
| 122 | for (OpenFlowSwitch sw: controller.getSwitches()) { |
| 123 | try { |
| 124 | listener.switchAdded(new Dpid(sw.getId())); |
| 125 | } catch (Exception e) { |
| 126 | log.warn("Failed initially adding {} : {}", sw.getStringId(), e.getMessage()); |
| 127 | log.debug("Error details:", e); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
Kavitha Alagesan | 47b80fa | 2016-11-10 16:11:32 +0530 | [diff] [blame] | 132 | private void disconnectDevices() { |
| 133 | for (OpenFlowSwitch sw: controller.getSwitches()) { |
| 134 | try { |
| 135 | listener.switchRemoved(new Dpid(sw.getId())); |
| 136 | } catch (Exception e) { |
| 137 | log.warn("Failed to remove {} : {}", sw.getStringId(), e.getMessage()); |
| 138 | log.debug("Error details:", e); |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
Jian Li | 1d13c26 | 2016-02-09 14:58:28 -0800 | [diff] [blame] | 143 | /** |
| 144 | * A listener for OpenFlow switch event. |
| 145 | */ |
| 146 | private class InternalDeviceProvider implements OpenFlowSwitchListener { |
| 147 | |
| 148 | @Override |
| 149 | public void switchAdded(Dpid dpid) { |
| 150 | if (providerService == null) { |
| 151 | return; |
| 152 | } |
| 153 | |
Jian Li | 1d13c26 | 2016-02-09 14:58:28 -0800 | [diff] [blame] | 154 | DeviceId deviceId = deviceId(uri(dpid)); |
| 155 | OpenFlowControlMessageAggregator ofcma = |
| 156 | new OpenFlowControlMessageAggregator(metricsService, |
| 157 | providerService, deviceId); |
| 158 | ScheduledFuture result = executor.scheduleAtFixedRate(ofcma, |
Jian Li | a1d7f27 | 2016-03-28 17:21:47 -0700 | [diff] [blame] | 159 | AGGR_INIT_DELAY, AGGR_PERIOD, AGGR_TIME_UNIT, true); |
Jian Li | 1d13c26 | 2016-02-09 14:58:28 -0800 | [diff] [blame] | 160 | aggregators.put(dpid, ofcma); |
| 161 | executorResults.put(dpid, result); |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public void switchRemoved(Dpid dpid) { |
| 166 | if (providerService == null) { |
| 167 | return; |
| 168 | } |
| 169 | |
Jian Li | 1d13c26 | 2016-02-09 14:58:28 -0800 | [diff] [blame] | 170 | // removes the aggregator when switch is removed |
| 171 | // this also stops the aggregator from running |
| 172 | OpenFlowControlMessageAggregator aggregator = aggregators.remove(dpid); |
| 173 | if (aggregator != null) { |
| 174 | executorResults.get(dpid).cancel(true); |
| 175 | executorResults.remove(dpid); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public void switchChanged(Dpid dpid) { |
| 181 | } |
| 182 | |
| 183 | @Override |
| 184 | public void portChanged(Dpid dpid, OFPortStatus status) { |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) { |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | /** |
Jian Li | a78cdb2 | 2016-04-21 13:03:58 -0700 | [diff] [blame] | 193 | * A listener for all OpenFlow control messages. |
Jian Li | 1d13c26 | 2016-02-09 14:58:28 -0800 | [diff] [blame] | 194 | */ |
Jian Li | a78cdb2 | 2016-04-21 13:03:58 -0700 | [diff] [blame] | 195 | private class InternalControlMessageListener implements OpenFlowMessageListener { |
Jian Li | 1d13c26 | 2016-02-09 14:58:28 -0800 | [diff] [blame] | 196 | |
| 197 | @Override |
Jian Li | a78cdb2 | 2016-04-21 13:03:58 -0700 | [diff] [blame] | 198 | public void handleIncomingMessage(Dpid dpid, OFMessage msg) { |
Jian Li | 7ceb7b0 | 2016-04-04 12:42:40 -0700 | [diff] [blame] | 199 | if (msg.getType() == OFType.PACKET_IN || |
| 200 | msg.getType() == OFType.FLOW_MOD || |
| 201 | msg.getType() == OFType.STATS_REPLY) { |
Jian Li | a371e7a | 2016-04-06 17:40:41 -0700 | [diff] [blame] | 202 | aggregators.computeIfPresent(dpid, (k, v) -> { |
| 203 | v.increment(msg); |
| 204 | return v; |
| 205 | }); |
Jian Li | 7ceb7b0 | 2016-04-04 12:42:40 -0700 | [diff] [blame] | 206 | } |
Jian Li | 1d13c26 | 2016-02-09 14:58:28 -0800 | [diff] [blame] | 207 | } |
Jian Li | 1d13c26 | 2016-02-09 14:58:28 -0800 | [diff] [blame] | 208 | |
| 209 | @Override |
Jian Li | a78cdb2 | 2016-04-21 13:03:58 -0700 | [diff] [blame] | 210 | public void handleOutgoingMessage(Dpid dpid, List<OFMessage> msgs) { |
| 211 | for (OFMessage msg : msgs) { |
| 212 | if (msg.getType() == OFType.PACKET_OUT || |
| 213 | msg.getType() == OFType.FLOW_MOD || |
| 214 | msg.getType() == OFType.STATS_REQUEST) { |
| 215 | aggregators.computeIfPresent(dpid, (k, v) -> { |
| 216 | v.increment(msg); |
| 217 | return v; |
| 218 | }); |
| 219 | } |
| 220 | } |
Jian Li | 1d13c26 | 2016-02-09 14:58:28 -0800 | [diff] [blame] | 221 | } |
Jian Li | e0e01c2 | 2016-02-09 14:02:49 -0800 | [diff] [blame] | 222 | } |
| 223 | } |