Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0a4e674 | 2016-09-15 23:03:10 -0700 | [diff] [blame] | 2 | * Copyright 2014-present 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 | |
Marc De Leenheer | 8aba62f | 2017-04-25 14:33:37 -0700 | [diff] [blame] | 18 | import com.google.common.base.Strings; |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.Lists; |
| 21 | import com.google.common.collect.Maps; |
| 22 | import com.google.common.collect.Sets; |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Activate; |
| 24 | import org.apache.felix.scr.annotations.Component; |
| 25 | import org.apache.felix.scr.annotations.Deactivate; |
Dusan Pajin | bab8a5e | 2015-07-24 17:37:19 +0200 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Modified; |
| 27 | import org.apache.felix.scr.annotations.Property; |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 28 | import org.apache.felix.scr.annotations.Reference; |
| 29 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 30 | import org.onlab.packet.ChassisId; |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 31 | import org.onlab.util.Frequency; |
| 32 | import org.onlab.util.Spectrum; |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 33 | import org.onosproject.cfg.ComponentConfigService; |
Thomas Vachuska | b52a014 | 2015-04-21 17:48:15 -0700 | [diff] [blame] | 34 | import org.onosproject.net.AnnotationKeys; |
Marc De Leenheer | fc913dd | 2015-07-30 16:04:55 -0700 | [diff] [blame] | 35 | import org.onosproject.net.ChannelSpacing; |
Toru Furusawa | 72ee30c | 2016-01-08 13:29:04 -0800 | [diff] [blame] | 36 | import org.onosproject.net.CltSignalType; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 37 | import org.onosproject.net.DefaultAnnotations; |
Yuta HIGUCHI | 2341e60 | 2017-03-08 20:10:08 -0800 | [diff] [blame] | 38 | import org.onosproject.net.DefaultAnnotations.Builder; |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 39 | import org.onosproject.net.Device; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 40 | import org.onosproject.net.DeviceId; |
Marc De Leenheer | fc913dd | 2015-07-30 16:04:55 -0700 | [diff] [blame] | 41 | import org.onosproject.net.GridType; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 42 | import org.onosproject.net.MastershipRole; |
Marc De Leenheer | fc913dd | 2015-07-30 16:04:55 -0700 | [diff] [blame] | 43 | import org.onosproject.net.OchSignal; |
| 44 | import org.onosproject.net.OduSignalType; |
Rimon Ashkenazy | 8ebfff0 | 2016-02-01 11:56:36 +0200 | [diff] [blame] | 45 | import org.onosproject.net.OtuSignalType; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 46 | import org.onosproject.net.Port; |
| 47 | import org.onosproject.net.PortNumber; |
| 48 | import org.onosproject.net.SparseAnnotations; |
Jimmy Yan | 4deb03b | 2016-06-24 10:53:54 -0700 | [diff] [blame] | 49 | import org.onosproject.net.behaviour.LambdaQuery; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 50 | import org.onosproject.net.device.DefaultDeviceDescription; |
| 51 | import org.onosproject.net.device.DefaultPortDescription; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 52 | import org.onosproject.net.device.DefaultPortStatistics; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 53 | import org.onosproject.net.device.DeviceDescription; |
| 54 | import org.onosproject.net.device.DeviceProvider; |
| 55 | import org.onosproject.net.device.DeviceProviderRegistry; |
| 56 | import org.onosproject.net.device.DeviceProviderService; |
| 57 | import org.onosproject.net.device.PortDescription; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 58 | import org.onosproject.net.device.PortStatistics; |
Victor Silva | ac8dab0 | 2016-11-28 16:33:08 -0300 | [diff] [blame] | 59 | import org.onosproject.net.driver.Driver; |
Jimmy Yan | 4deb03b | 2016-06-24 10:53:54 -0700 | [diff] [blame] | 60 | import org.onosproject.net.driver.DriverHandler; |
Victor Silva | ac8dab0 | 2016-11-28 16:33:08 -0300 | [diff] [blame] | 61 | import org.onosproject.net.driver.DriverService; |
Jimmy Yan | 4deb03b | 2016-06-24 10:53:54 -0700 | [diff] [blame] | 62 | import org.onosproject.net.driver.HandlerBehaviour; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 63 | import org.onosproject.net.provider.AbstractProvider; |
| 64 | import org.onosproject.net.provider.ProviderId; |
| 65 | import org.onosproject.openflow.controller.Dpid; |
| 66 | import org.onosproject.openflow.controller.OpenFlowController; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 67 | import org.onosproject.openflow.controller.OpenFlowEventListener; |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 68 | import org.onosproject.openflow.controller.OpenFlowOpticalSwitch; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 69 | import org.onosproject.openflow.controller.OpenFlowSwitch; |
| 70 | import org.onosproject.openflow.controller.OpenFlowSwitchListener; |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 71 | import org.onosproject.openflow.controller.PortDescPropertyType; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 72 | import org.onosproject.openflow.controller.RoleState; |
Dusan Pajin | bab8a5e | 2015-07-24 17:37:19 +0200 | [diff] [blame] | 73 | import org.osgi.service.component.ComponentContext; |
Marc De Leenheer | c662d32 | 2016-02-18 16:05:10 -0800 | [diff] [blame] | 74 | import org.projectfloodlight.openflow.protocol.OFCalientPortDescProp; |
| 75 | import org.projectfloodlight.openflow.protocol.OFCalientPortDescPropOptical; |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 76 | import org.projectfloodlight.openflow.protocol.OFCalientPortDescStatsEntry; |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 77 | import org.projectfloodlight.openflow.protocol.OFErrorMsg; |
| 78 | import org.projectfloodlight.openflow.protocol.OFErrorType; |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 79 | import org.projectfloodlight.openflow.protocol.OFExpPort; |
| 80 | import org.projectfloodlight.openflow.protocol.OFExpPortDescPropOpticalTransport; |
| 81 | import org.projectfloodlight.openflow.protocol.OFExpPortOpticalTransportLayerEntry; |
Ayaka Koshibe | e8708e3 | 2014-10-22 13:40:18 -0700 | [diff] [blame] | 82 | import org.projectfloodlight.openflow.protocol.OFFactory; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 83 | import org.projectfloodlight.openflow.protocol.OFMessage; |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 84 | import org.projectfloodlight.openflow.protocol.OFObject; |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 85 | import org.projectfloodlight.openflow.protocol.OFPortConfig; |
alshabib | 25c8eec | 2014-09-04 16:41:31 -0700 | [diff] [blame] | 86 | import org.projectfloodlight.openflow.protocol.OFPortDesc; |
Yuta HIGUCHI | 2341e60 | 2017-03-08 20:10:08 -0800 | [diff] [blame] | 87 | import org.projectfloodlight.openflow.protocol.OFPortDescPropEthernet; |
| 88 | import org.projectfloodlight.openflow.protocol.OFPortDescPropOptical; |
Marc De Leenheer | fc913dd | 2015-07-30 16:04:55 -0700 | [diff] [blame] | 89 | import org.projectfloodlight.openflow.protocol.OFPortDescPropOpticalTransport; |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 90 | import org.projectfloodlight.openflow.protocol.OFPortFeatures; |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 91 | import org.projectfloodlight.openflow.protocol.OFPortMod; |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 92 | import org.projectfloodlight.openflow.protocol.OFPortOptical; |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 93 | import org.projectfloodlight.openflow.protocol.OFPortOpticalTransportLayerClass; |
| 94 | import org.projectfloodlight.openflow.protocol.OFPortOpticalTransportSignalType; |
alshabib | afc514a | 2014-12-01 14:44:05 -0800 | [diff] [blame] | 95 | import org.projectfloodlight.openflow.protocol.OFPortReason; |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 96 | import org.projectfloodlight.openflow.protocol.OFPortState; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 97 | import org.projectfloodlight.openflow.protocol.OFPortStatsEntry; |
| 98 | import org.projectfloodlight.openflow.protocol.OFPortStatsReply; |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 99 | import org.projectfloodlight.openflow.protocol.OFPortStatus; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 100 | import org.projectfloodlight.openflow.protocol.OFStatsReply; |
Thomas Vachuska | 893bf4b | 2015-05-29 18:13:18 -0700 | [diff] [blame] | 101 | import org.projectfloodlight.openflow.protocol.OFStatsReplyFlags; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 102 | import org.projectfloodlight.openflow.protocol.OFStatsType; |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 103 | import org.projectfloodlight.openflow.protocol.OFVersion; |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 104 | import org.projectfloodlight.openflow.types.OFPort; |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 105 | import org.projectfloodlight.openflow.types.PortSpeed; |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 106 | import org.slf4j.Logger; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 107 | |
Marc De Leenheer | 8aba62f | 2017-04-25 14:33:37 -0700 | [diff] [blame] | 108 | import java.util.ArrayList; |
| 109 | import java.util.Collection; |
| 110 | import java.util.Collections; |
| 111 | import java.util.Comparator; |
| 112 | import java.util.Dictionary; |
Jimmy Jin | e9b7a02 | 2016-08-12 16:56:48 -0700 | [diff] [blame] | 113 | import java.util.EnumSet; |
Marc De Leenheer | 8aba62f | 2017-04-25 14:33:37 -0700 | [diff] [blame] | 114 | import java.util.HashMap; |
| 115 | import java.util.HashSet; |
| 116 | import java.util.List; |
Yuta HIGUCHI | 2341e60 | 2017-03-08 20:10:08 -0800 | [diff] [blame] | 117 | import java.util.Optional; |
Marc De Leenheer | 8aba62f | 2017-04-25 14:33:37 -0700 | [diff] [blame] | 118 | import java.util.Properties; |
| 119 | import java.util.Set; |
| 120 | import java.util.Timer; |
| 121 | |
| 122 | import static com.google.common.base.Preconditions.checkArgument; |
| 123 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 124 | import static org.onlab.util.Tools.get; |
| 125 | import static org.onosproject.net.DeviceId.deviceId; |
| 126 | import static org.onosproject.net.Port.Type.COPPER; |
| 127 | import static org.onosproject.net.Port.Type.FIBER; |
| 128 | import static org.onosproject.net.optical.device.OchPortHelper.ochPortDescription; |
| 129 | import static org.onosproject.net.optical.device.OduCltPortHelper.oduCltPortDescription; |
| 130 | import static org.onosproject.net.optical.device.OmsPortHelper.omsPortDescription; |
| 131 | import static org.onosproject.net.optical.device.OtuPortHelper.otuPortDescription; |
| 132 | import static org.onosproject.openflow.controller.Dpid.dpid; |
| 133 | import static org.onosproject.openflow.controller.Dpid.uri; |
| 134 | import static org.slf4j.LoggerFactory.getLogger; |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 135 | |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 136 | /** |
tom | b1260e4 | 2014-08-26 18:39:57 -0700 | [diff] [blame] | 137 | * Provider which uses an OpenFlow controller to detect network |
tom | e06f855 | 2014-08-26 16:58:42 -0700 | [diff] [blame] | 138 | * infrastructure devices. |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 139 | */ |
tom | b1260e4 | 2014-08-26 18:39:57 -0700 | [diff] [blame] | 140 | @Component(immediate = true) |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 141 | public class OpenFlowDeviceProvider extends AbstractProvider implements DeviceProvider { |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 142 | |
alshabib | a89cc58 | 2014-09-09 16:43:00 -0700 | [diff] [blame] | 143 | private static final Logger LOG = getLogger(OpenFlowDeviceProvider.class); |
Thomas Vachuska | d07c092 | 2015-10-06 14:48:06 -0700 | [diff] [blame] | 144 | |
Yuta HIGUCHI | 2341e60 | 2017-03-08 20:10:08 -0800 | [diff] [blame] | 145 | // TODO Some duplicate with one defined in OpticalAnnotations |
| 146 | // slice out optical specific handling and consolidate. |
| 147 | /** |
| 148 | * Annotation key for minimum frequency in Hz. |
| 149 | * Value is expected to be an integer. |
| 150 | */ |
| 151 | public static final String AK_MIN_FREQ_HZ = "minFrequency"; |
| 152 | |
| 153 | /** |
| 154 | * Annotation key for maximum frequency in Hz. |
| 155 | * Value is expected be an integer. |
| 156 | */ |
| 157 | public static final String AK_MAX_FREQ_HZ = "maxFrequency"; |
| 158 | |
| 159 | /** |
| 160 | * Annotation key for grid in Hz. |
| 161 | * Value is expected to be an integer. |
| 162 | */ |
| 163 | public static final String AK_GRID_HZ = "grid"; |
| 164 | |
| 165 | |
| 166 | /** |
| 167 | * Annotation key for minimum frequency in Hz. |
| 168 | * Value is expected to be an integer. |
| 169 | */ |
| 170 | public static final String AK_TX_MIN_FREQ_HZ = "txMinFrequency"; |
| 171 | |
| 172 | /** |
| 173 | * Annotation key for maximum frequency in Hz. |
| 174 | * Value is expected be an integer. |
| 175 | */ |
| 176 | public static final String AK_TX_MAX_FREQ_HZ = "txMaxFrequency"; |
| 177 | |
| 178 | /** |
| 179 | * Annotation key for grid in Hz. |
| 180 | * Value is expected to be an integer. |
| 181 | */ |
| 182 | public static final String AK_TX_GRID_HZ = "txGrid"; |
| 183 | |
| 184 | /** |
| 185 | * Annotation key for minimum frequency in Hz. |
| 186 | * Value is expected to be an integer. |
| 187 | */ |
| 188 | public static final String AK_RX_MIN_FREQ_HZ = "rxMinFrequency"; |
| 189 | |
| 190 | /** |
| 191 | * Annotation key for maximum frequency in Hz. |
| 192 | * Value is expected be an integer. |
| 193 | */ |
| 194 | public static final String AK_RX_MAX_FREQ_HZ = "rxMaxFrequency"; |
| 195 | |
| 196 | /** |
| 197 | * Annotation key for grid in Hz. |
| 198 | * Value is expected to be an integer. |
| 199 | */ |
| 200 | public static final String AK_RX_GRID_HZ = "rxGrid"; |
| 201 | |
Charles Chan | 25b7732 | 2015-12-13 01:00:56 -0800 | [diff] [blame] | 202 | //TODO consider renaming KBPS and MBPS (as they are used to convert by division) |
| 203 | private static final long KBPS = 1_000; |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 204 | private static final long MBPS = 1_000 * 1_000; |
Rimon Ashkenazy | e320103 | 2016-01-11 14:27:30 +0200 | [diff] [blame] | 205 | private static final Frequency FREQ50 = Frequency.ofGHz(50); |
| 206 | private static final Frequency FREQ191_7 = Frequency.ofGHz(191_700); |
Sho SHIMIZU | 00762ee | 2016-01-05 16:32:24 -0800 | [diff] [blame] | 207 | private static final Frequency FREQ4_4 = Frequency.ofGHz(4_400); |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 208 | |
| 209 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 210 | protected DeviceProviderRegistry providerRegistry; |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 211 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 212 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 213 | protected OpenFlowController controller; |
| 214 | |
Dusan Pajin | bab8a5e | 2015-07-24 17:37:19 +0200 | [diff] [blame] | 215 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 216 | protected ComponentConfigService cfgService; |
| 217 | |
Victor Silva | ac8dab0 | 2016-11-28 16:33:08 -0300 | [diff] [blame] | 218 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 219 | protected DriverService driverService; |
| 220 | |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 221 | private DeviceProviderService providerService; |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 222 | |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 223 | private final InternalDeviceProvider listener = new InternalDeviceProvider(); |
| 224 | |
Simon Hunt | 7a30765 | 2016-06-23 01:17:29 -0700 | [diff] [blame] | 225 | private static final String POLL_PROP_NAME = "portStatsPollFrequency"; |
| 226 | private static final int POLL_INTERVAL = 5; |
| 227 | @Property(name = POLL_PROP_NAME, intValue = POLL_INTERVAL, |
Dusan Pajin | bab8a5e | 2015-07-24 17:37:19 +0200 | [diff] [blame] | 228 | label = "Frequency (in seconds) for polling switch Port statistics") |
| 229 | private int portStatsPollFrequency = POLL_INTERVAL; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 230 | |
Victor Silva | ff5871b | 2016-10-04 18:08:47 -0300 | [diff] [blame] | 231 | private final Timer timer = new Timer("onos-openflow-portstats-collector"); |
sangyun-han | 07a572f | 2016-07-29 21:14:27 +0900 | [diff] [blame] | 232 | |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 233 | private HashMap<Dpid, PortStatsCollector> collectors = Maps.newHashMap(); |
tom | d40fc7a | 2014-09-04 16:41:10 -0700 | [diff] [blame] | 234 | |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 235 | /** |
| 236 | * Creates an OpenFlow device provider. |
| 237 | */ |
| 238 | public OpenFlowDeviceProvider() { |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 239 | super(new ProviderId("of", "org.onosproject.provider.openflow")); |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 240 | } |
| 241 | |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 242 | @Activate |
Dusan Pajin | bab8a5e | 2015-07-24 17:37:19 +0200 | [diff] [blame] | 243 | public void activate(ComponentContext context) { |
| 244 | cfgService.registerProperties(getClass()); |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 245 | providerService = providerRegistry.register(this); |
tom | d40fc7a | 2014-09-04 16:41:10 -0700 | [diff] [blame] | 246 | controller.addListener(listener); |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 247 | controller.addEventListener(listener); |
Jian Li | 152b885 | 2015-12-07 14:47:25 -0800 | [diff] [blame] | 248 | |
Victor Silva | 7944d14 | 2016-07-08 00:14:55 -0300 | [diff] [blame] | 249 | modified(context); |
| 250 | |
Thomas Vachuska | d07c092 | 2015-10-06 14:48:06 -0700 | [diff] [blame] | 251 | connectInitialDevices(); |
alshabib | a89cc58 | 2014-09-09 16:43:00 -0700 | [diff] [blame] | 252 | LOG.info("Started"); |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | @Deactivate |
Dusan Pajin | bab8a5e | 2015-07-24 17:37:19 +0200 | [diff] [blame] | 256 | public void deactivate(ComponentContext context) { |
| 257 | cfgService.unregisterProperties(getClass(), false); |
Ray Milkey | 7abe50b | 2016-04-13 11:57:44 -0700 | [diff] [blame] | 258 | listener.disable(); |
tom | d40fc7a | 2014-09-04 16:41:10 -0700 | [diff] [blame] | 259 | controller.removeListener(listener); |
Thomas Vachuska | d07c092 | 2015-10-06 14:48:06 -0700 | [diff] [blame] | 260 | providerRegistry.unregister(this); |
Thomas Vachuska | 74bcd92 | 2015-06-02 13:08:57 -0700 | [diff] [blame] | 261 | collectors.values().forEach(PortStatsCollector::stop); |
Thomas Vachuska | a394b95 | 2016-06-14 15:02:09 -0700 | [diff] [blame] | 262 | collectors.clear(); |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 263 | providerService = null; |
alshabib | a89cc58 | 2014-09-09 16:43:00 -0700 | [diff] [blame] | 264 | LOG.info("Stopped"); |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Dusan Pajin | bab8a5e | 2015-07-24 17:37:19 +0200 | [diff] [blame] | 267 | @Modified |
| 268 | public void modified(ComponentContext context) { |
Victor Silva | 7944d14 | 2016-07-08 00:14:55 -0300 | [diff] [blame] | 269 | Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties(); |
Dusan Pajin | bab8a5e | 2015-07-24 17:37:19 +0200 | [diff] [blame] | 270 | int newPortStatsPollFrequency; |
| 271 | try { |
Simon Hunt | 7a30765 | 2016-06-23 01:17:29 -0700 | [diff] [blame] | 272 | String s = get(properties, POLL_PROP_NAME); |
Dusan Pajin | bab8a5e | 2015-07-24 17:37:19 +0200 | [diff] [blame] | 273 | newPortStatsPollFrequency = isNullOrEmpty(s) ? portStatsPollFrequency : Integer.parseInt(s.trim()); |
| 274 | |
| 275 | } catch (NumberFormatException | ClassCastException e) { |
| 276 | newPortStatsPollFrequency = portStatsPollFrequency; |
| 277 | } |
| 278 | |
| 279 | if (newPortStatsPollFrequency != portStatsPollFrequency) { |
| 280 | portStatsPollFrequency = newPortStatsPollFrequency; |
| 281 | collectors.values().forEach(psc -> psc.adjustPollInterval(portStatsPollFrequency)); |
| 282 | } |
| 283 | |
| 284 | LOG.info("Settings: portStatsPollFrequency={}", portStatsPollFrequency); |
| 285 | } |
Ayaka Koshibe | e60d452 | 2014-10-28 15:07:00 -0700 | [diff] [blame] | 286 | |
Thomas Vachuska | d07c092 | 2015-10-06 14:48:06 -0700 | [diff] [blame] | 287 | private void connectInitialDevices() { |
| 288 | for (OpenFlowSwitch sw : controller.getSwitches()) { |
| 289 | try { |
| 290 | listener.switchAdded(new Dpid(sw.getId())); |
| 291 | } catch (Exception e) { |
| 292 | LOG.warn("Failed initially adding {} : {}", sw.getStringId(), e.getMessage()); |
| 293 | LOG.debug("Error details:", e); |
| 294 | // disconnect to trigger switch-add later |
| 295 | sw.disconnectSwitch(); |
| 296 | } |
Thomas Vachuska | d07c092 | 2015-10-06 14:48:06 -0700 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
Ayaka Koshibe | e60d452 | 2014-10-28 15:07:00 -0700 | [diff] [blame] | 300 | @Override |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 301 | public boolean isReachable(DeviceId deviceId) { |
| 302 | OpenFlowSwitch sw = controller.getSwitch(dpid(deviceId.uri())); |
Thomas Vachuska | d07c092 | 2015-10-06 14:48:06 -0700 | [diff] [blame] | 303 | return sw != null && sw.isConnected(); |
Ayaka Koshibe | e60d452 | 2014-10-28 15:07:00 -0700 | [diff] [blame] | 304 | } |
| 305 | |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 306 | @Override |
Ayaka Koshibe | 78bcbc1 | 2014-11-19 14:28:58 -0800 | [diff] [blame] | 307 | public void triggerProbe(DeviceId deviceId) { |
Madan Jampani | c332876 | 2015-05-31 12:39:27 -0700 | [diff] [blame] | 308 | LOG.debug("Triggering probe on device {}", deviceId); |
Ayaka Koshibe | e8708e3 | 2014-10-22 13:40:18 -0700 | [diff] [blame] | 309 | |
Yuta HIGUCHI | 802d0e8 | 2014-10-31 16:07:37 -0700 | [diff] [blame] | 310 | final Dpid dpid = dpid(deviceId.uri()); |
| 311 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
| 312 | if (sw == null || !sw.isConnected()) { |
Ayaka Koshibe | 78bcbc1 | 2014-11-19 14:28:58 -0800 | [diff] [blame] | 313 | LOG.error("Failed to probe device {} on sw={}", deviceId, sw); |
Yuta HIGUCHI | 802d0e8 | 2014-10-31 16:07:37 -0700 | [diff] [blame] | 314 | providerService.deviceDisconnected(deviceId); |
Thomas Vachuska | 74bcd92 | 2015-06-02 13:08:57 -0700 | [diff] [blame] | 315 | return; |
Yuta HIGUCHI | 802d0e8 | 2014-10-31 16:07:37 -0700 | [diff] [blame] | 316 | } else { |
Ayaka Koshibe | 78bcbc1 | 2014-11-19 14:28:58 -0800 | [diff] [blame] | 317 | LOG.trace("Confirmed device {} connection", deviceId); |
Yuta HIGUCHI | 802d0e8 | 2014-10-31 16:07:37 -0700 | [diff] [blame] | 318 | } |
Ayaka Koshibe | e8708e3 | 2014-10-22 13:40:18 -0700 | [diff] [blame] | 319 | |
Ayaka Koshibe | e60d452 | 2014-10-28 15:07:00 -0700 | [diff] [blame] | 320 | // 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] | 321 | OFFactory fact = sw.factory(); |
| 322 | switch (fact.getVersion()) { |
| 323 | case OF_10: |
| 324 | sw.sendMsg(fact.buildFeaturesRequest().setXid(0).build()); |
| 325 | break; |
| 326 | case OF_13: |
Yuta HIGUCHI | 6512f3e | 2017-05-18 17:21:24 -0700 | [diff] [blame] | 327 | case OF_14: |
| 328 | case OF_15: |
Ayaka Koshibe | e8708e3 | 2014-10-22 13:40:18 -0700 | [diff] [blame] | 329 | sw.sendMsg(fact.buildPortDescStatsRequest().setXid(0).build()); |
| 330 | break; |
| 331 | default: |
| 332 | LOG.warn("Unhandled protocol version"); |
| 333 | } |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | @Override |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 337 | public void roleChanged(DeviceId deviceId, MastershipRole newRole) { |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 338 | switch (newRole) { |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 339 | case MASTER: |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 340 | controller.setRole(dpid(deviceId.uri()), RoleState.MASTER); |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 341 | break; |
| 342 | case STANDBY: |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 343 | controller.setRole(dpid(deviceId.uri()), RoleState.EQUAL); |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 344 | break; |
| 345 | case NONE: |
Yuta HIGUCHI | 5481532 | 2014-10-31 23:17:08 -0700 | [diff] [blame] | 346 | controller.setRole(dpid(deviceId.uri()), RoleState.SLAVE); |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 347 | break; |
| 348 | default: |
| 349 | LOG.error("Unknown Mastership state : {}", newRole); |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 350 | |
| 351 | } |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 352 | LOG.debug("Accepting mastership role change to {} for device {}", newRole, deviceId); |
tom | ab21e7c | 2014-08-26 15:23:08 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 355 | @Override |
| 356 | public void changePortState(DeviceId deviceId, PortNumber portNumber, |
| 357 | boolean enable) { |
| 358 | final Dpid dpid = dpid(deviceId.uri()); |
| 359 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
| 360 | if (sw == null || !sw.isConnected()) { |
| 361 | LOG.error("Failed to change portState on device {}", deviceId); |
| 362 | return; |
| 363 | } |
| 364 | OFPortMod.Builder pmb = sw.factory().buildPortMod(); |
| 365 | OFPort port = OFPort.of((int) portNumber.toLong()); |
| 366 | pmb.setPortNo(port); |
Jimmy Jin | e9b7a02 | 2016-08-12 16:56:48 -0700 | [diff] [blame] | 367 | Set<OFPortConfig> portConfig = EnumSet.noneOf(OFPortConfig.class); |
| 368 | if (!enable) { |
| 369 | portConfig.add(OFPortConfig.PORT_DOWN); |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 370 | } |
Jimmy Jin | e9b7a02 | 2016-08-12 16:56:48 -0700 | [diff] [blame] | 371 | pmb.setConfig(portConfig); |
| 372 | Set<OFPortConfig> portMask = EnumSet.noneOf(OFPortConfig.class); |
| 373 | portMask.add(OFPortConfig.PORT_DOWN); |
| 374 | pmb.setMask(portMask); |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 375 | pmb.setAdvertise(0x0); |
| 376 | for (OFPortDesc pd : sw.getPorts()) { |
| 377 | if (pd.getPortNo().equals(port)) { |
| 378 | pmb.setHwAddr(pd.getHwAddr()); |
| 379 | break; |
| 380 | } |
| 381 | } |
| 382 | sw.sendMsg(Collections.singletonList(pmb.build())); |
| 383 | } |
| 384 | |
Thomas Vachuska | 893bf4b | 2015-05-29 18:13:18 -0700 | [diff] [blame] | 385 | private void pushPortMetrics(Dpid dpid, List<OFPortStatsEntry> portStatsEntries) { |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 386 | DeviceId deviceId = DeviceId.deviceId(Dpid.uri(dpid)); |
Ray Milkey | 3c4124a | 2016-08-31 12:09:06 -0700 | [diff] [blame] | 387 | Collection<PortStatistics> stats = |
| 388 | buildPortStatistics(deviceId, ImmutableList.copyOf(portStatsEntries)); |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 389 | providerService.updatePortStatistics(deviceId, stats); |
| 390 | } |
| 391 | |
Thomas Vachuska | 893bf4b | 2015-05-29 18:13:18 -0700 | [diff] [blame] | 392 | private Collection<PortStatistics> buildPortStatistics(DeviceId deviceId, |
| 393 | List<OFPortStatsEntry> entries) { |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 394 | HashSet<PortStatistics> stats = Sets.newHashSet(); |
| 395 | |
Thomas Vachuska | 893bf4b | 2015-05-29 18:13:18 -0700 | [diff] [blame] | 396 | for (OFPortStatsEntry entry : entries) { |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 397 | try { |
Thomas Vachuska | 4e6025e | 2015-06-03 11:25:21 -0700 | [diff] [blame] | 398 | if (entry == null || entry.getPortNo() == null || entry.getPortNo().getPortNumber() < 0) { |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 399 | continue; |
| 400 | } |
| 401 | DefaultPortStatistics.Builder builder = DefaultPortStatistics.builder(); |
| 402 | DefaultPortStatistics stat = builder.setDeviceId(deviceId) |
| 403 | .setPort(entry.getPortNo().getPortNumber()) |
| 404 | .setPacketsReceived(entry.getRxPackets().getValue()) |
| 405 | .setPacketsSent(entry.getTxPackets().getValue()) |
| 406 | .setBytesReceived(entry.getRxBytes().getValue()) |
| 407 | .setBytesSent(entry.getTxBytes().getValue()) |
| 408 | .setPacketsRxDropped(entry.getRxDropped().getValue()) |
| 409 | .setPacketsTxDropped(entry.getTxDropped().getValue()) |
| 410 | .setPacketsRxErrors(entry.getRxErrors().getValue()) |
| 411 | .setPacketsTxErrors(entry.getTxErrors().getValue()) |
| 412 | .setDurationSec(entry.getVersion() == OFVersion.OF_10 ? 0 : entry.getDurationSec()) |
| 413 | .setDurationNano(entry.getVersion() == OFVersion.OF_10 ? 0 : entry.getDurationNsec()) |
| 414 | .build(); |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 415 | |
Thomas Vachuska | f0397b5 | 2015-05-29 13:50:17 -0700 | [diff] [blame] | 416 | stats.add(stat); |
| 417 | } catch (Exception e) { |
| 418 | LOG.warn("Unable to process port stats", e); |
| 419 | } |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | return Collections.unmodifiableSet(stats); |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | private class InternalDeviceProvider implements OpenFlowSwitchListener, OpenFlowEventListener { |
Thomas Vachuska | 893bf4b | 2015-05-29 18:13:18 -0700 | [diff] [blame] | 426 | |
Srikanth Vavilapalli | 78baf58 | 2015-06-05 11:40:14 -0700 | [diff] [blame] | 427 | private HashMap<Dpid, List<OFPortStatsEntry>> portStatsReplies = new HashMap<>(); |
Ray Milkey | 7abe50b | 2016-04-13 11:57:44 -0700 | [diff] [blame] | 428 | private boolean isDisabled = false; |
Thomas Vachuska | 893bf4b | 2015-05-29 18:13:18 -0700 | [diff] [blame] | 429 | |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 430 | @Override |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 431 | public void switchAdded(Dpid dpid) { |
alshabib | 6f5460b | 2014-09-03 14:46:17 -0700 | [diff] [blame] | 432 | if (providerService == null) { |
| 433 | return; |
| 434 | } |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 435 | DeviceId did = deviceId(uri(dpid)); |
alshabib | 6f5460b | 2014-09-03 14:46:17 -0700 | [diff] [blame] | 436 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
Lei Xu | dee1aff | 2015-10-16 00:45:10 -0500 | [diff] [blame] | 437 | if (sw == null) { |
sangyun-han | 147e6f4 | 2016-07-26 17:14:41 +0900 | [diff] [blame] | 438 | LOG.error("Switch {} is not found", dpid); |
Lei Xu | dee1aff | 2015-10-16 00:45:10 -0500 | [diff] [blame] | 439 | return; |
| 440 | } |
alshabib | 6f5460b | 2014-09-03 14:46:17 -0700 | [diff] [blame] | 441 | |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 442 | ChassisId cId = new ChassisId(dpid.value()); |
Ray Milkey | e53f171 | 2015-01-16 09:17:16 -0800 | [diff] [blame] | 443 | |
Victor Silva | ac8dab0 | 2016-11-28 16:33:08 -0300 | [diff] [blame] | 444 | DefaultAnnotations.Builder annotationsBuilder = DefaultAnnotations.builder() |
andrea | fe3308f | 2015-10-06 15:51:25 -0700 | [diff] [blame] | 445 | .set(AnnotationKeys.PROTOCOL, sw.factory().getVersion().toString()) |
| 446 | .set(AnnotationKeys.CHANNEL_ID, sw.channelId()) |
Victor Silva | ac8dab0 | 2016-11-28 16:33:08 -0300 | [diff] [blame] | 447 | .set(AnnotationKeys.MANAGEMENT_ADDRESS, sw.channelId().split(":")[0]); |
| 448 | |
Yuta HIGUCHI | 2341e60 | 2017-03-08 20:10:08 -0800 | [diff] [blame] | 449 | // FIXME following ignores driver specified by name |
Victor Silva | ac8dab0 | 2016-11-28 16:33:08 -0300 | [diff] [blame] | 450 | Driver driver = driverService.getDriver(sw.manufacturerDescription(), |
| 451 | sw.hardwareDescription(), |
| 452 | sw.softwareDescription()); |
Thomas Vachuska | a79cbf7 | 2017-01-13 15:07:04 -0800 | [diff] [blame] | 453 | // FIXME: The following breaks the STC tests and will require to be revisited. |
| 454 | // if (driver != null) { |
| 455 | // annotationsBuilder.set(AnnotationKeys.DRIVER, driver.name()); |
| 456 | // } |
Victor Silva | ac8dab0 | 2016-11-28 16:33:08 -0300 | [diff] [blame] | 457 | |
| 458 | SparseAnnotations annotations = annotationsBuilder.build(); |
Ray Milkey | e53f171 | 2015-01-16 09:17:16 -0800 | [diff] [blame] | 459 | |
tom | d1900f3 | 2014-09-03 14:08:16 -0700 | [diff] [blame] | 460 | DeviceDescription description = |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 461 | new DefaultDeviceDescription(did.uri(), sw.deviceType(), |
Ray Milkey | d3edd03 | 2015-01-16 11:38:58 -0800 | [diff] [blame] | 462 | sw.manufacturerDescription(), |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 463 | sw.hardwareDescription(), |
| 464 | sw.softwareDescription(), |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 465 | sw.serialNumber(), |
Thomas Vachuska | 82041f5 | 2014-11-30 22:14:02 -0800 | [diff] [blame] | 466 | cId, annotations); |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 467 | providerService.deviceConnected(did, description); |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 468 | providerService.updatePorts(did, buildPortDescriptions(sw)); |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 469 | |
sangyun-han | 07a572f | 2016-07-29 21:14:27 +0900 | [diff] [blame] | 470 | PortStatsCollector psc = new PortStatsCollector(timer, sw, portStatsPollFrequency); |
Thomas Vachuska | a394b95 | 2016-06-14 15:02:09 -0700 | [diff] [blame] | 471 | stopCollectorIfNeeded(collectors.put(dpid, psc)); |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 472 | psc.start(); |
Lei Xu | dee1aff | 2015-10-16 00:45:10 -0500 | [diff] [blame] | 473 | |
| 474 | //figure out race condition for collectors.remove() and collectors.put() |
| 475 | if (controller.getSwitch(dpid) == null) { |
| 476 | switchRemoved(dpid); |
| 477 | } |
alshabib | 25c8eec | 2014-09-04 16:41:31 -0700 | [diff] [blame] | 478 | } |
| 479 | |
Thomas Vachuska | a394b95 | 2016-06-14 15:02:09 -0700 | [diff] [blame] | 480 | private void stopCollectorIfNeeded(PortStatsCollector collector) { |
| 481 | if (collector != null) { |
| 482 | collector.stop(); |
| 483 | } |
| 484 | } |
| 485 | |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 486 | @Override |
| 487 | public void switchRemoved(Dpid dpid) { |
Victor Silva | 03265dd | 2016-12-08 11:20:36 -0300 | [diff] [blame] | 488 | stopCollectorIfNeeded(collectors.remove(dpid)); |
alshabib | 6f5460b | 2014-09-03 14:46:17 -0700 | [diff] [blame] | 489 | if (providerService == null) { |
| 490 | return; |
| 491 | } |
tom | 782a7cf | 2014-09-11 23:58:38 -0700 | [diff] [blame] | 492 | providerService.deviceDisconnected(deviceId(uri(dpid))); |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 493 | } |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 494 | |
| 495 | @Override |
| 496 | public void switchChanged(Dpid dpid) { |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 497 | LOG.debug("switchChanged({})", dpid); |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 498 | if (providerService == null) { |
| 499 | return; |
| 500 | } |
| 501 | DeviceId did = deviceId(uri(dpid)); |
| 502 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
Lei Xu | dee1aff | 2015-10-16 00:45:10 -0500 | [diff] [blame] | 503 | if (sw == null) { |
sangyun-han | 147e6f4 | 2016-07-26 17:14:41 +0900 | [diff] [blame] | 504 | LOG.error("Switch {} is not found", dpid); |
Lei Xu | dee1aff | 2015-10-16 00:45:10 -0500 | [diff] [blame] | 505 | return; |
| 506 | } |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 507 | final List<PortDescription> ports = buildPortDescriptions(sw); |
| 508 | LOG.debug("switchChanged({}) {}", did, ports); |
| 509 | providerService.updatePorts(did, ports); |
Ayaka Koshibe | 38594c2 | 2014-10-22 13:36:12 -0700 | [diff] [blame] | 510 | } |
| 511 | |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 512 | @Override |
| 513 | public void portChanged(Dpid dpid, OFPortStatus status) { |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 514 | LOG.debug("portChanged({},{})", dpid, status); |
alshabib | afc514a | 2014-12-01 14:44:05 -0800 | [diff] [blame] | 515 | PortDescription portDescription = buildPortDescription(status); |
Michal Mach | ce77433 | 2017-01-25 11:02:55 +0100 | [diff] [blame] | 516 | if (status.getReason() != OFPortReason.DELETE) { |
| 517 | providerService.portStatusChanged(deviceId(uri(dpid)), portDescription); |
| 518 | } else { |
| 519 | providerService.deletePort(deviceId(uri(dpid)), portDescription); |
| 520 | } |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 523 | @Override |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 524 | public void receivedRoleReply(Dpid dpid, RoleState requested, RoleState response) { |
HIGUCHI Yuta | 1979f55 | 2015-12-28 21:24:26 -0800 | [diff] [blame] | 525 | LOG.debug("receivedRoleReply({},{},{})", dpid, requested, response); |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 526 | MastershipRole request = roleOf(requested); |
| 527 | MastershipRole reply = roleOf(response); |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 528 | providerService.receivedRoleReply(deviceId(uri(dpid)), request, reply); |
| 529 | } |
| 530 | |
| 531 | /** |
| 532 | * Translates a RoleState to the corresponding MastershipRole. |
| 533 | * |
Thomas Vachuska | fc52fec | 2015-05-18 19:13:56 -0700 | [diff] [blame] | 534 | * @param response role state |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 535 | * @return a MastershipRole |
| 536 | */ |
| 537 | private MastershipRole roleOf(RoleState response) { |
| 538 | switch (response) { |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 539 | case MASTER: |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 540 | return MastershipRole.MASTER; |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 541 | case EQUAL: |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 542 | return MastershipRole.STANDBY; |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 543 | case SLAVE: |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 544 | return MastershipRole.NONE; |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 545 | default: |
Ayaka Koshibe | 3ef2b0d | 2014-10-31 13:58:27 -0700 | [diff] [blame] | 546 | LOG.warn("unknown role {}", response); |
| 547 | return null; |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 548 | } |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 549 | } |
| 550 | |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 551 | /** |
| 552 | * Builds a list of port descriptions for a given list of ports. |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 553 | * |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 554 | * @return list of portdescriptions |
| 555 | */ |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 556 | private List<PortDescription> buildPortDescriptions(OpenFlowSwitch sw) { |
| 557 | final List<PortDescription> portDescs = new ArrayList<>(sw.getPorts().size()); |
Rimon Ashkenazy | 8ebfff0 | 2016-02-01 11:56:36 +0200 | [diff] [blame] | 558 | if (!((Device.Type.ROADM.equals(sw.deviceType())) || |
MaoLu | c201ae4 | 2017-02-06 17:57:01 -0800 | [diff] [blame] | 559 | (Device.Type.OTN.equals(sw.deviceType())) || |
| 560 | (Device.Type.OPTICAL_AMPLIFIER.equals(sw.deviceType())))) { |
Yuta HIGUCHI | 2341e60 | 2017-03-08 20:10:08 -0800 | [diff] [blame] | 561 | // build regular (=non-optical) Device ports |
| 562 | sw.getPorts().forEach(port -> portDescs.add(buildPortDescription(port))); |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 563 | } |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 564 | |
Yuta HIGUCHI | 2341e60 | 2017-03-08 20:10:08 -0800 | [diff] [blame] | 565 | // TODO handle Optical Device, but plain OF devices(1.4 and later) |
| 566 | |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 567 | OpenFlowOpticalSwitch opsw; |
| 568 | switch (sw.deviceType()) { |
| 569 | case ROADM: |
Rimon Ashkenazy | 8ebfff0 | 2016-02-01 11:56:36 +0200 | [diff] [blame] | 570 | case OTN: |
MaoLu | c201ae4 | 2017-02-06 17:57:01 -0800 | [diff] [blame] | 571 | case OPTICAL_AMPLIFIER: |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 572 | opsw = (OpenFlowOpticalSwitch) sw; |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 573 | List<OFPortDesc> ports = opsw.getPorts(); |
| 574 | LOG.debug("SW ID {} , ETH- ODU CLT Ports {}", opsw.getId(), ports); |
| 575 | // ODU client ports are reported as ETH |
| 576 | ports.forEach(port -> portDescs.add(buildOduCltPortDescription(port))); |
| 577 | |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 578 | opsw.getPortTypes().forEach(type -> { |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 579 | List<? extends OFObject> portsOf = opsw.getPortsOf(type); |
| 580 | LOG.debug("Ports Of{}", portsOf); |
| 581 | portsOf.forEach( |
| 582 | op -> { |
Jimmy Yan | 4deb03b | 2016-06-24 10:53:54 -0700 | [diff] [blame] | 583 | portDescs.add(buildPortDescription(type, op, opsw)); |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 584 | } |
| 585 | ); |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 586 | }); |
| 587 | break; |
| 588 | case FIBER_SWITCH: |
| 589 | opsw = (OpenFlowOpticalSwitch) sw; |
| 590 | opsw.getPortTypes().forEach(type -> { |
| 591 | opsw.getPortsOf(type).forEach( |
| 592 | op -> { |
| 593 | portDescs.add(buildPortDescription((OFCalientPortDescStatsEntry) op)); |
| 594 | } |
| 595 | ); |
| 596 | }); |
| 597 | break; |
| 598 | default: |
| 599 | break; |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 600 | } |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 601 | |
alshabib | 4680bb6 | 2014-09-04 17:15:08 -0700 | [diff] [blame] | 602 | return portDescs; |
| 603 | } |
| 604 | |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 605 | private PortDescription buildOduCltPortDescription(OFPortDesc port) { |
| 606 | PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber()); |
| 607 | boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN) && |
| 608 | !port.getConfig().contains(OFPortConfig.PORT_DOWN); |
Rimon Ashkenazy | d15ebf5 | 2016-01-06 14:36:13 +0200 | [diff] [blame] | 609 | Long portSpeedInMbps = portSpeed(port); |
Toru Furusawa | 72ee30c | 2016-01-08 13:29:04 -0800 | [diff] [blame] | 610 | CltSignalType sigType = null; |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 611 | |
Rimon Ashkenazy | d15ebf5 | 2016-01-06 14:36:13 +0200 | [diff] [blame] | 612 | switch (portSpeedInMbps.toString()) { |
| 613 | case "1000": |
Toru Furusawa | 72ee30c | 2016-01-08 13:29:04 -0800 | [diff] [blame] | 614 | sigType = CltSignalType.CLT_1GBE; |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 615 | break; |
Rimon Ashkenazy | d15ebf5 | 2016-01-06 14:36:13 +0200 | [diff] [blame] | 616 | case "10000": |
Toru Furusawa | 72ee30c | 2016-01-08 13:29:04 -0800 | [diff] [blame] | 617 | sigType = CltSignalType.CLT_10GBE; |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 618 | break; |
Rimon Ashkenazy | d15ebf5 | 2016-01-06 14:36:13 +0200 | [diff] [blame] | 619 | case "40000": |
Toru Furusawa | 72ee30c | 2016-01-08 13:29:04 -0800 | [diff] [blame] | 620 | sigType = CltSignalType.CLT_40GBE; |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 621 | break; |
Rimon Ashkenazy | d15ebf5 | 2016-01-06 14:36:13 +0200 | [diff] [blame] | 622 | case "100000": |
Toru Furusawa | 72ee30c | 2016-01-08 13:29:04 -0800 | [diff] [blame] | 623 | sigType = CltSignalType.CLT_100GBE; |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 624 | break; |
| 625 | default: |
Rimon Ashkenazy | d15ebf5 | 2016-01-06 14:36:13 +0200 | [diff] [blame] | 626 | throw new RuntimeException("Un recognize OduClt speed: " + portSpeedInMbps.toString()); |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | SparseAnnotations annotations = buildOduCltAnnotation(port); |
HIGUCHI Yuta | 4c0ef6b | 2016-05-02 19:45:41 -0700 | [diff] [blame] | 630 | return oduCltPortDescription(portNo, enabled, sigType, annotations); |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | private SparseAnnotations buildOduCltAnnotation(OFPortDesc port) { |
| 634 | SparseAnnotations annotations = null; |
| 635 | String portName = Strings.emptyToNull(port.getName()); |
| 636 | if (portName != null) { |
| 637 | annotations = DefaultAnnotations.builder() |
| 638 | .set(AnnotationKeys.PORT_NAME, portName) |
| 639 | .set(AnnotationKeys.STATIC_PORT, Boolean.TRUE.toString()).build(); |
| 640 | } |
| 641 | return annotations; |
| 642 | } |
| 643 | |
Jimmy Yan | 4deb03b | 2016-06-24 10:53:54 -0700 | [diff] [blame] | 644 | private PortDescription buildPortDescription(PortDescPropertyType ptype, OFObject port, |
| 645 | OpenFlowOpticalSwitch opsw) { |
yjimmyy | 646aa02 | 2016-07-05 12:09:50 -0700 | [diff] [blame] | 646 | if (port instanceof OFPortOptical) { |
Jimmy Yan | 4deb03b | 2016-06-24 10:53:54 -0700 | [diff] [blame] | 647 | return buildPortDescription(ptype, (OFPortOptical) port, opsw); |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 648 | } |
| 649 | return buildPortDescription(ptype, (OFExpPort) port); |
| 650 | } |
| 651 | |
Rimon Ashkenazy | 8ebfff0 | 2016-02-01 11:56:36 +0200 | [diff] [blame] | 652 | private boolean matchingOtuPortSignalTypes(OFPortOpticalTransportSignalType sigType, |
| 653 | OduSignalType oduSignalType) { |
| 654 | switch (sigType) { |
| 655 | case OTU2: |
| 656 | if (oduSignalType == OduSignalType.ODU2) { |
| 657 | return true; |
| 658 | } |
| 659 | break; |
| 660 | case OTU4: |
| 661 | if (oduSignalType == OduSignalType.ODU4) { |
| 662 | return true; |
| 663 | } |
| 664 | break; |
| 665 | default: |
| 666 | break; |
| 667 | } |
| 668 | return false; |
| 669 | } |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 670 | /** |
| 671 | * Build a portDescription from a given a port description describing some |
| 672 | * Optical port. |
| 673 | * |
| 674 | * @param ptype description property type. |
| 675 | * @param port the port to build from. |
| 676 | * @return portDescription for the port. |
| 677 | */ |
| 678 | private PortDescription buildPortDescription(PortDescPropertyType ptype, OFExpPort port) { |
| 679 | PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber()); |
| 680 | boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN) |
| 681 | && !port.getConfig().contains(OFPortConfig.PORT_DOWN); |
Yuta HIGUCHI | 2341e60 | 2017-03-08 20:10:08 -0800 | [diff] [blame] | 682 | SparseAnnotations annotations = makePortAnnotation(port.getName(), port.getHwAddr().toString()).build(); |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 683 | |
| 684 | OFExpPortDescPropOpticalTransport firstProp = port.getProperties().get(0); |
| 685 | OFPortOpticalTransportSignalType sigType = firstProp.getPortSignalType(); |
| 686 | |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 687 | PortDescription portDes = null; |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 688 | switch (sigType) { |
| 689 | case OMSN: |
HIGUCHI Yuta | 95d83e8 | 2016-04-26 12:13:48 -0700 | [diff] [blame] | 690 | portDes = omsPortDescription(portNo, enabled, |
Rimon Ashkenazy | e320103 | 2016-01-11 14:27:30 +0200 | [diff] [blame] | 691 | FREQ191_7, FREQ191_7.add(FREQ4_4), FREQ50, annotations); |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 692 | break; |
| 693 | case OCH: |
| 694 | OFExpPortOpticalTransportLayerEntry entry = firstProp.getFeatures().get(0).getValue().get(0); |
| 695 | OFPortOpticalTransportLayerClass layerClass = entry.getLayerClass(); |
| 696 | if (!OFPortOpticalTransportLayerClass.ODU.equals(layerClass)) { |
| 697 | LOG.error("Unsupported layer Class {} ", layerClass); |
| 698 | return null; |
| 699 | } |
| 700 | |
| 701 | // convert to ONOS OduSignalType |
| 702 | OduSignalType oduSignalType = OpenFlowDeviceValueMapper. |
| 703 | lookupOduSignalType((byte) entry.getSignalType()); |
| 704 | //OchSignal is needed for OchPortDescription constructor, |
| 705 | //yet not relevant for tunable OCH port, creating with default parameters |
| 706 | OchSignal signalId = new OchSignal(GridType.DWDM, ChannelSpacing.CHL_50GHZ, 1, 1); |
| 707 | |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 708 | portDes = ochPortDescription(portNo, enabled, |
| 709 | oduSignalType, true, |
| 710 | signalId, annotations); |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 711 | |
| 712 | break; |
| 713 | case OTU2: |
| 714 | case OTU4: |
Rimon Ashkenazy | 8ebfff0 | 2016-02-01 11:56:36 +0200 | [diff] [blame] | 715 | entry = firstProp.getFeatures().get(0).getValue().get(0); |
| 716 | layerClass = entry.getLayerClass(); |
| 717 | if (!OFPortOpticalTransportLayerClass.ODU.equals(layerClass)) { |
| 718 | LOG.error("Unsupported layer Class {} ", layerClass); |
| 719 | return null; |
| 720 | } |
| 721 | |
| 722 | // convert to ONOS OduSignalType |
| 723 | OduSignalType oduSignalTypeOtuPort = OpenFlowDeviceValueMapper. |
| 724 | lookupOduSignalType((byte) entry.getSignalType()); |
| 725 | if (!matchingOtuPortSignalTypes(sigType, oduSignalTypeOtuPort)) { |
| 726 | LOG.error("Wrong oduSignalType {} for OTU Port sigType {} ", oduSignalTypeOtuPort, sigType); |
| 727 | return null; |
| 728 | } |
| 729 | OtuSignalType otuSignalType = |
| 730 | ((sigType == OFPortOpticalTransportSignalType.OTU2) ? OtuSignalType.OTU2 : |
| 731 | OtuSignalType.OTU4); |
HIGUCHI Yuta | 5be3e82 | 2016-05-03 13:51:42 -0700 | [diff] [blame] | 732 | portDes = otuPortDescription(portNo, enabled, otuSignalType, annotations); |
Rimon Ashkenazy | 8ebfff0 | 2016-02-01 11:56:36 +0200 | [diff] [blame] | 733 | break; |
Yafit Hadar | a9a73de | 2015-09-06 13:52:52 +0300 | [diff] [blame] | 734 | default: |
| 735 | break; |
| 736 | } |
| 737 | |
| 738 | return portDes; |
| 739 | } |
| 740 | |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 741 | /** |
Yuta HIGUCHI | 2341e60 | 2017-03-08 20:10:08 -0800 | [diff] [blame] | 742 | * Creates an annotation builder for the port name if one is available. |
Ray Milkey | e0fade7 | 2015-01-15 13:29:47 -0800 | [diff] [blame] | 743 | * |
lishuai | 4ee4204 | 2015-11-30 17:19:21 +0800 | [diff] [blame] | 744 | * @param portName the port name |
| 745 | * @param portMac the port mac |
Yuta HIGUCHI | 2341e60 | 2017-03-08 20:10:08 -0800 | [diff] [blame] | 746 | * @return annotation builder containing port name and/or port MAC if any of |
Andrea Campanella | c8b871a | 2017-01-19 13:53:05 -0800 | [diff] [blame] | 747 | * the two is found, empty otherwise |
Ray Milkey | e0fade7 | 2015-01-15 13:29:47 -0800 | [diff] [blame] | 748 | */ |
Yuta HIGUCHI | 2341e60 | 2017-03-08 20:10:08 -0800 | [diff] [blame] | 749 | private DefaultAnnotations.Builder makePortAnnotation(String portName, String portMac) { |
Andrea Campanella | c8b871a | 2017-01-19 13:53:05 -0800 | [diff] [blame] | 750 | DefaultAnnotations.Builder builder = DefaultAnnotations.builder(); |
lishuai | 4ee4204 | 2015-11-30 17:19:21 +0800 | [diff] [blame] | 751 | String pName = Strings.emptyToNull(portName); |
| 752 | String pMac = Strings.emptyToNull(portMac); |
Andrea Campanella | c8b871a | 2017-01-19 13:53:05 -0800 | [diff] [blame] | 753 | if (pName != null) { |
| 754 | builder.set(AnnotationKeys.PORT_NAME, pName); |
Ray Milkey | e0fade7 | 2015-01-15 13:29:47 -0800 | [diff] [blame] | 755 | } |
Andrea Campanella | c8b871a | 2017-01-19 13:53:05 -0800 | [diff] [blame] | 756 | if (pMac != null) { |
| 757 | builder.set(AnnotationKeys.PORT_MAC, pMac); |
| 758 | } |
Yuta HIGUCHI | 2341e60 | 2017-03-08 20:10:08 -0800 | [diff] [blame] | 759 | return builder; |
| 760 | } |
| 761 | |
| 762 | private String mhzToAnnotation(long freqMhz) { |
| 763 | // annotations is in Hz |
| 764 | return Long.toString(freqMhz * 1_000_000); |
| 765 | } |
| 766 | |
| 767 | private PortDescription buildPortDescription14(OFPortDesc port) { |
| 768 | PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber()); |
| 769 | boolean enabled = |
| 770 | !port.getState().contains(OFPortState.LINK_DOWN) && |
| 771 | !port.getConfig().contains(OFPortConfig.PORT_DOWN); |
| 772 | Builder annotations = makePortAnnotation(port.getName(), |
| 773 | port.getHwAddr().toString()); |
| 774 | |
| 775 | Optional<OFPortDescPropEthernet> ether = port.getProperties().stream() |
| 776 | .filter(OFPortDescPropEthernet.class::isInstance) |
| 777 | .map(OFPortDescPropEthernet.class::cast) |
| 778 | .findAny(); |
| 779 | if (ether.isPresent()) { |
| 780 | // ethernet port |
| 781 | // TODO parse other part of OFPortDescPropEthernet if necessary |
| 782 | return new DefaultPortDescription(portNo, |
| 783 | enabled, |
| 784 | COPPER, |
| 785 | portSpeed(port), |
| 786 | annotations.build()); |
| 787 | } |
| 788 | |
| 789 | Optional<OFPortDescPropOptical> optical = port.getProperties().stream() |
| 790 | .filter(OFPortDescPropOptical.class::isInstance) |
| 791 | .map(OFPortDescPropOptical.class::cast) |
| 792 | .findAny(); |
| 793 | if (optical.isPresent()) { |
| 794 | // optical port |
| 795 | |
| 796 | // FIXME is there a generic way to access OFOpticalPortFeaturesSerializerVer14 |
| 797 | |
| 798 | // wire value for OFOpticalPortFeatures.USE_FREQ |
| 799 | final long useFreq = 0x8; |
| 800 | if ((optical.get().getSupported() & useFreq) != 0) { |
| 801 | // unit is in Frequency Mhz |
| 802 | long rxMinFreq = optical.get().getRxMinFreqLmda(); |
| 803 | long rxMaxFreq = optical.get().getRxMaxFreqLmda(); |
| 804 | long txMinFreq = optical.get().getTxMinFreqLmda(); |
| 805 | long txMaxFreq = optical.get().getTxMaxFreqLmda(); |
| 806 | |
| 807 | annotations.set(AK_RX_MIN_FREQ_HZ, mhzToAnnotation(rxMinFreq)); |
| 808 | annotations.set(AK_RX_MAX_FREQ_HZ, mhzToAnnotation(rxMaxFreq)); |
| 809 | |
| 810 | annotations.set(AK_TX_MIN_FREQ_HZ, mhzToAnnotation(txMinFreq)); |
| 811 | annotations.set(AK_TX_MAX_FREQ_HZ, mhzToAnnotation(txMaxFreq)); |
| 812 | |
| 813 | // FIXME pretty confident this is not going to happen |
| 814 | // unless Device models Tx/Rx ports as separate port |
| 815 | if (rxMinFreq == txMinFreq) { |
| 816 | annotations.set(AK_MIN_FREQ_HZ, |
| 817 | mhzToAnnotation(rxMinFreq)); |
| 818 | } |
| 819 | if (rxMaxFreq == txMaxFreq) { |
| 820 | annotations.set(AK_MAX_FREQ_HZ, |
| 821 | mhzToAnnotation(rxMaxFreq)); |
| 822 | } |
| 823 | |
| 824 | } else { |
| 825 | // unit is in Lambda nm * 100 |
| 826 | // TODO implement |
| 827 | LOG.trace("Optical parameter specified in Lambda not supported yet", portNo); |
| 828 | } |
| 829 | // TODO parse other part of OFPortDescPropOptical |
| 830 | // Tx/Rx tunable, ... |
| 831 | // Power is configurable or now |
| 832 | |
| 833 | // TODO How to determine appropriate port type? |
| 834 | |
| 835 | return new DefaultPortDescription(portNo, |
| 836 | enabled, |
| 837 | FIBER, |
| 838 | portSpeed(port), |
| 839 | annotations.build()); |
| 840 | } |
| 841 | |
| 842 | // fall back default |
| 843 | return new DefaultPortDescription(portNo, |
| 844 | enabled, |
| 845 | COPPER, |
| 846 | portSpeed(port), |
| 847 | annotations.build()); |
| 848 | |
Ray Milkey | e0fade7 | 2015-01-15 13:29:47 -0800 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | /** |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 852 | * Build a portDescription from a given Ethernet port description. |
tom | ff7eb7c | 2014-09-08 12:49:03 -0700 | [diff] [blame] | 853 | * |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 854 | * @param port the port to build from. |
| 855 | * @return portDescription for the port. |
| 856 | */ |
| 857 | private PortDescription buildPortDescription(OFPortDesc port) { |
Yuta HIGUCHI | 2341e60 | 2017-03-08 20:10:08 -0800 | [diff] [blame] | 858 | if (port.getVersion().wireVersion >= OFVersion.OF_14.getWireVersion()) { |
| 859 | return buildPortDescription14(port); |
| 860 | } |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 861 | PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber()); |
| 862 | boolean enabled = |
| 863 | !port.getState().contains(OFPortState.LINK_DOWN) && |
| 864 | !port.getConfig().contains(OFPortConfig.PORT_DOWN); |
| 865 | Port.Type type = port.getCurr().contains(OFPortFeatures.PF_FIBER) ? FIBER : COPPER; |
Yuta HIGUCHI | 2341e60 | 2017-03-08 20:10:08 -0800 | [diff] [blame] | 866 | SparseAnnotations annotations = makePortAnnotation(port.getName(), port.getHwAddr().toString()).build(); |
Ray Milkey | e0fade7 | 2015-01-15 13:29:47 -0800 | [diff] [blame] | 867 | return new DefaultPortDescription(portNo, enabled, type, |
| 868 | portSpeed(port), annotations); |
alshabib | a14f364 | 2014-09-05 09:31:31 -0700 | [diff] [blame] | 869 | } |
Ayaka Koshibe | ab91cc4 | 2014-09-25 10:20:52 -0700 | [diff] [blame] | 870 | |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 871 | /** |
| 872 | * Build a portDescription from a given a port description describing some |
| 873 | * Optical port. |
| 874 | * |
| 875 | * @param port description property type. |
| 876 | * @param port the port to build from. |
| 877 | * @return portDescription for the port. |
| 878 | */ |
Jimmy Yan | 4deb03b | 2016-06-24 10:53:54 -0700 | [diff] [blame] | 879 | private PortDescription buildPortDescription(PortDescPropertyType ptype, OFPortOptical port, |
| 880 | OpenFlowOpticalSwitch opsw) { |
Jon Hall | cbd1b39 | 2017-01-18 20:15:44 -0800 | [diff] [blame] | 881 | checkArgument(!port.getDesc().isEmpty()); |
Marc De Leenheer | fc913dd | 2015-07-30 16:04:55 -0700 | [diff] [blame] | 882 | |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 883 | // Minimally functional fixture. This needs to be fixed as we add better support. |
| 884 | PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber()); |
Ayaka Koshibe | 74b5527 | 2015-05-28 15:16:04 -0700 | [diff] [blame] | 885 | |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 886 | boolean enabled = !port.getState().contains(OFPortState.LINK_DOWN) |
| 887 | && !port.getConfig().contains(OFPortConfig.PORT_DOWN); |
Yuta HIGUCHI | 2341e60 | 2017-03-08 20:10:08 -0800 | [diff] [blame] | 888 | SparseAnnotations annotations = makePortAnnotation(port.getName(), port.getHwAddr().toString()).build(); |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 889 | |
| 890 | if (port.getVersion() == OFVersion.OF_13 |
| 891 | && ptype == PortDescPropertyType.OPTICAL_TRANSPORT) { |
| 892 | // At this point, not much is carried in the optical port message. |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 893 | LOG.debug("Optical transport port message {}", port.toString()); |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 894 | } else { |
| 895 | // removable once 1.4+ support complete. |
Ayaka Koshibe | ae54173 | 2015-05-19 13:37:27 -0700 | [diff] [blame] | 896 | LOG.debug("Unsupported optical port properties"); |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 897 | } |
Marc De Leenheer | fc913dd | 2015-07-30 16:04:55 -0700 | [diff] [blame] | 898 | |
| 899 | OFPortDescPropOpticalTransport desc = port.getDesc().get(0); |
| 900 | switch (desc.getPortSignalType()) { |
| 901 | // FIXME: use constants once loxi has full optical extensions |
| 902 | case 2: // OMS port |
| 903 | // Assume complete optical spectrum and 50 GHz grid |
Jimmy Yan | 4deb03b | 2016-06-24 10:53:54 -0700 | [diff] [blame] | 904 | Set<OchSignal> signals = null; |
| 905 | if (opsw instanceof HandlerBehaviour) { |
| 906 | DriverHandler driverHandler = ((HandlerBehaviour) opsw).handler(); |
| 907 | if (driverHandler != null && driverHandler.hasBehaviour(LambdaQuery.class)) { |
| 908 | try { |
| 909 | signals = driverHandler.behaviour(LambdaQuery.class).queryLambdas(portNo); |
| 910 | } catch (NullPointerException e) { |
| 911 | signals = null; |
| 912 | } |
| 913 | } |
| 914 | } |
| 915 | Frequency minFreq; |
| 916 | Frequency maxFreq; |
| 917 | Frequency channelSpacing; |
| 918 | if (signals == null || signals.isEmpty()) { |
| 919 | minFreq = Spectrum.U_BAND_MIN; |
| 920 | maxFreq = Spectrum.O_BAND_MAX; |
| 921 | channelSpacing = Frequency.ofGHz(50); |
| 922 | } else { |
| 923 | Comparator<OchSignal> compare = |
| 924 | (OchSignal a, OchSignal b) -> a.spacingMultiplier() - b.spacingMultiplier(); |
| 925 | OchSignal minOch = Collections.min(signals, compare); |
| 926 | OchSignal maxOch = Collections.max(signals, compare); |
| 927 | minFreq = minOch.centralFrequency(); |
| 928 | maxFreq = maxOch.centralFrequency(); |
| 929 | channelSpacing = minOch.channelSpacing().frequency(); |
| 930 | } |
| 931 | return omsPortDescription(portNo, enabled, minFreq, |
| 932 | maxFreq, channelSpacing, annotations); |
Marc De Leenheer | fc913dd | 2015-07-30 16:04:55 -0700 | [diff] [blame] | 933 | case 5: // OCH port |
| 934 | OchSignal signal = new OchSignal(GridType.DWDM, ChannelSpacing.CHL_50GHZ, 0, 4); |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 935 | return ochPortDescription(portNo, enabled, OduSignalType.ODU4, |
Marc De Leenheer | fc913dd | 2015-07-30 16:04:55 -0700 | [diff] [blame] | 936 | true, signal, annotations); |
| 937 | default: |
| 938 | break; |
| 939 | } |
| 940 | |
Ayaka Koshibe | 74b5527 | 2015-05-28 15:16:04 -0700 | [diff] [blame] | 941 | return new DefaultPortDescription(portNo, enabled, FIBER, 0, annotations); |
Ayaka Koshibe | 5460d62 | 2015-05-14 12:19:19 -0700 | [diff] [blame] | 942 | } |
| 943 | |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 944 | /** |
| 945 | * Build a portDescription from a given port description describing a fiber switch optical port. |
| 946 | * |
| 947 | * @param port description property type. |
| 948 | * @param port the port to build from. |
| 949 | * @return portDescription for the port. |
| 950 | */ |
| 951 | private PortDescription buildPortDescription(OFCalientPortDescStatsEntry port) { |
| 952 | PortNumber portNo = PortNumber.portNumber(port.getPortNo().getPortNumber()); |
| 953 | |
Marc De Leenheer | c662d32 | 2016-02-18 16:05:10 -0800 | [diff] [blame] | 954 | // Use the alias name if it's available |
| 955 | String name = port.getName(); |
| 956 | List<OFCalientPortDescProp> props = port.getProperties(); |
Jon Hall | cbd1b39 | 2017-01-18 20:15:44 -0800 | [diff] [blame] | 957 | if (props != null && !props.isEmpty()) { |
Marc De Leenheer | c662d32 | 2016-02-18 16:05:10 -0800 | [diff] [blame] | 958 | OFCalientPortDescPropOptical propOptical = (OFCalientPortDescPropOptical) props.get(0); |
| 959 | if (propOptical != null) { |
| 960 | name = propOptical.getInAlias(); |
| 961 | } |
| 962 | } |
| 963 | |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 964 | // FIXME when Calient OF agent reports port status |
| 965 | boolean enabled = true; |
Yuta HIGUCHI | 2341e60 | 2017-03-08 20:10:08 -0800 | [diff] [blame] | 966 | SparseAnnotations annotations = makePortAnnotation(name, port.getHwAddr().toString()).build(); |
Marc De Leenheer | b931137 | 2015-07-09 11:36:49 -0700 | [diff] [blame] | 967 | |
Marc De Leenheer | fc913dd | 2015-07-30 16:04:55 -0700 | [diff] [blame] | 968 | // S160 data sheet |
| 969 | // Wavelength range: 1260 - 1630 nm, grid is irrelevant for this type of switch |
HIGUCHI Yuta | 95d83e8 | 2016-04-26 12:13:48 -0700 | [diff] [blame] | 970 | return omsPortDescription(portNo, enabled, |
Marc De Leenheer | fc913dd | 2015-07-30 16:04:55 -0700 | [diff] [blame] | 971 | 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] | 972 | } |
| 973 | |
alshabib | afc514a | 2014-12-01 14:44:05 -0800 | [diff] [blame] | 974 | private PortDescription buildPortDescription(OFPortStatus status) { |
| 975 | OFPortDesc port = status.getDesc(); |
| 976 | if (status.getReason() != OFPortReason.DELETE) { |
| 977 | return buildPortDescription(port); |
| 978 | } else { |
Yuta HIGUCHI | 6512f3e | 2017-05-18 17:21:24 -0700 | [diff] [blame] | 979 | PortDescription desc = buildPortDescription(port); |
| 980 | if (desc.isEnabled()) { |
| 981 | return DefaultPortDescription.builder(desc) |
| 982 | .isEnabled(false) |
| 983 | .build(); |
| 984 | } |
| 985 | return desc; |
alshabib | afc514a | 2014-12-01 14:44:05 -0800 | [diff] [blame] | 986 | } |
| 987 | } |
| 988 | |
Yuta HIGUCHI | 2341e60 | 2017-03-08 20:10:08 -0800 | [diff] [blame] | 989 | /** |
| 990 | * Returns port speed in Mbps. |
| 991 | * |
| 992 | * @param port description to parse |
| 993 | * @return port speed in Mbps |
| 994 | */ |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 995 | private long portSpeed(OFPortDesc port) { |
Yuta HIGUCHI | 2341e60 | 2017-03-08 20:10:08 -0800 | [diff] [blame] | 996 | if (port.getVersion().getWireVersion() >= OFVersion.OF_14.getWireVersion()) { |
| 997 | // OFPortDescPropEthernet |
| 998 | return port.getProperties().stream() |
| 999 | .filter(OFPortDescPropEthernet.class::isInstance) |
| 1000 | .map(OFPortDescPropEthernet.class::cast) |
| 1001 | .mapToLong(OFPortDescPropEthernet::getCurrSpeed) |
| 1002 | .map(kbps -> kbps / KBPS) |
| 1003 | .findAny() |
| 1004 | .orElse(PortSpeed.SPEED_NONE.getSpeedBps() / MBPS); |
| 1005 | } |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 1006 | if (port.getVersion() == OFVersion.OF_13) { |
Charles Chan | 25b7732 | 2015-12-13 01:00:56 -0800 | [diff] [blame] | 1007 | // Note: getCurrSpeed() returns a value in kbps (this also applies to OF_11 and OF_12) |
| 1008 | return port.getCurrSpeed() / KBPS; |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 1009 | } |
Yuta HIGUCHI | 2341e60 | 2017-03-08 20:10:08 -0800 | [diff] [blame] | 1010 | // < OF1.3 |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 1011 | PortSpeed portSpeed = PortSpeed.SPEED_NONE; |
| 1012 | for (OFPortFeatures feat : port.getCurr()) { |
| 1013 | portSpeed = PortSpeed.max(portSpeed, feat.getPortSpeed()); |
| 1014 | } |
Thomas Vachuska | 98eda53 | 2014-10-29 17:31:02 -0700 | [diff] [blame] | 1015 | return portSpeed.getSpeedBps() / MBPS; |
Thomas Vachuska | d16ce18 | 2014-10-29 17:25:29 -0700 | [diff] [blame] | 1016 | } |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 1017 | |
| 1018 | @Override |
| 1019 | public void handleMessage(Dpid dpid, OFMessage msg) { |
Ray Milkey | 7abe50b | 2016-04-13 11:57:44 -0700 | [diff] [blame] | 1020 | if (isDisabled) { |
| 1021 | return; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 1022 | } |
Ray Milkey | 7abe50b | 2016-04-13 11:57:44 -0700 | [diff] [blame] | 1023 | |
| 1024 | try { |
| 1025 | switch (msg.getType()) { |
| 1026 | case STATS_REPLY: |
| 1027 | if (((OFStatsReply) msg).getStatsType() == OFStatsType.PORT) { |
| 1028 | OFPortStatsReply portStatsReply = (OFPortStatsReply) msg; |
| 1029 | List<OFPortStatsEntry> portStatsReplyList = portStatsReplies.get(dpid); |
| 1030 | if (portStatsReplyList == null) { |
Madan Jampani | 84382b9 | 2016-06-22 08:26:49 -0700 | [diff] [blame] | 1031 | portStatsReplyList = Lists.newCopyOnWriteArrayList(); |
Ray Milkey | 7abe50b | 2016-04-13 11:57:44 -0700 | [diff] [blame] | 1032 | } |
| 1033 | portStatsReplyList.addAll(portStatsReply.getEntries()); |
| 1034 | portStatsReplies.put(dpid, portStatsReplyList); |
| 1035 | if (!portStatsReply.getFlags().contains(OFStatsReplyFlags.REPLY_MORE)) { |
Charles Chan | b3ef0db | 2016-05-03 11:11:00 -0700 | [diff] [blame] | 1036 | List<OFPortStatsEntry> statsEntries = portStatsReplies.get(dpid); |
| 1037 | if (statsEntries != null) { |
| 1038 | pushPortMetrics(dpid, statsEntries); |
| 1039 | statsEntries.clear(); |
| 1040 | } |
Ray Milkey | 7abe50b | 2016-04-13 11:57:44 -0700 | [diff] [blame] | 1041 | } |
yjimmyy | 646aa02 | 2016-07-05 12:09:50 -0700 | [diff] [blame] | 1042 | } else if (((OFStatsReply) msg).getStatsType() == OFStatsType.EXPERIMENTER) { |
| 1043 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
sangyun-han | 147e6f4 | 2016-07-26 17:14:41 +0900 | [diff] [blame] | 1044 | if (sw == null) { |
| 1045 | LOG.error("Switch {} is not found", dpid); |
| 1046 | break; |
| 1047 | } |
yjimmyy | 646aa02 | 2016-07-05 12:09:50 -0700 | [diff] [blame] | 1048 | if (sw instanceof OpenFlowOpticalSwitch) { |
| 1049 | // Optical switch uses experimenter stats message to update power |
| 1050 | List<PortDescription> portDescs = |
| 1051 | ((OpenFlowOpticalSwitch) sw).processExpPortStats(msg); |
| 1052 | if (!portDescs.isEmpty()) { |
| 1053 | providerService.updatePorts(DeviceId.deviceId(Dpid.uri(dpid)), portDescs); |
| 1054 | } |
| 1055 | } |
Ray Milkey | 7abe50b | 2016-04-13 11:57:44 -0700 | [diff] [blame] | 1056 | } |
| 1057 | break; |
| 1058 | case ERROR: |
| 1059 | if (((OFErrorMsg) msg).getErrType() == OFErrorType.PORT_MOD_FAILED) { |
| 1060 | LOG.error("port mod failed"); |
| 1061 | } |
| 1062 | default: |
| 1063 | break; |
| 1064 | } |
| 1065 | } catch (IllegalStateException e) { |
| 1066 | // system is shutting down and the providerService is no longer |
| 1067 | // valid. Messages cannot be processed. |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | private void disable() { |
| 1072 | isDisabled = true; |
sangho | 538108b | 2015-04-08 14:29:20 -0700 | [diff] [blame] | 1073 | } |
alshabib | f1216ed | 2014-09-03 11:53:54 -0700 | [diff] [blame] | 1074 | } |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 1075 | |
| 1076 | |
tom | b5a46e6 | 2014-08-26 14:20:00 -0700 | [diff] [blame] | 1077 | } |