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