blob: 4fb0d7babcfd0b10dff1fadcb5d5da785276a5d1 [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 SHIMIZUd97a9502015-08-18 10:02:30 -070019import org.onosproject.net.Device;
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -070020import org.onosproject.net.Port;
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020021import org.onosproject.net.OchPort;
22import org.onosproject.net.TributarySlot;
23import org.onosproject.net.OduSignalType;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070024import org.onosproject.net.device.DeviceEvent;
25import org.onosproject.net.device.DeviceListener;
26import org.onosproject.net.newresource.ResourceAdminService;
27import org.onosproject.net.newresource.ResourcePath;
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020028import org.slf4j.Logger;
29import org.slf4j.LoggerFactory;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070030
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020031import java.util.List;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070032import java.util.concurrent.ExecutorService;
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020033import java.util.stream.Collectors;
34import java.util.stream.IntStream;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070035
36import static com.google.common.base.Preconditions.checkNotNull;
37
38/**
39 * An implementation of DeviceListener registering devices as resources.
40 */
41final class ResourceDeviceListener implements DeviceListener {
42
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020043 private static final Logger log = LoggerFactory.getLogger(ResourceDeviceListener.class);
44
45 private static final int TOTAL_ODU2_TRIBUTARY_SLOTS = 8;
46 private static final int TOTAL_ODU4_TRIBUTARY_SLOTS = 80;
47 private static final List<TributarySlot> ENTIRE_ODU2_TRIBUTARY_SLOTS = getEntireOdu2TributarySlots();
48 private static final List<TributarySlot> ENTIRE_ODU4_TRIBUTARY_SLOTS = getEntireOdu4TributarySlots();
49
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070050 private final ResourceAdminService adminService;
51 private final ExecutorService executor;
52
53 /**
54 * Creates an instance with the specified ResourceAdminService and ExecutorService.
55 *
56 * @param adminService instance invoked to register resources
57 * @param executor executor used for processing resource registration
58 */
59 ResourceDeviceListener(ResourceAdminService adminService, ExecutorService executor) {
60 this.adminService = checkNotNull(adminService);
61 this.executor = checkNotNull(executor);
62 }
63
64 @Override
65 public void event(DeviceEvent event) {
66 Device device = event.subject();
67 switch (event.type()) {
68 case DEVICE_ADDED:
69 registerDeviceResource(device);
70 break;
Sho SHIMIZUe60a5ab2015-08-20 11:51:49 -070071 case DEVICE_REMOVED:
72 unregisterDeviceResource(device);
73 break;
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -070074 case PORT_ADDED:
75 registerPortResource(device, event.port());
Sho SHIMIZUc2ddedd2015-08-20 11:54:29 -070076 break;
Sho SHIMIZUe2292842015-08-20 11:59:23 -070077 case PORT_REMOVED:
78 unregisterPortResource(device, event.port());
79 break;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070080 default:
81 break;
82 }
83 }
84
85 private void registerDeviceResource(Device device) {
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -080086 executor.submit(() -> adminService.registerResources(ResourcePath.discrete(device.id())));
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070087 }
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -070088
Sho SHIMIZUe60a5ab2015-08-20 11:51:49 -070089 private void unregisterDeviceResource(Device device) {
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -080090 executor.submit(() -> adminService.unregisterResources(ResourcePath.discrete(device.id())));
Sho SHIMIZUe60a5ab2015-08-20 11:51:49 -070091 }
92
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -070093 private void registerPortResource(Device device, Port port) {
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020094 ResourcePath portPath = ResourcePath.discrete(device.id(), port.number());
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -080095 executor.submit(() -> {
96 adminService.registerResources(portPath);
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020097
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -080098 switch (port.type()) {
99 case OCH:
100 // register ODU TributarySlots against the OCH port
101 registerTributarySlotsResources(((OchPort) port).signalType(), portPath);
102 break;
103 default:
104 break;
105 }
106 });
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200107 }
108
109 private void registerTributarySlotsResources(OduSignalType oduSignalType, ResourcePath portPath) {
110 switch (oduSignalType) {
111 case ODU2:
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800112 adminService.registerResources(Lists.transform(ENTIRE_ODU2_TRIBUTARY_SLOTS, portPath::child));
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200113 break;
114 case ODU4:
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800115 adminService.registerResources(Lists.transform(ENTIRE_ODU4_TRIBUTARY_SLOTS, portPath::child));
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200116 break;
117 default:
118 break;
119 }
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -0700120 }
Sho SHIMIZUe2292842015-08-20 11:59:23 -0700121
122 private void unregisterPortResource(Device device, Port port) {
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800123 ResourcePath resource = ResourcePath.discrete(device.id(), port.number());
124 executor.submit(() -> adminService.unregisterResources(resource));
Sho SHIMIZUe2292842015-08-20 11:59:23 -0700125 }
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200126
127 private static List<TributarySlot> getEntireOdu2TributarySlots() {
128 return IntStream.rangeClosed(1, TOTAL_ODU2_TRIBUTARY_SLOTS)
129 .mapToObj(x -> TributarySlot.of(x))
130 .collect(Collectors.toList());
131 }
132 private static List<TributarySlot> getEntireOdu4TributarySlots() {
133 return IntStream.rangeClosed(1, TOTAL_ODU4_TRIBUTARY_SLOTS)
134 .mapToObj(x -> TributarySlot.of(x))
135 .collect(Collectors.toList());
136 }
137
Sho SHIMIZUd97a9502015-08-18 10:02:30 -0700138}