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