blob: 2a85fbbbe2d35091f9fbb3ca7d0fe8e046e44e47 [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;
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020043import org.slf4j.Logger;
44import org.slf4j.LoggerFactory;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070045
HIGUCHI Yutad39e3762015-12-04 09:43:16 -080046import java.util.Collections;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080047import java.util.Optional;
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -080048import java.util.Set;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070049import java.util.concurrent.ExecutorService;
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020050import java.util.stream.Collectors;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070051
52import static com.google.common.base.Preconditions.checkNotNull;
53
54/**
55 * An implementation of DeviceListener registering devices as resources.
56 */
57final class ResourceDeviceListener implements DeviceListener {
58
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020059 private static final Logger log = LoggerFactory.getLogger(ResourceDeviceListener.class);
60
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070061 private final ResourceAdminService adminService;
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080062 private final DeviceService deviceService;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080063 private final DriverService driverService;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080064 private final NetworkConfigService netcfgService;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070065 private final ExecutorService executor;
66
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080067
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070068 /**
69 * Creates an instance with the specified ResourceAdminService and ExecutorService.
70 *
71 * @param adminService instance invoked to register resources
Jian Lidfba7392016-01-22 16:46:58 -080072 * @param deviceService {@link DeviceService} to be used
73 * @param driverService {@link DriverService} to be used
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080074 * @param netcfgService {@link NetworkConfigService} to be used.
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070075 * @param executor executor used for processing resource registration
76 */
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080077 ResourceDeviceListener(ResourceAdminService adminService, DeviceService deviceService, DriverService driverService,
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080078 NetworkConfigService netcfgService, ExecutorService executor) {
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070079 this.adminService = checkNotNull(adminService);
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080080 this.deviceService = checkNotNull(deviceService);
Sho SHIMIZU44f37612015-11-25 16:23:22 -080081 this.driverService = checkNotNull(driverService);
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080082 this.netcfgService = checkNotNull(netcfgService);
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070083 this.executor = checkNotNull(executor);
84 }
85
86 @Override
87 public void event(DeviceEvent event) {
88 Device device = event.subject();
89 switch (event.type()) {
90 case DEVICE_ADDED:
91 registerDeviceResource(device);
92 break;
Sho SHIMIZUe60a5ab2015-08-20 11:51:49 -070093 case DEVICE_REMOVED:
94 unregisterDeviceResource(device);
95 break;
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080096 case DEVICE_AVAILABILITY_CHANGED:
97 if (deviceService.isAvailable(device.id())) {
98 registerDeviceResource(device);
99 // TODO: do we need to walk the ports?
100 } else {
101 unregisterDeviceResource(device);
102 }
103 break;
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -0700104 case PORT_ADDED:
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800105 case PORT_UPDATED:
106 if (event.port().isEnabled()) {
107 registerPortResource(device, event.port());
108 } else {
109 unregisterPortResource(device, event.port());
110 }
Sho SHIMIZUc2ddedd2015-08-20 11:54:29 -0700111 break;
Sho SHIMIZUe2292842015-08-20 11:59:23 -0700112 case PORT_REMOVED:
113 unregisterPortResource(device, event.port());
114 break;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -0700115 default:
116 break;
117 }
118 }
119
120 private void registerDeviceResource(Device device) {
Sho SHIMIZUf95b96e2016-01-25 19:35:15 -0800121 executor.submit(() -> adminService.registerResources(Resource.discrete(device.id()).resource()));
Sho SHIMIZUd97a9502015-08-18 10:02:30 -0700122 }
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -0700123
Sho SHIMIZUe60a5ab2015-08-20 11:51:49 -0700124 private void unregisterDeviceResource(Device device) {
Sho SHIMIZUf95b96e2016-01-25 19:35:15 -0800125 executor.submit(() -> adminService.unregisterResources(Resource.discrete(device.id()).resource()));
Sho SHIMIZUe60a5ab2015-08-20 11:51:49 -0700126 }
127
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -0700128 private void registerPortResource(Device device, Port port) {
Sho SHIMIZUf95b96e2016-01-25 19:35:15 -0800129 Resource portPath = Resource.discrete(device.id(), port.number()).resource();
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800130 executor.submit(() -> {
131 adminService.registerResources(portPath);
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200132
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800133 queryBandwidth(device.id(), port.number())
134 .map(bw -> portPath.child(Bandwidth.class, bw.bps()))
135 .map(adminService::registerResources)
136 .ifPresent(success -> {
137 if (!success) {
138 log.error("Failed to register Bandwidth for {}", portPath.id());
139 }
140 });
141
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800142 // for VLAN IDs
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800143 Set<VlanId> vlans = queryVlanIds(device.id(), port.number());
144 if (!vlans.isEmpty()) {
145 adminService.registerResources(vlans.stream()
146 .map(portPath::child)
147 .collect(Collectors.toList()));
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800148 }
149
150 // for MPLS labels
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800151 Set<MplsLabel> mplsLabels = queryMplsLabels(device.id(), port.number());
152 if (!mplsLabels.isEmpty()) {
153 adminService.registerResources(mplsLabels.stream()
154 .map(portPath::child)
155 .collect(Collectors.toList()));
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800156 }
157
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800158 // for Lambdas
Marc De Leenheer622861d2015-12-15 22:52:52 -0800159 Set<OchSignal> lambdas = queryLambdas(device.id(), port.number());
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800160 if (!lambdas.isEmpty()) {
161 adminService.registerResources(lambdas.stream()
162 .map(portPath::child)
163 .collect(Collectors.toList()));
164 }
165
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800166 // for Tributary slots
Toru Furusawac23f5832015-12-04 11:39:18 -0800167 Set<TributarySlot> tSlots = queryTributarySlots(device.id(), port.number());
168 if (!tSlots.isEmpty()) {
169 adminService.registerResources(tSlots.stream()
170 .map(portPath::child)
171 .collect(Collectors.toList()));
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800172 }
173 });
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200174 }
175
Sho SHIMIZUe2292842015-08-20 11:59:23 -0700176 private void unregisterPortResource(Device device, Port port) {
Sho SHIMIZUf95b96e2016-01-25 19:35:15 -0800177 Resource resource = Resource.discrete(device.id(), port.number()).resource();
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800178 executor.submit(() -> adminService.unregisterResources(resource));
Sho SHIMIZUe2292842015-08-20 11:59:23 -0700179 }
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200180
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800181 /**
182 * Query bandwidth capacity on a port.
183 *
184 * @param did {@link DeviceId}
185 * @param number {@link PortNumber}
186 * @return bandwidth capacity
187 */
188 private Optional<Bandwidth> queryBandwidth(DeviceId did, PortNumber number) {
189 // Check and use netcfg first.
190 ConnectPoint cp = new ConnectPoint(did, number);
191 BandwidthCapacity config = netcfgService.getConfig(cp, BandwidthCapacity.class);
192 if (config != null) {
193 log.trace("Registering configured bandwidth {} for {}/{}", config.capacity(), did, number);
194 return Optional.of(config.capacity());
195 }
196
197 // populate bandwidth value, assuming portSpeed == bandwidth
198 Port port = deviceService.getPort(did, number);
199 if (port != null) {
200 return Optional.of(Bandwidth.mbps(port.portSpeed()));
201 }
202 return Optional.empty();
203 }
204
Marc De Leenheer622861d2015-12-15 22:52:52 -0800205 private Set<OchSignal> queryLambdas(DeviceId did, PortNumber port) {
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800206 try {
207 DriverHandler handler = driverService.createHandler(did);
HIGUCHI Yuta82b3c112016-01-07 22:22:26 -0800208 if (handler == null || !handler.hasBehaviour(LambdaQuery.class)) {
Marc De Leenheer622861d2015-12-15 22:52:52 -0800209 return Collections.emptySet();
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800210 }
211 LambdaQuery query = handler.behaviour(LambdaQuery.class);
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800212 if (query != null) {
Marc De Leenheer2c305302015-12-07 21:37:44 -0800213 return query.queryLambdas(port).stream()
214 .flatMap(x -> OchSignal.toFlexGrid(x).stream())
Marc De Leenheer622861d2015-12-15 22:52:52 -0800215 .collect(Collectors.toSet());
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800216 } else {
Marc De Leenheer622861d2015-12-15 22:52:52 -0800217 return Collections.emptySet();
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800218 }
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800219 } catch (ItemNotFoundException e) {
Marc De Leenheer622861d2015-12-15 22:52:52 -0800220 return Collections.emptySet();
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800221 }
222 }
223
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800224 private Set<VlanId> queryVlanIds(DeviceId device, PortNumber port) {
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800225 try {
226 DriverHandler handler = driverService.createHandler(device);
HIGUCHI Yuta82b3c112016-01-07 22:22:26 -0800227 if (handler == null || !handler.hasBehaviour(VlanQuery.class)) {
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800228 return ImmutableSet.of();
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800229 }
230
231 VlanQuery query = handler.behaviour(VlanQuery.class);
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800232 if (query == null) {
233 return ImmutableSet.of();
234 }
235 return query.queryVlanIds(port);
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800236 } catch (ItemNotFoundException e) {
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800237 return ImmutableSet.of();
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800238 }
239 }
240
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800241 private Set<MplsLabel> queryMplsLabels(DeviceId device, PortNumber port) {
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800242 try {
243 DriverHandler handler = driverService.createHandler(device);
HIGUCHI Yuta82b3c112016-01-07 22:22:26 -0800244 if (handler == null || !handler.hasBehaviour(MplsQuery.class)) {
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800245 return ImmutableSet.of();
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800246 }
247
248 MplsQuery query = handler.behaviour(MplsQuery.class);
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800249 if (query == null) {
250 return ImmutableSet.of();
251 }
252 return query.queryMplsLabels(port);
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800253 } catch (ItemNotFoundException e) {
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800254 return ImmutableSet.of();
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800255 }
256 }
257
Toru Furusawac23f5832015-12-04 11:39:18 -0800258 private Set<TributarySlot> queryTributarySlots(DeviceId device, PortNumber port) {
259 try {
Toru Furusawac23f5832015-12-04 11:39:18 -0800260 DriverHandler handler = driverService.createHandler(device);
HIGUCHI Yuta82b3c112016-01-07 22:22:26 -0800261 if (handler == null || !handler.hasBehaviour(TributarySlotQuery.class)) {
Toru Furusawac23f5832015-12-04 11:39:18 -0800262 return Collections.emptySet();
263 }
264 TributarySlotQuery query = handler.behaviour(TributarySlotQuery.class);
265 if (query != null) {
266 return query.queryTributarySlots(port);
267 } else {
268 return Collections.emptySet();
269 }
270 } catch (ItemNotFoundException e) {
271 return Collections.emptySet();
272 }
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200273 }
Sho SHIMIZUd97a9502015-08-18 10:02:30 -0700274}