Sho SHIMIZU | d97a950 | 2015-08-18 10:02:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 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 |
| 7 | * |
| 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. |
| 15 | */ |
| 16 | package org.onosproject.net.newresource.impl; |
| 17 | |
HIGUCHI Yuta | b7a15d7 | 2015-12-15 09:54:40 -0800 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 19 | import org.onlab.packet.MplsLabel; |
| 20 | import org.onlab.packet.VlanId; |
| 21 | import org.onlab.util.ItemNotFoundException; |
Sho SHIMIZU | d97a950 | 2015-08-18 10:02:30 -0700 | [diff] [blame] | 22 | import org.onosproject.net.Device; |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 23 | import org.onosproject.net.DeviceId; |
HIGUCHI Yuta | d39e376 | 2015-12-04 09:43:16 -0800 | [diff] [blame] | 24 | import org.onosproject.net.OchSignal; |
Toru Furusawa | c23f583 | 2015-12-04 11:39:18 -0800 | [diff] [blame^] | 25 | import org.onosproject.net.Port; |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 26 | import org.onosproject.net.PortNumber; |
Rimon Ashkenazy | e2410ff | 2015-11-10 14:11:08 +0200 | [diff] [blame] | 27 | import org.onosproject.net.TributarySlot; |
HIGUCHI Yuta | d39e376 | 2015-12-04 09:43:16 -0800 | [diff] [blame] | 28 | import org.onosproject.net.behaviour.LambdaQuery; |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 29 | import org.onosproject.net.behaviour.MplsQuery; |
Toru Furusawa | c23f583 | 2015-12-04 11:39:18 -0800 | [diff] [blame^] | 30 | import org.onosproject.net.behaviour.TributarySlotQuery; |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 31 | import org.onosproject.net.behaviour.VlanQuery; |
Sho SHIMIZU | d97a950 | 2015-08-18 10:02:30 -0700 | [diff] [blame] | 32 | import org.onosproject.net.device.DeviceEvent; |
| 33 | import org.onosproject.net.device.DeviceListener; |
HIGUCHI Yuta | 11d1609 | 2015-12-04 23:35:43 -0800 | [diff] [blame] | 34 | import org.onosproject.net.device.DeviceService; |
| 35 | import org.onosproject.net.driver.Driver; |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 36 | import org.onosproject.net.driver.DriverHandler; |
| 37 | import org.onosproject.net.driver.DriverService; |
Sho SHIMIZU | d97a950 | 2015-08-18 10:02:30 -0700 | [diff] [blame] | 38 | import org.onosproject.net.newresource.ResourceAdminService; |
| 39 | import org.onosproject.net.newresource.ResourcePath; |
Rimon Ashkenazy | e2410ff | 2015-11-10 14:11:08 +0200 | [diff] [blame] | 40 | import org.slf4j.Logger; |
| 41 | import org.slf4j.LoggerFactory; |
Sho SHIMIZU | d97a950 | 2015-08-18 10:02:30 -0700 | [diff] [blame] | 42 | |
HIGUCHI Yuta | d39e376 | 2015-12-04 09:43:16 -0800 | [diff] [blame] | 43 | import java.util.Collections; |
HIGUCHI Yuta | b7a15d7 | 2015-12-15 09:54:40 -0800 | [diff] [blame] | 44 | import java.util.Set; |
Sho SHIMIZU | d97a950 | 2015-08-18 10:02:30 -0700 | [diff] [blame] | 45 | import java.util.concurrent.ExecutorService; |
Rimon Ashkenazy | e2410ff | 2015-11-10 14:11:08 +0200 | [diff] [blame] | 46 | import java.util.stream.Collectors; |
Sho SHIMIZU | d97a950 | 2015-08-18 10:02:30 -0700 | [diff] [blame] | 47 | |
| 48 | import static com.google.common.base.Preconditions.checkNotNull; |
| 49 | |
| 50 | /** |
| 51 | * An implementation of DeviceListener registering devices as resources. |
| 52 | */ |
| 53 | final class ResourceDeviceListener implements DeviceListener { |
| 54 | |
Rimon Ashkenazy | e2410ff | 2015-11-10 14:11:08 +0200 | [diff] [blame] | 55 | private static final Logger log = LoggerFactory.getLogger(ResourceDeviceListener.class); |
| 56 | |
Sho SHIMIZU | d97a950 | 2015-08-18 10:02:30 -0700 | [diff] [blame] | 57 | private final ResourceAdminService adminService; |
HIGUCHI Yuta | 11d1609 | 2015-12-04 23:35:43 -0800 | [diff] [blame] | 58 | private final DeviceService deviceService; |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 59 | private final DriverService driverService; |
Sho SHIMIZU | d97a950 | 2015-08-18 10:02:30 -0700 | [diff] [blame] | 60 | private final ExecutorService executor; |
| 61 | |
| 62 | /** |
| 63 | * Creates an instance with the specified ResourceAdminService and ExecutorService. |
| 64 | * |
| 65 | * @param adminService instance invoked to register resources |
HIGUCHI Yuta | 11d1609 | 2015-12-04 23:35:43 -0800 | [diff] [blame] | 66 | * @param deviceService {@link DeviceService} to be used. |
Sho SHIMIZU | d97a950 | 2015-08-18 10:02:30 -0700 | [diff] [blame] | 67 | * @param executor executor used for processing resource registration |
| 68 | */ |
HIGUCHI Yuta | 11d1609 | 2015-12-04 23:35:43 -0800 | [diff] [blame] | 69 | ResourceDeviceListener(ResourceAdminService adminService, DeviceService deviceService, DriverService driverService, |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 70 | ExecutorService executor) { |
Sho SHIMIZU | d97a950 | 2015-08-18 10:02:30 -0700 | [diff] [blame] | 71 | this.adminService = checkNotNull(adminService); |
HIGUCHI Yuta | 11d1609 | 2015-12-04 23:35:43 -0800 | [diff] [blame] | 72 | this.deviceService = checkNotNull(deviceService); |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 73 | this.driverService = checkNotNull(driverService); |
Sho SHIMIZU | d97a950 | 2015-08-18 10:02:30 -0700 | [diff] [blame] | 74 | this.executor = checkNotNull(executor); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public void event(DeviceEvent event) { |
| 79 | Device device = event.subject(); |
| 80 | switch (event.type()) { |
| 81 | case DEVICE_ADDED: |
| 82 | registerDeviceResource(device); |
| 83 | break; |
Sho SHIMIZU | e60a5ab | 2015-08-20 11:51:49 -0700 | [diff] [blame] | 84 | case DEVICE_REMOVED: |
| 85 | unregisterDeviceResource(device); |
| 86 | break; |
HIGUCHI Yuta | 11d1609 | 2015-12-04 23:35:43 -0800 | [diff] [blame] | 87 | case DEVICE_AVAILABILITY_CHANGED: |
| 88 | if (deviceService.isAvailable(device.id())) { |
| 89 | registerDeviceResource(device); |
| 90 | // TODO: do we need to walk the ports? |
| 91 | } else { |
| 92 | unregisterDeviceResource(device); |
| 93 | } |
| 94 | break; |
Sho SHIMIZU | 08fdbd2 | 2015-08-19 16:59:35 -0700 | [diff] [blame] | 95 | case PORT_ADDED: |
HIGUCHI Yuta | 11d1609 | 2015-12-04 23:35:43 -0800 | [diff] [blame] | 96 | case PORT_UPDATED: |
| 97 | if (event.port().isEnabled()) { |
| 98 | registerPortResource(device, event.port()); |
| 99 | } else { |
| 100 | unregisterPortResource(device, event.port()); |
| 101 | } |
Sho SHIMIZU | c2ddedd | 2015-08-20 11:54:29 -0700 | [diff] [blame] | 102 | break; |
Sho SHIMIZU | e229284 | 2015-08-20 11:59:23 -0700 | [diff] [blame] | 103 | case PORT_REMOVED: |
| 104 | unregisterPortResource(device, event.port()); |
| 105 | break; |
Sho SHIMIZU | d97a950 | 2015-08-18 10:02:30 -0700 | [diff] [blame] | 106 | default: |
| 107 | break; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | private void registerDeviceResource(Device device) { |
Sho SHIMIZU | 2c7cecf | 2015-11-11 14:16:14 -0800 | [diff] [blame] | 112 | executor.submit(() -> adminService.registerResources(ResourcePath.discrete(device.id()))); |
Sho SHIMIZU | d97a950 | 2015-08-18 10:02:30 -0700 | [diff] [blame] | 113 | } |
Sho SHIMIZU | 08fdbd2 | 2015-08-19 16:59:35 -0700 | [diff] [blame] | 114 | |
Sho SHIMIZU | e60a5ab | 2015-08-20 11:51:49 -0700 | [diff] [blame] | 115 | private void unregisterDeviceResource(Device device) { |
Sho SHIMIZU | 2c7cecf | 2015-11-11 14:16:14 -0800 | [diff] [blame] | 116 | executor.submit(() -> adminService.unregisterResources(ResourcePath.discrete(device.id()))); |
Sho SHIMIZU | e60a5ab | 2015-08-20 11:51:49 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Sho SHIMIZU | 08fdbd2 | 2015-08-19 16:59:35 -0700 | [diff] [blame] | 119 | private void registerPortResource(Device device, Port port) { |
Rimon Ashkenazy | e2410ff | 2015-11-10 14:11:08 +0200 | [diff] [blame] | 120 | ResourcePath portPath = ResourcePath.discrete(device.id(), port.number()); |
Sho SHIMIZU | 2c7cecf | 2015-11-11 14:16:14 -0800 | [diff] [blame] | 121 | executor.submit(() -> { |
| 122 | adminService.registerResources(portPath); |
Rimon Ashkenazy | e2410ff | 2015-11-10 14:11:08 +0200 | [diff] [blame] | 123 | |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 124 | // for VLAN IDs |
HIGUCHI Yuta | b7a15d7 | 2015-12-15 09:54:40 -0800 | [diff] [blame] | 125 | Set<VlanId> vlans = queryVlanIds(device.id(), port.number()); |
| 126 | if (!vlans.isEmpty()) { |
| 127 | adminService.registerResources(vlans.stream() |
| 128 | .map(portPath::child) |
| 129 | .collect(Collectors.toList())); |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | // for MPLS labels |
HIGUCHI Yuta | b7a15d7 | 2015-12-15 09:54:40 -0800 | [diff] [blame] | 133 | Set<MplsLabel> mplsLabels = queryMplsLabels(device.id(), port.number()); |
| 134 | if (!mplsLabels.isEmpty()) { |
| 135 | adminService.registerResources(mplsLabels.stream() |
| 136 | .map(portPath::child) |
| 137 | .collect(Collectors.toList())); |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 138 | } |
| 139 | |
HIGUCHI Yuta | d39e376 | 2015-12-04 09:43:16 -0800 | [diff] [blame] | 140 | // for Lambdas |
Marc De Leenheer | 622861d | 2015-12-15 22:52:52 -0800 | [diff] [blame] | 141 | Set<OchSignal> lambdas = queryLambdas(device.id(), port.number()); |
HIGUCHI Yuta | d39e376 | 2015-12-04 09:43:16 -0800 | [diff] [blame] | 142 | if (!lambdas.isEmpty()) { |
| 143 | adminService.registerResources(lambdas.stream() |
| 144 | .map(portPath::child) |
| 145 | .collect(Collectors.toList())); |
| 146 | } |
| 147 | |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 148 | // for Tributary slots |
Toru Furusawa | c23f583 | 2015-12-04 11:39:18 -0800 | [diff] [blame^] | 149 | Set<TributarySlot> tSlots = queryTributarySlots(device.id(), port.number()); |
| 150 | if (!tSlots.isEmpty()) { |
| 151 | adminService.registerResources(tSlots.stream() |
| 152 | .map(portPath::child) |
| 153 | .collect(Collectors.toList())); |
Sho SHIMIZU | 2c7cecf | 2015-11-11 14:16:14 -0800 | [diff] [blame] | 154 | } |
| 155 | }); |
Rimon Ashkenazy | e2410ff | 2015-11-10 14:11:08 +0200 | [diff] [blame] | 156 | } |
| 157 | |
Sho SHIMIZU | e229284 | 2015-08-20 11:59:23 -0700 | [diff] [blame] | 158 | private void unregisterPortResource(Device device, Port port) { |
Sho SHIMIZU | 2c7cecf | 2015-11-11 14:16:14 -0800 | [diff] [blame] | 159 | ResourcePath resource = ResourcePath.discrete(device.id(), port.number()); |
| 160 | executor.submit(() -> adminService.unregisterResources(resource)); |
Sho SHIMIZU | e229284 | 2015-08-20 11:59:23 -0700 | [diff] [blame] | 161 | } |
Rimon Ashkenazy | e2410ff | 2015-11-10 14:11:08 +0200 | [diff] [blame] | 162 | |
Marc De Leenheer | 622861d | 2015-12-15 22:52:52 -0800 | [diff] [blame] | 163 | private Set<OchSignal> queryLambdas(DeviceId did, PortNumber port) { |
HIGUCHI Yuta | d39e376 | 2015-12-04 09:43:16 -0800 | [diff] [blame] | 164 | try { |
HIGUCHI Yuta | 11d1609 | 2015-12-04 23:35:43 -0800 | [diff] [blame] | 165 | // DriverHandler does not provide a way to check if a |
| 166 | // behaviour is supported. |
| 167 | Driver driver = driverService.getDriver(did); |
| 168 | if (driver == null || !driver.hasBehaviour(LambdaQuery.class)) { |
Marc De Leenheer | 622861d | 2015-12-15 22:52:52 -0800 | [diff] [blame] | 169 | return Collections.emptySet(); |
HIGUCHI Yuta | 11d1609 | 2015-12-04 23:35:43 -0800 | [diff] [blame] | 170 | } |
HIGUCHI Yuta | d39e376 | 2015-12-04 09:43:16 -0800 | [diff] [blame] | 171 | DriverHandler handler = driverService.createHandler(did); |
| 172 | if (handler == null) { |
Marc De Leenheer | 622861d | 2015-12-15 22:52:52 -0800 | [diff] [blame] | 173 | return Collections.emptySet(); |
HIGUCHI Yuta | d39e376 | 2015-12-04 09:43:16 -0800 | [diff] [blame] | 174 | } |
| 175 | LambdaQuery query = handler.behaviour(LambdaQuery.class); |
HIGUCHI Yuta | 11d1609 | 2015-12-04 23:35:43 -0800 | [diff] [blame] | 176 | if (query != null) { |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 177 | return query.queryLambdas(port).stream() |
| 178 | .flatMap(x -> OchSignal.toFlexGrid(x).stream()) |
Marc De Leenheer | 622861d | 2015-12-15 22:52:52 -0800 | [diff] [blame] | 179 | .collect(Collectors.toSet()); |
HIGUCHI Yuta | 11d1609 | 2015-12-04 23:35:43 -0800 | [diff] [blame] | 180 | } else { |
Marc De Leenheer | 622861d | 2015-12-15 22:52:52 -0800 | [diff] [blame] | 181 | return Collections.emptySet(); |
HIGUCHI Yuta | 11d1609 | 2015-12-04 23:35:43 -0800 | [diff] [blame] | 182 | } |
HIGUCHI Yuta | d39e376 | 2015-12-04 09:43:16 -0800 | [diff] [blame] | 183 | } catch (ItemNotFoundException e) { |
Marc De Leenheer | 622861d | 2015-12-15 22:52:52 -0800 | [diff] [blame] | 184 | return Collections.emptySet(); |
HIGUCHI Yuta | d39e376 | 2015-12-04 09:43:16 -0800 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
HIGUCHI Yuta | b7a15d7 | 2015-12-15 09:54:40 -0800 | [diff] [blame] | 188 | private Set<VlanId> queryVlanIds(DeviceId device, PortNumber port) { |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 189 | try { |
HIGUCHI Yuta | 11d1609 | 2015-12-04 23:35:43 -0800 | [diff] [blame] | 190 | // DriverHandler does not provide a way to check if a |
| 191 | // behaviour is supported. |
| 192 | Driver driver = driverService.getDriver(device); |
| 193 | if (driver == null || !driver.hasBehaviour(VlanQuery.class)) { |
| 194 | // device does not support this |
HIGUCHI Yuta | b7a15d7 | 2015-12-15 09:54:40 -0800 | [diff] [blame] | 195 | return ImmutableSet.of(); |
HIGUCHI Yuta | 11d1609 | 2015-12-04 23:35:43 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 198 | DriverHandler handler = driverService.createHandler(device); |
| 199 | if (handler == null) { |
HIGUCHI Yuta | b7a15d7 | 2015-12-15 09:54:40 -0800 | [diff] [blame] | 200 | return ImmutableSet.of(); |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | VlanQuery query = handler.behaviour(VlanQuery.class); |
HIGUCHI Yuta | b7a15d7 | 2015-12-15 09:54:40 -0800 | [diff] [blame] | 204 | if (query == null) { |
| 205 | return ImmutableSet.of(); |
| 206 | } |
| 207 | return query.queryVlanIds(port); |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 208 | } catch (ItemNotFoundException e) { |
HIGUCHI Yuta | b7a15d7 | 2015-12-15 09:54:40 -0800 | [diff] [blame] | 209 | return ImmutableSet.of(); |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
HIGUCHI Yuta | b7a15d7 | 2015-12-15 09:54:40 -0800 | [diff] [blame] | 213 | private Set<MplsLabel> queryMplsLabels(DeviceId device, PortNumber port) { |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 214 | try { |
HIGUCHI Yuta | 11d1609 | 2015-12-04 23:35:43 -0800 | [diff] [blame] | 215 | // DriverHandler does not provide a way to check if a |
| 216 | // behaviour is supported. |
| 217 | Driver driver = driverService.getDriver(device); |
| 218 | if (driver == null || !driver.hasBehaviour(MplsQuery.class)) { |
| 219 | // device does not support this |
HIGUCHI Yuta | b7a15d7 | 2015-12-15 09:54:40 -0800 | [diff] [blame] | 220 | return ImmutableSet.of(); |
HIGUCHI Yuta | 11d1609 | 2015-12-04 23:35:43 -0800 | [diff] [blame] | 221 | } |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 222 | DriverHandler handler = driverService.createHandler(device); |
| 223 | if (handler == null) { |
HIGUCHI Yuta | b7a15d7 | 2015-12-15 09:54:40 -0800 | [diff] [blame] | 224 | return ImmutableSet.of(); |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | MplsQuery query = handler.behaviour(MplsQuery.class); |
HIGUCHI Yuta | b7a15d7 | 2015-12-15 09:54:40 -0800 | [diff] [blame] | 228 | if (query == null) { |
| 229 | return ImmutableSet.of(); |
| 230 | } |
| 231 | return query.queryMplsLabels(port); |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 232 | } catch (ItemNotFoundException e) { |
HIGUCHI Yuta | b7a15d7 | 2015-12-15 09:54:40 -0800 | [diff] [blame] | 233 | return ImmutableSet.of(); |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | |
Toru Furusawa | c23f583 | 2015-12-04 11:39:18 -0800 | [diff] [blame^] | 237 | private Set<TributarySlot> queryTributarySlots(DeviceId device, PortNumber port) { |
| 238 | try { |
| 239 | // DriverHandler does not provide a way to check if a |
| 240 | // behaviour is supported. |
| 241 | Driver driver = driverService.getDriver(device); |
| 242 | if (driver == null || !driver.hasBehaviour(TributarySlotQuery.class)) { |
| 243 | return Collections.emptySet(); |
| 244 | } |
| 245 | DriverHandler handler = driverService.createHandler(device); |
| 246 | if (handler == null) { |
| 247 | return Collections.emptySet(); |
| 248 | } |
| 249 | TributarySlotQuery query = handler.behaviour(TributarySlotQuery.class); |
| 250 | if (query != null) { |
| 251 | return query.queryTributarySlots(port); |
| 252 | } else { |
| 253 | return Collections.emptySet(); |
| 254 | } |
| 255 | } catch (ItemNotFoundException e) { |
| 256 | return Collections.emptySet(); |
| 257 | } |
Rimon Ashkenazy | e2410ff | 2015-11-10 14:11:08 +0200 | [diff] [blame] | 258 | } |
Sho SHIMIZU | d97a950 | 2015-08-18 10:02:30 -0700 | [diff] [blame] | 259 | } |