blob: c8b5ba53ae5b8a6c17a621f96ab5564f793c2eed [file] [log] [blame]
Sho SHIMIZUd97a9502015-08-18 10:02:30 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Sho SHIMIZUd97a9502015-08-18 10:02:30 -07003 *
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 SHIMIZUe18cb122016-02-22 21:04:56 -080016package org.onosproject.net.resource.impl;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070017
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -080018import com.google.common.collect.ImmutableSet;
Sho SHIMIZU72f81b12016-02-09 09:26:17 -080019import com.google.common.collect.Lists;
HIGUCHI Yuta6057d082016-04-29 11:57:23 -070020
Sho SHIMIZU44f37612015-11-25 16:23:22 -080021import org.onlab.packet.MplsLabel;
22import org.onlab.packet.VlanId;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080023import org.onlab.util.Bandwidth;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080024import org.onlab.util.ItemNotFoundException;
Sho SHIMIZU76b046e2016-02-24 11:45:44 -080025import org.onosproject.mastership.MastershipService;
HIGUCHI Yuta6057d082016-04-29 11:57:23 -070026import org.onosproject.net.ChannelSpacing;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080027import org.onosproject.net.ConnectPoint;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070028import org.onosproject.net.Device;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080029import org.onosproject.net.DeviceId;
HIGUCHI Yuta6057d082016-04-29 11:57:23 -070030import org.onosproject.net.GridType;
HIGUCHI Yutad39e3762015-12-04 09:43:16 -080031import org.onosproject.net.OchSignal;
Toru Furusawac23f5832015-12-04 11:39:18 -080032import org.onosproject.net.Port;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080033import org.onosproject.net.PortNumber;
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020034import org.onosproject.net.TributarySlot;
HIGUCHI Yutad39e3762015-12-04 09:43:16 -080035import org.onosproject.net.behaviour.LambdaQuery;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080036import org.onosproject.net.behaviour.MplsQuery;
Toru Furusawac23f5832015-12-04 11:39:18 -080037import org.onosproject.net.behaviour.TributarySlotQuery;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080038import org.onosproject.net.behaviour.VlanQuery;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080039import org.onosproject.net.config.NetworkConfigService;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070040import org.onosproject.net.device.DeviceEvent;
41import org.onosproject.net.device.DeviceListener;
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080042import org.onosproject.net.device.DeviceService;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080043import org.onosproject.net.driver.DriverHandler;
44import org.onosproject.net.driver.DriverService;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080045import org.onosproject.net.resource.DiscreteResource;
46import org.onosproject.net.resource.ResourceAdminService;
Sho SHIMIZUb6c63a32016-05-26 12:07:19 -070047import org.onosproject.net.config.basics.BandwidthCapacity;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080048import org.onosproject.net.resource.Resource;
Sho SHIMIZUa6b4dc72016-03-11 19:00:20 -080049import org.onosproject.net.resource.ResourceQueryService;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080050import org.onosproject.net.resource.Resources;
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020051import org.slf4j.Logger;
52import org.slf4j.LoggerFactory;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070053
HIGUCHI Yutad39e3762015-12-04 09:43:16 -080054import java.util.Collections;
Naoki Shiotabe394c82016-02-03 16:44:29 -080055import java.util.LinkedList;
56import java.util.List;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080057import java.util.Optional;
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -080058import java.util.Set;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070059import java.util.concurrent.ExecutorService;
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020060import java.util.stream.Collectors;
HIGUCHI Yuta6057d082016-04-29 11:57:23 -070061import java.util.stream.Stream;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070062
63import static com.google.common.base.Preconditions.checkNotNull;
64
65/**
66 * An implementation of DeviceListener registering devices as resources.
67 */
68final class ResourceDeviceListener implements DeviceListener {
69
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020070 private static final Logger log = LoggerFactory.getLogger(ResourceDeviceListener.class);
71
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070072 private final ResourceAdminService adminService;
Sho SHIMIZUa6b4dc72016-03-11 19:00:20 -080073 private final ResourceQueryService resourceService;
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080074 private final DeviceService deviceService;
Sho SHIMIZU76b046e2016-02-24 11:45:44 -080075 private final MastershipService mastershipService;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080076 private final DriverService driverService;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080077 private final NetworkConfigService netcfgService;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070078 private final ExecutorService executor;
79
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080080
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070081 /**
82 * Creates an instance with the specified ResourceAdminService and ExecutorService.
83 *
84 * @param adminService instance invoked to register resources
Sho SHIMIZUa6b4dc72016-03-11 19:00:20 -080085 * @param resourceService {@link ResourceQueryService} to be used
Jian Lidfba7392016-01-22 16:46:58 -080086 * @param deviceService {@link DeviceService} to be used
Sho SHIMIZU76b046e2016-02-24 11:45:44 -080087 * @param mastershipService {@link MastershipService} to be used
Jian Lidfba7392016-01-22 16:46:58 -080088 * @param driverService {@link DriverService} to be used
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080089 * @param netcfgService {@link NetworkConfigService} to be used.
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070090 * @param executor executor used for processing resource registration
91 */
Sho SHIMIZUa6b4dc72016-03-11 19:00:20 -080092 ResourceDeviceListener(ResourceAdminService adminService, ResourceQueryService resourceService,
Sho SHIMIZU76b046e2016-02-24 11:45:44 -080093 DeviceService deviceService, MastershipService mastershipService,
94 DriverService driverService, NetworkConfigService netcfgService,
95 ExecutorService executor) {
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070096 this.adminService = checkNotNull(adminService);
Naoki Shiotabe394c82016-02-03 16:44:29 -080097 this.resourceService = checkNotNull(resourceService);
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080098 this.deviceService = checkNotNull(deviceService);
Sho SHIMIZU76b046e2016-02-24 11:45:44 -080099 this.mastershipService = checkNotNull(mastershipService);
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800100 this.driverService = checkNotNull(driverService);
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800101 this.netcfgService = checkNotNull(netcfgService);
Sho SHIMIZUd97a9502015-08-18 10:02:30 -0700102 this.executor = checkNotNull(executor);
103 }
104
105 @Override
106 public void event(DeviceEvent event) {
Sho SHIMIZUf84ea7f2016-12-22 14:47:17 -0800107 executor.execute(() -> {
Jordan Halterman225f4dc2018-03-22 16:28:22 -0700108 Device device = event.subject();
109 // registration happens only when the caller is the master of the device
110 if (!mastershipService.isLocalMaster(device.id())) {
111 return;
112 }
113
114 switch (event.type()) {
115 case DEVICE_ADDED:
116 registerDeviceResource(device);
117 break;
118 case DEVICE_REMOVED:
119 unregisterDeviceResource(device);
120 break;
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;
129 case PORT_ADDED:
130 case PORT_UPDATED:
131 if (event.port().isEnabled()) {
132 registerPortResource(device, event.port());
133 } else {
134 unregisterPortResource(device, event.port());
135 }
136 break;
137 case PORT_REMOVED:
138 unregisterPortResource(device, event.port());
139 break;
140 default:
141 break;
Sho SHIMIZUf84ea7f2016-12-22 14:47:17 -0800142 }
143 });
Sho SHIMIZUd97a9502015-08-18 10:02:30 -0700144 }
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -0700145
Jordan Halterman225f4dc2018-03-22 16:28:22 -0700146 private void registerDeviceResource(Device device) {
147 boolean success = adminService.register(Resources.discrete(device.id()).resource());
148 if (!success) {
149 log.error("Failed to register Device: {}", device.id());
150 }
151 }
152
Sho SHIMIZUe60a5ab2015-08-20 11:51:49 -0700153 private void unregisterDeviceResource(Device device) {
Jordan Halterman225f4dc2018-03-22 16:28:22 -0700154 DiscreteResource devResource = Resources.discrete(device.id()).resource();
155 List<Resource> allResources = getDescendantResources(devResource);
156 adminService.unregister(Lists.transform(allResources, Resource::id));
Sho SHIMIZUe60a5ab2015-08-20 11:51:49 -0700157 }
158
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -0700159 private void registerPortResource(Device device, Port port) {
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800160 Resource portPath = Resources.discrete(device.id(), port.number()).resource();
Jordan Halterman225f4dc2018-03-22 16:28:22 -0700161 if (!adminService.register(portPath)) {
162 log.error("Failed to register Port: {}", portPath.id());
163 }
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200164
Jordan Halterman225f4dc2018-03-22 16:28:22 -0700165 queryBandwidth(device.id(), port.number())
166 .map(bw -> portPath.child(Bandwidth.class, bw.bps()))
167 .map(adminService::register)
168 .ifPresent(success -> {
Sho SHIMIZUf84ea7f2016-12-22 14:47:17 -0800169 if (!success) {
Jordan Halterman225f4dc2018-03-22 16:28:22 -0700170 log.error("Failed to register Bandwidth for {}", portPath.id());
Sho SHIMIZUf84ea7f2016-12-22 14:47:17 -0800171 }
Jordan Halterman225f4dc2018-03-22 16:28:22 -0700172 });
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800173
Jordan Halterman225f4dc2018-03-22 16:28:22 -0700174 // for VLAN IDs
175 Set<VlanId> vlans = queryVlanIds(device.id(), port.number());
176 if (!vlans.isEmpty()) {
177 boolean success = adminService.register(vlans.stream()
178 .map(portPath::child)
179 .collect(Collectors.toList()));
180 if (!success) {
181 log.error("Failed to register VLAN IDs for {}", portPath.id());
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800182 }
Jordan Halterman225f4dc2018-03-22 16:28:22 -0700183 }
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800184
Jordan Halterman225f4dc2018-03-22 16:28:22 -0700185 // for MPLS labels
186 Set<MplsLabel> mplsLabels = queryMplsLabels(device.id(), port.number());
187 if (!mplsLabels.isEmpty()) {
188 boolean success = adminService.register(mplsLabels.stream()
189 .map(portPath::child)
190 .collect(Collectors.toList()));
191 if (!success) {
192 log.error("Failed to register MPLS Labels for {}", portPath.id());
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800193 }
Jordan Halterman225f4dc2018-03-22 16:28:22 -0700194 }
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800195
Jordan Halterman225f4dc2018-03-22 16:28:22 -0700196 // for Lambdas
197 Set<OchSignal> lambdas = queryLambdas(device.id(), port.number());
198 if (!lambdas.isEmpty()) {
199 boolean success = adminService.register(lambdas.stream()
200 .map(portPath::child)
201 .collect(Collectors.toList()));
202 if (!success) {
203 log.error("Failed to register lambdas for {}", portPath.id());
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800204 }
Jordan Halterman225f4dc2018-03-22 16:28:22 -0700205 }
206
207 // for Tributary slots
208 Set<TributarySlot> tSlots = queryTributarySlots(device.id(), port.number());
209 if (!tSlots.isEmpty()) {
210 boolean success = adminService.register(tSlots.stream()
211 .map(portPath::child)
212 .collect(Collectors.toList()));
213 if (!success) {
214 log.error("Failed to register tributary slots for {}", portPath.id());
215 }
216 }
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200217 }
218
Sho SHIMIZUe2292842015-08-20 11:59:23 -0700219 private void unregisterPortResource(Device device, Port port) {
Jordan Halterman225f4dc2018-03-22 16:28:22 -0700220 DiscreteResource portResource = Resources.discrete(device.id(), port.number()).resource();
221 List<Resource> allResources = getDescendantResources(portResource);
222 adminService.unregister(Lists.transform(allResources, Resource::id));
Naoki Shiotabe394c82016-02-03 16:44:29 -0800223 }
224
225 // Returns list of all descendant resources of given resource, including itself.
Sho SHIMIZU2fbc33c2016-02-09 14:03:23 -0800226 private List<Resource> getDescendantResources(DiscreteResource parent) {
Naoki Shiotabe394c82016-02-03 16:44:29 -0800227 LinkedList<Resource> allResources = new LinkedList<>();
228 allResources.add(parent);
229
Sho SHIMIZU2fbc33c2016-02-09 14:03:23 -0800230 Set<Resource> nextResources = resourceService.getRegisteredResources(parent.id());
Naoki Shiotabe394c82016-02-03 16:44:29 -0800231 while (!nextResources.isEmpty()) {
232 Set<Resource> currentResources = nextResources;
233 // resource list should be ordered from leaf to root
234 allResources.addAll(0, currentResources);
235
236 nextResources = currentResources.stream()
Sho SHIMIZU2fbc33c2016-02-09 14:03:23 -0800237 .filter(r -> r instanceof DiscreteResource)
238 .map(r -> (DiscreteResource) r)
239 .flatMap(r -> resourceService.getRegisteredResources(r.id()).stream())
Naoki Shiotabe394c82016-02-03 16:44:29 -0800240 .collect(Collectors.toSet());
241 }
242
243 return allResources;
Sho SHIMIZUe2292842015-08-20 11:59:23 -0700244 }
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200245
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800246 /**
247 * Query bandwidth capacity on a port.
248 *
249 * @param did {@link DeviceId}
250 * @param number {@link PortNumber}
251 * @return bandwidth capacity
252 */
253 private Optional<Bandwidth> queryBandwidth(DeviceId did, PortNumber number) {
254 // Check and use netcfg first.
255 ConnectPoint cp = new ConnectPoint(did, number);
256 BandwidthCapacity config = netcfgService.getConfig(cp, BandwidthCapacity.class);
257 if (config != null) {
258 log.trace("Registering configured bandwidth {} for {}/{}", config.capacity(), did, number);
259 return Optional.of(config.capacity());
260 }
261
262 // populate bandwidth value, assuming portSpeed == bandwidth
263 Port port = deviceService.getPort(did, number);
264 if (port != null) {
265 return Optional.of(Bandwidth.mbps(port.portSpeed()));
266 }
267 return Optional.empty();
268 }
269
Marc De Leenheer622861d2015-12-15 22:52:52 -0800270 private Set<OchSignal> queryLambdas(DeviceId did, PortNumber port) {
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800271 try {
272 DriverHandler handler = driverService.createHandler(did);
HIGUCHI Yuta82b3c112016-01-07 22:22:26 -0800273 if (handler == null || !handler.hasBehaviour(LambdaQuery.class)) {
Marc De Leenheer622861d2015-12-15 22:52:52 -0800274 return Collections.emptySet();
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800275 }
276 LambdaQuery query = handler.behaviour(LambdaQuery.class);
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800277 if (query != null) {
Marc De Leenheer2c305302015-12-07 21:37:44 -0800278 return query.queryLambdas(port).stream()
HIGUCHI Yuta6057d082016-04-29 11:57:23 -0700279 .flatMap(ResourceDeviceListener::toResourceGrid)
Yuta HIGUCHI498fa1d2017-05-17 16:08:40 -0700280 .collect(ImmutableSet.toImmutableSet());
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800281 } else {
Marc De Leenheer622861d2015-12-15 22:52:52 -0800282 return Collections.emptySet();
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800283 }
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800284 } catch (ItemNotFoundException e) {
Marc De Leenheer622861d2015-12-15 22:52:52 -0800285 return Collections.emptySet();
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800286 }
287 }
288
HIGUCHI Yuta6057d082016-04-29 11:57:23 -0700289 /**
290 * Convert {@link OchSignal} into gridtype used to track Resource.
291 *
292 * @param ochSignal {@link OchSignal}
293 * @return {@code ochSignal} mapped to Stream of flex grid slots with 6.25 GHz spacing
294 * and 12.5 GHz slot width.
295 */
296 private static Stream<OchSignal> toResourceGrid(OchSignal ochSignal) {
297 if (ochSignal.gridType() != GridType.FLEX) {
298 return OchSignal.toFlexGrid(ochSignal).stream();
299 }
300 if (ochSignal.gridType() == GridType.FLEX &&
301 ochSignal.channelSpacing() == ChannelSpacing.CHL_6P25GHZ &&
302 ochSignal.slotGranularity() == 1) {
303 // input was already flex grid slots with 6.25 GHz spacing and 12.5 GHz slot width.
304 return Stream.of(ochSignal);
305 }
306 // FIXME handle FLEX but not 6.25 GHz spacing or 12.5 GHz slot width case.
307 log.error("Converting {} to resource tracking grid not supported yet.", ochSignal);
308 return Stream.<OchSignal>builder().build();
309 }
310
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800311 private Set<VlanId> queryVlanIds(DeviceId device, PortNumber port) {
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800312 try {
313 DriverHandler handler = driverService.createHandler(device);
HIGUCHI Yuta82b3c112016-01-07 22:22:26 -0800314 if (handler == null || !handler.hasBehaviour(VlanQuery.class)) {
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800315 return ImmutableSet.of();
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800316 }
317
318 VlanQuery query = handler.behaviour(VlanQuery.class);
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800319 if (query == null) {
320 return ImmutableSet.of();
321 }
322 return query.queryVlanIds(port);
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800323 } catch (ItemNotFoundException e) {
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800324 return ImmutableSet.of();
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800325 }
326 }
327
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800328 private Set<MplsLabel> queryMplsLabels(DeviceId device, PortNumber port) {
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800329 try {
330 DriverHandler handler = driverService.createHandler(device);
HIGUCHI Yuta82b3c112016-01-07 22:22:26 -0800331 if (handler == null || !handler.hasBehaviour(MplsQuery.class)) {
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800332 return ImmutableSet.of();
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800333 }
334
335 MplsQuery query = handler.behaviour(MplsQuery.class);
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800336 if (query == null) {
337 return ImmutableSet.of();
338 }
339 return query.queryMplsLabels(port);
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800340 } catch (ItemNotFoundException e) {
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800341 return ImmutableSet.of();
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800342 }
343 }
344
Toru Furusawac23f5832015-12-04 11:39:18 -0800345 private Set<TributarySlot> queryTributarySlots(DeviceId device, PortNumber port) {
346 try {
Toru Furusawac23f5832015-12-04 11:39:18 -0800347 DriverHandler handler = driverService.createHandler(device);
HIGUCHI Yuta82b3c112016-01-07 22:22:26 -0800348 if (handler == null || !handler.hasBehaviour(TributarySlotQuery.class)) {
Toru Furusawac23f5832015-12-04 11:39:18 -0800349 return Collections.emptySet();
350 }
351 TributarySlotQuery query = handler.behaviour(TributarySlotQuery.class);
352 if (query != null) {
353 return query.queryTributarySlots(port);
354 } else {
355 return Collections.emptySet();
356 }
357 } catch (ItemNotFoundException e) {
358 return Collections.emptySet();
359 }
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200360 }
Sho SHIMIZUd97a9502015-08-18 10:02:30 -0700361}