Jian Li | e0e01c2 | 2016-02-09 14:02:49 -0800 | [diff] [blame] | 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 | |
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; |
| 35 | import org.onosproject.openflow.controller.OpenFlowEventListener; |
| 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 | 1d13c26 | 2016-02-09 14:58:28 -0800 | [diff] [blame] | 45 | import java.util.concurrent.ScheduledFuture; |
| 46 | import java.util.concurrent.TimeUnit; |
| 47 | |
| 48 | import static org.onosproject.net.DeviceId.deviceId; |
| 49 | import static org.onosproject.openflow.controller.Dpid.uri; |
Jian Li | e0e01c2 | 2016-02-09 14:02:49 -0800 | [diff] [blame] | 50 | import static org.slf4j.LoggerFactory.getLogger; |
| 51 | |
| 52 | /** |
| 53 | * Provider which uses an OpenFlow controller to collect control message. |
| 54 | */ |
| 55 | @Component(immediate = true) |
| 56 | public class OpenFlowControlMessageProvider extends AbstractProvider |
| 57 | implements ControlMessageProvider { |
| 58 | |
Jian Li | 1d13c26 | 2016-02-09 14:58:28 -0800 | [diff] [blame] | 59 | private final Logger log = getLogger(getClass()); |
Jian Li | e0e01c2 | 2016-02-09 14:02:49 -0800 | [diff] [blame] | 60 | |
| 61 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 62 | protected ControlMessageProviderRegistry providerRegistry; |
| 63 | |
Jian Li | 1d13c26 | 2016-02-09 14:58:28 -0800 | [diff] [blame] | 64 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 65 | protected OpenFlowController controller; |
| 66 | |
| 67 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 68 | protected MetricsService metricsService; |
| 69 | |
Jian Li | e0e01c2 | 2016-02-09 14:02:49 -0800 | [diff] [blame] | 70 | private ControlMessageProviderService providerService; |
| 71 | |
Jian Li | 1d13c26 | 2016-02-09 14:58:28 -0800 | [diff] [blame] | 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(); |
Jian Li | a1d7f27 | 2016-03-28 17:21:47 -0700 | [diff] [blame] | 81 | private SharedScheduledExecutorService executor; |
Jian Li | 1d13c26 | 2016-02-09 14:58:28 -0800 | [diff] [blame] | 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 | |
Jian Li | e0e01c2 | 2016-02-09 14:02:49 -0800 | [diff] [blame] | 87 | /** |
| 88 | * Creates a provider with the supplier identifier. |
| 89 | */ |
| 90 | public OpenFlowControlMessageProvider() { |
| 91 | super(new ProviderId("of", "org.onosproject.provider.openflow")); |
| 92 | } |
| 93 | |
| 94 | @Activate |
| 95 | protected void activate() { |
| 96 | providerService = providerRegistry.register(this); |
Jian Li | 1d13c26 | 2016-02-09 14:58:28 -0800 | [diff] [blame] | 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 | |
Jian Li | 66f1526 | 2016-03-03 11:18:40 -0800 | [diff] [blame] | 108 | executor = SharedScheduledExecutors.getSingleThreadExecutor(); |
Jian Li | 1d13c26 | 2016-02-09 14:58:28 -0800 | [diff] [blame] | 109 | |
| 110 | connectInitialDevices(); |
| 111 | log.info("Started"); |
Jian Li | e0e01c2 | 2016-02-09 14:02:49 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | @Deactivate |
| 115 | protected void deactivate() { |
Jian Li | 1d13c26 | 2016-02-09 14:58:28 -0800 | [diff] [blame] | 116 | controller.removeListener(listener); |
Jian Li | e0e01c2 | 2016-02-09 14:02:49 -0800 | [diff] [blame] | 117 | providerRegistry.unregister(this); |
| 118 | providerService = null; |
Jian Li | 1d13c26 | 2016-02-09 14:58:28 -0800 | [diff] [blame] | 119 | |
| 120 | // stops listening all OpenFlow incoming message events |
| 121 | controller.monitorAllEvents(false); |
| 122 | controller.removeEventListener(inMsgListener); |
| 123 | |
| 124 | // stops listening all OpenFlow outgoing message events |
| 125 | controller.getSwitches().forEach(sw -> sw.removeEventListener(outMsgListener)); |
| 126 | |
| 127 | log.info("Stopped"); |
| 128 | } |
| 129 | |
| 130 | private void connectInitialDevices() { |
| 131 | for (OpenFlowSwitch sw: controller.getSwitches()) { |
| 132 | try { |
| 133 | listener.switchAdded(new Dpid(sw.getId())); |
| 134 | } catch (Exception e) { |
| 135 | log.warn("Failed initially adding {} : {}", sw.getStringId(), e.getMessage()); |
| 136 | log.debug("Error details:", e); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * A listener for OpenFlow switch event. |
| 143 | */ |
| 144 | private class InternalDeviceProvider implements OpenFlowSwitchListener { |
| 145 | |
| 146 | @Override |
| 147 | public void switchAdded(Dpid dpid) { |
| 148 | if (providerService == null) { |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
| 153 | if (sw != null) { |
| 154 | // start to monitor the outgoing control messages |
| 155 | sw.addEventListener(outMsgListener); |
| 156 | } |
| 157 | |
| 158 | DeviceId deviceId = deviceId(uri(dpid)); |
| 159 | OpenFlowControlMessageAggregator ofcma = |
| 160 | new OpenFlowControlMessageAggregator(metricsService, |
| 161 | providerService, deviceId); |
| 162 | ScheduledFuture result = executor.scheduleAtFixedRate(ofcma, |
Jian Li | a1d7f27 | 2016-03-28 17:21:47 -0700 | [diff] [blame] | 163 | AGGR_INIT_DELAY, AGGR_PERIOD, AGGR_TIME_UNIT, true); |
Jian Li | 1d13c26 | 2016-02-09 14:58:28 -0800 | [diff] [blame] | 164 | aggregators.put(dpid, ofcma); |
| 165 | executorResults.put(dpid, result); |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public void switchRemoved(Dpid dpid) { |
| 170 | if (providerService == null) { |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
| 175 | if (sw != null) { |
| 176 | // stop monitoring the outgoing control messages |
| 177 | sw.removeEventListener(outMsgListener); |
| 178 | } |
| 179 | |
| 180 | // removes the aggregator when switch is removed |
| 181 | // this also stops the aggregator from running |
| 182 | OpenFlowControlMessageAggregator aggregator = aggregators.remove(dpid); |
| 183 | if (aggregator != null) { |
| 184 | executorResults.get(dpid).cancel(true); |
| 185 | executorResults.remove(dpid); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public void switchChanged(Dpid dpid) { |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public void portChanged(Dpid dpid, OFPortStatus status) { |
| 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) { |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * A listener for incoming OpenFlow messages. |
| 204 | */ |
| 205 | private class InternalIncomingMessageProvider implements OpenFlowEventListener { |
| 206 | |
| 207 | @Override |
| 208 | public void handleMessage(Dpid dpid, OFMessage msg) { |
Jian Li | 7ceb7b0 | 2016-04-04 12:42:40 -0700 | [diff] [blame^] | 209 | if (msg.getType() == OFType.PACKET_IN || |
| 210 | msg.getType() == OFType.FLOW_MOD || |
| 211 | msg.getType() == OFType.STATS_REPLY) { |
| 212 | aggregators.get(dpid).increment(msg); |
| 213 | } |
Jian Li | 1d13c26 | 2016-02-09 14:58:28 -0800 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * A listener for outgoing OpenFlow messages. |
| 219 | */ |
| 220 | private class InternalOutgoingMessageProvider implements OpenFlowEventListener { |
| 221 | |
| 222 | @Override |
| 223 | public void handleMessage(Dpid dpid, OFMessage msg) { |
| 224 | aggregators.get(dpid).increment(msg); |
| 225 | } |
Jian Li | e0e01c2 | 2016-02-09 14:02:49 -0800 | [diff] [blame] | 226 | } |
| 227 | } |