blob: 114c4c70b59a53d00089997c5e4dad267b13fb04 [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
18import org.onosproject.net.Device;
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -070019import org.onosproject.net.Port;
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020020import org.onosproject.net.OchPort;
21import org.onosproject.net.TributarySlot;
22import org.onosproject.net.OduSignalType;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070023import org.onosproject.net.device.DeviceEvent;
24import org.onosproject.net.device.DeviceListener;
25import org.onosproject.net.newresource.ResourceAdminService;
26import org.onosproject.net.newresource.ResourcePath;
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020027import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070029
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020030import java.util.List;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070031import java.util.concurrent.ExecutorService;
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020032import java.util.stream.Collectors;
33import java.util.stream.IntStream;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070034
35import static com.google.common.base.Preconditions.checkNotNull;
36
37/**
38 * An implementation of DeviceListener registering devices as resources.
39 */
40final class ResourceDeviceListener implements DeviceListener {
41
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020042 private static final Logger log = LoggerFactory.getLogger(ResourceDeviceListener.class);
43
44 private static final int TOTAL_ODU2_TRIBUTARY_SLOTS = 8;
45 private static final int TOTAL_ODU4_TRIBUTARY_SLOTS = 80;
46 private static final List<TributarySlot> ENTIRE_ODU2_TRIBUTARY_SLOTS = getEntireOdu2TributarySlots();
47 private static final List<TributarySlot> ENTIRE_ODU4_TRIBUTARY_SLOTS = getEntireOdu4TributarySlots();
48
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070049 private final ResourceAdminService adminService;
50 private final ExecutorService executor;
51
52 /**
53 * Creates an instance with the specified ResourceAdminService and ExecutorService.
54 *
55 * @param adminService instance invoked to register resources
56 * @param executor executor used for processing resource registration
57 */
58 ResourceDeviceListener(ResourceAdminService adminService, ExecutorService executor) {
59 this.adminService = checkNotNull(adminService);
60 this.executor = checkNotNull(executor);
61 }
62
63 @Override
64 public void event(DeviceEvent event) {
65 Device device = event.subject();
66 switch (event.type()) {
67 case DEVICE_ADDED:
68 registerDeviceResource(device);
69 break;
Sho SHIMIZUe60a5ab2015-08-20 11:51:49 -070070 case DEVICE_REMOVED:
71 unregisterDeviceResource(device);
72 break;
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -070073 case PORT_ADDED:
74 registerPortResource(device, event.port());
Sho SHIMIZUc2ddedd2015-08-20 11:54:29 -070075 break;
Sho SHIMIZUe2292842015-08-20 11:59:23 -070076 case PORT_REMOVED:
77 unregisterPortResource(device, event.port());
78 break;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070079 default:
80 break;
81 }
82 }
83
84 private void registerDeviceResource(Device device) {
Sho SHIMIZUea17b832015-08-19 17:08:08 -070085 executor.submit(() -> adminService.registerResources(ResourcePath.ROOT, device.id()));
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070086 }
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -070087
Sho SHIMIZUe60a5ab2015-08-20 11:51:49 -070088 private void unregisterDeviceResource(Device device) {
89 executor.submit(() -> adminService.unregisterResources(ResourcePath.ROOT, device.id()));
90 }
91
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -070092 private void registerPortResource(Device device, Port port) {
Sho SHIMIZU60ac58e2015-11-11 12:16:38 -080093 ResourcePath parent = ResourcePath.discrete(device.id());
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020094 executor.submit(() -> registerPortResource(device, port, parent));
95 }
96
97 private void registerPortResource(Device device, Port port, ResourcePath parent) {
98 adminService.registerResources(parent, port.number());
99 ResourcePath portPath = ResourcePath.discrete(device.id(), port.number());
100
101 switch (port.type()) {
102 case OCH:
103 // register ODU TributarySlots against the OCH port
104 registerTributarySlotsResources(((OchPort) port).signalType(), portPath);
105 break;
106 default:
107 break;
108 }
109 }
110
111 private void registerTributarySlotsResources(OduSignalType oduSignalType, ResourcePath portPath) {
112 switch (oduSignalType) {
113 case ODU2:
114 adminService.registerResources(portPath, ENTIRE_ODU2_TRIBUTARY_SLOTS);
115 break;
116 case ODU4:
117 adminService.registerResources(portPath, ENTIRE_ODU4_TRIBUTARY_SLOTS);
118 break;
119 default:
120 break;
121 }
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -0700122 }
Sho SHIMIZUe2292842015-08-20 11:59:23 -0700123
124 private void unregisterPortResource(Device device, Port port) {
Sho SHIMIZU60ac58e2015-11-11 12:16:38 -0800125 ResourcePath parent = ResourcePath.discrete(device.id());
Sho SHIMIZUe2292842015-08-20 11:59:23 -0700126 executor.submit(() -> adminService.unregisterResources(parent, port.number()));
127 }
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200128
129 private static List<TributarySlot> getEntireOdu2TributarySlots() {
130 return IntStream.rangeClosed(1, TOTAL_ODU2_TRIBUTARY_SLOTS)
131 .mapToObj(x -> TributarySlot.of(x))
132 .collect(Collectors.toList());
133 }
134 private static List<TributarySlot> getEntireOdu4TributarySlots() {
135 return IntStream.rangeClosed(1, TOTAL_ODU4_TRIBUTARY_SLOTS)
136 .mapToObj(x -> TributarySlot.of(x))
137 .collect(Collectors.toList());
138 }
139
Sho SHIMIZUd97a9502015-08-18 10:02:30 -0700140}