Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.provider.of.device.impl; |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 17 | |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 18 | import static com.google.common.base.Preconditions.checkArgument; |
| 19 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 20 | import static org.onlab.util.Tools.get; |
| 21 | import static org.onosproject.net.DeviceId.deviceId; |
| 22 | import static org.onosproject.net.Port.Type.COPPER; |
| 23 | import static org.onosproject.net.Port.Type.FIBER; |
| 24 | import static org.onosproject.openflow.controller.Dpid.dpid; |
| 25 | import static org.onosproject.openflow.controller.Dpid.uri; |
| 26 | import static org.slf4j.LoggerFactory.getLogger; |
| 27 | |
| 28 | import java.util.ArrayList; |
| 29 | import java.util.Collection; |
| 30 | import java.util.Collections; |
| 31 | import java.util.Dictionary; |
| 32 | import java.util.HashMap; |
| 33 | import java.util.HashSet; |
| 34 | import java.util.List; |
| 35 | |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 36 | import org.apache.felix.scr.annotations.Activate; |
| 37 | import org.apache.felix.scr.annotations.Component; |
| 38 | import org.apache.felix.scr.annotations.Deactivate; |
Dusan Pajin | bab8a5e | 2015-07-24 17:37:19 +0200 | [diff] [blame] | 39 | import org.apache.felix.scr.annotations.Modified; |
| 40 | import org.apache.felix.scr.annotations.Property; |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 41 | import org.apache.felix.scr.annotations.Reference; |
| 42 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 43 | import org.onlab.packet.ChassisId; |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 44 | import org.onlab.util.Frequency; |
| 45 | import org.onlab.util.Spectrum; |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 46 | import org.onosproject.cfg.ComponentConfigService; |
Thomas Vachuska | b52a014 | 2015-04-21 17:48:15 -0700 | [diff] [blame] | 47 | import org.onosproject.net.AnnotationKeys; |
Marc De Leenheer | fc913dd | 2015-07-30 16:04:55 -0700 | [diff] [blame] | 48 | import org.onosproject.net.ChannelSpacing; |
Toru Furusawa | 72ee30c | 2016-01-08 13:29:04 -0800 | [diff] [blame^] | 49 | import org.onosproject.net.CltSignalType; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 50 | import org.onosproject.net.DefaultAnnotations; |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 51 | import org.onosproject.net.Device; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 52 | import org.onosproject.net.DeviceId; |
Marc De Leenheer | fc913dd | 2015-07-30 16:04:55 -0700 | [diff] [blame] | 53 | import org.onosproject.net.GridType; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 54 | import org.onosproject.net.MastershipRole; |
Marc De Leenheer | fc913dd | 2015-07-30 16:04:55 -0700 | [diff] [blame] | 55 | import org.onosproject.net.OchSignal; |
| 56 | import org.onosproject.net.OduSignalType; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 57 | import org.onosproject.net.Port; |
| 58 | import org.onosproject.net.PortNumber; |
| 59 | import org.onosproject.net.SparseAnnotations; |
| 60 | import org.onosproject.net.device.DefaultDeviceDescription; |
| 61 | import org.onosproject.net.device.DefaultPortDescription; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 62 | import org.onosproject.net.device.DefaultPortStatistics; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 63 | import org.onosproject.net.device.DeviceDescription; |
| 64 | import org.onosproject.net.device.DeviceProvider; |
| 65 | import org.onosproject.net.device.DeviceProviderRegistry; |
| 66 | import org.onosproject.net.device.DeviceProviderService; |
Marc De Leenheer | fc913dd | 2015-07-30 16:04:55 -0700 | [diff] [blame] | 67 | import org.onosproject.net.device.OchPortDescription; |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 68 | import org.onosproject.net.device.OduCltPortDescription; |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 69 | import org.onosproject.net.device.OmsPortDescription; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 70 | import org.onosproject.net.device.PortDescription; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 71 | import org.onosproject.net.device.PortStatistics; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 72 | import org.onosproject.net.provider.AbstractProvider; |
| 73 | import org.onosproject.net.provider.ProviderId; |
| 74 | import org.onosproject.openflow.controller.Dpid; |
| 75 | import org.onosproject.openflow.controller.OpenFlowController; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 76 | import org.onosproject.openflow.controller.OpenFlowEventListener; |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 77 | import org.onosproject.openflow.controller.OpenFlowOpticalSwitch; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 78 | import org.onosproject.openflow.controller.OpenFlowSwitch; |
| 79 | import org.onosproject.openflow.controller.OpenFlowSwitchListener; |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 80 | import org.onosproject.openflow.controller.PortDescPropertyType; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 81 | import org.onosproject.openflow.controller.RoleState; |
Dusan Pajin | bab8a5e | 2015-07-24 17:37:19 +0200 | [diff] [blame] | 82 | import org.osgi.service.component.ComponentContext; |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 83 | import org.projectfloodlight.openflow.protocol.OFCalientPortDescStatsEntry; |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 84 | import org.projectfloodlight.openflow.protocol.OFExpPort; |
| 85 | import org.projectfloodlight.openflow.protocol.OFExpPortDescPropOpticalTransport; |
| 86 | import org.projectfloodlight.openflow.protocol.OFExpPortOpticalTransportLayerEntry; |
Ayaka Koshibe | e8708e3 | 2014-10-22 13:40:18 -0700 | [diff] [blame] | 87 | import org.projectfloodlight.openflow.protocol.OFFactory; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 88 | import org.projectfloodlight.openflow.protocol.OFMessage; |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 89 | import org.projectfloodlight.openflow.protocol.OFObject; |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 90 | import org.projectfloodlight.openflow.protocol.OFPortConfig; |
alshabib | 25c8eec | 2014-09-04 16:41:31 -0700 | [diff] [blame] | 91 | import org.projectfloodlight.openflow.protocol.OFPortDesc; |
Marc De Leenheer | fc913dd | 2015-07-30 16:04:55 -0700 | [diff] [blame] | 92 | import org.projectfloodlight.openflow.protocol.OFPortDescPropOpticalTransport; |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 93 | import org.projectfloodlight.openflow.protocol.OFPortFeatures; |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 94 | import org.projectfloodlight.openflow.protocol.OFPortOptical; |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 95 | import org.projectfloodlight.openflow.protocol.OFPortOpticalTransportLayerClass; |
| 96 | import org.projectfloodlight.openflow.protocol.OFPortOpticalTransportSignalType; |
alshabib | afc514a | 2014-12-01 14:44:05 -0800 | [diff] [blame] | 97 | import org.projectfloodlight.openflow.protocol.OFPortReason; |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 98 | import org.projectfloodlight.openflow.protocol.OFPortState; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 99 | import org.projectfloodlight.openflow.protocol.OFPortStatsEntry; |
| 100 | import org.projectfloodlight.openflow.protocol.OFPortStatsReply; |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 101 | import org.projectfloodlight.openflow.protocol.OFPortStatus; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 102 | import org.projectfloodlight.openflow.protocol.OFStatsReply; |
Thomas Vachuska | 893bf4b | 2015-05-29 18:13:18 -0700 | [diff] [blame] | 103 | import org.projectfloodlight.openflow.protocol.OFStatsReplyFlags; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 104 | import org.projectfloodlight.openflow.protocol.OFStatsType; |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 105 | import org.projectfloodlight.openflow.protocol.OFVersion; |
| 106 | import org.projectfloodlight.openflow.types.PortSpeed; |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 107 | import org.slf4j.Logger; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 108 | |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 109 | import com.google.common.base.Strings; |
| 110 | import com.google.common.collect.Lists; |
| 111 | import com.google.common.collect.Maps; |
| 112 | import com.google.common.collect.Sets; |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 113 | |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 114 | /** |
tom | b1260e4 | 2014-08-26 18:39:57 -0700 | [diff] [blame] | 115 | * Provider which uses an OpenFlow controller to detect network |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 116 | * infrastructure devices. |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 117 | */ |
tom | b1260e4 | 2014-08-26 18:39:57 -0700 | [diff] [blame] | 118 | @Component(immediate = true) |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 119 | public class OpenFlowDeviceProvider extends AbstractProvider implements DeviceProvider { |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 120 | |
alshabib | a89cc58 | 2014-09-09 16:43:00 -0700 | [diff] [blame] | 121 | private static final Logger LOG = getLogger(OpenFlowDeviceProvider.class); |
Thomas Vachuska | d07c092 | 2015-10-06 14:48:06 -0700 | [diff] [blame] | 122 | |
Charles Chan | 25b7732 | 2015-12-13 01:00:56 -0800 | [diff] [blame] | 123 | //TODO consider renaming KBPS and MBPS (as they are used to convert by division) |
| 124 | private static final long KBPS = 1_000; |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 125 | private static final long MBPS = 1_000 * 1_000; |
Rimon Ashkenazy | e320103 | 2016-01-11 14:27:30 +0200 | [diff] [blame] | 126 | private static final Frequency FREQ50 = Frequency.ofGHz(50); |
| 127 | private static final Frequency FREQ191_7 = Frequency.ofGHz(191_700); |
Sho SHIMIZU | 00762ee | 2016-01-05 16:32:24 -0800 | [diff] [blame] | 128 | private static final Frequency FREQ4_4 = Frequency.ofGHz(4_400); |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 129 | |
| 130 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 131 | protected DeviceProviderRegistry providerRegistry; |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 132 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 133 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 134 | protected OpenFlowController controller; |
| 135 | |
Dusan Pajin | bab8a5e | 2015-07-24 17:37:19 +0200 | [diff] [blame] | 136 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 137 | protected ComponentConfigService cfgService; |
| 138 | |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 139 | private DeviceProviderService providerService; |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 140 | |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 141 | private final InternalDeviceProvider listener = new InternalDeviceProvider(); |
| 142 | |
Jian Li | 152b885 | 2015-12-07 14:47:25 -0800 | [diff] [blame] | 143 | private final IncomingMessageProvider inMsgListener = new IncomingMessageProvider(); |
| 144 | |
| 145 | private final OutgoingMessageProvider outMsgListener = new OutgoingMessageProvider(); |
| 146 | |
| 147 | private boolean isCtrlMsgMonitor; |
| 148 | |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 149 | // TODO: We need to make the poll interval configurable. |
Srikanth Vavilapalli | 78baf58 | 2015-06-05 11:40:14 -0700 | [diff] [blame] | 150 | static final int POLL_INTERVAL = 5; |
Dusan Pajin | bab8a5e | 2015-07-24 17:37:19 +0200 | [diff] [blame] | 151 | @Property(name = "PortStatsPollFrequency", intValue = POLL_INTERVAL, |
| 152 | label = "Frequency (in seconds) for polling switch Port statistics") |
| 153 | private int portStatsPollFrequency = POLL_INTERVAL; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 154 | |
| 155 | private HashMap<Dpid, PortStatsCollector> collectors = Maps.newHashMap(); |
tom | d40fc7a | 2014-09-04 16:41:10 -0700 | [diff] [blame] | 156 | |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 157 | /** |
| 158 | * Creates an OpenFlow device provider. |
| 159 | */ |
| 160 | public OpenFlowDeviceProvider() { |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 161 | super(new ProviderId("of", "org.onosproject.provider.openflow")); |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 162 | } |
| 163 | |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 164 | @Activate |
Dusan Pajin | bab8a5e | 2015-07-24 17:37:19 +0200 | [diff] [blame] | 165 | public void activate(ComponentContext context) { |
| 166 | cfgService.registerProperties(getClass()); |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 167 | providerService = providerRegistry.register(this); |
tom | d40fc7a | 2014-09-04 16:41:10 -0700 | [diff] [blame] | 168 | controller.addListener(listener); |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 169 | controller.addEventListener(listener); |
Jian Li | 152b885 | 2015-12-07 14:47:25 -0800 | [diff] [blame] | 170 | |
Thomas Vachuska | d07c092 | 2015-10-06 14:48:06 -0700 | [diff] [blame] | 171 | connectInitialDevices(); |
alshabib | a89cc58 | 2014-09-09 16:43:00 -0700 | [diff] [blame] | 172 | LOG.info("Started"); |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | @Deactivate |
Dusan Pajin | bab8a5e | 2015-07-24 17:37:19 +0200 | [diff] [blame] | 176 | public void deactivate(ComponentContext context) { |
| 177 | cfgService.unregisterProperties(getClass(), false); |
tom | d40fc7a | 2014-09-04 16:41:10 -0700 | [diff] [blame] | 178 | controller.removeListener(listener); |
Thomas Vachuska | d07c092 | 2015-10-06 14:48:06 -0700 | [diff] [blame] | 179 | providerRegistry.unregister(this); |
Thomas Vachuska | 74bcd92 | 2015-06-02 13:08:57 -0700 | [diff] [blame] | 180 | collectors.values().forEach(PortStatsCollector::stop); |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 181 | providerService = null; |
alshabib | a89cc58 | 2014-09-09 16:43:00 -0700 | [diff] [blame] | 182 | LOG.info("Stopped"); |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Dusan Pajin | bab8a5e | 2015-07-24 17:37:19 +0200 | [diff] [blame] | 185 | @Modified |
| 186 | public void modified(ComponentContext context) { |
| 187 | Dictionary<?, ?> properties = context.getProperties(); |
| 188 | int newPortStatsPollFrequency; |
| 189 | try { |
| 190 | String s = get(properties, "PortStatsPollFrequency"); |
| 191 | newPortStatsPollFrequency = isNullOrEmpty(s) ? portStatsPollFrequency : Integer.parseInt(s.trim()); |
| 192 | |
| 193 | } catch (NumberFormatException | ClassCastException e) { |
| 194 | newPortStatsPollFrequency = portStatsPollFrequency; |
| 195 | } |
| 196 | |
| 197 | if (newPortStatsPollFrequency != portStatsPollFrequency) { |
| 198 | portStatsPollFrequency = newPortStatsPollFrequency; |
| 199 | collectors.values().forEach(psc -> psc.adjustPollInterval(portStatsPollFrequency)); |
| 200 | } |
| 201 | |
| 202 | LOG.info("Settings: portStatsPollFrequency={}", portStatsPollFrequency); |
| 203 | } |
Ayaka Koshibe | e60d452 | 2014-10-28 15:07:00 -0700 | [diff] [blame] | 204 | |
Thomas Vachuska | d07c092 | 2015-10-06 14:48:06 -0700 | [diff] [blame] | 205 | private void connectInitialDevices() { |
| 206 | for (OpenFlowSwitch sw : controller.getSwitches()) { |
| 207 | try { |
| 208 | listener.switchAdded(new Dpid(sw.getId())); |
| 209 | } catch (Exception e) { |
| 210 | LOG.warn("Failed initially adding {} : {}", sw.getStringId(), e.getMessage()); |
| 211 | LOG.debug("Error details:", e); |
| 212 | // disconnect to trigger switch-add later |
| 213 | sw.disconnectSwitch(); |
| 214 | } |
| 215 | PortStatsCollector psc = new PortStatsCollector(sw, portStatsPollFrequency); |
| 216 | psc.start(); |
| 217 | collectors.put(new Dpid(sw.getId()), psc); |
| 218 | } |
| 219 | } |
| 220 | |
Ayaka Koshibe | e60d452 | 2014-10-28 15:07:00 -0700 | [diff] [blame] | 221 | @Override |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 222 | public boolean isReachable(DeviceId deviceId) { |
| 223 | OpenFlowSwitch sw = controller.getSwitch(dpid(deviceId.uri())); |
Thomas Vachuska | d07c092 | 2015-10-06 14:48:06 -0700 | [diff] [blame] | 224 | return sw != null && sw.isConnected(); |
Ayaka Koshibe | e60d452 | 2014-10-28 15:07:00 -0700 | [diff] [blame] | 225 | } |
| 226 | |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 227 | @Override |
Ayaka Koshibe | 78bcbc1 | 2014-11-19 14:28:58 -0800 | [diff] [blame] | 228 | public void triggerProbe(DeviceId deviceId) { |
Madan Jampani | c332876 | 2015-05-31 12:39:27 -0700 | [diff] [blame] | 229 | LOG.debug("Triggering probe on device {}", deviceId); |
Ayaka Koshibe | e8708e3 | 2014-10-22 13:40:18 -0700 | [diff] [blame] | 230 | |
Yuta HIGUCHI | 802d0e8 | 2014-10-31 16:07:37 -0700 | [diff] [blame] | 231 | final Dpid dpid = dpid(deviceId.uri()); |
| 232 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
| 233 | if (sw == null || !sw.isConnected()) { |
Ayaka Koshibe | 78bcbc1 | 2014-11-19 14:28:58 -0800 | [diff] [blame] | 234 | LOG.error("Failed to probe device {} on sw={}", deviceId, sw); |
Yuta HIGUCHI | 802d0e8 | 2014-10-31 16:07:37 -0700 | [diff] [blame] | 235 | providerService.deviceDisconnected(deviceId); |
Thomas Vachuska | 74bcd92 | 2015-06-02 13:08:57 -0700 | [diff] [blame] | 236 | return; |
Yuta HIGUCHI | 802d0e8 | 2014-10-31 16:07:37 -0700 | [diff] [blame] | 237 | } else { |
Ayaka Koshibe | 78bcbc1 | 2014-11-19 14:28:58 -0800 | [diff] [blame] | 238 | LOG.trace("Confirmed device {} connection", deviceId); |
Yuta HIGUCHI | 802d0e8 | 2014-10-31 16:07:37 -0700 | [diff] [blame] | 239 | } |
Ayaka Koshibe | e8708e3 | 2014-10-22 13:40:18 -0700 | [diff] [blame] | 240 | |
Ayaka Koshibe | e60d452 | 2014-10-28 15:07:00 -0700 | [diff] [blame] | 241 | // Prompt an update of port information. We can use any XID for this. |
Ayaka Koshibe | e8708e3 | 2014-10-22 13:40:18 -0700 | [diff] [blame] | 242 | OFFactory fact = sw.factory(); |
| 243 | switch (fact.getVersion()) { |
| 244 | case OF_10: |
| 245 | sw.sendMsg(fact.buildFeaturesRequest().setXid(0).build()); |
| 246 | break; |
| 247 | case OF_13: |
| 248 | sw.sendMsg(fact.buildPortDescStatsRequest().setXid(0).build()); |
| 249 | break; |
| 250 | default: |
| 251 | LOG.warn("Unhandled protocol version"); |
| 252 | } |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | @Override |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 256 | public void roleChanged(DeviceId deviceId, MastershipRole newRole) { |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 257 | switch (newRole) { |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 258 | case MASTER: |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 259 | controller.setRole(dpid(deviceId.uri()), RoleState.MASTER); |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 260 | break; |
| 261 | case STANDBY: |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 262 | controller.setRole(dpid(deviceId.uri()), RoleState.EQUAL); |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 263 | break; |
| 264 | case NONE: |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 265 | controller.setRole(dpid(deviceId.uri()), RoleState.SLAVE); |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 266 | break; |
| 267 | default: |
| 268 | LOG.error("Unknown Mastership state : {}", newRole); |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 269 | |
| 270 | } |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 271 | LOG.debug("Accepting mastership role change to {} for device {}", newRole, deviceId); |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Jian Li | 152b885 | 2015-12-07 14:47:25 -0800 | [diff] [blame] | 274 | |
| 275 | /** |
| 276 | * Enable OpenFlow control message monitoring. |
| 277 | */ |
| 278 | public void enableCtrlMsgMonitor() { |
| 279 | isCtrlMsgMonitor = true; |
| 280 | controller.addEventListener(inMsgListener); |
Jian Li | 28247b5 | 2016-01-07 17:24:15 -0800 | [diff] [blame] | 281 | controller.monitorAllEvents(isCtrlMsgMonitor); |
Jian Li | 152b885 | 2015-12-07 14:47:25 -0800 | [diff] [blame] | 282 | for (OpenFlowSwitch sw : controller.getSwitches()) { |
| 283 | sw.addEventListener(outMsgListener); |
| 284 | } |
| 285 | LOG.info("Enable control message monitoring."); |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Disable OpenFlow control message monitoring. |
| 290 | */ |
| 291 | public void disableCtrlMsgMonitor() { |
| 292 | isCtrlMsgMonitor = false; |
Jian Li | 28247b5 | 2016-01-07 17:24:15 -0800 | [diff] [blame] | 293 | controller.monitorAllEvents(isCtrlMsgMonitor); |
Jian Li | 152b885 | 2015-12-07 14:47:25 -0800 | [diff] [blame] | 294 | controller.removeEventListener(inMsgListener); |
| 295 | for (OpenFlowSwitch sw: controller.getSwitches()) { |
| 296 | sw.removeEventListener(outMsgListener); |
| 297 | } |
| 298 | LOG.info("Disable control message monitoring"); |
| 299 | } |
| 300 | |
Thomas Vachuska | 893bf4b | 2015-05-29 18:13:18 -0700 | [diff] [blame] | 301 | private void pushPortMetrics(Dpid dpid, List<OFPortStatsEntry> portStatsEntries) { |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 302 | DeviceId deviceId = DeviceId.deviceId(Dpid.uri(dpid)); |
Thomas Vachuska | 893bf4b | 2015-05-29 18:13:18 -0700 | [diff] [blame] | 303 | Collection<PortStatistics> stats = buildPortStatistics(deviceId, portStatsEntries); |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 304 | providerService.updatePortStatistics(deviceId, stats); |
| 305 | } |
| 306 | |
Thomas Vachuska | 893bf4b | 2015-05-29 18:13:18 -0700 | [diff] [blame] | 307 | private Collection<PortStatistics> buildPortStatistics(DeviceId deviceId, |
| 308 | List<OFPortStatsEntry> entries) { |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 309 | HashSet<PortStatistics> stats = Sets.newHashSet(); |
| 310 | |
Thomas Vachuska | 893bf4b | 2015-05-29 18:13:18 -0700 | [diff] [blame] | 311 | for (OFPortStatsEntry entry : entries) { |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 312 | try { |
Thomas Vachuska | 4e6025e | 2015-06-03 11:25:21 -0700 | [diff] [blame] | 313 | if (entry == null || entry.getPortNo() == null || entry.getPortNo().getPortNumber() < 0) { |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 314 | continue; |
| 315 | } |
| 316 | DefaultPortStatistics.Builder builder = DefaultPortStatistics.builder(); |
| 317 | DefaultPortStatistics stat = builder.setDeviceId(deviceId) |
| 318 | .setPort(entry.getPortNo().getPortNumber()) |
| 319 | .setPacketsReceived(entry.getRxPackets().getValue()) |
| 320 | .setPacketsSent(entry.getTxPackets().getValue()) |
| 321 | .setBytesReceived(entry.getRxBytes().getValue()) |
| 322 | .setBytesSent(entry.getTxBytes().getValue()) |
| 323 | .setPacketsRxDropped(entry.getRxDropped().getValue()) |
| 324 | .setPacketsTxDropped(entry.getTxDropped().getValue()) |
| 325 | .setPacketsRxErrors(entry.getRxErrors().getValue()) |
| 326 | .setPacketsTxErrors(entry.getTxErrors().getValue()) |
| 327 | .setDurationSec(entry.getVersion() == OFVersion.OF_10 ? 0 : entry.getDurationSec()) |
| 328 | .setDurationNano(entry.getVersion() == OFVersion.OF_10 ? 0 : entry.getDurationNsec()) |
| 329 | .build(); |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 330 | |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 331 | stats.add(stat); |
| 332 | } catch (Exception e) { |
| 333 | LOG.warn("Unable to process port stats", e); |
| 334 | } |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | return Collections.unmodifiableSet(stats); |
| 338 | |
| 339 | } |
| 340 | |
Jian Li | 152b885 | 2015-12-07 14:47:25 -0800 | [diff] [blame] | 341 | /** |
| 342 | * A listener for incoming OpenFlow messages. |
| 343 | */ |
| 344 | private class IncomingMessageProvider implements OpenFlowEventListener { |
| 345 | |
| 346 | @Override |
| 347 | public void handleMessage(Dpid dpid, OFMessage msg) { |
| 348 | if (isCtrlMsgMonitor) { |
Jian Li | 28247b5 | 2016-01-07 17:24:15 -0800 | [diff] [blame] | 349 | // TODO: handle all incoming OF messages |
Jian Li | 152b885 | 2015-12-07 14:47:25 -0800 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * A listener for outgoing OpenFlow messages. |
| 356 | */ |
| 357 | private class OutgoingMessageProvider implements OpenFlowEventListener { |
| 358 | |
| 359 | @Override |
| 360 | public void handleMessage(Dpid dpid, OFMessage msg) { |
| 361 | if (isCtrlMsgMonitor) { |
Jian Li | 28247b5 | 2016-01-07 17:24:15 -0800 | [diff] [blame] | 362 | // TODO: handle all outgoing OF messages |
Jian Li | 152b885 | 2015-12-07 14:47:25 -0800 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | } |
| 366 | |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 367 | private class InternalDeviceProvider implements OpenFlowSwitchListener, OpenFlowEventListener { |
Thomas Vachuska | 893bf4b | 2015-05-29 18:13:18 -0700 | [diff] [blame] | 368 | |
Srikanth Vavilapalli | 78baf58 | 2015-06-05 11:40:14 -0700 | [diff] [blame] | 369 | private HashMap<Dpid, List<OFPortStatsEntry>> portStatsReplies = new HashMap<>(); |
Thomas Vachuska | 893bf4b | 2015-05-29 18:13:18 -0700 | [diff] [blame] | 370 | |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 371 | @Override |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 372 | public void switchAdded(Dpid dpid) { |
alshabib | 6f5460b | 2014-09-03 14:46:17 -0700 | [diff] [blame] | 373 | if (providerService == null) { |
| 374 | return; |
| 375 | } |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 376 | DeviceId did = deviceId(uri(dpid)); |
alshabib | 6f5460b | 2014-09-03 14:46:17 -0700 | [diff] [blame] | 377 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
Lei Xu | dee1aff | 2015-10-16 00:45:10 -0500 | [diff] [blame] | 378 | if (sw == null) { |
| 379 | return; |
| 380 | } |
alshabib | 6f5460b | 2014-09-03 14:46:17 -0700 | [diff] [blame] | 381 | |
Jian Li | 152b885 | 2015-12-07 14:47:25 -0800 | [diff] [blame] | 382 | if (isCtrlMsgMonitor) { |
| 383 | // start to monitor the outgoing control messages |
| 384 | sw.addEventListener(outMsgListener); |
| 385 | } |
| 386 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 387 | ChassisId cId = new ChassisId(dpid.value()); |
Ray Milkey | e53f171 | 2015-01-16 09:17:16 -0800 | [diff] [blame] | 388 | |
Thomas Vachuska | 82041f5 | 2014-11-30 22:14:02 -0800 | [diff] [blame] | 389 | SparseAnnotations annotations = DefaultAnnotations.builder() |
andrea | fe3308f | 2015-10-06 15:51:25 -0700 | [diff] [blame] | 390 | .set(AnnotationKeys.PROTOCOL, sw.factory().getVersion().toString()) |
| 391 | .set(AnnotationKeys.CHANNEL_ID, sw.channelId()) |
| 392 | .set(AnnotationKeys.MANAGEMENT_ADDRESS, sw.channelId().split(":")[0]) |
Ray Milkey | e53f171 | 2015-01-16 09:17:16 -0800 | [diff] [blame] | 393 | .build(); |
| 394 | |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 395 | DeviceDescription description = |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 396 | new DefaultDeviceDescription(did.uri(), sw.deviceType(), |
Ray Milkey | d3edd03 | 2015-01-16 11:38:58 -0800 | [diff] [blame] | 397 | sw.manufacturerDescription(), |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 398 | sw.hardwareDescription(), |
| 399 | sw.softwareDescription(), |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 400 | sw.serialNumber(), |
Thomas Vachuska | 82041f5 | 2014-11-30 22:14:02 -0800 | [diff] [blame] | 401 | cId, annotations); |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 402 | providerService.deviceConnected(did, description); |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 403 | providerService.updatePorts(did, buildPortDescriptions(sw)); |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 404 | |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 405 | PortStatsCollector psc = |
Lei Xu | dee1aff | 2015-10-16 00:45:10 -0500 | [diff] [blame] | 406 | new PortStatsCollector(sw, portStatsPollFrequency); |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 407 | psc.start(); |
| 408 | collectors.put(dpid, psc); |
Lei Xu | dee1aff | 2015-10-16 00:45:10 -0500 | [diff] [blame] | 409 | |
| 410 | //figure out race condition for collectors.remove() and collectors.put() |
| 411 | if (controller.getSwitch(dpid) == null) { |
| 412 | switchRemoved(dpid); |
| 413 | } |
alshabib | 25c8eec | 2014-09-04 16:41:31 -0700 | [diff] [blame] | 414 | } |
| 415 | |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 416 | @Override |
| 417 | public void switchRemoved(Dpid dpid) { |
alshabib | 6f5460b | 2014-09-03 14:46:17 -0700 | [diff] [blame] | 418 | if (providerService == null) { |
| 419 | return; |
| 420 | } |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 421 | providerService.deviceDisconnected(deviceId(uri(dpid))); |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 422 | |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 423 | PortStatsCollector collector = collectors.remove(dpid); |
| 424 | if (collector != null) { |
| 425 | collector.stop(); |
| 426 | } |
Jian Li | 152b885 | 2015-12-07 14:47:25 -0800 | [diff] [blame] | 427 | |
| 428 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
| 429 | if (sw != null) { |
| 430 | if (isCtrlMsgMonitor) { |
| 431 | // stop monitoring the outgoing control messages |
| 432 | sw.removeEventListener(outMsgListener); |
| 433 | } |
| 434 | } |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 435 | } |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 436 | |
| 437 | @Override |
| 438 | public void switchChanged(Dpid dpid) { |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 439 | LOG.debug("switchChanged({})", dpid); |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 440 | if (providerService == null) { |
| 441 | return; |
| 442 | } |
| 443 | DeviceId did = deviceId(uri(dpid)); |
| 444 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
Lei Xu | dee1aff | 2015-10-16 00:45:10 -0500 | [diff] [blame] | 445 | if (sw == null) { |
| 446 | return; |
| 447 | } |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 448 | final List<PortDescription> ports = buildPortDescriptions(sw); |
| 449 | LOG.debug("switchChanged({}) {}", did, ports); |
| 450 | providerService.updatePorts(did, ports); |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 451 | } |
| 452 | |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 453 | @Override |
| 454 | public void portChanged(Dpid dpid, OFPortStatus status) { |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 455 | LOG.debug("portChanged({},{})", dpid, status); |
alshabib | afc514a | 2014-12-01 14:44:05 -0800 | [diff] [blame] | 456 | PortDescription portDescription = buildPortDescription(status); |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 457 | providerService.portStatusChanged(deviceId(uri(dpid)), portDescription); |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 458 | } |
| 459 | |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 460 | @Override |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 461 | public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) { |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 462 | LOG.debug("receivedRoleReply({},{},{})", dpid, requested, response); |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 463 | MastershipRole request = roleOf(requested); |
| 464 | MastershipRole reply = roleOf(response); |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 465 | providerService.receivedRoleReply(deviceId(uri(dpid)), request, reply); |
| 466 | } |
| 467 | |
| 468 | /** |
| 469 | * Translates a RoleState to the corresponding MastershipRole. |
| 470 | * |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 471 | * @param response role state |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 472 | * @return a MastershipRole |
| 473 | */ |
| 474 | private MastershipRole roleOf(RoleState response) { |
| 475 | switch (response) { |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 476 | case MASTER: |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 477 | return MastershipRole.MASTER; |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 478 | case EQUAL: |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 479 | return MastershipRole.STANDBY; |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 480 | case SLAVE: |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 481 | return MastershipRole.NONE; |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 482 | default: |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 483 | LOG.warn("unknown role {}", response); |
| 484 | return null; |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 485 | } |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 486 | } |
| 487 | |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 488 | /** |
| 489 | * Builds a list of port descriptions for a given list of ports. |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 490 | * |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 491 | * @return list of portdescriptions |
| 492 | */ |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 493 | private List<PortDescription> buildPortDescriptions(OpenFlowSwitch sw) { |
| 494 | final List<PortDescription> portDescs = new ArrayList<>(sw.getPorts().size()); |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 495 | if (!(Device.Type.ROADM.equals(sw.deviceType()))) { |
| 496 | sw.getPorts().forEach(port -> portDescs.add(buildPortDescription(port))); |
| 497 | } |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 498 | |
| 499 | OpenFlowOpticalSwitch opsw; |
| 500 | switch (sw.deviceType()) { |
| 501 | case ROADM: |
| 502 | opsw = (OpenFlowOpticalSwitch) sw; |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 503 | List<OFPortDesc> ports = opsw.getPorts(); |
| 504 | LOG.debug("SW ID {} , ETH- ODU CLT Ports {}", opsw.getId(), ports); |
| 505 | // ODU client ports are reported as ETH |
| 506 | ports.forEach(port -> portDescs.add(buildOduCltPortDescription(port))); |
| 507 | |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 508 | opsw.getPortTypes().forEach(type -> { |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 509 | List<? extends OFObject> portsOf = opsw.getPortsOf(type); |
| 510 | LOG.debug("Ports Of{}", portsOf); |
| 511 | portsOf.forEach( |
| 512 | op -> { |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 513 | portDescs.add(buildPortDescription(type, op)); |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 514 | } |
| 515 | ); |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 516 | }); |
| 517 | break; |
| 518 | case FIBER_SWITCH: |
| 519 | opsw = (OpenFlowOpticalSwitch) sw; |
| 520 | opsw.getPortTypes().forEach(type -> { |
| 521 | opsw.getPortsOf(type).forEach( |
| 522 | op -> { |
| 523 | portDescs.add(buildPortDescription((OFCalientPortDescStatsEntry) op)); |
| 524 | } |
| 525 | ); |
| 526 | }); |
| 527 | break; |
| 528 | default: |
| 529 | break; |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 530 | } |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 531 | |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 532 | return portDescs; |
| 533 | } |
| 534 | |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 535 | private PortDescription buildOduCltPortDescription(OFPortDesc port) { |
| 536 | PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber()); |
| 537 | boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN) && |
| 538 | !port.getConfig().contains(OFPortConfig.PORT_DOWN); |
Rimon Ashkenazy | d15ebf5 | 2016-01-06 14:36:13 +0200 | [diff] [blame] | 539 | Long portSpeedInMbps = portSpeed(port); |
Toru Furusawa | 72ee30c | 2016-01-08 13:29:04 -0800 | [diff] [blame^] | 540 | CltSignalType sigType = null; |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 541 | |
Rimon Ashkenazy | d15ebf5 | 2016-01-06 14:36:13 +0200 | [diff] [blame] | 542 | switch (portSpeedInMbps.toString()) { |
| 543 | case "1000": |
Toru Furusawa | 72ee30c | 2016-01-08 13:29:04 -0800 | [diff] [blame^] | 544 | sigType = CltSignalType.CLT_1GBE; |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 545 | break; |
Rimon Ashkenazy | d15ebf5 | 2016-01-06 14:36:13 +0200 | [diff] [blame] | 546 | case "10000": |
Toru Furusawa | 72ee30c | 2016-01-08 13:29:04 -0800 | [diff] [blame^] | 547 | sigType = CltSignalType.CLT_10GBE; |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 548 | break; |
Rimon Ashkenazy | d15ebf5 | 2016-01-06 14:36:13 +0200 | [diff] [blame] | 549 | case "40000": |
Toru Furusawa | 72ee30c | 2016-01-08 13:29:04 -0800 | [diff] [blame^] | 550 | sigType = CltSignalType.CLT_40GBE; |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 551 | break; |
Rimon Ashkenazy | d15ebf5 | 2016-01-06 14:36:13 +0200 | [diff] [blame] | 552 | case "100000": |
Toru Furusawa | 72ee30c | 2016-01-08 13:29:04 -0800 | [diff] [blame^] | 553 | sigType = CltSignalType.CLT_100GBE; |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 554 | break; |
| 555 | default: |
Rimon Ashkenazy | d15ebf5 | 2016-01-06 14:36:13 +0200 | [diff] [blame] | 556 | throw new RuntimeException("Un recognize OduClt speed: " + portSpeedInMbps.toString()); |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | SparseAnnotations annotations = buildOduCltAnnotation(port); |
| 560 | return new OduCltPortDescription(portNo, enabled, sigType, annotations); |
| 561 | } |
| 562 | |
| 563 | private SparseAnnotations buildOduCltAnnotation(OFPortDesc port) { |
| 564 | SparseAnnotations annotations = null; |
| 565 | String portName = Strings.emptyToNull(port.getName()); |
| 566 | if (portName != null) { |
| 567 | annotations = DefaultAnnotations.builder() |
| 568 | .set(AnnotationKeys.PORT_NAME, portName) |
| 569 | .set(AnnotationKeys.STATIC_PORT, Boolean.TRUE.toString()).build(); |
| 570 | } |
| 571 | return annotations; |
| 572 | } |
| 573 | |
| 574 | private PortDescription buildPortDescription(PortDescPropertyType ptype, OFObject port) { |
| 575 | if (port instanceof OFPortOptical) { |
| 576 | return buildPortDescription(ptype, (OFPortOptical) port); |
| 577 | } |
| 578 | return buildPortDescription(ptype, (OFExpPort) port); |
| 579 | } |
| 580 | |
| 581 | /** |
| 582 | * Build a portDescription from a given a port description describing some |
| 583 | * Optical port. |
| 584 | * |
| 585 | * @param ptype description property type. |
| 586 | * @param port the port to build from. |
| 587 | * @return portDescription for the port. |
| 588 | */ |
| 589 | private PortDescription buildPortDescription(PortDescPropertyType ptype, OFExpPort port) { |
| 590 | PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber()); |
| 591 | boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN) |
| 592 | && !port.getConfig().contains(OFPortConfig.PORT_DOWN); |
lishuai | 4ee4204 | 2015-11-30 17:19:21 +0800 | [diff] [blame] | 593 | SparseAnnotations annotations = makePortAnnotation(port.getName(), port.getHwAddr().toString()); |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 594 | |
| 595 | OFExpPortDescPropOpticalTransport firstProp = port.getProperties().get(0); |
| 596 | OFPortOpticalTransportSignalType sigType = firstProp.getPortSignalType(); |
| 597 | |
| 598 | DefaultPortDescription portDes = null; |
| 599 | switch (sigType) { |
| 600 | case OMSN: |
Sho SHIMIZU | 70e76d8 | 2016-01-05 16:26:26 -0800 | [diff] [blame] | 601 | portDes = new OmsPortDescription(portNo, enabled, |
Rimon Ashkenazy | e320103 | 2016-01-11 14:27:30 +0200 | [diff] [blame] | 602 | FREQ191_7, FREQ191_7.add(FREQ4_4), FREQ50, annotations); |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 603 | break; |
| 604 | case OCH: |
| 605 | OFExpPortOpticalTransportLayerEntry entry = firstProp.getFeatures().get(0).getValue().get(0); |
| 606 | OFPortOpticalTransportLayerClass layerClass = entry.getLayerClass(); |
| 607 | if (!OFPortOpticalTransportLayerClass.ODU.equals(layerClass)) { |
| 608 | LOG.error("Unsupported layer Class {} ", layerClass); |
| 609 | return null; |
| 610 | } |
| 611 | |
| 612 | // convert to ONOS OduSignalType |
| 613 | OduSignalType oduSignalType = OpenFlowDeviceValueMapper. |
| 614 | lookupOduSignalType((byte) entry.getSignalType()); |
| 615 | //OchSignal is needed for OchPortDescription constructor, |
| 616 | //yet not relevant for tunable OCH port, creating with default parameters |
| 617 | OchSignal signalId = new OchSignal(GridType.DWDM, ChannelSpacing.CHL_50GHZ, 1, 1); |
| 618 | |
| 619 | portDes = new OchPortDescription(portNo, enabled, |
| 620 | oduSignalType, true, signalId, annotations); |
| 621 | |
| 622 | break; |
| 623 | case OTU2: |
| 624 | case OTU4: |
| 625 | LOG.error("Signal tpye OTU2/4 not supported yet ", port.toString()); |
| 626 | break; |
| 627 | default: |
| 628 | break; |
| 629 | } |
| 630 | |
| 631 | return portDes; |
| 632 | } |
| 633 | |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 634 | /** |
Ray Milkey | e0fade7 | 2015-01-15 13:29:47 -0800 | [diff] [blame] | 635 | * Creates an annotation for the port name if one is available. |
| 636 | * |
lishuai | 4ee4204 | 2015-11-30 17:19:21 +0800 | [diff] [blame] | 637 | * @param portName the port name |
| 638 | * @param portMac the port mac |
Ray Milkey | e0fade7 | 2015-01-15 13:29:47 -0800 | [diff] [blame] | 639 | * @return annotation containing the port name if one is found, |
| 640 | * null otherwise |
| 641 | */ |
lishuai | 4ee4204 | 2015-11-30 17:19:21 +0800 | [diff] [blame] | 642 | private SparseAnnotations makePortAnnotation(String portName, String portMac) { |
Ray Milkey | e0fade7 | 2015-01-15 13:29:47 -0800 | [diff] [blame] | 643 | SparseAnnotations annotations = null; |
lishuai | 4ee4204 | 2015-11-30 17:19:21 +0800 | [diff] [blame] | 644 | String pName = Strings.emptyToNull(portName); |
| 645 | String pMac = Strings.emptyToNull(portMac); |
Ray Milkey | e0fade7 | 2015-01-15 13:29:47 -0800 | [diff] [blame] | 646 | if (portName != null) { |
| 647 | annotations = DefaultAnnotations.builder() |
lishuai | 4ee4204 | 2015-11-30 17:19:21 +0800 | [diff] [blame] | 648 | .set(AnnotationKeys.PORT_NAME, pName) |
| 649 | .set(AnnotationKeys.PORT_MAC, pMac).build(); |
Ray Milkey | e0fade7 | 2015-01-15 13:29:47 -0800 | [diff] [blame] | 650 | } |
| 651 | return annotations; |
| 652 | } |
| 653 | |
| 654 | /** |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 655 | * Build a portDescription from a given Ethernet port description. |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 656 | * |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 657 | * @param port the port to build from. |
| 658 | * @return portDescription for the port. |
| 659 | */ |
| 660 | private PortDescription buildPortDescription(OFPortDesc port) { |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 661 | PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber()); |
| 662 | boolean enabled = |
| 663 | !port.getState().contains(OFPortState.LINK_DOWN) && |
| 664 | !port.getConfig().contains(OFPortConfig.PORT_DOWN); |
| 665 | Port.Type type = port.getCurr().contains(OFPortFeatures.PF_FIBER) ? FIBER : COPPER; |
lishuai | 4ee4204 | 2015-11-30 17:19:21 +0800 | [diff] [blame] | 666 | SparseAnnotations annotations = makePortAnnotation(port.getName(), port.getHwAddr().toString()); |
Ray Milkey | e0fade7 | 2015-01-15 13:29:47 -0800 | [diff] [blame] | 667 | return new DefaultPortDescription(portNo, enabled, type, |
| 668 | portSpeed(port), annotations); |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 669 | } |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 670 | |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 671 | /** |
| 672 | * Build a portDescription from a given a port description describing some |
| 673 | * Optical port. |
| 674 | * |
| 675 | * @param port description property type. |
| 676 | * @param port the port to build from. |
| 677 | * @return portDescription for the port. |
| 678 | */ |
| 679 | private PortDescription buildPortDescription(PortDescPropertyType ptype, OFPortOptical port) { |
Marc De Leenheer | fc913dd | 2015-07-30 16:04:55 -0700 | [diff] [blame] | 680 | checkArgument(port.getDesc().size() >= 1); |
| 681 | |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 682 | // Minimally functional fixture. This needs to be fixed as we add better support. |
| 683 | PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber()); |
Ayaka Koshibe | 74b5527 | 2015-05-28 15:16:04 -0700 | [diff] [blame] | 684 | |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 685 | boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN) |
| 686 | && !port.getConfig().contains(OFPortConfig.PORT_DOWN); |
lishuai | 4ee4204 | 2015-11-30 17:19:21 +0800 | [diff] [blame] | 687 | SparseAnnotations annotations = makePortAnnotation(port.getName(), port.getHwAddr().toString()); |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 688 | |
| 689 | if (port.getVersion() == OFVersion.OF_13 |
| 690 | && ptype == PortDescPropertyType.OPTICAL_TRANSPORT) { |
| 691 | // At this point, not much is carried in the optical port message. |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 692 | LOG.debug("Optical transport port message {}", port.toString()); |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 693 | } else { |
| 694 | // removable once 1.4+ support complete. |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 695 | LOG.debug("Unsupported optical port properties"); |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 696 | } |
Marc De Leenheer | fc913dd | 2015-07-30 16:04:55 -0700 | [diff] [blame] | 697 | |
| 698 | OFPortDescPropOpticalTransport desc = port.getDesc().get(0); |
| 699 | switch (desc.getPortSignalType()) { |
| 700 | // FIXME: use constants once loxi has full optical extensions |
| 701 | case 2: // OMS port |
| 702 | // Assume complete optical spectrum and 50 GHz grid |
| 703 | // LINC-OE is only supported optical OF device for now |
| 704 | return new OmsPortDescription(portNo, enabled, |
| 705 | Spectrum.U_BAND_MIN, Spectrum.O_BAND_MAX, Frequency.ofGHz(50), annotations); |
| 706 | case 5: // OCH port |
| 707 | OchSignal signal = new OchSignal(GridType.DWDM, ChannelSpacing.CHL_50GHZ, 0, 4); |
| 708 | return new OchPortDescription(portNo, enabled, OduSignalType.ODU4, |
| 709 | true, signal, annotations); |
| 710 | default: |
| 711 | break; |
| 712 | } |
| 713 | |
Ayaka Koshibe | 74b5527 | 2015-05-28 15:16:04 -0700 | [diff] [blame] | 714 | return new DefaultPortDescription(portNo, enabled, FIBER, 0, annotations); |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 715 | } |
| 716 | |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 717 | /** |
| 718 | * Build a portDescription from a given port description describing a fiber switch optical port. |
| 719 | * |
| 720 | * @param port description property type. |
| 721 | * @param port the port to build from. |
| 722 | * @return portDescription for the port. |
| 723 | */ |
| 724 | private PortDescription buildPortDescription(OFCalientPortDescStatsEntry port) { |
| 725 | PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber()); |
| 726 | |
| 727 | // FIXME when Calient OF agent reports port status |
| 728 | boolean enabled = true; |
lishuai | 4ee4204 | 2015-11-30 17:19:21 +0800 | [diff] [blame] | 729 | SparseAnnotations annotations = makePortAnnotation(port.getName(), port.getHwAddr().toString()); |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 730 | |
Marc De Leenheer | fc913dd | 2015-07-30 16:04:55 -0700 | [diff] [blame] | 731 | // S160 data sheet |
| 732 | // Wavelength range: 1260 - 1630 nm, grid is irrelevant for this type of switch |
| 733 | return new OmsPortDescription(portNo, enabled, |
| 734 | Spectrum.U_BAND_MIN, Spectrum.O_BAND_MAX, Frequency.ofGHz(100), annotations); |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 735 | } |
| 736 | |
alshabib | afc514a | 2014-12-01 14:44:05 -0800 | [diff] [blame] | 737 | private PortDescription buildPortDescription(OFPortStatus status) { |
| 738 | OFPortDesc port = status.getDesc(); |
| 739 | if (status.getReason() != OFPortReason.DELETE) { |
| 740 | return buildPortDescription(port); |
| 741 | } else { |
| 742 | PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber()); |
| 743 | Port.Type type = port.getCurr().contains(OFPortFeatures.PF_FIBER) ? FIBER : COPPER; |
lishuai | 4ee4204 | 2015-11-30 17:19:21 +0800 | [diff] [blame] | 744 | SparseAnnotations annotations = makePortAnnotation(port.getName(), port.getHwAddr().toString()); |
Ray Milkey | e0fade7 | 2015-01-15 13:29:47 -0800 | [diff] [blame] | 745 | return new DefaultPortDescription(portNo, false, type, |
| 746 | portSpeed(port), annotations); |
alshabib | afc514a | 2014-12-01 14:44:05 -0800 | [diff] [blame] | 747 | } |
| 748 | } |
| 749 | |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 750 | private long portSpeed(OFPortDesc port) { |
| 751 | if (port.getVersion() == OFVersion.OF_13) { |
Charles Chan | 25b7732 | 2015-12-13 01:00:56 -0800 | [diff] [blame] | 752 | // Note: getCurrSpeed() returns a value in kbps (this also applies to OF_11 and OF_12) |
| 753 | return port.getCurrSpeed() / KBPS; |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | PortSpeed portSpeed = PortSpeed.SPEED_NONE; |
| 757 | for (OFPortFeatures feat : port.getCurr()) { |
| 758 | portSpeed = PortSpeed.max(portSpeed, feat.getPortSpeed()); |
| 759 | } |
Thomas Vachuska | 98eda53 | 2014-10-29 17:31:02 -0700 | [diff] [blame] | 760 | return portSpeed.getSpeedBps() / MBPS; |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 761 | } |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 762 | |
| 763 | @Override |
| 764 | public void handleMessage(Dpid dpid, OFMessage msg) { |
| 765 | switch (msg.getType()) { |
| 766 | case STATS_REPLY: |
| 767 | if (((OFStatsReply) msg).getStatsType() == OFStatsType.PORT) { |
Thomas Vachuska | 893bf4b | 2015-05-29 18:13:18 -0700 | [diff] [blame] | 768 | OFPortStatsReply portStatsReply = (OFPortStatsReply) msg; |
Srikanth Vavilapalli | 78baf58 | 2015-06-05 11:40:14 -0700 | [diff] [blame] | 769 | List<OFPortStatsEntry> portStatsReplyList = portStatsReplies.get(dpid); |
| 770 | if (portStatsReplyList == null) { |
| 771 | portStatsReplyList = Lists.newArrayList(); |
| 772 | } |
| 773 | portStatsReplyList.addAll(portStatsReply.getEntries()); |
| 774 | portStatsReplies.put(dpid, portStatsReplyList); |
Thomas Vachuska | 893bf4b | 2015-05-29 18:13:18 -0700 | [diff] [blame] | 775 | if (!portStatsReply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) { |
Srikanth Vavilapalli | 78baf58 | 2015-06-05 11:40:14 -0700 | [diff] [blame] | 776 | pushPortMetrics(dpid, portStatsReplies.get(dpid)); |
| 777 | portStatsReplies.get(dpid).clear(); |
Thomas Vachuska | 893bf4b | 2015-05-29 18:13:18 -0700 | [diff] [blame] | 778 | } |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 779 | } |
| 780 | break; |
| 781 | default: |
| 782 | break; |
| 783 | } |
| 784 | } |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 785 | } |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 786 | } |