blob: a1e545f02835dbe54954163b785b2c7124aa1690 [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
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -080018import com.google.common.collect.Lists;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080019import org.onlab.packet.MplsLabel;
20import org.onlab.packet.VlanId;
21import org.onlab.util.ItemNotFoundException;
Marc De Leenheer2c305302015-12-07 21:37:44 -080022import org.onosproject.net.DefaultOchSignalComparator;
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 Yutad39e3762015-12-04 09:43:16 -080047import java.util.SortedSet;
Marc De Leenheer2c305302015-12-07 21:37:44 -080048import java.util.TreeSet;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070049import java.util.concurrent.ExecutorService;
Marc De Leenheer2c305302015-12-07 21:37:44 -080050import java.util.function.Supplier;
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020051import java.util.stream.Collectors;
52import java.util.stream.IntStream;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070053
54import static com.google.common.base.Preconditions.checkNotNull;
55
56/**
57 * An implementation of DeviceListener registering devices as resources.
58 */
59final class ResourceDeviceListener implements DeviceListener {
60
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020061 private static final Logger log = LoggerFactory.getLogger(ResourceDeviceListener.class);
62
Sho SHIMIZU44f37612015-11-25 16:23:22 -080063 private static final int MAX_VLAN_ID = VlanId.MAX_VLAN;
64 private static final List<VlanId> ENTIRE_VLAN_IDS = getEntireVlans();
65
66 private static final int MAX_MPLS_LABEL = 1048576;
67 private static final List<MplsLabel> ENTIRE_MPLS_LABELS = getEntireMplsLabels();
68
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020069 private static final int TOTAL_ODU2_TRIBUTARY_SLOTS = 8;
70 private static final int TOTAL_ODU4_TRIBUTARY_SLOTS = 80;
71 private static final List<TributarySlot> ENTIRE_ODU2_TRIBUTARY_SLOTS = getEntireOdu2TributarySlots();
72 private static final List<TributarySlot> ENTIRE_ODU4_TRIBUTARY_SLOTS = getEntireOdu4TributarySlots();
73
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070074 private final ResourceAdminService adminService;
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080075 private final DeviceService deviceService;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080076 private final DriverService driverService;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070077 private final ExecutorService executor;
78
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080079
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070080 /**
81 * Creates an instance with the specified ResourceAdminService and ExecutorService.
82 *
83 * @param adminService instance invoked to register resources
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080084 * @param deviceService {@link DeviceService} to be used.
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070085 * @param executor executor used for processing resource registration
86 */
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080087 ResourceDeviceListener(ResourceAdminService adminService, DeviceService deviceService, DriverService driverService,
Sho SHIMIZU44f37612015-11-25 16:23:22 -080088 ExecutorService executor) {
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070089 this.adminService = checkNotNull(adminService);
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080090 this.deviceService = checkNotNull(deviceService);
Sho SHIMIZU44f37612015-11-25 16:23:22 -080091 this.driverService = checkNotNull(driverService);
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070092 this.executor = checkNotNull(executor);
93 }
94
95 @Override
96 public void event(DeviceEvent event) {
97 Device device = event.subject();
98 switch (event.type()) {
99 case DEVICE_ADDED:
100 registerDeviceResource(device);
101 break;
Sho SHIMIZUe60a5ab2015-08-20 11:51:49 -0700102 case DEVICE_REMOVED:
103 unregisterDeviceResource(device);
104 break;
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800105 case DEVICE_AVAILABILITY_CHANGED:
106 if (deviceService.isAvailable(device.id())) {
107 registerDeviceResource(device);
108 // TODO: do we need to walk the ports?
109 } else {
110 unregisterDeviceResource(device);
111 }
112 break;
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -0700113 case PORT_ADDED:
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800114 case PORT_UPDATED:
115 if (event.port().isEnabled()) {
116 registerPortResource(device, event.port());
117 } else {
118 unregisterPortResource(device, event.port());
119 }
Sho SHIMIZUc2ddedd2015-08-20 11:54:29 -0700120 break;
Sho SHIMIZUe2292842015-08-20 11:59:23 -0700121 case PORT_REMOVED:
122 unregisterPortResource(device, event.port());
123 break;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -0700124 default:
125 break;
126 }
127 }
128
129 private void registerDeviceResource(Device device) {
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800130 executor.submit(() -> adminService.registerResources(ResourcePath.discrete(device.id())));
Sho SHIMIZUd97a9502015-08-18 10:02:30 -0700131 }
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -0700132
Sho SHIMIZUe60a5ab2015-08-20 11:51:49 -0700133 private void unregisterDeviceResource(Device device) {
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800134 executor.submit(() -> adminService.unregisterResources(ResourcePath.discrete(device.id())));
Sho SHIMIZUe60a5ab2015-08-20 11:51:49 -0700135 }
136
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -0700137 private void registerPortResource(Device device, Port port) {
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200138 ResourcePath portPath = ResourcePath.discrete(device.id(), port.number());
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800139 executor.submit(() -> {
140 adminService.registerResources(portPath);
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200141
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800142 // for VLAN IDs
143 if (isVlanEnabled(device.id(), port.number())) {
144 adminService.registerResources(Lists.transform(ENTIRE_VLAN_IDS, portPath::child));
145 }
146
147 // for MPLS labels
148 if (isMplsEnabled(device.id(), port.number())) {
149 adminService.registerResources(Lists.transform(ENTIRE_MPLS_LABELS, portPath::child));
150 }
151
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800152 // for Lambdas
153 SortedSet<OchSignal> lambdas = queryLambdas(device.id(), port.number());
154 if (!lambdas.isEmpty()) {
155 adminService.registerResources(lambdas.stream()
156 .map(portPath::child)
157 .collect(Collectors.toList()));
158 }
159
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800160 // for Tributary slots
161 // TODO: need to define Behaviour to make a query about OCh port
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800162 switch (port.type()) {
163 case OCH:
164 // register ODU TributarySlots against the OCH port
165 registerTributarySlotsResources(((OchPort) port).signalType(), portPath);
166 break;
167 default:
168 break;
169 }
170 });
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200171 }
172
173 private void registerTributarySlotsResources(OduSignalType oduSignalType, ResourcePath portPath) {
174 switch (oduSignalType) {
175 case ODU2:
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800176 adminService.registerResources(Lists.transform(ENTIRE_ODU2_TRIBUTARY_SLOTS, portPath::child));
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200177 break;
178 case ODU4:
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800179 adminService.registerResources(Lists.transform(ENTIRE_ODU4_TRIBUTARY_SLOTS, portPath::child));
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200180 break;
181 default:
182 break;
183 }
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -0700184 }
Sho SHIMIZUe2292842015-08-20 11:59:23 -0700185
186 private void unregisterPortResource(Device device, Port port) {
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800187 ResourcePath resource = ResourcePath.discrete(device.id(), port.number());
188 executor.submit(() -> adminService.unregisterResources(resource));
Sho SHIMIZUe2292842015-08-20 11:59:23 -0700189 }
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200190
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800191 private SortedSet<OchSignal> queryLambdas(DeviceId did, PortNumber port) {
192 try {
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800193 // DriverHandler does not provide a way to check if a
194 // behaviour is supported.
195 Driver driver = driverService.getDriver(did);
196 if (driver == null || !driver.hasBehaviour(LambdaQuery.class)) {
197 return Collections.emptySortedSet();
198 }
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800199 DriverHandler handler = driverService.createHandler(did);
200 if (handler == null) {
201 return Collections.emptySortedSet();
202 }
203 LambdaQuery query = handler.behaviour(LambdaQuery.class);
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800204 if (query != null) {
Marc De Leenheer2c305302015-12-07 21:37:44 -0800205 Supplier<SortedSet<OchSignal>> supplier = () -> new TreeSet<>(new DefaultOchSignalComparator());
206 return query.queryLambdas(port).stream()
207 .flatMap(x -> OchSignal.toFlexGrid(x).stream())
208 .collect(Collectors.toCollection(supplier));
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800209 } else {
210 return Collections.emptySortedSet();
211 }
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800212 } catch (ItemNotFoundException e) {
213 return Collections.emptySortedSet();
214 }
215 }
216
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800217 private boolean isVlanEnabled(DeviceId device, PortNumber port) {
218 try {
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800219 // DriverHandler does not provide a way to check if a
220 // behaviour is supported.
221 Driver driver = driverService.getDriver(device);
222 if (driver == null || !driver.hasBehaviour(VlanQuery.class)) {
223 // device does not support this
224 return false;
225 }
226
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800227 DriverHandler handler = driverService.createHandler(device);
228 if (handler == null) {
229 return false;
230 }
231
232 VlanQuery query = handler.behaviour(VlanQuery.class);
233 return query != null && query.isEnabled(port);
234 } catch (ItemNotFoundException e) {
235 return false;
236 }
237 }
238
239 private boolean isMplsEnabled(DeviceId device, PortNumber port) {
240 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
246 return false;
247 }
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800248 DriverHandler handler = driverService.createHandler(device);
249 if (handler == null) {
250 return false;
251 }
252
253 MplsQuery query = handler.behaviour(MplsQuery.class);
254 return query != null && query.isEnabled(port);
255 } catch (ItemNotFoundException e) {
256 return false;
257 }
258 }
259
260 private static List<VlanId> getEntireVlans() {
261 return IntStream.range(0, MAX_VLAN_ID)
262 .mapToObj(x -> VlanId.vlanId((short) x))
263 .collect(Collectors.toList());
264 }
265
266 private static List<MplsLabel> getEntireMplsLabels() {
267 // potentially many objects are created
268 return IntStream.range(0, MAX_MPLS_LABEL)
269 .mapToObj(MplsLabel::mplsLabel)
270 .collect(Collectors.toList());
271 }
272
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200273 private static List<TributarySlot> getEntireOdu2TributarySlots() {
274 return IntStream.rangeClosed(1, TOTAL_ODU2_TRIBUTARY_SLOTS)
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800275 .mapToObj(TributarySlot::of)
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200276 .collect(Collectors.toList());
277 }
278 private static List<TributarySlot> getEntireOdu4TributarySlots() {
279 return IntStream.rangeClosed(1, TOTAL_ODU4_TRIBUTARY_SLOTS)
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800280 .mapToObj(TributarySlot::of)
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200281 .collect(Collectors.toList());
282 }
Sho SHIMIZUd97a9502015-08-18 10:02:30 -0700283}