blob: a8850df39a19df2a624d3ea672f24705bb4cf583 [file] [log] [blame]
Sho SHIMIZUd97a9502015-08-18 10:02:30 -07001/*
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 */
16package org.onosproject.net.newresource.impl;
17
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -080018import com.google.common.collect.ImmutableSet;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080019import org.onlab.packet.MplsLabel;
20import org.onlab.packet.VlanId;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080021import org.onlab.util.Bandwidth;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080022import org.onlab.util.ItemNotFoundException;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080023import org.onosproject.net.ConnectPoint;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070024import org.onosproject.net.Device;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080025import org.onosproject.net.DeviceId;
HIGUCHI Yutad39e3762015-12-04 09:43:16 -080026import org.onosproject.net.OchSignal;
Toru Furusawac23f5832015-12-04 11:39:18 -080027import org.onosproject.net.Port;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080028import org.onosproject.net.PortNumber;
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020029import org.onosproject.net.TributarySlot;
HIGUCHI Yutad39e3762015-12-04 09:43:16 -080030import org.onosproject.net.behaviour.LambdaQuery;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080031import org.onosproject.net.behaviour.MplsQuery;
Toru Furusawac23f5832015-12-04 11:39:18 -080032import org.onosproject.net.behaviour.TributarySlotQuery;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080033import org.onosproject.net.behaviour.VlanQuery;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080034import org.onosproject.net.config.NetworkConfigService;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070035import org.onosproject.net.device.DeviceEvent;
36import org.onosproject.net.device.DeviceListener;
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080037import org.onosproject.net.device.DeviceService;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080038import org.onosproject.net.driver.DriverHandler;
39import org.onosproject.net.driver.DriverService;
Sho SHIMIZU2fbc33c2016-02-09 14:03:23 -080040import org.onosproject.net.newresource.DiscreteResource;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070041import org.onosproject.net.newresource.ResourceAdminService;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080042import org.onosproject.net.newresource.BandwidthCapacity;
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080043import org.onosproject.net.newresource.Resource;
Sho SHIMIZU460b9722016-01-28 10:48:26 -080044import org.onosproject.net.newresource.Resources;
Naoki Shiotabe394c82016-02-03 16:44:29 -080045import org.onosproject.net.newresource.ResourceService;
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020046import org.slf4j.Logger;
47import org.slf4j.LoggerFactory;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070048
HIGUCHI Yutad39e3762015-12-04 09:43:16 -080049import java.util.Collections;
Naoki Shiotabe394c82016-02-03 16:44:29 -080050import java.util.LinkedList;
51import java.util.List;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080052import java.util.Optional;
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -080053import java.util.Set;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070054import java.util.concurrent.ExecutorService;
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020055import java.util.stream.Collectors;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070056
57import static com.google.common.base.Preconditions.checkNotNull;
58
59/**
60 * An implementation of DeviceListener registering devices as resources.
61 */
62final class ResourceDeviceListener implements DeviceListener {
63
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020064 private static final Logger log = LoggerFactory.getLogger(ResourceDeviceListener.class);
65
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070066 private final ResourceAdminService adminService;
Naoki Shiotabe394c82016-02-03 16:44:29 -080067 private final ResourceService resourceService;
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080068 private final DeviceService deviceService;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080069 private final DriverService driverService;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080070 private final NetworkConfigService netcfgService;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070071 private final ExecutorService executor;
72
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080073
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070074 /**
75 * Creates an instance with the specified ResourceAdminService and ExecutorService.
76 *
77 * @param adminService instance invoked to register resources
Jian Lidfba7392016-01-22 16:46:58 -080078 * @param deviceService {@link DeviceService} to be used
79 * @param driverService {@link DriverService} to be used
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080080 * @param netcfgService {@link NetworkConfigService} to be used.
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070081 * @param executor executor used for processing resource registration
82 */
Naoki Shiotabe394c82016-02-03 16:44:29 -080083 ResourceDeviceListener(ResourceAdminService adminService, ResourceService resourceService,
84 DeviceService deviceService, DriverService driverService,
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080085 NetworkConfigService netcfgService, ExecutorService executor) {
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070086 this.adminService = checkNotNull(adminService);
Naoki Shiotabe394c82016-02-03 16:44:29 -080087 this.resourceService = checkNotNull(resourceService);
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080088 this.deviceService = checkNotNull(deviceService);
Sho SHIMIZU44f37612015-11-25 16:23:22 -080089 this.driverService = checkNotNull(driverService);
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080090 this.netcfgService = checkNotNull(netcfgService);
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070091 this.executor = checkNotNull(executor);
92 }
93
94 @Override
95 public void event(DeviceEvent event) {
96 Device device = event.subject();
97 switch (event.type()) {
98 case DEVICE_ADDED:
99 registerDeviceResource(device);
100 break;
Sho SHIMIZUe60a5ab2015-08-20 11:51:49 -0700101 case DEVICE_REMOVED:
102 unregisterDeviceResource(device);
103 break;
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800104 case DEVICE_AVAILABILITY_CHANGED:
105 if (deviceService.isAvailable(device.id())) {
106 registerDeviceResource(device);
107 // TODO: do we need to walk the ports?
108 } else {
109 unregisterDeviceResource(device);
110 }
111 break;
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -0700112 case PORT_ADDED:
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800113 case PORT_UPDATED:
114 if (event.port().isEnabled()) {
115 registerPortResource(device, event.port());
116 } else {
117 unregisterPortResource(device, event.port());
118 }
Sho SHIMIZUc2ddedd2015-08-20 11:54:29 -0700119 break;
Sho SHIMIZUe2292842015-08-20 11:59:23 -0700120 case PORT_REMOVED:
121 unregisterPortResource(device, event.port());
122 break;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -0700123 default:
124 break;
125 }
126 }
127
128 private void registerDeviceResource(Device device) {
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800129 executor.submit(() -> adminService.registerResources(Resources.discrete(device.id()).resource()));
Sho SHIMIZUd97a9502015-08-18 10:02:30 -0700130 }
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -0700131
Sho SHIMIZUe60a5ab2015-08-20 11:51:49 -0700132 private void unregisterDeviceResource(Device device) {
Naoki Shiotabe394c82016-02-03 16:44:29 -0800133 executor.submit(() -> {
Sho SHIMIZU2fbc33c2016-02-09 14:03:23 -0800134 DiscreteResource devResource = Resources.discrete(device.id()).resource();
Naoki Shiotabe394c82016-02-03 16:44:29 -0800135 List<Resource> allResources = getDescendantResources(devResource);
136 adminService.unregisterResources(allResources);
137 });
Sho SHIMIZUe60a5ab2015-08-20 11:51:49 -0700138 }
139
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -0700140 private void registerPortResource(Device device, Port port) {
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800141 Resource portPath = Resources.discrete(device.id(), port.number()).resource();
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800142 executor.submit(() -> {
143 adminService.registerResources(portPath);
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200144
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800145 queryBandwidth(device.id(), port.number())
146 .map(bw -> portPath.child(Bandwidth.class, bw.bps()))
147 .map(adminService::registerResources)
148 .ifPresent(success -> {
149 if (!success) {
150 log.error("Failed to register Bandwidth for {}", portPath.id());
151 }
152 });
153
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800154 // for VLAN IDs
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800155 Set<VlanId> vlans = queryVlanIds(device.id(), port.number());
156 if (!vlans.isEmpty()) {
157 adminService.registerResources(vlans.stream()
158 .map(portPath::child)
159 .collect(Collectors.toList()));
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800160 }
161
162 // for MPLS labels
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800163 Set<MplsLabel> mplsLabels = queryMplsLabels(device.id(), port.number());
164 if (!mplsLabels.isEmpty()) {
165 adminService.registerResources(mplsLabels.stream()
166 .map(portPath::child)
167 .collect(Collectors.toList()));
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800168 }
169
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800170 // for Lambdas
Marc De Leenheer622861d2015-12-15 22:52:52 -0800171 Set<OchSignal> lambdas = queryLambdas(device.id(), port.number());
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800172 if (!lambdas.isEmpty()) {
173 adminService.registerResources(lambdas.stream()
174 .map(portPath::child)
175 .collect(Collectors.toList()));
176 }
177
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800178 // for Tributary slots
Toru Furusawac23f5832015-12-04 11:39:18 -0800179 Set<TributarySlot> tSlots = queryTributarySlots(device.id(), port.number());
180 if (!tSlots.isEmpty()) {
181 adminService.registerResources(tSlots.stream()
182 .map(portPath::child)
183 .collect(Collectors.toList()));
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800184 }
185 });
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200186 }
187
Sho SHIMIZUe2292842015-08-20 11:59:23 -0700188 private void unregisterPortResource(Device device, Port port) {
Naoki Shiotabe394c82016-02-03 16:44:29 -0800189 executor.submit(() -> {
Sho SHIMIZU2fbc33c2016-02-09 14:03:23 -0800190 DiscreteResource portResource = Resources.discrete(device.id(), port.number()).resource();
Naoki Shiotabe394c82016-02-03 16:44:29 -0800191 List<Resource> allResources = getDescendantResources(portResource);
192 adminService.unregisterResources(allResources);
193 });
194 }
195
196 // Returns list of all descendant resources of given resource, including itself.
Sho SHIMIZU2fbc33c2016-02-09 14:03:23 -0800197 private List<Resource> getDescendantResources(DiscreteResource parent) {
Naoki Shiotabe394c82016-02-03 16:44:29 -0800198 LinkedList<Resource> allResources = new LinkedList<>();
199 allResources.add(parent);
200
Sho SHIMIZU2fbc33c2016-02-09 14:03:23 -0800201 Set<Resource> nextResources = resourceService.getRegisteredResources(parent.id());
Naoki Shiotabe394c82016-02-03 16:44:29 -0800202 while (!nextResources.isEmpty()) {
203 Set<Resource> currentResources = nextResources;
204 // resource list should be ordered from leaf to root
205 allResources.addAll(0, currentResources);
206
207 nextResources = currentResources.stream()
Sho SHIMIZU2fbc33c2016-02-09 14:03:23 -0800208 .filter(r -> r instanceof DiscreteResource)
209 .map(r -> (DiscreteResource) r)
210 .flatMap(r -> resourceService.getRegisteredResources(r.id()).stream())
Naoki Shiotabe394c82016-02-03 16:44:29 -0800211 .collect(Collectors.toSet());
212 }
213
214 return allResources;
Sho SHIMIZUe2292842015-08-20 11:59:23 -0700215 }
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200216
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800217 /**
218 * Query bandwidth capacity on a port.
219 *
220 * @param did {@link DeviceId}
221 * @param number {@link PortNumber}
222 * @return bandwidth capacity
223 */
224 private Optional<Bandwidth> queryBandwidth(DeviceId did, PortNumber number) {
225 // Check and use netcfg first.
226 ConnectPoint cp = new ConnectPoint(did, number);
227 BandwidthCapacity config = netcfgService.getConfig(cp, BandwidthCapacity.class);
228 if (config != null) {
229 log.trace("Registering configured bandwidth {} for {}/{}", config.capacity(), did, number);
230 return Optional.of(config.capacity());
231 }
232
233 // populate bandwidth value, assuming portSpeed == bandwidth
234 Port port = deviceService.getPort(did, number);
235 if (port != null) {
236 return Optional.of(Bandwidth.mbps(port.portSpeed()));
237 }
238 return Optional.empty();
239 }
240
Marc De Leenheer622861d2015-12-15 22:52:52 -0800241 private Set<OchSignal> queryLambdas(DeviceId did, PortNumber port) {
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800242 try {
243 DriverHandler handler = driverService.createHandler(did);
HIGUCHI Yuta82b3c112016-01-07 22:22:26 -0800244 if (handler == null || !handler.hasBehaviour(LambdaQuery.class)) {
Marc De Leenheer622861d2015-12-15 22:52:52 -0800245 return Collections.emptySet();
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800246 }
247 LambdaQuery query = handler.behaviour(LambdaQuery.class);
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800248 if (query != null) {
Marc De Leenheer2c305302015-12-07 21:37:44 -0800249 return query.queryLambdas(port).stream()
250 .flatMap(x -> OchSignal.toFlexGrid(x).stream())
Marc De Leenheer622861d2015-12-15 22:52:52 -0800251 .collect(Collectors.toSet());
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800252 } else {
Marc De Leenheer622861d2015-12-15 22:52:52 -0800253 return Collections.emptySet();
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800254 }
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800255 } catch (ItemNotFoundException e) {
Marc De Leenheer622861d2015-12-15 22:52:52 -0800256 return Collections.emptySet();
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800257 }
258 }
259
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800260 private Set<VlanId> queryVlanIds(DeviceId device, PortNumber port) {
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800261 try {
262 DriverHandler handler = driverService.createHandler(device);
HIGUCHI Yuta82b3c112016-01-07 22:22:26 -0800263 if (handler == null || !handler.hasBehaviour(VlanQuery.class)) {
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800264 return ImmutableSet.of();
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800265 }
266
267 VlanQuery query = handler.behaviour(VlanQuery.class);
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800268 if (query == null) {
269 return ImmutableSet.of();
270 }
271 return query.queryVlanIds(port);
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800272 } catch (ItemNotFoundException e) {
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800273 return ImmutableSet.of();
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800274 }
275 }
276
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800277 private Set<MplsLabel> queryMplsLabels(DeviceId device, PortNumber port) {
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800278 try {
279 DriverHandler handler = driverService.createHandler(device);
HIGUCHI Yuta82b3c112016-01-07 22:22:26 -0800280 if (handler == null || !handler.hasBehaviour(MplsQuery.class)) {
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800281 return ImmutableSet.of();
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800282 }
283
284 MplsQuery query = handler.behaviour(MplsQuery.class);
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800285 if (query == null) {
286 return ImmutableSet.of();
287 }
288 return query.queryMplsLabels(port);
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800289 } catch (ItemNotFoundException e) {
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800290 return ImmutableSet.of();
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800291 }
292 }
293
Toru Furusawac23f5832015-12-04 11:39:18 -0800294 private Set<TributarySlot> queryTributarySlots(DeviceId device, PortNumber port) {
295 try {
Toru Furusawac23f5832015-12-04 11:39:18 -0800296 DriverHandler handler = driverService.createHandler(device);
HIGUCHI Yuta82b3c112016-01-07 22:22:26 -0800297 if (handler == null || !handler.hasBehaviour(TributarySlotQuery.class)) {
Toru Furusawac23f5832015-12-04 11:39:18 -0800298 return Collections.emptySet();
299 }
300 TributarySlotQuery query = handler.behaviour(TributarySlotQuery.class);
301 if (query != null) {
302 return query.queryTributarySlots(port);
303 } else {
304 return Collections.emptySet();
305 }
306 } catch (ItemNotFoundException e) {
307 return Collections.emptySet();
308 }
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200309 }
Sho SHIMIZUd97a9502015-08-18 10:02:30 -0700310}