blob: d1f6aab897a5fa5b44d62a6924429301afb82b51 [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 SHIMIZU2c7cecf2015-11-11 14:16:14 -080019import com.google.common.collect.Lists;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080020import org.onlab.packet.MplsLabel;
21import org.onlab.packet.VlanId;
22import org.onlab.util.ItemNotFoundException;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070023import org.onosproject.net.Device;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080024import org.onosproject.net.DeviceId;
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -070025import org.onosproject.net.Port;
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020026import org.onosproject.net.OchPort;
HIGUCHI Yutad39e3762015-12-04 09:43:16 -080027import org.onosproject.net.OchSignal;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080028import org.onosproject.net.PortNumber;
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020029import org.onosproject.net.TributarySlot;
30import org.onosproject.net.OduSignalType;
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;
33import org.onosproject.net.behaviour.VlanQuery;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070034import org.onosproject.net.device.DeviceEvent;
35import org.onosproject.net.device.DeviceListener;
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080036import org.onosproject.net.device.DeviceService;
37import org.onosproject.net.driver.Driver;
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;
41import org.onosproject.net.newresource.ResourcePath;
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020042import org.slf4j.Logger;
43import org.slf4j.LoggerFactory;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070044
HIGUCHI Yutad39e3762015-12-04 09:43:16 -080045import java.util.Collections;
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020046import java.util.List;
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -080047import java.util.Set;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070048import java.util.concurrent.ExecutorService;
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020049import java.util.stream.Collectors;
50import java.util.stream.IntStream;
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
61 private static final int TOTAL_ODU2_TRIBUTARY_SLOTS = 8;
62 private static final int TOTAL_ODU4_TRIBUTARY_SLOTS = 80;
63 private static final List<TributarySlot> ENTIRE_ODU2_TRIBUTARY_SLOTS = getEntireOdu2TributarySlots();
64 private static final List<TributarySlot> ENTIRE_ODU4_TRIBUTARY_SLOTS = getEntireOdu4TributarySlots();
65
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070066 private final ResourceAdminService adminService;
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080067 private final DeviceService deviceService;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080068 private final DriverService driverService;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070069 private final ExecutorService executor;
70
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080071
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070072 /**
73 * Creates an instance with the specified ResourceAdminService and ExecutorService.
74 *
75 * @param adminService instance invoked to register resources
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080076 * @param deviceService {@link DeviceService} to be used.
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070077 * @param executor executor used for processing resource registration
78 */
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080079 ResourceDeviceListener(ResourceAdminService adminService, DeviceService deviceService, DriverService driverService,
Sho SHIMIZU44f37612015-11-25 16:23:22 -080080 ExecutorService executor) {
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070081 this.adminService = checkNotNull(adminService);
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080082 this.deviceService = checkNotNull(deviceService);
Sho SHIMIZU44f37612015-11-25 16:23:22 -080083 this.driverService = checkNotNull(driverService);
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 SHIMIZU2c7cecf2015-11-11 14:16:14 -0800122 executor.submit(() -> adminService.registerResources(ResourcePath.discrete(device.id())));
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 SHIMIZU2c7cecf2015-11-11 14:16:14 -0800126 executor.submit(() -> adminService.unregisterResources(ResourcePath.discrete(device.id())));
Sho SHIMIZUe60a5ab2015-08-20 11:51:49 -0700127 }
128
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -0700129 private void registerPortResource(Device device, Port port) {
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200130 ResourcePath portPath = ResourcePath.discrete(device.id(), port.number());
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800131 executor.submit(() -> {
132 adminService.registerResources(portPath);
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200133
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800134 // for VLAN IDs
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800135 Set<VlanId> vlans = queryVlanIds(device.id(), port.number());
136 if (!vlans.isEmpty()) {
137 adminService.registerResources(vlans.stream()
138 .map(portPath::child)
139 .collect(Collectors.toList()));
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800140 }
141
142 // for MPLS labels
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800143 Set<MplsLabel> mplsLabels = queryMplsLabels(device.id(), port.number());
144 if (!mplsLabels.isEmpty()) {
145 adminService.registerResources(mplsLabels.stream()
146 .map(portPath::child)
147 .collect(Collectors.toList()));
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800148 }
149
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800150 // for Lambdas
Marc De Leenheer622861d2015-12-15 22:52:52 -0800151 Set<OchSignal> lambdas = queryLambdas(device.id(), port.number());
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800152 if (!lambdas.isEmpty()) {
153 adminService.registerResources(lambdas.stream()
154 .map(portPath::child)
155 .collect(Collectors.toList()));
156 }
157
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800158 // for Tributary slots
159 // TODO: need to define Behaviour to make a query about OCh port
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800160 switch (port.type()) {
161 case OCH:
162 // register ODU TributarySlots against the OCH port
163 registerTributarySlotsResources(((OchPort) port).signalType(), portPath);
164 break;
165 default:
166 break;
167 }
168 });
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200169 }
170
171 private void registerTributarySlotsResources(OduSignalType oduSignalType, ResourcePath portPath) {
172 switch (oduSignalType) {
173 case ODU2:
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800174 adminService.registerResources(Lists.transform(ENTIRE_ODU2_TRIBUTARY_SLOTS, portPath::child));
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200175 break;
176 case ODU4:
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800177 adminService.registerResources(Lists.transform(ENTIRE_ODU4_TRIBUTARY_SLOTS, portPath::child));
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200178 break;
179 default:
180 break;
181 }
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -0700182 }
Sho SHIMIZUe2292842015-08-20 11:59:23 -0700183
184 private void unregisterPortResource(Device device, Port port) {
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800185 ResourcePath resource = ResourcePath.discrete(device.id(), port.number());
186 executor.submit(() -> adminService.unregisterResources(resource));
Sho SHIMIZUe2292842015-08-20 11:59:23 -0700187 }
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200188
Marc De Leenheer622861d2015-12-15 22:52:52 -0800189 private Set<OchSignal> queryLambdas(DeviceId did, PortNumber port) {
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800190 try {
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800191 // DriverHandler does not provide a way to check if a
192 // behaviour is supported.
193 Driver driver = driverService.getDriver(did);
194 if (driver == null || !driver.hasBehaviour(LambdaQuery.class)) {
Marc De Leenheer622861d2015-12-15 22:52:52 -0800195 return Collections.emptySet();
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800196 }
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800197 DriverHandler handler = driverService.createHandler(did);
198 if (handler == null) {
Marc De Leenheer622861d2015-12-15 22:52:52 -0800199 return Collections.emptySet();
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800200 }
201 LambdaQuery query = handler.behaviour(LambdaQuery.class);
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800202 if (query != null) {
Marc De Leenheer2c305302015-12-07 21:37:44 -0800203 return query.queryLambdas(port).stream()
204 .flatMap(x -> OchSignal.toFlexGrid(x).stream())
Marc De Leenheer622861d2015-12-15 22:52:52 -0800205 .collect(Collectors.toSet());
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800206 } else {
Marc De Leenheer622861d2015-12-15 22:52:52 -0800207 return Collections.emptySet();
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800208 }
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800209 } catch (ItemNotFoundException e) {
Marc De Leenheer622861d2015-12-15 22:52:52 -0800210 return Collections.emptySet();
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800211 }
212 }
213
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800214 private Set<VlanId> queryVlanIds(DeviceId device, PortNumber port) {
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800215 try {
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800216 // DriverHandler does not provide a way to check if a
217 // behaviour is supported.
218 Driver driver = driverService.getDriver(device);
219 if (driver == null || !driver.hasBehaviour(VlanQuery.class)) {
220 // device does not support this
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800221 return ImmutableSet.of();
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800222 }
223
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800224 DriverHandler handler = driverService.createHandler(device);
225 if (handler == null) {
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800226 return ImmutableSet.of();
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800227 }
228
229 VlanQuery query = handler.behaviour(VlanQuery.class);
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800230 if (query == null) {
231 return ImmutableSet.of();
232 }
233 return query.queryVlanIds(port);
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800234 } catch (ItemNotFoundException e) {
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800235 return ImmutableSet.of();
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800236 }
237 }
238
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800239 private Set<MplsLabel> queryMplsLabels(DeviceId device, PortNumber port) {
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800240 try {
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800241 // DriverHandler does not provide a way to check if a
242 // behaviour is supported.
243 Driver driver = driverService.getDriver(device);
244 if (driver == null || !driver.hasBehaviour(MplsQuery.class)) {
245 // device does not support this
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800246 return ImmutableSet.of();
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800247 }
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800248 DriverHandler handler = driverService.createHandler(device);
249 if (handler == null) {
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800250 return ImmutableSet.of();
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800251 }
252
253 MplsQuery query = handler.behaviour(MplsQuery.class);
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800254 if (query == null) {
255 return ImmutableSet.of();
256 }
257 return query.queryMplsLabels(port);
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800258 } catch (ItemNotFoundException e) {
HIGUCHI Yutab7a15d72015-12-15 09:54:40 -0800259 return ImmutableSet.of();
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800260 }
261 }
262
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200263 private static List<TributarySlot> getEntireOdu2TributarySlots() {
264 return IntStream.rangeClosed(1, TOTAL_ODU2_TRIBUTARY_SLOTS)
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800265 .mapToObj(TributarySlot::of)
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200266 .collect(Collectors.toList());
267 }
268 private static List<TributarySlot> getEntireOdu4TributarySlots() {
269 return IntStream.rangeClosed(1, TOTAL_ODU4_TRIBUTARY_SLOTS)
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800270 .mapToObj(TributarySlot::of)
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200271 .collect(Collectors.toList());
272 }
Sho SHIMIZUd97a9502015-08-18 10:02:30 -0700273}