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