blob: ac9f894f7707544612b4016d3ecfae2d80330922 [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 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;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080024import org.onosproject.net.ConnectPoint;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070025import org.onosproject.net.Device;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080026import org.onosproject.net.DeviceId;
HIGUCHI Yutad39e3762015-12-04 09:43:16 -080027import org.onosproject.net.OchSignal;
Toru Furusawac23f5832015-12-04 11:39:18 -080028import org.onosproject.net.Port;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080029import org.onosproject.net.PortNumber;
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020030import org.onosproject.net.TributarySlot;
HIGUCHI Yutad39e3762015-12-04 09:43:16 -080031import org.onosproject.net.behaviour.LambdaQuery;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080032import org.onosproject.net.behaviour.MplsQuery;
Toru Furusawac23f5832015-12-04 11:39:18 -080033import org.onosproject.net.behaviour.TributarySlotQuery;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080034import org.onosproject.net.behaviour.VlanQuery;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080035import org.onosproject.net.config.NetworkConfigService;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070036import org.onosproject.net.device.DeviceEvent;
37import org.onosproject.net.device.DeviceListener;
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080038import org.onosproject.net.device.DeviceService;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080039import org.onosproject.net.driver.DriverHandler;
40import org.onosproject.net.driver.DriverService;
Sho SHIMIZU2fbc33c2016-02-09 14:03:23 -080041import org.onosproject.net.newresource.DiscreteResource;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070042import org.onosproject.net.newresource.ResourceAdminService;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080043import org.onosproject.net.newresource.BandwidthCapacity;
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080044import org.onosproject.net.newresource.Resource;
Sho SHIMIZU460b9722016-01-28 10:48:26 -080045import org.onosproject.net.newresource.Resources;
Naoki Shiotabe394c82016-02-03 16:44:29 -080046import org.onosproject.net.newresource.ResourceService;
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020047import org.slf4j.Logger;
48import org.slf4j.LoggerFactory;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070049
HIGUCHI Yutad39e3762015-12-04 09:43:16 -080050import java.util.Collections;
Naoki Shiotabe394c82016-02-03 16:44:29 -080051import java.util.LinkedList;
52import java.util.List;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080053import java.util.Optional;
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -080054import java.util.Set;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070055import java.util.concurrent.ExecutorService;
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020056import java.util.stream.Collectors;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070057
58import static com.google.common.base.Preconditions.checkNotNull;
59
60/**
61 * An implementation of DeviceListener registering devices as resources.
62 */
63final class ResourceDeviceListener implements DeviceListener {
64
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020065 private static final Logger log = LoggerFactory.getLogger(ResourceDeviceListener.class);
66
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070067 private final ResourceAdminService adminService;
Naoki Shiotabe394c82016-02-03 16:44:29 -080068 private final ResourceService resourceService;
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080069 private final DeviceService deviceService;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080070 private final DriverService driverService;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080071 private final NetworkConfigService netcfgService;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070072 private final ExecutorService executor;
73
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080074
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070075 /**
76 * Creates an instance with the specified ResourceAdminService and ExecutorService.
77 *
78 * @param adminService instance invoked to register resources
Thomas Vachuska708d3032016-02-18 11:11:46 -080079 * @param resourceService {@link ResourceService} to be used
Jian Lidfba7392016-01-22 16:46:58 -080080 * @param deviceService {@link DeviceService} to be used
81 * @param driverService {@link DriverService} to be used
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080082 * @param netcfgService {@link NetworkConfigService} to be used.
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070083 * @param executor executor used for processing resource registration
84 */
Naoki Shiotabe394c82016-02-03 16:44:29 -080085 ResourceDeviceListener(ResourceAdminService adminService, ResourceService resourceService,
86 DeviceService deviceService, DriverService driverService,
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080087 NetworkConfigService netcfgService, ExecutorService executor) {
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070088 this.adminService = checkNotNull(adminService);
Naoki Shiotabe394c82016-02-03 16:44:29 -080089 this.resourceService = checkNotNull(resourceService);
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080090 this.deviceService = checkNotNull(deviceService);
Sho SHIMIZU44f37612015-11-25 16:23:22 -080091 this.driverService = checkNotNull(driverService);
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080092 this.netcfgService = checkNotNull(netcfgService);
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070093 this.executor = checkNotNull(executor);
94 }
95
96 @Override
97 public void event(DeviceEvent event) {
98 Device device = event.subject();
99 switch (event.type()) {
100 case DEVICE_ADDED:
101 registerDeviceResource(device);
102 break;
Sho SHIMIZUe60a5ab2015-08-20 11:51:49 -0700103 case DEVICE_REMOVED:
104 unregisterDeviceResource(device);
105 break;
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800106 case DEVICE_AVAILABILITY_CHANGED:
107 if (deviceService.isAvailable(device.id())) {
108 registerDeviceResource(device);
109 // TODO: do we need to walk the ports?
110 } else {
111 unregisterDeviceResource(device);
112 }
113 break;
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -0700114 case PORT_ADDED:
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800115 case PORT_UPDATED:
116 if (event.port().isEnabled()) {
117 registerPortResource(device, event.port());
118 } else {
119 unregisterPortResource(device, event.port());
120 }
Sho SHIMIZUc2ddedd2015-08-20 11:54:29 -0700121 break;
Sho SHIMIZUe2292842015-08-20 11:59:23 -0700122 case PORT_REMOVED:
123 unregisterPortResource(device, event.port());
124 break;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -0700125 default:
126 break;
127 }
128 }
129
130 private void registerDeviceResource(Device device) {
Sho SHIMIZU7b326972016-02-09 15:53:39 -0800131 executor.submit(() -> adminService.register(Resources.discrete(device.id()).resource()));
Sho SHIMIZUd97a9502015-08-18 10:02:30 -0700132 }
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -0700133
Sho SHIMIZUe60a5ab2015-08-20 11:51:49 -0700134 private void unregisterDeviceResource(Device device) {
Naoki Shiotabe394c82016-02-03 16:44:29 -0800135 executor.submit(() -> {
Sho SHIMIZU2fbc33c2016-02-09 14:03:23 -0800136 DiscreteResource devResource = Resources.discrete(device.id()).resource();
Naoki Shiotabe394c82016-02-03 16:44:29 -0800137 List<Resource> allResources = getDescendantResources(devResource);
Sho SHIMIZU7b326972016-02-09 15:53:39 -0800138 adminService.unregister(Lists.transform(allResources, Resource::id));
Naoki Shiotabe394c82016-02-03 16:44:29 -0800139 });
Sho SHIMIZUe60a5ab2015-08-20 11:51:49 -0700140 }
141
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -0700142 private void registerPortResource(Device device, Port port) {
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800143 Resource portPath = Resources.discrete(device.id(), port.number()).resource();
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800144 executor.submit(() -> {
Sho SHIMIZU7b326972016-02-09 15:53:39 -0800145 adminService.register(portPath);
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200146
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800147 queryBandwidth(device.id(), port.number())
148 .map(bw -> portPath.child(Bandwidth.class, bw.bps()))
Sho SHIMIZU7b326972016-02-09 15:53:39 -0800149 .map(adminService::register)
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800150 .ifPresent(success -> {
151 if (!success) {
152 log.error("Failed to register Bandwidth for {}", portPath.id());
153 }
154 });
155
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800156 // for VLAN IDs
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800157 Set<VlanId> vlans = queryVlanIds(device.id(), port.number());
158 if (!vlans.isEmpty()) {
Sho SHIMIZU7b326972016-02-09 15:53:39 -0800159 adminService.register(vlans.stream()
160 .map(portPath::child)
161 .collect(Collectors.toList()));
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800162 }
163
164 // for MPLS labels
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800165 Set<MplsLabel> mplsLabels = queryMplsLabels(device.id(), port.number());
166 if (!mplsLabels.isEmpty()) {
Sho SHIMIZU7b326972016-02-09 15:53:39 -0800167 adminService.register(mplsLabels.stream()
168 .map(portPath::child)
169 .collect(Collectors.toList()));
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800170 }
171
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800172 // for Lambdas
Marc De Leenheer622861d2015-12-15 22:52:52 -0800173 Set<OchSignal> lambdas = queryLambdas(device.id(), port.number());
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800174 if (!lambdas.isEmpty()) {
Sho SHIMIZU7b326972016-02-09 15:53:39 -0800175 adminService.register(lambdas.stream()
176 .map(portPath::child)
177 .collect(Collectors.toList()));
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800178 }
179
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800180 // for Tributary slots
Toru Furusawac23f5832015-12-04 11:39:18 -0800181 Set<TributarySlot> tSlots = queryTributarySlots(device.id(), port.number());
182 if (!tSlots.isEmpty()) {
Sho SHIMIZU7b326972016-02-09 15:53:39 -0800183 adminService.register(tSlots.stream()
184 .map(portPath::child)
185 .collect(Collectors.toList()));
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800186 }
187 });
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200188 }
189
Sho SHIMIZUe2292842015-08-20 11:59:23 -0700190 private void unregisterPortResource(Device device, Port port) {
Naoki Shiotabe394c82016-02-03 16:44:29 -0800191 executor.submit(() -> {
Sho SHIMIZU2fbc33c2016-02-09 14:03:23 -0800192 DiscreteResource portResource = Resources.discrete(device.id(), port.number()).resource();
Naoki Shiotabe394c82016-02-03 16:44:29 -0800193 List<Resource> allResources = getDescendantResources(portResource);
Sho SHIMIZU7b326972016-02-09 15:53:39 -0800194 adminService.unregister(Lists.transform(allResources, Resource::id));
Naoki Shiotabe394c82016-02-03 16:44:29 -0800195 });
196 }
197
198 // Returns list of all descendant resources of given resource, including itself.
Sho SHIMIZU2fbc33c2016-02-09 14:03:23 -0800199 private List<Resource> getDescendantResources(DiscreteResource parent) {
Naoki Shiotabe394c82016-02-03 16:44:29 -0800200 LinkedList<Resource> allResources = new LinkedList<>();
201 allResources.add(parent);
202
Sho SHIMIZU2fbc33c2016-02-09 14:03:23 -0800203 Set<Resource> nextResources = resourceService.getRegisteredResources(parent.id());
Naoki Shiotabe394c82016-02-03 16:44:29 -0800204 while (!nextResources.isEmpty()) {
205 Set<Resource> currentResources = nextResources;
206 // resource list should be ordered from leaf to root
207 allResources.addAll(0, currentResources);
208
209 nextResources = currentResources.stream()
Sho SHIMIZU2fbc33c2016-02-09 14:03:23 -0800210 .filter(r -> r instanceof DiscreteResource)
211 .map(r -> (DiscreteResource) r)
212 .flatMap(r -> resourceService.getRegisteredResources(r.id()).stream())
Naoki Shiotabe394c82016-02-03 16:44:29 -0800213 .collect(Collectors.toSet());
214 }
215
216 return allResources;
Sho SHIMIZUe2292842015-08-20 11:59:23 -0700217 }
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200218
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800219 /**
220 * Query bandwidth capacity on a port.
221 *
222 * @param did {@link DeviceId}
223 * @param number {@link PortNumber}
224 * @return bandwidth capacity
225 */
226 private Optional<Bandwidth> queryBandwidth(DeviceId did, PortNumber number) {
227 // Check and use netcfg first.
228 ConnectPoint cp = new ConnectPoint(did, number);
229 BandwidthCapacity config = netcfgService.getConfig(cp, BandwidthCapacity.class);
230 if (config != null) {
231 log.trace("Registering configured bandwidth {} for {}/{}", config.capacity(), did, number);
232 return Optional.of(config.capacity());
233 }
234
235 // populate bandwidth value, assuming portSpeed == bandwidth
236 Port port = deviceService.getPort(did, number);
237 if (port != null) {
238 return Optional.of(Bandwidth.mbps(port.portSpeed()));
239 }
240 return Optional.empty();
241 }
242
Marc De Leenheer622861d2015-12-15 22:52:52 -0800243 private Set<OchSignal> queryLambdas(DeviceId did, PortNumber port) {
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800244 try {
245 DriverHandler handler = driverService.createHandler(did);
HIGUCHI Yuta82b3c112016-01-07 22:22:26 -0800246 if (handler == null || !handler.hasBehaviour(LambdaQuery.class)) {
Marc De Leenheer622861d2015-12-15 22:52:52 -0800247 return Collections.emptySet();
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800248 }
249 LambdaQuery query = handler.behaviour(LambdaQuery.class);
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800250 if (query != null) {
Marc De Leenheer2c305302015-12-07 21:37:44 -0800251 return query.queryLambdas(port).stream()
252 .flatMap(x -> OchSignal.toFlexGrid(x).stream())
Marc De Leenheer622861d2015-12-15 22:52:52 -0800253 .collect(Collectors.toSet());
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800254 } else {
Marc De Leenheer622861d2015-12-15 22:52:52 -0800255 return Collections.emptySet();
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800256 }
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800257 } catch (ItemNotFoundException e) {
Marc De Leenheer622861d2015-12-15 22:52:52 -0800258 return Collections.emptySet();
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800259 }
260 }
261
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800262 private Set<VlanId> queryVlanIds(DeviceId device, PortNumber port) {
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800263 try {
264 DriverHandler handler = driverService.createHandler(device);
HIGUCHI Yuta82b3c112016-01-07 22:22:26 -0800265 if (handler == null || !handler.hasBehaviour(VlanQuery.class)) {
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800266 return ImmutableSet.of();
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800267 }
268
269 VlanQuery query = handler.behaviour(VlanQuery.class);
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800270 if (query == null) {
271 return ImmutableSet.of();
272 }
273 return query.queryVlanIds(port);
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800274 } catch (ItemNotFoundException e) {
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800275 return ImmutableSet.of();
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800276 }
277 }
278
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800279 private Set<MplsLabel> queryMplsLabels(DeviceId device, PortNumber port) {
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800280 try {
281 DriverHandler handler = driverService.createHandler(device);
HIGUCHI Yuta82b3c112016-01-07 22:22:26 -0800282 if (handler == null || !handler.hasBehaviour(MplsQuery.class)) {
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800283 return ImmutableSet.of();
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800284 }
285
286 MplsQuery query = handler.behaviour(MplsQuery.class);
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800287 if (query == null) {
288 return ImmutableSet.of();
289 }
290 return query.queryMplsLabels(port);
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800291 } catch (ItemNotFoundException e) {
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800292 return ImmutableSet.of();
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800293 }
294 }
295
Toru Furusawac23f5832015-12-04 11:39:18 -0800296 private Set<TributarySlot> queryTributarySlots(DeviceId device, PortNumber port) {
297 try {
Toru Furusawac23f5832015-12-04 11:39:18 -0800298 DriverHandler handler = driverService.createHandler(device);
HIGUCHI Yuta82b3c112016-01-07 22:22:26 -0800299 if (handler == null || !handler.hasBehaviour(TributarySlotQuery.class)) {
Toru Furusawac23f5832015-12-04 11:39:18 -0800300 return Collections.emptySet();
301 }
302 TributarySlotQuery query = handler.behaviour(TributarySlotQuery.class);
303 if (query != null) {
304 return query.queryTributarySlots(port);
305 } else {
306 return Collections.emptySet();
307 }
308 } catch (ItemNotFoundException e) {
309 return Collections.emptySet();
310 }
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200311 }
Sho SHIMIZUd97a9502015-08-18 10:02:30 -0700312}