Sho SHIMIZU | d97a950 | 2015-08-18 10:02:30 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.net.newresource.impl; |
| 17 | |
Sho SHIMIZU | 2c7cecf | 2015-11-11 14:16:14 -0800 | [diff] [blame^] | 18 | import com.google.common.collect.Lists; |
Sho SHIMIZU | d97a950 | 2015-08-18 10:02:30 -0700 | [diff] [blame] | 19 | import org.onosproject.net.Device; |
Sho SHIMIZU | 08fdbd2 | 2015-08-19 16:59:35 -0700 | [diff] [blame] | 20 | import org.onosproject.net.Port; |
Rimon Ashkenazy | e2410ff | 2015-11-10 14:11:08 +0200 | [diff] [blame] | 21 | import org.onosproject.net.OchPort; |
| 22 | import org.onosproject.net.TributarySlot; |
| 23 | import org.onosproject.net.OduSignalType; |
Sho SHIMIZU | d97a950 | 2015-08-18 10:02:30 -0700 | [diff] [blame] | 24 | import org.onosproject.net.device.DeviceEvent; |
| 25 | import org.onosproject.net.device.DeviceListener; |
| 26 | import org.onosproject.net.newresource.ResourceAdminService; |
| 27 | import org.onosproject.net.newresource.ResourcePath; |
Rimon Ashkenazy | e2410ff | 2015-11-10 14:11:08 +0200 | [diff] [blame] | 28 | import org.slf4j.Logger; |
| 29 | import org.slf4j.LoggerFactory; |
Sho SHIMIZU | d97a950 | 2015-08-18 10:02:30 -0700 | [diff] [blame] | 30 | |
Rimon Ashkenazy | e2410ff | 2015-11-10 14:11:08 +0200 | [diff] [blame] | 31 | import java.util.List; |
Sho SHIMIZU | d97a950 | 2015-08-18 10:02:30 -0700 | [diff] [blame] | 32 | import java.util.concurrent.ExecutorService; |
Rimon Ashkenazy | e2410ff | 2015-11-10 14:11:08 +0200 | [diff] [blame] | 33 | import java.util.stream.Collectors; |
| 34 | import java.util.stream.IntStream; |
Sho SHIMIZU | d97a950 | 2015-08-18 10:02:30 -0700 | [diff] [blame] | 35 | |
| 36 | import static com.google.common.base.Preconditions.checkNotNull; |
| 37 | |
| 38 | /** |
| 39 | * An implementation of DeviceListener registering devices as resources. |
| 40 | */ |
| 41 | final class ResourceDeviceListener implements DeviceListener { |
| 42 | |
Rimon Ashkenazy | e2410ff | 2015-11-10 14:11:08 +0200 | [diff] [blame] | 43 | 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 SHIMIZU | d97a950 | 2015-08-18 10:02:30 -0700 | [diff] [blame] | 50 | 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 SHIMIZU | e60a5ab | 2015-08-20 11:51:49 -0700 | [diff] [blame] | 71 | case DEVICE_REMOVED: |
| 72 | unregisterDeviceResource(device); |
| 73 | break; |
Sho SHIMIZU | 08fdbd2 | 2015-08-19 16:59:35 -0700 | [diff] [blame] | 74 | case PORT_ADDED: |
| 75 | registerPortResource(device, event.port()); |
Sho SHIMIZU | c2ddedd | 2015-08-20 11:54:29 -0700 | [diff] [blame] | 76 | break; |
Sho SHIMIZU | e229284 | 2015-08-20 11:59:23 -0700 | [diff] [blame] | 77 | case PORT_REMOVED: |
| 78 | unregisterPortResource(device, event.port()); |
| 79 | break; |
Sho SHIMIZU | d97a950 | 2015-08-18 10:02:30 -0700 | [diff] [blame] | 80 | default: |
| 81 | break; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | private void registerDeviceResource(Device device) { |
Sho SHIMIZU | 2c7cecf | 2015-11-11 14:16:14 -0800 | [diff] [blame^] | 86 | executor.submit(() -> adminService.registerResources(ResourcePath.discrete(device.id()))); |
Sho SHIMIZU | d97a950 | 2015-08-18 10:02:30 -0700 | [diff] [blame] | 87 | } |
Sho SHIMIZU | 08fdbd2 | 2015-08-19 16:59:35 -0700 | [diff] [blame] | 88 | |
Sho SHIMIZU | e60a5ab | 2015-08-20 11:51:49 -0700 | [diff] [blame] | 89 | private void unregisterDeviceResource(Device device) { |
Sho SHIMIZU | 2c7cecf | 2015-11-11 14:16:14 -0800 | [diff] [blame^] | 90 | executor.submit(() -> adminService.unregisterResources(ResourcePath.discrete(device.id()))); |
Sho SHIMIZU | e60a5ab | 2015-08-20 11:51:49 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Sho SHIMIZU | 08fdbd2 | 2015-08-19 16:59:35 -0700 | [diff] [blame] | 93 | private void registerPortResource(Device device, Port port) { |
Rimon Ashkenazy | e2410ff | 2015-11-10 14:11:08 +0200 | [diff] [blame] | 94 | ResourcePath portPath = ResourcePath.discrete(device.id(), port.number()); |
Sho SHIMIZU | 2c7cecf | 2015-11-11 14:16:14 -0800 | [diff] [blame^] | 95 | executor.submit(() -> { |
| 96 | adminService.registerResources(portPath); |
Rimon Ashkenazy | e2410ff | 2015-11-10 14:11:08 +0200 | [diff] [blame] | 97 | |
Sho SHIMIZU | 2c7cecf | 2015-11-11 14:16:14 -0800 | [diff] [blame^] | 98 | 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 Ashkenazy | e2410ff | 2015-11-10 14:11:08 +0200 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | private void registerTributarySlotsResources(OduSignalType oduSignalType, ResourcePath portPath) { |
| 110 | switch (oduSignalType) { |
| 111 | case ODU2: |
Sho SHIMIZU | 2c7cecf | 2015-11-11 14:16:14 -0800 | [diff] [blame^] | 112 | adminService.registerResources(Lists.transform(ENTIRE_ODU2_TRIBUTARY_SLOTS, portPath::child)); |
Rimon Ashkenazy | e2410ff | 2015-11-10 14:11:08 +0200 | [diff] [blame] | 113 | break; |
| 114 | case ODU4: |
Sho SHIMIZU | 2c7cecf | 2015-11-11 14:16:14 -0800 | [diff] [blame^] | 115 | adminService.registerResources(Lists.transform(ENTIRE_ODU4_TRIBUTARY_SLOTS, portPath::child)); |
Rimon Ashkenazy | e2410ff | 2015-11-10 14:11:08 +0200 | [diff] [blame] | 116 | break; |
| 117 | default: |
| 118 | break; |
| 119 | } |
Sho SHIMIZU | 08fdbd2 | 2015-08-19 16:59:35 -0700 | [diff] [blame] | 120 | } |
Sho SHIMIZU | e229284 | 2015-08-20 11:59:23 -0700 | [diff] [blame] | 121 | |
| 122 | private void unregisterPortResource(Device device, Port port) { |
Sho SHIMIZU | 2c7cecf | 2015-11-11 14:16:14 -0800 | [diff] [blame^] | 123 | ResourcePath resource = ResourcePath.discrete(device.id(), port.number()); |
| 124 | executor.submit(() -> adminService.unregisterResources(resource)); |
Sho SHIMIZU | e229284 | 2015-08-20 11:59:23 -0700 | [diff] [blame] | 125 | } |
Rimon Ashkenazy | e2410ff | 2015-11-10 14:11:08 +0200 | [diff] [blame] | 126 | |
| 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 SHIMIZU | d97a950 | 2015-08-18 10:02:30 -0700 | [diff] [blame] | 138 | } |