blob: f4ae80562731949cfcc240c895a62e079c44d6b8 [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 SHIMIZUd97a9502015-08-18 10:02:30 -070040import org.onosproject.net.newresource.ResourceAdminService;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080041import org.onosproject.net.newresource.BandwidthCapacity;
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080042import org.onosproject.net.newresource.Resource;
Sho SHIMIZU460b9722016-01-28 10:48:26 -080043import org.onosproject.net.newresource.Resources;
Naoki Shiotabe394c82016-02-03 16:44:29 -080044import org.onosproject.net.newresource.ResourceService;
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020045import org.slf4j.Logger;
46import org.slf4j.LoggerFactory;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070047
HIGUCHI Yutad39e3762015-12-04 09:43:16 -080048import java.util.Collections;
Naoki Shiotabe394c82016-02-03 16:44:29 -080049import java.util.LinkedList;
50import java.util.List;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080051import java.util.Optional;
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -080052import java.util.Set;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070053import java.util.concurrent.ExecutorService;
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020054import java.util.stream.Collectors;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070055
56import static com.google.common.base.Preconditions.checkNotNull;
57
58/**
59 * An implementation of DeviceListener registering devices as resources.
60 */
61final class ResourceDeviceListener implements DeviceListener {
62
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020063 private static final Logger log = LoggerFactory.getLogger(ResourceDeviceListener.class);
64
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070065 private final ResourceAdminService adminService;
Naoki Shiotabe394c82016-02-03 16:44:29 -080066 private final ResourceService resourceService;
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080067 private final DeviceService deviceService;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080068 private final DriverService driverService;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080069 private final NetworkConfigService netcfgService;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070070 private final ExecutorService executor;
71
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080072
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070073 /**
74 * Creates an instance with the specified ResourceAdminService and ExecutorService.
75 *
76 * @param adminService instance invoked to register resources
Jian Lidfba7392016-01-22 16:46:58 -080077 * @param deviceService {@link DeviceService} to be used
78 * @param driverService {@link DriverService} to be used
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080079 * @param netcfgService {@link NetworkConfigService} to be used.
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070080 * @param executor executor used for processing resource registration
81 */
Naoki Shiotabe394c82016-02-03 16:44:29 -080082 ResourceDeviceListener(ResourceAdminService adminService, ResourceService resourceService,
83 DeviceService deviceService, DriverService driverService,
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080084 NetworkConfigService netcfgService, ExecutorService executor) {
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070085 this.adminService = checkNotNull(adminService);
Naoki Shiotabe394c82016-02-03 16:44:29 -080086 this.resourceService = checkNotNull(resourceService);
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080087 this.deviceService = checkNotNull(deviceService);
Sho SHIMIZU44f37612015-11-25 16:23:22 -080088 this.driverService = checkNotNull(driverService);
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080089 this.netcfgService = checkNotNull(netcfgService);
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070090 this.executor = checkNotNull(executor);
91 }
92
93 @Override
94 public void event(DeviceEvent event) {
95 Device device = event.subject();
96 switch (event.type()) {
97 case DEVICE_ADDED:
98 registerDeviceResource(device);
99 break;
Sho SHIMIZUe60a5ab2015-08-20 11:51:49 -0700100 case DEVICE_REMOVED:
101 unregisterDeviceResource(device);
102 break;
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800103 case DEVICE_AVAILABILITY_CHANGED:
104 if (deviceService.isAvailable(device.id())) {
105 registerDeviceResource(device);
106 // TODO: do we need to walk the ports?
107 } else {
108 unregisterDeviceResource(device);
109 }
110 break;
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -0700111 case PORT_ADDED:
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800112 case PORT_UPDATED:
113 if (event.port().isEnabled()) {
114 registerPortResource(device, event.port());
115 } else {
116 unregisterPortResource(device, event.port());
117 }
Sho SHIMIZUc2ddedd2015-08-20 11:54:29 -0700118 break;
Sho SHIMIZUe2292842015-08-20 11:59:23 -0700119 case PORT_REMOVED:
120 unregisterPortResource(device, event.port());
121 break;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -0700122 default:
123 break;
124 }
125 }
126
127 private void registerDeviceResource(Device device) {
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800128 executor.submit(() -> adminService.registerResources(Resources.discrete(device.id()).resource()));
Sho SHIMIZUd97a9502015-08-18 10:02:30 -0700129 }
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -0700130
Sho SHIMIZUe60a5ab2015-08-20 11:51:49 -0700131 private void unregisterDeviceResource(Device device) {
Naoki Shiotabe394c82016-02-03 16:44:29 -0800132 executor.submit(() -> {
133 Resource devResource = Resources.discrete(device.id()).resource();
134 List<Resource> allResources = getDescendantResources(devResource);
135 adminService.unregisterResources(allResources);
136 });
Sho SHIMIZUe60a5ab2015-08-20 11:51:49 -0700137 }
138
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -0700139 private void registerPortResource(Device device, Port port) {
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800140 Resource portPath = Resources.discrete(device.id(), port.number()).resource();
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800141 executor.submit(() -> {
142 adminService.registerResources(portPath);
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200143
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800144 queryBandwidth(device.id(), port.number())
145 .map(bw -> portPath.child(Bandwidth.class, bw.bps()))
146 .map(adminService::registerResources)
147 .ifPresent(success -> {
148 if (!success) {
149 log.error("Failed to register Bandwidth for {}", portPath.id());
150 }
151 });
152
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800153 // for VLAN IDs
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800154 Set<VlanId> vlans = queryVlanIds(device.id(), port.number());
155 if (!vlans.isEmpty()) {
156 adminService.registerResources(vlans.stream()
157 .map(portPath::child)
158 .collect(Collectors.toList()));
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800159 }
160
161 // for MPLS labels
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800162 Set<MplsLabel> mplsLabels = queryMplsLabels(device.id(), port.number());
163 if (!mplsLabels.isEmpty()) {
164 adminService.registerResources(mplsLabels.stream()
165 .map(portPath::child)
166 .collect(Collectors.toList()));
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800167 }
168
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800169 // for Lambdas
Marc De Leenheer622861d2015-12-15 22:52:52 -0800170 Set<OchSignal> lambdas = queryLambdas(device.id(), port.number());
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800171 if (!lambdas.isEmpty()) {
172 adminService.registerResources(lambdas.stream()
173 .map(portPath::child)
174 .collect(Collectors.toList()));
175 }
176
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800177 // for Tributary slots
Toru Furusawac23f5832015-12-04 11:39:18 -0800178 Set<TributarySlot> tSlots = queryTributarySlots(device.id(), port.number());
179 if (!tSlots.isEmpty()) {
180 adminService.registerResources(tSlots.stream()
181 .map(portPath::child)
182 .collect(Collectors.toList()));
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800183 }
184 });
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200185 }
186
Sho SHIMIZUe2292842015-08-20 11:59:23 -0700187 private void unregisterPortResource(Device device, Port port) {
Naoki Shiotabe394c82016-02-03 16:44:29 -0800188 executor.submit(() -> {
189 Resource portResource = Resources.discrete(device.id(), port.number()).resource();
190 List<Resource> allResources = getDescendantResources(portResource);
191 adminService.unregisterResources(allResources);
192 });
193 }
194
195 // Returns list of all descendant resources of given resource, including itself.
196 private List<Resource> getDescendantResources(Resource parent) {
197 LinkedList<Resource> allResources = new LinkedList<>();
198 allResources.add(parent);
199
200 Set<Resource> nextResources = resourceService.getRegisteredResources(parent);
201 while (!nextResources.isEmpty()) {
202 Set<Resource> currentResources = nextResources;
203 // resource list should be ordered from leaf to root
204 allResources.addAll(0, currentResources);
205
206 nextResources = currentResources.stream()
207 .flatMap(r -> resourceService.getRegisteredResources(r).stream())
208 .collect(Collectors.toSet());
209 }
210
211 return allResources;
Sho SHIMIZUe2292842015-08-20 11:59:23 -0700212 }
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200213
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800214 /**
215 * Query bandwidth capacity on a port.
216 *
217 * @param did {@link DeviceId}
218 * @param number {@link PortNumber}
219 * @return bandwidth capacity
220 */
221 private Optional<Bandwidth> queryBandwidth(DeviceId did, PortNumber number) {
222 // Check and use netcfg first.
223 ConnectPoint cp = new ConnectPoint(did, number);
224 BandwidthCapacity config = netcfgService.getConfig(cp, BandwidthCapacity.class);
225 if (config != null) {
226 log.trace("Registering configured bandwidth {} for {}/{}", config.capacity(), did, number);
227 return Optional.of(config.capacity());
228 }
229
230 // populate bandwidth value, assuming portSpeed == bandwidth
231 Port port = deviceService.getPort(did, number);
232 if (port != null) {
233 return Optional.of(Bandwidth.mbps(port.portSpeed()));
234 }
235 return Optional.empty();
236 }
237
Marc De Leenheer622861d2015-12-15 22:52:52 -0800238 private Set<OchSignal> queryLambdas(DeviceId did, PortNumber port) {
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800239 try {
240 DriverHandler handler = driverService.createHandler(did);
HIGUCHI Yuta82b3c112016-01-07 22:22:26 -0800241 if (handler == null || !handler.hasBehaviour(LambdaQuery.class)) {
Marc De Leenheer622861d2015-12-15 22:52:52 -0800242 return Collections.emptySet();
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800243 }
244 LambdaQuery query = handler.behaviour(LambdaQuery.class);
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800245 if (query != null) {
Marc De Leenheer2c305302015-12-07 21:37:44 -0800246 return query.queryLambdas(port).stream()
247 .flatMap(x -> OchSignal.toFlexGrid(x).stream())
Marc De Leenheer622861d2015-12-15 22:52:52 -0800248 .collect(Collectors.toSet());
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800249 } else {
Marc De Leenheer622861d2015-12-15 22:52:52 -0800250 return Collections.emptySet();
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800251 }
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800252 } catch (ItemNotFoundException e) {
Marc De Leenheer622861d2015-12-15 22:52:52 -0800253 return Collections.emptySet();
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800254 }
255 }
256
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800257 private Set<VlanId> queryVlanIds(DeviceId device, PortNumber port) {
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800258 try {
259 DriverHandler handler = driverService.createHandler(device);
HIGUCHI Yuta82b3c112016-01-07 22:22:26 -0800260 if (handler == null || !handler.hasBehaviour(VlanQuery.class)) {
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800261 return ImmutableSet.of();
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800262 }
263
264 VlanQuery query = handler.behaviour(VlanQuery.class);
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800265 if (query == null) {
266 return ImmutableSet.of();
267 }
268 return query.queryVlanIds(port);
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800269 } catch (ItemNotFoundException e) {
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800270 return ImmutableSet.of();
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800271 }
272 }
273
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800274 private Set<MplsLabel> queryMplsLabels(DeviceId device, PortNumber port) {
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800275 try {
276 DriverHandler handler = driverService.createHandler(device);
HIGUCHI Yuta82b3c112016-01-07 22:22:26 -0800277 if (handler == null || !handler.hasBehaviour(MplsQuery.class)) {
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800278 return ImmutableSet.of();
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800279 }
280
281 MplsQuery query = handler.behaviour(MplsQuery.class);
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800282 if (query == null) {
283 return ImmutableSet.of();
284 }
285 return query.queryMplsLabels(port);
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800286 } catch (ItemNotFoundException e) {
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800287 return ImmutableSet.of();
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800288 }
289 }
290
Toru Furusawac23f5832015-12-04 11:39:18 -0800291 private Set<TributarySlot> queryTributarySlots(DeviceId device, PortNumber port) {
292 try {
Toru Furusawac23f5832015-12-04 11:39:18 -0800293 DriverHandler handler = driverService.createHandler(device);
HIGUCHI Yuta82b3c112016-01-07 22:22:26 -0800294 if (handler == null || !handler.hasBehaviour(TributarySlotQuery.class)) {
Toru Furusawac23f5832015-12-04 11:39:18 -0800295 return Collections.emptySet();
296 }
297 TributarySlotQuery query = handler.behaviour(TributarySlotQuery.class);
298 if (query != null) {
299 return query.queryTributarySlots(port);
300 } else {
301 return Collections.emptySet();
302 }
303 } catch (ItemNotFoundException e) {
304 return Collections.emptySet();
305 }
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200306 }
Sho SHIMIZUd97a9502015-08-18 10:02:30 -0700307}