blob: 3ff3706a8d031beafa3991a10b1019aaa9126acc [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
Jian Lidfba7392016-01-22 16:46:58 -080079 * @param deviceService {@link DeviceService} to be used
80 * @param driverService {@link DriverService} to be used
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080081 * @param netcfgService {@link NetworkConfigService} to be used.
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070082 * @param executor executor used for processing resource registration
83 */
Naoki Shiotabe394c82016-02-03 16:44:29 -080084 ResourceDeviceListener(ResourceAdminService adminService, ResourceService resourceService,
85 DeviceService deviceService, DriverService driverService,
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080086 NetworkConfigService netcfgService, ExecutorService executor) {
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070087 this.adminService = checkNotNull(adminService);
Naoki Shiotabe394c82016-02-03 16:44:29 -080088 this.resourceService = checkNotNull(resourceService);
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080089 this.deviceService = checkNotNull(deviceService);
Sho SHIMIZU44f37612015-11-25 16:23:22 -080090 this.driverService = checkNotNull(driverService);
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080091 this.netcfgService = checkNotNull(netcfgService);
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070092 this.executor = checkNotNull(executor);
93 }
94
95 @Override
96 public void event(DeviceEvent event) {
97 Device device = event.subject();
98 switch (event.type()) {
99 case DEVICE_ADDED:
100 registerDeviceResource(device);
101 break;
Sho SHIMIZUe60a5ab2015-08-20 11:51:49 -0700102 case DEVICE_REMOVED:
103 unregisterDeviceResource(device);
104 break;
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800105 case DEVICE_AVAILABILITY_CHANGED:
106 if (deviceService.isAvailable(device.id())) {
107 registerDeviceResource(device);
108 // TODO: do we need to walk the ports?
109 } else {
110 unregisterDeviceResource(device);
111 }
112 break;
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -0700113 case PORT_ADDED:
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800114 case PORT_UPDATED:
115 if (event.port().isEnabled()) {
116 registerPortResource(device, event.port());
117 } else {
118 unregisterPortResource(device, event.port());
119 }
Sho SHIMIZUc2ddedd2015-08-20 11:54:29 -0700120 break;
Sho SHIMIZUe2292842015-08-20 11:59:23 -0700121 case PORT_REMOVED:
122 unregisterPortResource(device, event.port());
123 break;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -0700124 default:
125 break;
126 }
127 }
128
129 private void registerDeviceResource(Device device) {
Sho SHIMIZU7b326972016-02-09 15:53:39 -0800130 executor.submit(() -> adminService.register(Resources.discrete(device.id()).resource()));
Sho SHIMIZUd97a9502015-08-18 10:02:30 -0700131 }
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -0700132
Sho SHIMIZUe60a5ab2015-08-20 11:51:49 -0700133 private void unregisterDeviceResource(Device device) {
Naoki Shiotabe394c82016-02-03 16:44:29 -0800134 executor.submit(() -> {
Sho SHIMIZU2fbc33c2016-02-09 14:03:23 -0800135 DiscreteResource devResource = Resources.discrete(device.id()).resource();
Naoki Shiotabe394c82016-02-03 16:44:29 -0800136 List<Resource> allResources = getDescendantResources(devResource);
Sho SHIMIZU7b326972016-02-09 15:53:39 -0800137 adminService.unregister(Lists.transform(allResources, Resource::id));
Naoki Shiotabe394c82016-02-03 16:44:29 -0800138 });
Sho SHIMIZUe60a5ab2015-08-20 11:51:49 -0700139 }
140
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -0700141 private void registerPortResource(Device device, Port port) {
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800142 Resource portPath = Resources.discrete(device.id(), port.number()).resource();
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800143 executor.submit(() -> {
Sho SHIMIZU7b326972016-02-09 15:53:39 -0800144 adminService.register(portPath);
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200145
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800146 queryBandwidth(device.id(), port.number())
147 .map(bw -> portPath.child(Bandwidth.class, bw.bps()))
Sho SHIMIZU7b326972016-02-09 15:53:39 -0800148 .map(adminService::register)
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800149 .ifPresent(success -> {
150 if (!success) {
151 log.error("Failed to register Bandwidth for {}", portPath.id());
152 }
153 });
154
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800155 // for VLAN IDs
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800156 Set<VlanId> vlans = queryVlanIds(device.id(), port.number());
157 if (!vlans.isEmpty()) {
Sho SHIMIZU7b326972016-02-09 15:53:39 -0800158 adminService.register(vlans.stream()
159 .map(portPath::child)
160 .collect(Collectors.toList()));
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800161 }
162
163 // for MPLS labels
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800164 Set<MplsLabel> mplsLabels = queryMplsLabels(device.id(), port.number());
165 if (!mplsLabels.isEmpty()) {
Sho SHIMIZU7b326972016-02-09 15:53:39 -0800166 adminService.register(mplsLabels.stream()
167 .map(portPath::child)
168 .collect(Collectors.toList()));
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800169 }
170
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800171 // for Lambdas
Marc De Leenheer622861d2015-12-15 22:52:52 -0800172 Set<OchSignal> lambdas = queryLambdas(device.id(), port.number());
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800173 if (!lambdas.isEmpty()) {
Sho SHIMIZU7b326972016-02-09 15:53:39 -0800174 adminService.register(lambdas.stream()
175 .map(portPath::child)
176 .collect(Collectors.toList()));
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800177 }
178
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800179 // for Tributary slots
Toru Furusawac23f5832015-12-04 11:39:18 -0800180 Set<TributarySlot> tSlots = queryTributarySlots(device.id(), port.number());
181 if (!tSlots.isEmpty()) {
Sho SHIMIZU7b326972016-02-09 15:53:39 -0800182 adminService.register(tSlots.stream()
183 .map(portPath::child)
184 .collect(Collectors.toList()));
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800185 }
186 });
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200187 }
188
Sho SHIMIZUe2292842015-08-20 11:59:23 -0700189 private void unregisterPortResource(Device device, Port port) {
Naoki Shiotabe394c82016-02-03 16:44:29 -0800190 executor.submit(() -> {
Sho SHIMIZU2fbc33c2016-02-09 14:03:23 -0800191 DiscreteResource portResource = Resources.discrete(device.id(), port.number()).resource();
Naoki Shiotabe394c82016-02-03 16:44:29 -0800192 List<Resource> allResources = getDescendantResources(portResource);
Sho SHIMIZU7b326972016-02-09 15:53:39 -0800193 adminService.unregister(Lists.transform(allResources, Resource::id));
Naoki Shiotabe394c82016-02-03 16:44:29 -0800194 });
195 }
196
197 // Returns list of all descendant resources of given resource, including itself.
Sho SHIMIZU2fbc33c2016-02-09 14:03:23 -0800198 private List<Resource> getDescendantResources(DiscreteResource parent) {
Naoki Shiotabe394c82016-02-03 16:44:29 -0800199 LinkedList<Resource> allResources = new LinkedList<>();
200 allResources.add(parent);
201
Sho SHIMIZU2fbc33c2016-02-09 14:03:23 -0800202 Set<Resource> nextResources = resourceService.getRegisteredResources(parent.id());
Naoki Shiotabe394c82016-02-03 16:44:29 -0800203 while (!nextResources.isEmpty()) {
204 Set<Resource> currentResources = nextResources;
205 // resource list should be ordered from leaf to root
206 allResources.addAll(0, currentResources);
207
208 nextResources = currentResources.stream()
Sho SHIMIZU2fbc33c2016-02-09 14:03:23 -0800209 .filter(r -> r instanceof DiscreteResource)
210 .map(r -> (DiscreteResource) r)
211 .flatMap(r -> resourceService.getRegisteredResources(r.id()).stream())
Naoki Shiotabe394c82016-02-03 16:44:29 -0800212 .collect(Collectors.toSet());
213 }
214
215 return allResources;
Sho SHIMIZUe2292842015-08-20 11:59:23 -0700216 }
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200217
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800218 /**
219 * Query bandwidth capacity on a port.
220 *
221 * @param did {@link DeviceId}
222 * @param number {@link PortNumber}
223 * @return bandwidth capacity
224 */
225 private Optional<Bandwidth> queryBandwidth(DeviceId did, PortNumber number) {
226 // Check and use netcfg first.
227 ConnectPoint cp = new ConnectPoint(did, number);
228 BandwidthCapacity config = netcfgService.getConfig(cp, BandwidthCapacity.class);
229 if (config != null) {
230 log.trace("Registering configured bandwidth {} for {}/{}", config.capacity(), did, number);
231 return Optional.of(config.capacity());
232 }
233
234 // populate bandwidth value, assuming portSpeed == bandwidth
235 Port port = deviceService.getPort(did, number);
236 if (port != null) {
237 return Optional.of(Bandwidth.mbps(port.portSpeed()));
238 }
239 return Optional.empty();
240 }
241
Marc De Leenheer622861d2015-12-15 22:52:52 -0800242 private Set<OchSignal> queryLambdas(DeviceId did, PortNumber port) {
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800243 try {
244 DriverHandler handler = driverService.createHandler(did);
HIGUCHI Yuta82b3c112016-01-07 22:22:26 -0800245 if (handler == null || !handler.hasBehaviour(LambdaQuery.class)) {
Marc De Leenheer622861d2015-12-15 22:52:52 -0800246 return Collections.emptySet();
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800247 }
248 LambdaQuery query = handler.behaviour(LambdaQuery.class);
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800249 if (query != null) {
Marc De Leenheer2c305302015-12-07 21:37:44 -0800250 return query.queryLambdas(port).stream()
251 .flatMap(x -> OchSignal.toFlexGrid(x).stream())
Marc De Leenheer622861d2015-12-15 22:52:52 -0800252 .collect(Collectors.toSet());
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800253 } else {
Marc De Leenheer622861d2015-12-15 22:52:52 -0800254 return Collections.emptySet();
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800255 }
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800256 } catch (ItemNotFoundException e) {
Marc De Leenheer622861d2015-12-15 22:52:52 -0800257 return Collections.emptySet();
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800258 }
259 }
260
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800261 private Set<VlanId> queryVlanIds(DeviceId device, PortNumber port) {
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800262 try {
263 DriverHandler handler = driverService.createHandler(device);
HIGUCHI Yuta82b3c112016-01-07 22:22:26 -0800264 if (handler == null || !handler.hasBehaviour(VlanQuery.class)) {
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800265 return ImmutableSet.of();
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800266 }
267
268 VlanQuery query = handler.behaviour(VlanQuery.class);
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800269 if (query == null) {
270 return ImmutableSet.of();
271 }
272 return query.queryVlanIds(port);
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800273 } catch (ItemNotFoundException e) {
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800274 return ImmutableSet.of();
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800275 }
276 }
277
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800278 private Set<MplsLabel> queryMplsLabels(DeviceId device, PortNumber port) {
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800279 try {
280 DriverHandler handler = driverService.createHandler(device);
HIGUCHI Yuta82b3c112016-01-07 22:22:26 -0800281 if (handler == null || !handler.hasBehaviour(MplsQuery.class)) {
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800282 return ImmutableSet.of();
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800283 }
284
285 MplsQuery query = handler.behaviour(MplsQuery.class);
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800286 if (query == null) {
287 return ImmutableSet.of();
288 }
289 return query.queryMplsLabels(port);
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800290 } catch (ItemNotFoundException e) {
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800291 return ImmutableSet.of();
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800292 }
293 }
294
Toru Furusawac23f5832015-12-04 11:39:18 -0800295 private Set<TributarySlot> queryTributarySlots(DeviceId device, PortNumber port) {
296 try {
Toru Furusawac23f5832015-12-04 11:39:18 -0800297 DriverHandler handler = driverService.createHandler(device);
HIGUCHI Yuta82b3c112016-01-07 22:22:26 -0800298 if (handler == null || !handler.hasBehaviour(TributarySlotQuery.class)) {
Toru Furusawac23f5832015-12-04 11:39:18 -0800299 return Collections.emptySet();
300 }
301 TributarySlotQuery query = handler.behaviour(TributarySlotQuery.class);
302 if (query != null) {
303 return query.queryTributarySlots(port);
304 } else {
305 return Collections.emptySet();
306 }
307 } catch (ItemNotFoundException e) {
308 return Collections.emptySet();
309 }
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200310 }
Sho SHIMIZUd97a9502015-08-18 10:02:30 -0700311}