blob: 73997ac5c2938c21189e8eb8f1828b98b26b746c [file] [log] [blame]
Sho SHIMIZUd97a9502015-08-18 10:02:30 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
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;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080020import org.onlab.packet.MplsLabel;
21import org.onlab.packet.VlanId;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080022import org.onlab.util.Bandwidth;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080023import org.onlab.util.ItemNotFoundException;
Sho SHIMIZU76b046e2016-02-24 11:45:44 -080024import org.onosproject.mastership.MastershipService;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080025import org.onosproject.net.ConnectPoint;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070026import org.onosproject.net.Device;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080027import org.onosproject.net.DeviceId;
HIGUCHI Yutad39e3762015-12-04 09:43:16 -080028import org.onosproject.net.OchSignal;
Toru Furusawac23f5832015-12-04 11:39:18 -080029import org.onosproject.net.Port;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080030import org.onosproject.net.PortNumber;
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020031import org.onosproject.net.TributarySlot;
HIGUCHI Yutad39e3762015-12-04 09:43:16 -080032import org.onosproject.net.behaviour.LambdaQuery;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080033import org.onosproject.net.behaviour.MplsQuery;
Toru Furusawac23f5832015-12-04 11:39:18 -080034import org.onosproject.net.behaviour.TributarySlotQuery;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080035import org.onosproject.net.behaviour.VlanQuery;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080036import org.onosproject.net.config.NetworkConfigService;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070037import org.onosproject.net.device.DeviceEvent;
38import org.onosproject.net.device.DeviceListener;
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080039import org.onosproject.net.device.DeviceService;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080040import org.onosproject.net.driver.DriverHandler;
41import org.onosproject.net.driver.DriverService;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080042import org.onosproject.net.resource.DiscreteResource;
43import org.onosproject.net.resource.ResourceAdminService;
44import org.onosproject.net.resource.BandwidthCapacity;
45import org.onosproject.net.resource.Resource;
Sho SHIMIZUa6b4dc72016-03-11 19:00:20 -080046import org.onosproject.net.resource.ResourceQueryService;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080047import org.onosproject.net.resource.Resources;
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020048import org.slf4j.Logger;
49import org.slf4j.LoggerFactory;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070050
HIGUCHI Yutad39e3762015-12-04 09:43:16 -080051import java.util.Collections;
Naoki Shiotabe394c82016-02-03 16:44:29 -080052import java.util.LinkedList;
53import java.util.List;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080054import java.util.Optional;
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -080055import java.util.Set;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070056import java.util.concurrent.ExecutorService;
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020057import java.util.stream.Collectors;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070058
59import static com.google.common.base.Preconditions.checkNotNull;
60
61/**
62 * An implementation of DeviceListener registering devices as resources.
63 */
64final class ResourceDeviceListener implements DeviceListener {
65
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020066 private static final Logger log = LoggerFactory.getLogger(ResourceDeviceListener.class);
67
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070068 private final ResourceAdminService adminService;
Sho SHIMIZUa6b4dc72016-03-11 19:00:20 -080069 private final ResourceQueryService resourceService;
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080070 private final DeviceService deviceService;
Sho SHIMIZU76b046e2016-02-24 11:45:44 -080071 private final MastershipService mastershipService;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080072 private final DriverService driverService;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080073 private final NetworkConfigService netcfgService;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070074 private final ExecutorService executor;
75
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080076
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070077 /**
78 * Creates an instance with the specified ResourceAdminService and ExecutorService.
79 *
80 * @param adminService instance invoked to register resources
Sho SHIMIZUa6b4dc72016-03-11 19:00:20 -080081 * @param resourceService {@link ResourceQueryService} to be used
Jian Lidfba7392016-01-22 16:46:58 -080082 * @param deviceService {@link DeviceService} to be used
Sho SHIMIZU76b046e2016-02-24 11:45:44 -080083 * @param mastershipService {@link MastershipService} to be used
Jian Lidfba7392016-01-22 16:46:58 -080084 * @param driverService {@link DriverService} to be used
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080085 * @param netcfgService {@link NetworkConfigService} to be used.
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070086 * @param executor executor used for processing resource registration
87 */
Sho SHIMIZUa6b4dc72016-03-11 19:00:20 -080088 ResourceDeviceListener(ResourceAdminService adminService, ResourceQueryService resourceService,
Sho SHIMIZU76b046e2016-02-24 11:45:44 -080089 DeviceService deviceService, MastershipService mastershipService,
90 DriverService driverService, NetworkConfigService netcfgService,
91 ExecutorService executor) {
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070092 this.adminService = checkNotNull(adminService);
Naoki Shiotabe394c82016-02-03 16:44:29 -080093 this.resourceService = checkNotNull(resourceService);
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080094 this.deviceService = checkNotNull(deviceService);
Sho SHIMIZU76b046e2016-02-24 11:45:44 -080095 this.mastershipService = checkNotNull(mastershipService);
Sho SHIMIZU44f37612015-11-25 16:23:22 -080096 this.driverService = checkNotNull(driverService);
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080097 this.netcfgService = checkNotNull(netcfgService);
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070098 this.executor = checkNotNull(executor);
99 }
100
101 @Override
102 public void event(DeviceEvent event) {
103 Device device = event.subject();
Sho SHIMIZU76b046e2016-02-24 11:45:44 -0800104 // registration happens only when the caller is the master of the device
105 if (!mastershipService.isLocalMaster(device.id())) {
106 return;
107 }
108
Sho SHIMIZUd97a9502015-08-18 10:02:30 -0700109 switch (event.type()) {
110 case DEVICE_ADDED:
111 registerDeviceResource(device);
112 break;
Sho SHIMIZUe60a5ab2015-08-20 11:51:49 -0700113 case DEVICE_REMOVED:
114 unregisterDeviceResource(device);
115 break;
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800116 case DEVICE_AVAILABILITY_CHANGED:
117 if (deviceService.isAvailable(device.id())) {
118 registerDeviceResource(device);
119 // TODO: do we need to walk the ports?
120 } else {
121 unregisterDeviceResource(device);
122 }
123 break;
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -0700124 case PORT_ADDED:
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800125 case PORT_UPDATED:
126 if (event.port().isEnabled()) {
127 registerPortResource(device, event.port());
128 } else {
129 unregisterPortResource(device, event.port());
130 }
Sho SHIMIZUc2ddedd2015-08-20 11:54:29 -0700131 break;
Sho SHIMIZUe2292842015-08-20 11:59:23 -0700132 case PORT_REMOVED:
133 unregisterPortResource(device, event.port());
134 break;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -0700135 default:
136 break;
137 }
138 }
139
140 private void registerDeviceResource(Device device) {
HIGUCHI Yuta060da9a2016-03-11 19:16:35 -0800141 executor.execute(() -> adminService.register(Resources.discrete(device.id()).resource()));
Sho SHIMIZUd97a9502015-08-18 10:02:30 -0700142 }
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -0700143
Sho SHIMIZUe60a5ab2015-08-20 11:51:49 -0700144 private void unregisterDeviceResource(Device device) {
HIGUCHI Yuta060da9a2016-03-11 19:16:35 -0800145 executor.execute(() -> {
Sho SHIMIZU2fbc33c2016-02-09 14:03:23 -0800146 DiscreteResource devResource = Resources.discrete(device.id()).resource();
Naoki Shiotabe394c82016-02-03 16:44:29 -0800147 List<Resource> allResources = getDescendantResources(devResource);
Sho SHIMIZU7b326972016-02-09 15:53:39 -0800148 adminService.unregister(Lists.transform(allResources, Resource::id));
Naoki Shiotabe394c82016-02-03 16:44:29 -0800149 });
Sho SHIMIZUe60a5ab2015-08-20 11:51:49 -0700150 }
151
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -0700152 private void registerPortResource(Device device, Port port) {
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800153 Resource portPath = Resources.discrete(device.id(), port.number()).resource();
HIGUCHI Yuta060da9a2016-03-11 19:16:35 -0800154 executor.execute(() -> {
Sho SHIMIZU7b326972016-02-09 15:53:39 -0800155 adminService.register(portPath);
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200156
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800157 queryBandwidth(device.id(), port.number())
158 .map(bw -> portPath.child(Bandwidth.class, bw.bps()))
Sho SHIMIZU7b326972016-02-09 15:53:39 -0800159 .map(adminService::register)
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800160 .ifPresent(success -> {
161 if (!success) {
Ray Milkey0ae473d2016-04-04 10:56:47 -0700162 log.warn("Failed to register Bandwidth for {}", portPath.id());
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800163 }
164 });
165
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800166 // for VLAN IDs
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800167 Set<VlanId> vlans = queryVlanIds(device.id(), port.number());
168 if (!vlans.isEmpty()) {
Sho SHIMIZU7b326972016-02-09 15:53:39 -0800169 adminService.register(vlans.stream()
170 .map(portPath::child)
171 .collect(Collectors.toList()));
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800172 }
173
174 // for MPLS labels
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800175 Set<MplsLabel> mplsLabels = queryMplsLabels(device.id(), port.number());
176 if (!mplsLabels.isEmpty()) {
Sho SHIMIZU7b326972016-02-09 15:53:39 -0800177 adminService.register(mplsLabels.stream()
178 .map(portPath::child)
179 .collect(Collectors.toList()));
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800180 }
181
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800182 // for Lambdas
Marc De Leenheer622861d2015-12-15 22:52:52 -0800183 Set<OchSignal> lambdas = queryLambdas(device.id(), port.number());
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800184 if (!lambdas.isEmpty()) {
Sho SHIMIZU7b326972016-02-09 15:53:39 -0800185 adminService.register(lambdas.stream()
186 .map(portPath::child)
187 .collect(Collectors.toList()));
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800188 }
189
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800190 // for Tributary slots
Toru Furusawac23f5832015-12-04 11:39:18 -0800191 Set<TributarySlot> tSlots = queryTributarySlots(device.id(), port.number());
192 if (!tSlots.isEmpty()) {
Sho SHIMIZU7b326972016-02-09 15:53:39 -0800193 adminService.register(tSlots.stream()
194 .map(portPath::child)
195 .collect(Collectors.toList()));
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800196 }
197 });
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200198 }
199
Sho SHIMIZUe2292842015-08-20 11:59:23 -0700200 private void unregisterPortResource(Device device, Port port) {
HIGUCHI Yuta060da9a2016-03-11 19:16:35 -0800201 executor.execute(() -> {
Sho SHIMIZU2fbc33c2016-02-09 14:03:23 -0800202 DiscreteResource portResource = Resources.discrete(device.id(), port.number()).resource();
Naoki Shiotabe394c82016-02-03 16:44:29 -0800203 List<Resource> allResources = getDescendantResources(portResource);
Sho SHIMIZU7b326972016-02-09 15:53:39 -0800204 adminService.unregister(Lists.transform(allResources, Resource::id));
Naoki Shiotabe394c82016-02-03 16:44:29 -0800205 });
206 }
207
208 // Returns list of all descendant resources of given resource, including itself.
Sho SHIMIZU2fbc33c2016-02-09 14:03:23 -0800209 private List<Resource> getDescendantResources(DiscreteResource parent) {
Naoki Shiotabe394c82016-02-03 16:44:29 -0800210 LinkedList<Resource> allResources = new LinkedList<>();
211 allResources.add(parent);
212
Sho SHIMIZU2fbc33c2016-02-09 14:03:23 -0800213 Set<Resource> nextResources = resourceService.getRegisteredResources(parent.id());
Naoki Shiotabe394c82016-02-03 16:44:29 -0800214 while (!nextResources.isEmpty()) {
215 Set<Resource> currentResources = nextResources;
216 // resource list should be ordered from leaf to root
217 allResources.addAll(0, currentResources);
218
219 nextResources = currentResources.stream()
Sho SHIMIZU2fbc33c2016-02-09 14:03:23 -0800220 .filter(r -> r instanceof DiscreteResource)
221 .map(r -> (DiscreteResource) r)
222 .flatMap(r -> resourceService.getRegisteredResources(r.id()).stream())
Naoki Shiotabe394c82016-02-03 16:44:29 -0800223 .collect(Collectors.toSet());
224 }
225
226 return allResources;
Sho SHIMIZUe2292842015-08-20 11:59:23 -0700227 }
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200228
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800229 /**
230 * Query bandwidth capacity on a port.
231 *
232 * @param did {@link DeviceId}
233 * @param number {@link PortNumber}
234 * @return bandwidth capacity
235 */
236 private Optional<Bandwidth> queryBandwidth(DeviceId did, PortNumber number) {
237 // Check and use netcfg first.
238 ConnectPoint cp = new ConnectPoint(did, number);
239 BandwidthCapacity config = netcfgService.getConfig(cp, BandwidthCapacity.class);
240 if (config != null) {
241 log.trace("Registering configured bandwidth {} for {}/{}", config.capacity(), did, number);
242 return Optional.of(config.capacity());
243 }
244
245 // populate bandwidth value, assuming portSpeed == bandwidth
246 Port port = deviceService.getPort(did, number);
247 if (port != null) {
248 return Optional.of(Bandwidth.mbps(port.portSpeed()));
249 }
250 return Optional.empty();
251 }
252
Marc De Leenheer622861d2015-12-15 22:52:52 -0800253 private Set<OchSignal> queryLambdas(DeviceId did, PortNumber port) {
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800254 try {
255 DriverHandler handler = driverService.createHandler(did);
HIGUCHI Yuta82b3c112016-01-07 22:22:26 -0800256 if (handler == null || !handler.hasBehaviour(LambdaQuery.class)) {
Marc De Leenheer622861d2015-12-15 22:52:52 -0800257 return Collections.emptySet();
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800258 }
259 LambdaQuery query = handler.behaviour(LambdaQuery.class);
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800260 if (query != null) {
Marc De Leenheer2c305302015-12-07 21:37:44 -0800261 return query.queryLambdas(port).stream()
262 .flatMap(x -> OchSignal.toFlexGrid(x).stream())
Marc De Leenheer622861d2015-12-15 22:52:52 -0800263 .collect(Collectors.toSet());
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800264 } else {
Marc De Leenheer622861d2015-12-15 22:52:52 -0800265 return Collections.emptySet();
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800266 }
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800267 } catch (ItemNotFoundException e) {
Marc De Leenheer622861d2015-12-15 22:52:52 -0800268 return Collections.emptySet();
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800269 }
270 }
271
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800272 private Set<VlanId> queryVlanIds(DeviceId device, PortNumber port) {
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800273 try {
274 DriverHandler handler = driverService.createHandler(device);
HIGUCHI Yuta82b3c112016-01-07 22:22:26 -0800275 if (handler == null || !handler.hasBehaviour(VlanQuery.class)) {
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800276 return ImmutableSet.of();
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800277 }
278
279 VlanQuery query = handler.behaviour(VlanQuery.class);
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800280 if (query == null) {
281 return ImmutableSet.of();
282 }
283 return query.queryVlanIds(port);
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800284 } catch (ItemNotFoundException e) {
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800285 return ImmutableSet.of();
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800286 }
287 }
288
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800289 private Set<MplsLabel> queryMplsLabels(DeviceId device, PortNumber port) {
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800290 try {
291 DriverHandler handler = driverService.createHandler(device);
HIGUCHI Yuta82b3c112016-01-07 22:22:26 -0800292 if (handler == null || !handler.hasBehaviour(MplsQuery.class)) {
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800293 return ImmutableSet.of();
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800294 }
295
296 MplsQuery query = handler.behaviour(MplsQuery.class);
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800297 if (query == null) {
298 return ImmutableSet.of();
299 }
300 return query.queryMplsLabels(port);
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800301 } catch (ItemNotFoundException e) {
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800302 return ImmutableSet.of();
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800303 }
304 }
305
Toru Furusawac23f5832015-12-04 11:39:18 -0800306 private Set<TributarySlot> queryTributarySlots(DeviceId device, PortNumber port) {
307 try {
Toru Furusawac23f5832015-12-04 11:39:18 -0800308 DriverHandler handler = driverService.createHandler(device);
HIGUCHI Yuta82b3c112016-01-07 22:22:26 -0800309 if (handler == null || !handler.hasBehaviour(TributarySlotQuery.class)) {
Toru Furusawac23f5832015-12-04 11:39:18 -0800310 return Collections.emptySet();
311 }
312 TributarySlotQuery query = handler.behaviour(TributarySlotQuery.class);
313 if (query != null) {
314 return query.queryTributarySlots(port);
315 } else {
316 return Collections.emptySet();
317 }
318 } catch (ItemNotFoundException e) {
319 return Collections.emptySet();
320 }
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200321 }
Sho SHIMIZUd97a9502015-08-18 10:02:30 -0700322}