blob: 0b793a9c21057c4f1b31a76d92e31e2d900e07c2 [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;
21import org.onlab.util.ItemNotFoundException;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070022import org.onosproject.net.Device;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080023import org.onosproject.net.DeviceId;
HIGUCHI Yutad39e3762015-12-04 09:43:16 -080024import org.onosproject.net.OchSignal;
Toru Furusawac23f5832015-12-04 11:39:18 -080025import org.onosproject.net.Port;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080026import org.onosproject.net.PortNumber;
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020027import org.onosproject.net.TributarySlot;
HIGUCHI Yutad39e3762015-12-04 09:43:16 -080028import org.onosproject.net.behaviour.LambdaQuery;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080029import org.onosproject.net.behaviour.MplsQuery;
Toru Furusawac23f5832015-12-04 11:39:18 -080030import org.onosproject.net.behaviour.TributarySlotQuery;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080031import org.onosproject.net.behaviour.VlanQuery;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070032import org.onosproject.net.device.DeviceEvent;
33import org.onosproject.net.device.DeviceListener;
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080034import org.onosproject.net.device.DeviceService;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080035import org.onosproject.net.driver.DriverHandler;
36import org.onosproject.net.driver.DriverService;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070037import org.onosproject.net.newresource.ResourceAdminService;
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080038import org.onosproject.net.newresource.Resource;
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020039import org.slf4j.Logger;
40import org.slf4j.LoggerFactory;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070041
HIGUCHI Yutad39e3762015-12-04 09:43:16 -080042import java.util.Collections;
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -080043import java.util.Set;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070044import java.util.concurrent.ExecutorService;
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020045import java.util.stream.Collectors;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070046
47import static com.google.common.base.Preconditions.checkNotNull;
48
49/**
50 * An implementation of DeviceListener registering devices as resources.
51 */
52final class ResourceDeviceListener implements DeviceListener {
53
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020054 private static final Logger log = LoggerFactory.getLogger(ResourceDeviceListener.class);
55
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070056 private final ResourceAdminService adminService;
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080057 private final DeviceService deviceService;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080058 private final DriverService driverService;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070059 private final ExecutorService executor;
60
61 /**
62 * Creates an instance with the specified ResourceAdminService and ExecutorService.
63 *
64 * @param adminService instance invoked to register resources
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080065 * @param deviceService {@link DeviceService} to be used.
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070066 * @param executor executor used for processing resource registration
67 */
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080068 ResourceDeviceListener(ResourceAdminService adminService, DeviceService deviceService, DriverService driverService,
Sho SHIMIZU44f37612015-11-25 16:23:22 -080069 ExecutorService executor) {
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070070 this.adminService = checkNotNull(adminService);
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080071 this.deviceService = checkNotNull(deviceService);
Sho SHIMIZU44f37612015-11-25 16:23:22 -080072 this.driverService = checkNotNull(driverService);
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070073 this.executor = checkNotNull(executor);
74 }
75
76 @Override
77 public void event(DeviceEvent event) {
78 Device device = event.subject();
79 switch (event.type()) {
80 case DEVICE_ADDED:
81 registerDeviceResource(device);
82 break;
Sho SHIMIZUe60a5ab2015-08-20 11:51:49 -070083 case DEVICE_REMOVED:
84 unregisterDeviceResource(device);
85 break;
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080086 case DEVICE_AVAILABILITY_CHANGED:
87 if (deviceService.isAvailable(device.id())) {
88 registerDeviceResource(device);
89 // TODO: do we need to walk the ports?
90 } else {
91 unregisterDeviceResource(device);
92 }
93 break;
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -070094 case PORT_ADDED:
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080095 case PORT_UPDATED:
96 if (event.port().isEnabled()) {
97 registerPortResource(device, event.port());
98 } else {
99 unregisterPortResource(device, event.port());
100 }
Sho SHIMIZUc2ddedd2015-08-20 11:54:29 -0700101 break;
Sho SHIMIZUe2292842015-08-20 11:59:23 -0700102 case PORT_REMOVED:
103 unregisterPortResource(device, event.port());
104 break;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -0700105 default:
106 break;
107 }
108 }
109
110 private void registerDeviceResource(Device device) {
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800111 executor.submit(() -> adminService.registerResources(Resource.discrete(device.id())));
Sho SHIMIZUd97a9502015-08-18 10:02:30 -0700112 }
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -0700113
Sho SHIMIZUe60a5ab2015-08-20 11:51:49 -0700114 private void unregisterDeviceResource(Device device) {
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800115 executor.submit(() -> adminService.unregisterResources(Resource.discrete(device.id())));
Sho SHIMIZUe60a5ab2015-08-20 11:51:49 -0700116 }
117
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -0700118 private void registerPortResource(Device device, Port port) {
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800119 Resource portPath = Resource.discrete(device.id(), port.number());
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800120 executor.submit(() -> {
121 adminService.registerResources(portPath);
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200122
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800123 // for VLAN IDs
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800124 Set<VlanId> vlans = queryVlanIds(device.id(), port.number());
125 if (!vlans.isEmpty()) {
126 adminService.registerResources(vlans.stream()
127 .map(portPath::child)
128 .collect(Collectors.toList()));
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800129 }
130
131 // for MPLS labels
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800132 Set<MplsLabel> mplsLabels = queryMplsLabels(device.id(), port.number());
133 if (!mplsLabels.isEmpty()) {
134 adminService.registerResources(mplsLabels.stream()
135 .map(portPath::child)
136 .collect(Collectors.toList()));
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800137 }
138
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800139 // for Lambdas
Marc De Leenheer622861d2015-12-15 22:52:52 -0800140 Set<OchSignal> lambdas = queryLambdas(device.id(), port.number());
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800141 if (!lambdas.isEmpty()) {
142 adminService.registerResources(lambdas.stream()
143 .map(portPath::child)
144 .collect(Collectors.toList()));
145 }
146
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800147 // for Tributary slots
Toru Furusawac23f5832015-12-04 11:39:18 -0800148 Set<TributarySlot> tSlots = queryTributarySlots(device.id(), port.number());
149 if (!tSlots.isEmpty()) {
150 adminService.registerResources(tSlots.stream()
151 .map(portPath::child)
152 .collect(Collectors.toList()));
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800153 }
154 });
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200155 }
156
Sho SHIMIZUe2292842015-08-20 11:59:23 -0700157 private void unregisterPortResource(Device device, Port port) {
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800158 Resource resource = Resource.discrete(device.id(), port.number());
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800159 executor.submit(() -> adminService.unregisterResources(resource));
Sho SHIMIZUe2292842015-08-20 11:59:23 -0700160 }
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200161
Marc De Leenheer622861d2015-12-15 22:52:52 -0800162 private Set<OchSignal> queryLambdas(DeviceId did, PortNumber port) {
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800163 try {
164 DriverHandler handler = driverService.createHandler(did);
HIGUCHI Yuta82b3c112016-01-07 22:22:26 -0800165 if (handler == null || !handler.hasBehaviour(LambdaQuery.class)) {
Marc De Leenheer622861d2015-12-15 22:52:52 -0800166 return Collections.emptySet();
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800167 }
168 LambdaQuery query = handler.behaviour(LambdaQuery.class);
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800169 if (query != null) {
Marc De Leenheer2c305302015-12-07 21:37:44 -0800170 return query.queryLambdas(port).stream()
171 .flatMap(x -> OchSignal.toFlexGrid(x).stream())
Marc De Leenheer622861d2015-12-15 22:52:52 -0800172 .collect(Collectors.toSet());
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800173 } else {
Marc De Leenheer622861d2015-12-15 22:52:52 -0800174 return Collections.emptySet();
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800175 }
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800176 } catch (ItemNotFoundException e) {
Marc De Leenheer622861d2015-12-15 22:52:52 -0800177 return Collections.emptySet();
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800178 }
179 }
180
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800181 private Set<VlanId> queryVlanIds(DeviceId device, PortNumber port) {
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800182 try {
183 DriverHandler handler = driverService.createHandler(device);
HIGUCHI Yuta82b3c112016-01-07 22:22:26 -0800184 if (handler == null || !handler.hasBehaviour(VlanQuery.class)) {
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800185 return ImmutableSet.of();
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800186 }
187
188 VlanQuery query = handler.behaviour(VlanQuery.class);
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800189 if (query == null) {
190 return ImmutableSet.of();
191 }
192 return query.queryVlanIds(port);
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800193 } catch (ItemNotFoundException e) {
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800194 return ImmutableSet.of();
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800195 }
196 }
197
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800198 private Set<MplsLabel> queryMplsLabels(DeviceId device, PortNumber port) {
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800199 try {
200 DriverHandler handler = driverService.createHandler(device);
HIGUCHI Yuta82b3c112016-01-07 22:22:26 -0800201 if (handler == null || !handler.hasBehaviour(MplsQuery.class)) {
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800202 return ImmutableSet.of();
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800203 }
204
205 MplsQuery query = handler.behaviour(MplsQuery.class);
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800206 if (query == null) {
207 return ImmutableSet.of();
208 }
209 return query.queryMplsLabels(port);
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800210 } catch (ItemNotFoundException e) {
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800211 return ImmutableSet.of();
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800212 }
213 }
214
Toru Furusawac23f5832015-12-04 11:39:18 -0800215 private Set<TributarySlot> queryTributarySlots(DeviceId device, PortNumber port) {
216 try {
Toru Furusawac23f5832015-12-04 11:39:18 -0800217 DriverHandler handler = driverService.createHandler(device);
HIGUCHI Yuta82b3c112016-01-07 22:22:26 -0800218 if (handler == null || !handler.hasBehaviour(TributarySlotQuery.class)) {
Toru Furusawac23f5832015-12-04 11:39:18 -0800219 return Collections.emptySet();
220 }
221 TributarySlotQuery query = handler.behaviour(TributarySlotQuery.class);
222 if (query != null) {
223 return query.queryTributarySlots(port);
224 } else {
225 return Collections.emptySet();
226 }
227 } catch (ItemNotFoundException e) {
228 return Collections.emptySet();
229 }
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200230 }
Sho SHIMIZUd97a9502015-08-18 10:02:30 -0700231}