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