Sho SHIMIZU | d97a950 | 2015-08-18 10:02:30 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Sho SHIMIZU | d97a950 | 2015-08-18 10:02:30 -0700 | [diff] [blame] | 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 | */ |
Sho SHIMIZU | e18cb12 | 2016-02-22 21:04:56 -0800 | [diff] [blame] | 16 | package org.onosproject.net.resource.impl; |
Sho SHIMIZU | d97a950 | 2015-08-18 10:02:30 -0700 | [diff] [blame] | 17 | |
HIGUCHI Yuta | b7a15d7 | 2015-12-15 09:54:40 -0800 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
Sho SHIMIZU | 72f81b1 | 2016-02-09 09:26:17 -0800 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
HIGUCHI Yuta | 6057d08 | 2016-04-29 11:57:23 -0700 | [diff] [blame] | 20 | |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 21 | import org.onlab.packet.MplsLabel; |
| 22 | import org.onlab.packet.VlanId; |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 23 | import org.onlab.util.Bandwidth; |
Yuta HIGUCHI | edd932f | 2017-03-11 13:47:41 -0800 | [diff] [blame] | 24 | import org.onlab.util.GuavaCollectors; |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 25 | import org.onlab.util.ItemNotFoundException; |
Sho SHIMIZU | 76b046e | 2016-02-24 11:45:44 -0800 | [diff] [blame] | 26 | import org.onosproject.mastership.MastershipService; |
HIGUCHI Yuta | 6057d08 | 2016-04-29 11:57:23 -0700 | [diff] [blame] | 27 | import org.onosproject.net.ChannelSpacing; |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 28 | import org.onosproject.net.ConnectPoint; |
Sho SHIMIZU | d97a950 | 2015-08-18 10:02:30 -0700 | [diff] [blame] | 29 | import org.onosproject.net.Device; |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 30 | import org.onosproject.net.DeviceId; |
HIGUCHI Yuta | 6057d08 | 2016-04-29 11:57:23 -0700 | [diff] [blame] | 31 | import org.onosproject.net.GridType; |
HIGUCHI Yuta | d39e376 | 2015-12-04 09:43:16 -0800 | [diff] [blame] | 32 | import org.onosproject.net.OchSignal; |
Toru Furusawa | c23f583 | 2015-12-04 11:39:18 -0800 | [diff] [blame] | 33 | import org.onosproject.net.Port; |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 34 | import org.onosproject.net.PortNumber; |
Rimon Ashkenazy | e2410ff | 2015-11-10 14:11:08 +0200 | [diff] [blame] | 35 | import org.onosproject.net.TributarySlot; |
HIGUCHI Yuta | d39e376 | 2015-12-04 09:43:16 -0800 | [diff] [blame] | 36 | import org.onosproject.net.behaviour.LambdaQuery; |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 37 | import org.onosproject.net.behaviour.MplsQuery; |
Toru Furusawa | c23f583 | 2015-12-04 11:39:18 -0800 | [diff] [blame] | 38 | import org.onosproject.net.behaviour.TributarySlotQuery; |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 39 | import org.onosproject.net.behaviour.VlanQuery; |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 40 | import org.onosproject.net.config.NetworkConfigService; |
Sho SHIMIZU | d97a950 | 2015-08-18 10:02:30 -0700 | [diff] [blame] | 41 | import org.onosproject.net.device.DeviceEvent; |
| 42 | import org.onosproject.net.device.DeviceListener; |
HIGUCHI Yuta | 11d1609 | 2015-12-04 23:35:43 -0800 | [diff] [blame] | 43 | import org.onosproject.net.device.DeviceService; |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 44 | import org.onosproject.net.driver.DriverHandler; |
| 45 | import org.onosproject.net.driver.DriverService; |
Sho SHIMIZU | e18cb12 | 2016-02-22 21:04:56 -0800 | [diff] [blame] | 46 | import org.onosproject.net.resource.DiscreteResource; |
| 47 | import org.onosproject.net.resource.ResourceAdminService; |
Sho SHIMIZU | b6c63a3 | 2016-05-26 12:07:19 -0700 | [diff] [blame] | 48 | import org.onosproject.net.config.basics.BandwidthCapacity; |
Sho SHIMIZU | e18cb12 | 2016-02-22 21:04:56 -0800 | [diff] [blame] | 49 | import org.onosproject.net.resource.Resource; |
Sho SHIMIZU | a6b4dc7 | 2016-03-11 19:00:20 -0800 | [diff] [blame] | 50 | import org.onosproject.net.resource.ResourceQueryService; |
Sho SHIMIZU | e18cb12 | 2016-02-22 21:04:56 -0800 | [diff] [blame] | 51 | import org.onosproject.net.resource.Resources; |
Rimon Ashkenazy | e2410ff | 2015-11-10 14:11:08 +0200 | [diff] [blame] | 52 | import org.slf4j.Logger; |
| 53 | import org.slf4j.LoggerFactory; |
Sho SHIMIZU | d97a950 | 2015-08-18 10:02:30 -0700 | [diff] [blame] | 54 | |
HIGUCHI Yuta | d39e376 | 2015-12-04 09:43:16 -0800 | [diff] [blame] | 55 | import java.util.Collections; |
Naoki Shiota | be394c8 | 2016-02-03 16:44:29 -0800 | [diff] [blame] | 56 | import java.util.LinkedList; |
| 57 | import java.util.List; |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 58 | import java.util.Optional; |
HIGUCHI Yuta | b7a15d7 | 2015-12-15 09:54:40 -0800 | [diff] [blame] | 59 | import java.util.Set; |
Sho SHIMIZU | d97a950 | 2015-08-18 10:02:30 -0700 | [diff] [blame] | 60 | import java.util.concurrent.ExecutorService; |
Rimon Ashkenazy | e2410ff | 2015-11-10 14:11:08 +0200 | [diff] [blame] | 61 | import java.util.stream.Collectors; |
HIGUCHI Yuta | 6057d08 | 2016-04-29 11:57:23 -0700 | [diff] [blame] | 62 | import java.util.stream.Stream; |
Sho SHIMIZU | d97a950 | 2015-08-18 10:02:30 -0700 | [diff] [blame] | 63 | |
| 64 | import static com.google.common.base.Preconditions.checkNotNull; |
| 65 | |
| 66 | /** |
| 67 | * An implementation of DeviceListener registering devices as resources. |
| 68 | */ |
| 69 | final class ResourceDeviceListener implements DeviceListener { |
| 70 | |
Rimon Ashkenazy | e2410ff | 2015-11-10 14:11:08 +0200 | [diff] [blame] | 71 | private static final Logger log = LoggerFactory.getLogger(ResourceDeviceListener.class); |
| 72 | |
Sho SHIMIZU | d97a950 | 2015-08-18 10:02:30 -0700 | [diff] [blame] | 73 | private final ResourceAdminService adminService; |
Sho SHIMIZU | a6b4dc7 | 2016-03-11 19:00:20 -0800 | [diff] [blame] | 74 | private final ResourceQueryService resourceService; |
HIGUCHI Yuta | 11d1609 | 2015-12-04 23:35:43 -0800 | [diff] [blame] | 75 | private final DeviceService deviceService; |
Sho SHIMIZU | 76b046e | 2016-02-24 11:45:44 -0800 | [diff] [blame] | 76 | private final MastershipService mastershipService; |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 77 | private final DriverService driverService; |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 78 | private final NetworkConfigService netcfgService; |
Sho SHIMIZU | d97a950 | 2015-08-18 10:02:30 -0700 | [diff] [blame] | 79 | private final ExecutorService executor; |
| 80 | |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 81 | |
Sho SHIMIZU | d97a950 | 2015-08-18 10:02:30 -0700 | [diff] [blame] | 82 | /** |
| 83 | * Creates an instance with the specified ResourceAdminService and ExecutorService. |
| 84 | * |
| 85 | * @param adminService instance invoked to register resources |
Sho SHIMIZU | a6b4dc7 | 2016-03-11 19:00:20 -0800 | [diff] [blame] | 86 | * @param resourceService {@link ResourceQueryService} to be used |
Jian Li | dfba739 | 2016-01-22 16:46:58 -0800 | [diff] [blame] | 87 | * @param deviceService {@link DeviceService} to be used |
Sho SHIMIZU | 76b046e | 2016-02-24 11:45:44 -0800 | [diff] [blame] | 88 | * @param mastershipService {@link MastershipService} to be used |
Jian Li | dfba739 | 2016-01-22 16:46:58 -0800 | [diff] [blame] | 89 | * @param driverService {@link DriverService} to be used |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 90 | * @param netcfgService {@link NetworkConfigService} to be used. |
Sho SHIMIZU | d97a950 | 2015-08-18 10:02:30 -0700 | [diff] [blame] | 91 | * @param executor executor used for processing resource registration |
| 92 | */ |
Sho SHIMIZU | a6b4dc7 | 2016-03-11 19:00:20 -0800 | [diff] [blame] | 93 | ResourceDeviceListener(ResourceAdminService adminService, ResourceQueryService resourceService, |
Sho SHIMIZU | 76b046e | 2016-02-24 11:45:44 -0800 | [diff] [blame] | 94 | DeviceService deviceService, MastershipService mastershipService, |
| 95 | DriverService driverService, NetworkConfigService netcfgService, |
| 96 | ExecutorService executor) { |
Sho SHIMIZU | d97a950 | 2015-08-18 10:02:30 -0700 | [diff] [blame] | 97 | this.adminService = checkNotNull(adminService); |
Naoki Shiota | be394c8 | 2016-02-03 16:44:29 -0800 | [diff] [blame] | 98 | this.resourceService = checkNotNull(resourceService); |
HIGUCHI Yuta | 11d1609 | 2015-12-04 23:35:43 -0800 | [diff] [blame] | 99 | this.deviceService = checkNotNull(deviceService); |
Sho SHIMIZU | 76b046e | 2016-02-24 11:45:44 -0800 | [diff] [blame] | 100 | this.mastershipService = checkNotNull(mastershipService); |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 101 | this.driverService = checkNotNull(driverService); |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 102 | this.netcfgService = checkNotNull(netcfgService); |
Sho SHIMIZU | d97a950 | 2015-08-18 10:02:30 -0700 | [diff] [blame] | 103 | this.executor = checkNotNull(executor); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public void event(DeviceEvent event) { |
| 108 | Device device = event.subject(); |
Sho SHIMIZU | 76b046e | 2016-02-24 11:45:44 -0800 | [diff] [blame] | 109 | // registration happens only when the caller is the master of the device |
| 110 | if (!mastershipService.isLocalMaster(device.id())) { |
| 111 | return; |
| 112 | } |
| 113 | |
Sho SHIMIZU | d97a950 | 2015-08-18 10:02:30 -0700 | [diff] [blame] | 114 | switch (event.type()) { |
| 115 | case DEVICE_ADDED: |
| 116 | registerDeviceResource(device); |
| 117 | break; |
Sho SHIMIZU | e60a5ab | 2015-08-20 11:51:49 -0700 | [diff] [blame] | 118 | case DEVICE_REMOVED: |
| 119 | unregisterDeviceResource(device); |
| 120 | break; |
HIGUCHI Yuta | 11d1609 | 2015-12-04 23:35:43 -0800 | [diff] [blame] | 121 | case DEVICE_AVAILABILITY_CHANGED: |
| 122 | if (deviceService.isAvailable(device.id())) { |
| 123 | registerDeviceResource(device); |
| 124 | // TODO: do we need to walk the ports? |
| 125 | } else { |
| 126 | unregisterDeviceResource(device); |
| 127 | } |
| 128 | break; |
Sho SHIMIZU | 08fdbd2 | 2015-08-19 16:59:35 -0700 | [diff] [blame] | 129 | case PORT_ADDED: |
HIGUCHI Yuta | 11d1609 | 2015-12-04 23:35:43 -0800 | [diff] [blame] | 130 | case PORT_UPDATED: |
| 131 | if (event.port().isEnabled()) { |
| 132 | registerPortResource(device, event.port()); |
| 133 | } else { |
| 134 | unregisterPortResource(device, event.port()); |
| 135 | } |
Sho SHIMIZU | c2ddedd | 2015-08-20 11:54:29 -0700 | [diff] [blame] | 136 | break; |
Sho SHIMIZU | e229284 | 2015-08-20 11:59:23 -0700 | [diff] [blame] | 137 | case PORT_REMOVED: |
| 138 | unregisterPortResource(device, event.port()); |
| 139 | break; |
Sho SHIMIZU | d97a950 | 2015-08-18 10:02:30 -0700 | [diff] [blame] | 140 | default: |
| 141 | break; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | private void registerDeviceResource(Device device) { |
Sho SHIMIZU | f84ea7f | 2016-12-22 14:47:17 -0800 | [diff] [blame] | 146 | executor.execute(() -> { |
| 147 | boolean success = adminService.register(Resources.discrete(device.id()).resource()); |
| 148 | if (!success) { |
Yuta HIGUCHI | 1f3b372 | 2017-03-17 19:33:07 -0700 | [diff] [blame] | 149 | log.error("Failed to register Device: {}", device.id()); |
Sho SHIMIZU | f84ea7f | 2016-12-22 14:47:17 -0800 | [diff] [blame] | 150 | } |
| 151 | }); |
Sho SHIMIZU | d97a950 | 2015-08-18 10:02:30 -0700 | [diff] [blame] | 152 | } |
Sho SHIMIZU | 08fdbd2 | 2015-08-19 16:59:35 -0700 | [diff] [blame] | 153 | |
Sho SHIMIZU | e60a5ab | 2015-08-20 11:51:49 -0700 | [diff] [blame] | 154 | private void unregisterDeviceResource(Device device) { |
HIGUCHI Yuta | 060da9a | 2016-03-11 19:16:35 -0800 | [diff] [blame] | 155 | executor.execute(() -> { |
Sho SHIMIZU | 2fbc33c | 2016-02-09 14:03:23 -0800 | [diff] [blame] | 156 | DiscreteResource devResource = Resources.discrete(device.id()).resource(); |
Naoki Shiota | be394c8 | 2016-02-03 16:44:29 -0800 | [diff] [blame] | 157 | List<Resource> allResources = getDescendantResources(devResource); |
Sho SHIMIZU | 7b32697 | 2016-02-09 15:53:39 -0800 | [diff] [blame] | 158 | adminService.unregister(Lists.transform(allResources, Resource::id)); |
Naoki Shiota | be394c8 | 2016-02-03 16:44:29 -0800 | [diff] [blame] | 159 | }); |
Sho SHIMIZU | e60a5ab | 2015-08-20 11:51:49 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Sho SHIMIZU | 08fdbd2 | 2015-08-19 16:59:35 -0700 | [diff] [blame] | 162 | private void registerPortResource(Device device, Port port) { |
Sho SHIMIZU | 460b972 | 2016-01-28 10:48:26 -0800 | [diff] [blame] | 163 | Resource portPath = Resources.discrete(device.id(), port.number()).resource(); |
HIGUCHI Yuta | 060da9a | 2016-03-11 19:16:35 -0800 | [diff] [blame] | 164 | executor.execute(() -> { |
Yuta HIGUCHI | 1f3b372 | 2017-03-17 19:33:07 -0700 | [diff] [blame] | 165 | if (!adminService.register(portPath)) { |
| 166 | log.error("Failed to register Port: {}", portPath.id()); |
| 167 | } |
Rimon Ashkenazy | e2410ff | 2015-11-10 14:11:08 +0200 | [diff] [blame] | 168 | |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 169 | queryBandwidth(device.id(), port.number()) |
| 170 | .map(bw -> portPath.child(Bandwidth.class, bw.bps())) |
Sho SHIMIZU | 7b32697 | 2016-02-09 15:53:39 -0800 | [diff] [blame] | 171 | .map(adminService::register) |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 172 | .ifPresent(success -> { |
| 173 | if (!success) { |
Yuta HIGUCHI | 1f3b372 | 2017-03-17 19:33:07 -0700 | [diff] [blame] | 174 | log.error("Failed to register Bandwidth for {}", portPath.id()); |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 175 | } |
| 176 | }); |
| 177 | |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 178 | // for VLAN IDs |
HIGUCHI Yuta | b7a15d7 | 2015-12-15 09:54:40 -0800 | [diff] [blame] | 179 | Set<VlanId> vlans = queryVlanIds(device.id(), port.number()); |
| 180 | if (!vlans.isEmpty()) { |
Sho SHIMIZU | f84ea7f | 2016-12-22 14:47:17 -0800 | [diff] [blame] | 181 | boolean success = adminService.register(vlans.stream() |
Sho SHIMIZU | 7b32697 | 2016-02-09 15:53:39 -0800 | [diff] [blame] | 182 | .map(portPath::child) |
| 183 | .collect(Collectors.toList())); |
Sho SHIMIZU | f84ea7f | 2016-12-22 14:47:17 -0800 | [diff] [blame] | 184 | if (!success) { |
Yuta HIGUCHI | 1f3b372 | 2017-03-17 19:33:07 -0700 | [diff] [blame] | 185 | log.error("Failed to register VLAN IDs for {}", portPath.id()); |
Sho SHIMIZU | f84ea7f | 2016-12-22 14:47:17 -0800 | [diff] [blame] | 186 | } |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | // for MPLS labels |
HIGUCHI Yuta | b7a15d7 | 2015-12-15 09:54:40 -0800 | [diff] [blame] | 190 | Set<MplsLabel> mplsLabels = queryMplsLabels(device.id(), port.number()); |
| 191 | if (!mplsLabels.isEmpty()) { |
Sho SHIMIZU | f84ea7f | 2016-12-22 14:47:17 -0800 | [diff] [blame] | 192 | boolean success = adminService.register(mplsLabels.stream() |
Sho SHIMIZU | 7b32697 | 2016-02-09 15:53:39 -0800 | [diff] [blame] | 193 | .map(portPath::child) |
| 194 | .collect(Collectors.toList())); |
Sho SHIMIZU | f84ea7f | 2016-12-22 14:47:17 -0800 | [diff] [blame] | 195 | if (!success) { |
Yuta HIGUCHI | 1f3b372 | 2017-03-17 19:33:07 -0700 | [diff] [blame] | 196 | log.error("Failed to register MPLS Labels for {}", portPath.id()); |
Sho SHIMIZU | f84ea7f | 2016-12-22 14:47:17 -0800 | [diff] [blame] | 197 | } |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 198 | } |
| 199 | |
HIGUCHI Yuta | d39e376 | 2015-12-04 09:43:16 -0800 | [diff] [blame] | 200 | // for Lambdas |
Marc De Leenheer | 622861d | 2015-12-15 22:52:52 -0800 | [diff] [blame] | 201 | Set<OchSignal> lambdas = queryLambdas(device.id(), port.number()); |
HIGUCHI Yuta | d39e376 | 2015-12-04 09:43:16 -0800 | [diff] [blame] | 202 | if (!lambdas.isEmpty()) { |
Sho SHIMIZU | f84ea7f | 2016-12-22 14:47:17 -0800 | [diff] [blame] | 203 | boolean success = adminService.register(lambdas.stream() |
Sho SHIMIZU | 7b32697 | 2016-02-09 15:53:39 -0800 | [diff] [blame] | 204 | .map(portPath::child) |
| 205 | .collect(Collectors.toList())); |
Sho SHIMIZU | f84ea7f | 2016-12-22 14:47:17 -0800 | [diff] [blame] | 206 | if (!success) { |
Yuta HIGUCHI | 1f3b372 | 2017-03-17 19:33:07 -0700 | [diff] [blame] | 207 | log.error("Failed to register lambdas for {}", portPath.id()); |
Sho SHIMIZU | f84ea7f | 2016-12-22 14:47:17 -0800 | [diff] [blame] | 208 | } |
HIGUCHI Yuta | d39e376 | 2015-12-04 09:43:16 -0800 | [diff] [blame] | 209 | } |
| 210 | |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 211 | // for Tributary slots |
Toru Furusawa | c23f583 | 2015-12-04 11:39:18 -0800 | [diff] [blame] | 212 | Set<TributarySlot> tSlots = queryTributarySlots(device.id(), port.number()); |
| 213 | if (!tSlots.isEmpty()) { |
Sho SHIMIZU | f84ea7f | 2016-12-22 14:47:17 -0800 | [diff] [blame] | 214 | boolean success = adminService.register(tSlots.stream() |
Sho SHIMIZU | 7b32697 | 2016-02-09 15:53:39 -0800 | [diff] [blame] | 215 | .map(portPath::child) |
| 216 | .collect(Collectors.toList())); |
Sho SHIMIZU | f84ea7f | 2016-12-22 14:47:17 -0800 | [diff] [blame] | 217 | if (!success) { |
Yuta HIGUCHI | 1f3b372 | 2017-03-17 19:33:07 -0700 | [diff] [blame] | 218 | log.error("Failed to register tributary slots for {}", portPath.id()); |
Sho SHIMIZU | f84ea7f | 2016-12-22 14:47:17 -0800 | [diff] [blame] | 219 | } |
Sho SHIMIZU | 2c7cecf | 2015-11-11 14:16:14 -0800 | [diff] [blame] | 220 | } |
| 221 | }); |
Rimon Ashkenazy | e2410ff | 2015-11-10 14:11:08 +0200 | [diff] [blame] | 222 | } |
| 223 | |
Sho SHIMIZU | e229284 | 2015-08-20 11:59:23 -0700 | [diff] [blame] | 224 | private void unregisterPortResource(Device device, Port port) { |
HIGUCHI Yuta | 060da9a | 2016-03-11 19:16:35 -0800 | [diff] [blame] | 225 | executor.execute(() -> { |
Sho SHIMIZU | 2fbc33c | 2016-02-09 14:03:23 -0800 | [diff] [blame] | 226 | DiscreteResource portResource = Resources.discrete(device.id(), port.number()).resource(); |
Naoki Shiota | be394c8 | 2016-02-03 16:44:29 -0800 | [diff] [blame] | 227 | List<Resource> allResources = getDescendantResources(portResource); |
Sho SHIMIZU | 7b32697 | 2016-02-09 15:53:39 -0800 | [diff] [blame] | 228 | adminService.unregister(Lists.transform(allResources, Resource::id)); |
Naoki Shiota | be394c8 | 2016-02-03 16:44:29 -0800 | [diff] [blame] | 229 | }); |
| 230 | } |
| 231 | |
| 232 | // Returns list of all descendant resources of given resource, including itself. |
Sho SHIMIZU | 2fbc33c | 2016-02-09 14:03:23 -0800 | [diff] [blame] | 233 | private List<Resource> getDescendantResources(DiscreteResource parent) { |
Naoki Shiota | be394c8 | 2016-02-03 16:44:29 -0800 | [diff] [blame] | 234 | LinkedList<Resource> allResources = new LinkedList<>(); |
| 235 | allResources.add(parent); |
| 236 | |
Sho SHIMIZU | 2fbc33c | 2016-02-09 14:03:23 -0800 | [diff] [blame] | 237 | Set<Resource> nextResources = resourceService.getRegisteredResources(parent.id()); |
Naoki Shiota | be394c8 | 2016-02-03 16:44:29 -0800 | [diff] [blame] | 238 | while (!nextResources.isEmpty()) { |
| 239 | Set<Resource> currentResources = nextResources; |
| 240 | // resource list should be ordered from leaf to root |
| 241 | allResources.addAll(0, currentResources); |
| 242 | |
| 243 | nextResources = currentResources.stream() |
Sho SHIMIZU | 2fbc33c | 2016-02-09 14:03:23 -0800 | [diff] [blame] | 244 | .filter(r -> r instanceof DiscreteResource) |
| 245 | .map(r -> (DiscreteResource) r) |
| 246 | .flatMap(r -> resourceService.getRegisteredResources(r.id()).stream()) |
Naoki Shiota | be394c8 | 2016-02-03 16:44:29 -0800 | [diff] [blame] | 247 | .collect(Collectors.toSet()); |
| 248 | } |
| 249 | |
| 250 | return allResources; |
Sho SHIMIZU | e229284 | 2015-08-20 11:59:23 -0700 | [diff] [blame] | 251 | } |
Rimon Ashkenazy | e2410ff | 2015-11-10 14:11:08 +0200 | [diff] [blame] | 252 | |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 253 | /** |
| 254 | * Query bandwidth capacity on a port. |
| 255 | * |
| 256 | * @param did {@link DeviceId} |
| 257 | * @param number {@link PortNumber} |
| 258 | * @return bandwidth capacity |
| 259 | */ |
| 260 | private Optional<Bandwidth> queryBandwidth(DeviceId did, PortNumber number) { |
| 261 | // Check and use netcfg first. |
| 262 | ConnectPoint cp = new ConnectPoint(did, number); |
| 263 | BandwidthCapacity config = netcfgService.getConfig(cp, BandwidthCapacity.class); |
| 264 | if (config != null) { |
| 265 | log.trace("Registering configured bandwidth {} for {}/{}", config.capacity(), did, number); |
| 266 | return Optional.of(config.capacity()); |
| 267 | } |
| 268 | |
| 269 | // populate bandwidth value, assuming portSpeed == bandwidth |
| 270 | Port port = deviceService.getPort(did, number); |
| 271 | if (port != null) { |
| 272 | return Optional.of(Bandwidth.mbps(port.portSpeed())); |
| 273 | } |
| 274 | return Optional.empty(); |
| 275 | } |
| 276 | |
Marc De Leenheer | 622861d | 2015-12-15 22:52:52 -0800 | [diff] [blame] | 277 | private Set<OchSignal> queryLambdas(DeviceId did, PortNumber port) { |
HIGUCHI Yuta | d39e376 | 2015-12-04 09:43:16 -0800 | [diff] [blame] | 278 | try { |
| 279 | DriverHandler handler = driverService.createHandler(did); |
HIGUCHI Yuta | 82b3c11 | 2016-01-07 22:22:26 -0800 | [diff] [blame] | 280 | if (handler == null || !handler.hasBehaviour(LambdaQuery.class)) { |
Marc De Leenheer | 622861d | 2015-12-15 22:52:52 -0800 | [diff] [blame] | 281 | return Collections.emptySet(); |
HIGUCHI Yuta | d39e376 | 2015-12-04 09:43:16 -0800 | [diff] [blame] | 282 | } |
| 283 | LambdaQuery query = handler.behaviour(LambdaQuery.class); |
HIGUCHI Yuta | 11d1609 | 2015-12-04 23:35:43 -0800 | [diff] [blame] | 284 | if (query != null) { |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 285 | return query.queryLambdas(port).stream() |
HIGUCHI Yuta | 6057d08 | 2016-04-29 11:57:23 -0700 | [diff] [blame] | 286 | .flatMap(ResourceDeviceListener::toResourceGrid) |
Yuta HIGUCHI | edd932f | 2017-03-11 13:47:41 -0800 | [diff] [blame] | 287 | .collect(GuavaCollectors.toImmutableSet()); |
HIGUCHI Yuta | 11d1609 | 2015-12-04 23:35:43 -0800 | [diff] [blame] | 288 | } else { |
Marc De Leenheer | 622861d | 2015-12-15 22:52:52 -0800 | [diff] [blame] | 289 | return Collections.emptySet(); |
HIGUCHI Yuta | 11d1609 | 2015-12-04 23:35:43 -0800 | [diff] [blame] | 290 | } |
HIGUCHI Yuta | d39e376 | 2015-12-04 09:43:16 -0800 | [diff] [blame] | 291 | } catch (ItemNotFoundException e) { |
Marc De Leenheer | 622861d | 2015-12-15 22:52:52 -0800 | [diff] [blame] | 292 | return Collections.emptySet(); |
HIGUCHI Yuta | d39e376 | 2015-12-04 09:43:16 -0800 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
HIGUCHI Yuta | 6057d08 | 2016-04-29 11:57:23 -0700 | [diff] [blame] | 296 | /** |
| 297 | * Convert {@link OchSignal} into gridtype used to track Resource. |
| 298 | * |
| 299 | * @param ochSignal {@link OchSignal} |
| 300 | * @return {@code ochSignal} mapped to Stream of flex grid slots with 6.25 GHz spacing |
| 301 | * and 12.5 GHz slot width. |
| 302 | */ |
| 303 | private static Stream<OchSignal> toResourceGrid(OchSignal ochSignal) { |
| 304 | if (ochSignal.gridType() != GridType.FLEX) { |
| 305 | return OchSignal.toFlexGrid(ochSignal).stream(); |
| 306 | } |
| 307 | if (ochSignal.gridType() == GridType.FLEX && |
| 308 | ochSignal.channelSpacing() == ChannelSpacing.CHL_6P25GHZ && |
| 309 | ochSignal.slotGranularity() == 1) { |
| 310 | // input was already flex grid slots with 6.25 GHz spacing and 12.5 GHz slot width. |
| 311 | return Stream.of(ochSignal); |
| 312 | } |
| 313 | // FIXME handle FLEX but not 6.25 GHz spacing or 12.5 GHz slot width case. |
| 314 | log.error("Converting {} to resource tracking grid not supported yet.", ochSignal); |
| 315 | return Stream.<OchSignal>builder().build(); |
| 316 | } |
| 317 | |
HIGUCHI Yuta | b7a15d7 | 2015-12-15 09:54:40 -0800 | [diff] [blame] | 318 | private Set<VlanId> queryVlanIds(DeviceId device, PortNumber port) { |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 319 | try { |
| 320 | DriverHandler handler = driverService.createHandler(device); |
HIGUCHI Yuta | 82b3c11 | 2016-01-07 22:22:26 -0800 | [diff] [blame] | 321 | if (handler == null || !handler.hasBehaviour(VlanQuery.class)) { |
HIGUCHI Yuta | b7a15d7 | 2015-12-15 09:54:40 -0800 | [diff] [blame] | 322 | return ImmutableSet.of(); |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | VlanQuery query = handler.behaviour(VlanQuery.class); |
HIGUCHI Yuta | b7a15d7 | 2015-12-15 09:54:40 -0800 | [diff] [blame] | 326 | if (query == null) { |
| 327 | return ImmutableSet.of(); |
| 328 | } |
| 329 | return query.queryVlanIds(port); |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 330 | } catch (ItemNotFoundException e) { |
HIGUCHI Yuta | b7a15d7 | 2015-12-15 09:54:40 -0800 | [diff] [blame] | 331 | return ImmutableSet.of(); |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | |
HIGUCHI Yuta | b7a15d7 | 2015-12-15 09:54:40 -0800 | [diff] [blame] | 335 | private Set<MplsLabel> queryMplsLabels(DeviceId device, PortNumber port) { |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 336 | try { |
| 337 | DriverHandler handler = driverService.createHandler(device); |
HIGUCHI Yuta | 82b3c11 | 2016-01-07 22:22:26 -0800 | [diff] [blame] | 338 | if (handler == null || !handler.hasBehaviour(MplsQuery.class)) { |
HIGUCHI Yuta | b7a15d7 | 2015-12-15 09:54:40 -0800 | [diff] [blame] | 339 | return ImmutableSet.of(); |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | MplsQuery query = handler.behaviour(MplsQuery.class); |
HIGUCHI Yuta | b7a15d7 | 2015-12-15 09:54:40 -0800 | [diff] [blame] | 343 | if (query == null) { |
| 344 | return ImmutableSet.of(); |
| 345 | } |
| 346 | return query.queryMplsLabels(port); |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 347 | } catch (ItemNotFoundException e) { |
HIGUCHI Yuta | b7a15d7 | 2015-12-15 09:54:40 -0800 | [diff] [blame] | 348 | return ImmutableSet.of(); |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 349 | } |
| 350 | } |
| 351 | |
Toru Furusawa | c23f583 | 2015-12-04 11:39:18 -0800 | [diff] [blame] | 352 | private Set<TributarySlot> queryTributarySlots(DeviceId device, PortNumber port) { |
| 353 | try { |
Toru Furusawa | c23f583 | 2015-12-04 11:39:18 -0800 | [diff] [blame] | 354 | DriverHandler handler = driverService.createHandler(device); |
HIGUCHI Yuta | 82b3c11 | 2016-01-07 22:22:26 -0800 | [diff] [blame] | 355 | if (handler == null || !handler.hasBehaviour(TributarySlotQuery.class)) { |
Toru Furusawa | c23f583 | 2015-12-04 11:39:18 -0800 | [diff] [blame] | 356 | return Collections.emptySet(); |
| 357 | } |
| 358 | TributarySlotQuery query = handler.behaviour(TributarySlotQuery.class); |
| 359 | if (query != null) { |
| 360 | return query.queryTributarySlots(port); |
| 361 | } else { |
| 362 | return Collections.emptySet(); |
| 363 | } |
| 364 | } catch (ItemNotFoundException e) { |
| 365 | return Collections.emptySet(); |
| 366 | } |
Rimon Ashkenazy | e2410ff | 2015-11-10 14:11:08 +0200 | [diff] [blame] | 367 | } |
Sho SHIMIZU | d97a950 | 2015-08-18 10:02:30 -0700 | [diff] [blame] | 368 | } |