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