blob: 64691d5e213f0d2aee49738113ecefb02c218319 [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;
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020044import org.slf4j.Logger;
45import org.slf4j.LoggerFactory;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070046
HIGUCHI Yutad39e3762015-12-04 09:43:16 -080047import java.util.Collections;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080048import java.util.Optional;
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -080049import java.util.Set;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070050import java.util.concurrent.ExecutorService;
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020051import java.util.stream.Collectors;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070052
53import static com.google.common.base.Preconditions.checkNotNull;
54
55/**
56 * An implementation of DeviceListener registering devices as resources.
57 */
58final class ResourceDeviceListener implements DeviceListener {
59
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020060 private static final Logger log = LoggerFactory.getLogger(ResourceDeviceListener.class);
61
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070062 private final ResourceAdminService adminService;
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080063 private final DeviceService deviceService;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080064 private final DriverService driverService;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080065 private final NetworkConfigService netcfgService;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070066 private final ExecutorService executor;
67
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080068
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070069 /**
70 * Creates an instance with the specified ResourceAdminService and ExecutorService.
71 *
72 * @param adminService instance invoked to register resources
Jian Lidfba7392016-01-22 16:46:58 -080073 * @param deviceService {@link DeviceService} to be used
74 * @param driverService {@link DriverService} to be used
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080075 * @param netcfgService {@link NetworkConfigService} to be used.
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070076 * @param executor executor used for processing resource registration
77 */
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080078 ResourceDeviceListener(ResourceAdminService adminService, DeviceService deviceService, DriverService driverService,
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080079 NetworkConfigService netcfgService, ExecutorService executor) {
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070080 this.adminService = checkNotNull(adminService);
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080081 this.deviceService = checkNotNull(deviceService);
Sho SHIMIZU44f37612015-11-25 16:23:22 -080082 this.driverService = checkNotNull(driverService);
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080083 this.netcfgService = checkNotNull(netcfgService);
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070084 this.executor = checkNotNull(executor);
85 }
86
87 @Override
88 public void event(DeviceEvent event) {
89 Device device = event.subject();
90 switch (event.type()) {
91 case DEVICE_ADDED:
92 registerDeviceResource(device);
93 break;
Sho SHIMIZUe60a5ab2015-08-20 11:51:49 -070094 case DEVICE_REMOVED:
95 unregisterDeviceResource(device);
96 break;
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080097 case DEVICE_AVAILABILITY_CHANGED:
98 if (deviceService.isAvailable(device.id())) {
99 registerDeviceResource(device);
100 // TODO: do we need to walk the ports?
101 } else {
102 unregisterDeviceResource(device);
103 }
104 break;
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -0700105 case PORT_ADDED:
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800106 case PORT_UPDATED:
107 if (event.port().isEnabled()) {
108 registerPortResource(device, event.port());
109 } else {
110 unregisterPortResource(device, event.port());
111 }
Sho SHIMIZUc2ddedd2015-08-20 11:54:29 -0700112 break;
Sho SHIMIZUe2292842015-08-20 11:59:23 -0700113 case PORT_REMOVED:
114 unregisterPortResource(device, event.port());
115 break;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -0700116 default:
117 break;
118 }
119 }
120
121 private void registerDeviceResource(Device device) {
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800122 executor.submit(() -> adminService.registerResources(Resources.discrete(device.id()).resource()));
Sho SHIMIZUd97a9502015-08-18 10:02:30 -0700123 }
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -0700124
Sho SHIMIZUe60a5ab2015-08-20 11:51:49 -0700125 private void unregisterDeviceResource(Device device) {
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800126 executor.submit(() -> adminService.unregisterResources(Resources.discrete(device.id()).resource()));
Sho SHIMIZUe60a5ab2015-08-20 11:51:49 -0700127 }
128
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -0700129 private void registerPortResource(Device device, Port port) {
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800130 Resource portPath = Resources.discrete(device.id(), port.number()).resource();
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800131 executor.submit(() -> {
132 adminService.registerResources(portPath);
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200133
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800134 queryBandwidth(device.id(), port.number())
135 .map(bw -> portPath.child(Bandwidth.class, bw.bps()))
136 .map(adminService::registerResources)
137 .ifPresent(success -> {
138 if (!success) {
139 log.error("Failed to register Bandwidth for {}", portPath.id());
140 }
141 });
142
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800143 // for VLAN IDs
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800144 Set<VlanId> vlans = queryVlanIds(device.id(), port.number());
145 if (!vlans.isEmpty()) {
146 adminService.registerResources(vlans.stream()
147 .map(portPath::child)
148 .collect(Collectors.toList()));
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800149 }
150
151 // for MPLS labels
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800152 Set<MplsLabel> mplsLabels = queryMplsLabels(device.id(), port.number());
153 if (!mplsLabels.isEmpty()) {
154 adminService.registerResources(mplsLabels.stream()
155 .map(portPath::child)
156 .collect(Collectors.toList()));
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800157 }
158
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800159 // for Lambdas
Marc De Leenheer622861d2015-12-15 22:52:52 -0800160 Set<OchSignal> lambdas = queryLambdas(device.id(), port.number());
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800161 if (!lambdas.isEmpty()) {
162 adminService.registerResources(lambdas.stream()
163 .map(portPath::child)
164 .collect(Collectors.toList()));
165 }
166
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800167 // for Tributary slots
Toru Furusawac23f5832015-12-04 11:39:18 -0800168 Set<TributarySlot> tSlots = queryTributarySlots(device.id(), port.number());
169 if (!tSlots.isEmpty()) {
170 adminService.registerResources(tSlots.stream()
171 .map(portPath::child)
172 .collect(Collectors.toList()));
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800173 }
174 });
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200175 }
176
Sho SHIMIZUe2292842015-08-20 11:59:23 -0700177 private void unregisterPortResource(Device device, Port port) {
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800178 Resource resource = Resources.discrete(device.id(), port.number()).resource();
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800179 executor.submit(() -> adminService.unregisterResources(resource));
Sho SHIMIZUe2292842015-08-20 11:59:23 -0700180 }
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200181
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800182 /**
183 * Query bandwidth capacity on a port.
184 *
185 * @param did {@link DeviceId}
186 * @param number {@link PortNumber}
187 * @return bandwidth capacity
188 */
189 private Optional<Bandwidth> queryBandwidth(DeviceId did, PortNumber number) {
190 // Check and use netcfg first.
191 ConnectPoint cp = new ConnectPoint(did, number);
192 BandwidthCapacity config = netcfgService.getConfig(cp, BandwidthCapacity.class);
193 if (config != null) {
194 log.trace("Registering configured bandwidth {} for {}/{}", config.capacity(), did, number);
195 return Optional.of(config.capacity());
196 }
197
198 // populate bandwidth value, assuming portSpeed == bandwidth
199 Port port = deviceService.getPort(did, number);
200 if (port != null) {
201 return Optional.of(Bandwidth.mbps(port.portSpeed()));
202 }
203 return Optional.empty();
204 }
205
Marc De Leenheer622861d2015-12-15 22:52:52 -0800206 private Set<OchSignal> queryLambdas(DeviceId did, PortNumber port) {
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800207 try {
208 DriverHandler handler = driverService.createHandler(did);
HIGUCHI Yuta82b3c112016-01-07 22:22:26 -0800209 if (handler == null || !handler.hasBehaviour(LambdaQuery.class)) {
Marc De Leenheer622861d2015-12-15 22:52:52 -0800210 return Collections.emptySet();
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800211 }
212 LambdaQuery query = handler.behaviour(LambdaQuery.class);
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800213 if (query != null) {
Marc De Leenheer2c305302015-12-07 21:37:44 -0800214 return query.queryLambdas(port).stream()
215 .flatMap(x -> OchSignal.toFlexGrid(x).stream())
Marc De Leenheer622861d2015-12-15 22:52:52 -0800216 .collect(Collectors.toSet());
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800217 } else {
Marc De Leenheer622861d2015-12-15 22:52:52 -0800218 return Collections.emptySet();
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800219 }
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800220 } catch (ItemNotFoundException e) {
Marc De Leenheer622861d2015-12-15 22:52:52 -0800221 return Collections.emptySet();
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800222 }
223 }
224
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800225 private Set<VlanId> queryVlanIds(DeviceId device, PortNumber port) {
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800226 try {
227 DriverHandler handler = driverService.createHandler(device);
HIGUCHI Yuta82b3c112016-01-07 22:22:26 -0800228 if (handler == null || !handler.hasBehaviour(VlanQuery.class)) {
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800229 return ImmutableSet.of();
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800230 }
231
232 VlanQuery query = handler.behaviour(VlanQuery.class);
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800233 if (query == null) {
234 return ImmutableSet.of();
235 }
236 return query.queryVlanIds(port);
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800237 } catch (ItemNotFoundException e) {
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800238 return ImmutableSet.of();
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800239 }
240 }
241
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800242 private Set<MplsLabel> queryMplsLabels(DeviceId device, PortNumber port) {
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800243 try {
244 DriverHandler handler = driverService.createHandler(device);
HIGUCHI Yuta82b3c112016-01-07 22:22:26 -0800245 if (handler == null || !handler.hasBehaviour(MplsQuery.class)) {
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800246 return ImmutableSet.of();
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800247 }
248
249 MplsQuery query = handler.behaviour(MplsQuery.class);
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800250 if (query == null) {
251 return ImmutableSet.of();
252 }
253 return query.queryMplsLabels(port);
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800254 } catch (ItemNotFoundException e) {
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800255 return ImmutableSet.of();
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800256 }
257 }
258
Toru Furusawac23f5832015-12-04 11:39:18 -0800259 private Set<TributarySlot> queryTributarySlots(DeviceId device, PortNumber port) {
260 try {
Toru Furusawac23f5832015-12-04 11:39:18 -0800261 DriverHandler handler = driverService.createHandler(device);
HIGUCHI Yuta82b3c112016-01-07 22:22:26 -0800262 if (handler == null || !handler.hasBehaviour(TributarySlotQuery.class)) {
Toru Furusawac23f5832015-12-04 11:39:18 -0800263 return Collections.emptySet();
264 }
265 TributarySlotQuery query = handler.behaviour(TributarySlotQuery.class);
266 if (query != null) {
267 return query.queryTributarySlots(port);
268 } else {
269 return Collections.emptySet();
270 }
271 } catch (ItemNotFoundException e) {
272 return Collections.emptySet();
273 }
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200274 }
Sho SHIMIZUd97a9502015-08-18 10:02:30 -0700275}