2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.net.newresource.impl;
18 import com.google.common.collect.Lists;
19 import org.onosproject.net.Device;
20 import org.onosproject.net.Port;
21 import org.onosproject.net.OchPort;
22 import org.onosproject.net.TributarySlot;
23 import org.onosproject.net.OduSignalType;
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;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 import java.util.List;
32 import java.util.concurrent.ExecutorService;
33 import java.util.stream.Collectors;
34 import java.util.stream.IntStream;
36 import static com.google.common.base.Preconditions.checkNotNull;
39 * An implementation of DeviceListener registering devices as resources.
41 final class ResourceDeviceListener implements DeviceListener {
43 private static final Logger log = LoggerFactory.getLogger(ResourceDeviceListener.class);
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();
50 private final ResourceAdminService adminService;
51 private final ExecutorService executor;
54 * Creates an instance with the specified ResourceAdminService and ExecutorService.
56 * @param adminService instance invoked to register resources
57 * @param executor executor used for processing resource registration
59 ResourceDeviceListener(ResourceAdminService adminService, ExecutorService executor) {
60 this.adminService = checkNotNull(adminService);
61 this.executor = checkNotNull(executor);
65 public void event(DeviceEvent event) {
66 Device device = event.subject();
67 switch (event.type()) {
69 registerDeviceResource(device);
72 unregisterDeviceResource(device);
75 registerPortResource(device, event.port());
78 unregisterPortResource(device, event.port());
85 private void registerDeviceResource(Device device) {
86 executor.submit(() -> adminService.registerResources(ResourcePath.discrete(device.id())));
89 private void unregisterDeviceResource(Device device) {
90 executor.submit(() -> adminService.unregisterResources(ResourcePath.discrete(device.id())));
93 private void registerPortResource(Device device, Port port) {
94 ResourcePath portPath = ResourcePath.discrete(device.id(), port.number());
95 executor.submit(() -> {
96 adminService.registerResources(portPath);
98 switch (port.type()) {
100 // register ODU TributarySlots against the OCH port
101 registerTributarySlotsResources(((OchPort) port).signalType(), portPath);
109 private void registerTributarySlotsResources(OduSignalType oduSignalType, ResourcePath portPath) {
110 switch (oduSignalType) {
112 adminService.registerResources(Lists.transform(ENTIRE_ODU2_TRIBUTARY_SLOTS, portPath::child));
115 adminService.registerResources(Lists.transform(ENTIRE_ODU4_TRIBUTARY_SLOTS, portPath::child));
122 private void unregisterPortResource(Device device, Port port) {
123 ResourcePath resource = ResourcePath.discrete(device.id(), port.number());
124 executor.submit(() -> adminService.unregisterResources(resource));
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());
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());