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