blob: 9df5f50316743e22153fc2c276e8c417766cda21 [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;
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
Sho SHIMIZU44f37612015-11-25 16:23:22 -080061 private static final int MAX_VLAN_ID = VlanId.MAX_VLAN;
62 private static final List<VlanId> ENTIRE_VLAN_IDS = getEntireVlans();
63
HIGUCHI Yuta2cc93f42015-12-14 10:58:28 -080064 // Ref: http://www.iana.org/assignments/mpls-label-values/mpls-label-values.xhtml
65 // Smallest non-reserved MPLS label
66 private static final int MIN_UNRESERVED_LABEL = 0x10;
67 // Max non-reserved MPLS label = 239
68 private static final int MAX_UNRESERVED_LABEL = 0xEF;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080069 private static final List<MplsLabel> ENTIRE_MPLS_LABELS = getEntireMplsLabels();
70
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020071 private static final int TOTAL_ODU2_TRIBUTARY_SLOTS = 8;
72 private static final int TOTAL_ODU4_TRIBUTARY_SLOTS = 80;
73 private static final List<TributarySlot> ENTIRE_ODU2_TRIBUTARY_SLOTS = getEntireOdu2TributarySlots();
74 private static final List<TributarySlot> ENTIRE_ODU4_TRIBUTARY_SLOTS = getEntireOdu4TributarySlots();
75
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070076 private final ResourceAdminService adminService;
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080077 private final DeviceService deviceService;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080078 private final DriverService driverService;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070079 private final ExecutorService executor;
80
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080081
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070082 /**
83 * Creates an instance with the specified ResourceAdminService and ExecutorService.
84 *
85 * @param adminService instance invoked to register resources
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080086 * @param deviceService {@link DeviceService} to be used.
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070087 * @param executor executor used for processing resource registration
88 */
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080089 ResourceDeviceListener(ResourceAdminService adminService, DeviceService deviceService, DriverService driverService,
Sho SHIMIZU44f37612015-11-25 16:23:22 -080090 ExecutorService executor) {
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070091 this.adminService = checkNotNull(adminService);
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080092 this.deviceService = checkNotNull(deviceService);
Sho SHIMIZU44f37612015-11-25 16:23:22 -080093 this.driverService = checkNotNull(driverService);
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070094 this.executor = checkNotNull(executor);
95 }
96
97 @Override
98 public void event(DeviceEvent event) {
99 Device device = event.subject();
100 switch (event.type()) {
101 case DEVICE_ADDED:
102 registerDeviceResource(device);
103 break;
Sho SHIMIZUe60a5ab2015-08-20 11:51:49 -0700104 case DEVICE_REMOVED:
105 unregisterDeviceResource(device);
106 break;
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800107 case DEVICE_AVAILABILITY_CHANGED:
108 if (deviceService.isAvailable(device.id())) {
109 registerDeviceResource(device);
110 // TODO: do we need to walk the ports?
111 } else {
112 unregisterDeviceResource(device);
113 }
114 break;
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -0700115 case PORT_ADDED:
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800116 case PORT_UPDATED:
117 if (event.port().isEnabled()) {
118 registerPortResource(device, event.port());
119 } else {
120 unregisterPortResource(device, event.port());
121 }
Sho SHIMIZUc2ddedd2015-08-20 11:54:29 -0700122 break;
Sho SHIMIZUe2292842015-08-20 11:59:23 -0700123 case PORT_REMOVED:
124 unregisterPortResource(device, event.port());
125 break;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -0700126 default:
127 break;
128 }
129 }
130
131 private void registerDeviceResource(Device device) {
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800132 executor.submit(() -> adminService.registerResources(ResourcePath.discrete(device.id())));
Sho SHIMIZUd97a9502015-08-18 10:02:30 -0700133 }
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -0700134
Sho SHIMIZUe60a5ab2015-08-20 11:51:49 -0700135 private void unregisterDeviceResource(Device device) {
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800136 executor.submit(() -> adminService.unregisterResources(ResourcePath.discrete(device.id())));
Sho SHIMIZUe60a5ab2015-08-20 11:51:49 -0700137 }
138
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -0700139 private void registerPortResource(Device device, Port port) {
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200140 ResourcePath portPath = ResourcePath.discrete(device.id(), port.number());
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800141 executor.submit(() -> {
142 adminService.registerResources(portPath);
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200143
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800144 // for VLAN IDs
145 if (isVlanEnabled(device.id(), port.number())) {
146 adminService.registerResources(Lists.transform(ENTIRE_VLAN_IDS, portPath::child));
147 }
148
149 // for MPLS labels
150 if (isMplsEnabled(device.id(), port.number())) {
151 adminService.registerResources(Lists.transform(ENTIRE_MPLS_LABELS, portPath::child));
152 }
153
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800154 // for Lambdas
155 SortedSet<OchSignal> lambdas = queryLambdas(device.id(), port.number());
156 if (!lambdas.isEmpty()) {
157 adminService.registerResources(lambdas.stream()
158 .map(portPath::child)
159 .collect(Collectors.toList()));
160 }
161
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800162 // for Tributary slots
163 // TODO: need to define Behaviour to make a query about OCh port
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800164 switch (port.type()) {
165 case OCH:
166 // register ODU TributarySlots against the OCH port
167 registerTributarySlotsResources(((OchPort) port).signalType(), portPath);
168 break;
169 default:
170 break;
171 }
172 });
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200173 }
174
175 private void registerTributarySlotsResources(OduSignalType oduSignalType, ResourcePath portPath) {
176 switch (oduSignalType) {
177 case ODU2:
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800178 adminService.registerResources(Lists.transform(ENTIRE_ODU2_TRIBUTARY_SLOTS, portPath::child));
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200179 break;
180 case ODU4:
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800181 adminService.registerResources(Lists.transform(ENTIRE_ODU4_TRIBUTARY_SLOTS, portPath::child));
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200182 break;
183 default:
184 break;
185 }
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -0700186 }
Sho SHIMIZUe2292842015-08-20 11:59:23 -0700187
188 private void unregisterPortResource(Device device, Port port) {
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800189 ResourcePath resource = ResourcePath.discrete(device.id(), port.number());
190 executor.submit(() -> adminService.unregisterResources(resource));
Sho SHIMIZUe2292842015-08-20 11:59:23 -0700191 }
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200192
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800193 private SortedSet<OchSignal> queryLambdas(DeviceId did, PortNumber port) {
194 try {
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800195 // DriverHandler does not provide a way to check if a
196 // behaviour is supported.
197 Driver driver = driverService.getDriver(did);
198 if (driver == null || !driver.hasBehaviour(LambdaQuery.class)) {
199 return Collections.emptySortedSet();
200 }
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800201 DriverHandler handler = driverService.createHandler(did);
202 if (handler == null) {
203 return Collections.emptySortedSet();
204 }
205 LambdaQuery query = handler.behaviour(LambdaQuery.class);
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800206 if (query != null) {
Marc De Leenheer2c305302015-12-07 21:37:44 -0800207 return query.queryLambdas(port).stream()
208 .flatMap(x -> OchSignal.toFlexGrid(x).stream())
Marc De Leenheercef45552015-12-14 19:59:59 -0800209 .collect(Collectors.toCollection(DefaultOchSignalComparator::newOchSignalTreeSet));
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800210 } else {
211 return Collections.emptySortedSet();
212 }
HIGUCHI Yutad39e3762015-12-04 09:43:16 -0800213 } catch (ItemNotFoundException e) {
214 return Collections.emptySortedSet();
215 }
216 }
217
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800218 private boolean isVlanEnabled(DeviceId device, PortNumber port) {
219 try {
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800220 // DriverHandler does not provide a way to check if a
221 // behaviour is supported.
222 Driver driver = driverService.getDriver(device);
223 if (driver == null || !driver.hasBehaviour(VlanQuery.class)) {
224 // device does not support this
225 return false;
226 }
227
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800228 DriverHandler handler = driverService.createHandler(device);
229 if (handler == null) {
230 return false;
231 }
232
233 VlanQuery query = handler.behaviour(VlanQuery.class);
234 return query != null && query.isEnabled(port);
235 } catch (ItemNotFoundException e) {
236 return false;
237 }
238 }
239
240 private boolean isMplsEnabled(DeviceId device, PortNumber port) {
241 try {
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800242 // DriverHandler does not provide a way to check if a
243 // behaviour is supported.
244 Driver driver = driverService.getDriver(device);
245 if (driver == null || !driver.hasBehaviour(MplsQuery.class)) {
246 // device does not support this
247 return false;
248 }
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800249 DriverHandler handler = driverService.createHandler(device);
250 if (handler == null) {
251 return false;
252 }
253
254 MplsQuery query = handler.behaviour(MplsQuery.class);
255 return query != null && query.isEnabled(port);
256 } catch (ItemNotFoundException e) {
257 return false;
258 }
259 }
260
261 private static List<VlanId> getEntireVlans() {
262 return IntStream.range(0, MAX_VLAN_ID)
263 .mapToObj(x -> VlanId.vlanId((short) x))
264 .collect(Collectors.toList());
265 }
266
267 private static List<MplsLabel> getEntireMplsLabels() {
HIGUCHI Yuta2cc93f42015-12-14 10:58:28 -0800268 return IntStream.range(MIN_UNRESERVED_LABEL, MAX_UNRESERVED_LABEL + 1)
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800269 .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}