blob: fa07b8c9cb021b234282f26c397fd90edfc749d5 [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;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070022import org.onosproject.net.Device;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080023import org.onosproject.net.DeviceId;
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -070024import org.onosproject.net.Port;
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020025import org.onosproject.net.OchPort;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080026import org.onosproject.net.PortNumber;
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020027import org.onosproject.net.TributarySlot;
28import org.onosproject.net.OduSignalType;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080029import org.onosproject.net.behaviour.MplsQuery;
30import org.onosproject.net.behaviour.VlanQuery;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070031import org.onosproject.net.device.DeviceEvent;
32import org.onosproject.net.device.DeviceListener;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080033import org.onosproject.net.driver.DriverHandler;
34import org.onosproject.net.driver.DriverService;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070035import org.onosproject.net.newresource.ResourceAdminService;
36import org.onosproject.net.newresource.ResourcePath;
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020037import org.slf4j.Logger;
38import org.slf4j.LoggerFactory;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070039
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020040import java.util.List;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070041import java.util.concurrent.ExecutorService;
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020042import java.util.stream.Collectors;
43import java.util.stream.IntStream;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070044
45import static com.google.common.base.Preconditions.checkNotNull;
46
47/**
48 * An implementation of DeviceListener registering devices as resources.
49 */
50final class ResourceDeviceListener implements DeviceListener {
51
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020052 private static final Logger log = LoggerFactory.getLogger(ResourceDeviceListener.class);
53
Sho SHIMIZU44f37612015-11-25 16:23:22 -080054 private static final int MAX_VLAN_ID = VlanId.MAX_VLAN;
55 private static final List<VlanId> ENTIRE_VLAN_IDS = getEntireVlans();
56
57 private static final int MAX_MPLS_LABEL = 1048576;
58 private static final List<MplsLabel> ENTIRE_MPLS_LABELS = getEntireMplsLabels();
59
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +020060 private static final int TOTAL_ODU2_TRIBUTARY_SLOTS = 8;
61 private static final int TOTAL_ODU4_TRIBUTARY_SLOTS = 80;
62 private static final List<TributarySlot> ENTIRE_ODU2_TRIBUTARY_SLOTS = getEntireOdu2TributarySlots();
63 private static final List<TributarySlot> ENTIRE_ODU4_TRIBUTARY_SLOTS = getEntireOdu4TributarySlots();
64
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070065 private final ResourceAdminService adminService;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080066 private final DriverService driverService;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070067 private final ExecutorService executor;
68
69 /**
70 * Creates an instance with the specified ResourceAdminService and ExecutorService.
71 *
72 * @param adminService instance invoked to register resources
73 * @param executor executor used for processing resource registration
74 */
Sho SHIMIZU44f37612015-11-25 16:23:22 -080075 ResourceDeviceListener(ResourceAdminService adminService, DriverService driverService,
76 ExecutorService executor) {
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070077 this.adminService = checkNotNull(adminService);
Sho SHIMIZU44f37612015-11-25 16:23:22 -080078 this.driverService = checkNotNull(driverService);
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070079 this.executor = checkNotNull(executor);
80 }
81
82 @Override
83 public void event(DeviceEvent event) {
84 Device device = event.subject();
85 switch (event.type()) {
86 case DEVICE_ADDED:
87 registerDeviceResource(device);
88 break;
Sho SHIMIZUe60a5ab2015-08-20 11:51:49 -070089 case DEVICE_REMOVED:
90 unregisterDeviceResource(device);
91 break;
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -070092 case PORT_ADDED:
93 registerPortResource(device, event.port());
Sho SHIMIZUc2ddedd2015-08-20 11:54:29 -070094 break;
Sho SHIMIZUe2292842015-08-20 11:59:23 -070095 case PORT_REMOVED:
96 unregisterPortResource(device, event.port());
97 break;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070098 default:
99 break;
100 }
101 }
102
103 private void registerDeviceResource(Device device) {
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800104 executor.submit(() -> adminService.registerResources(ResourcePath.discrete(device.id())));
Sho SHIMIZUd97a9502015-08-18 10:02:30 -0700105 }
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -0700106
Sho SHIMIZUe60a5ab2015-08-20 11:51:49 -0700107 private void unregisterDeviceResource(Device device) {
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800108 executor.submit(() -> adminService.unregisterResources(ResourcePath.discrete(device.id())));
Sho SHIMIZUe60a5ab2015-08-20 11:51:49 -0700109 }
110
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -0700111 private void registerPortResource(Device device, Port port) {
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200112 ResourcePath portPath = ResourcePath.discrete(device.id(), port.number());
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800113 executor.submit(() -> {
114 adminService.registerResources(portPath);
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200115
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800116 // for VLAN IDs
117 if (isVlanEnabled(device.id(), port.number())) {
118 adminService.registerResources(Lists.transform(ENTIRE_VLAN_IDS, portPath::child));
119 }
120
121 // for MPLS labels
122 if (isMplsEnabled(device.id(), port.number())) {
123 adminService.registerResources(Lists.transform(ENTIRE_MPLS_LABELS, portPath::child));
124 }
125
126 // for Tributary slots
127 // TODO: need to define Behaviour to make a query about OCh port
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800128 switch (port.type()) {
129 case OCH:
130 // register ODU TributarySlots against the OCH port
131 registerTributarySlotsResources(((OchPort) port).signalType(), portPath);
132 break;
133 default:
134 break;
135 }
136 });
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200137 }
138
139 private void registerTributarySlotsResources(OduSignalType oduSignalType, ResourcePath portPath) {
140 switch (oduSignalType) {
141 case ODU2:
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800142 adminService.registerResources(Lists.transform(ENTIRE_ODU2_TRIBUTARY_SLOTS, portPath::child));
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200143 break;
144 case ODU4:
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800145 adminService.registerResources(Lists.transform(ENTIRE_ODU4_TRIBUTARY_SLOTS, portPath::child));
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200146 break;
147 default:
148 break;
149 }
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -0700150 }
Sho SHIMIZUe2292842015-08-20 11:59:23 -0700151
152 private void unregisterPortResource(Device device, Port port) {
Sho SHIMIZU2c7cecf2015-11-11 14:16:14 -0800153 ResourcePath resource = ResourcePath.discrete(device.id(), port.number());
154 executor.submit(() -> adminService.unregisterResources(resource));
Sho SHIMIZUe2292842015-08-20 11:59:23 -0700155 }
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200156
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800157 private boolean isVlanEnabled(DeviceId device, PortNumber port) {
158 try {
159 DriverHandler handler = driverService.createHandler(device);
160 if (handler == null) {
161 return false;
162 }
163
164 VlanQuery query = handler.behaviour(VlanQuery.class);
165 return query != null && query.isEnabled(port);
166 } catch (ItemNotFoundException e) {
167 return false;
168 }
169 }
170
171 private boolean isMplsEnabled(DeviceId device, PortNumber port) {
172 try {
173 DriverHandler handler = driverService.createHandler(device);
174 if (handler == null) {
175 return false;
176 }
177
178 MplsQuery query = handler.behaviour(MplsQuery.class);
179 return query != null && query.isEnabled(port);
180 } catch (ItemNotFoundException e) {
181 return false;
182 }
183 }
184
185 private static List<VlanId> getEntireVlans() {
186 return IntStream.range(0, MAX_VLAN_ID)
187 .mapToObj(x -> VlanId.vlanId((short) x))
188 .collect(Collectors.toList());
189 }
190
191 private static List<MplsLabel> getEntireMplsLabels() {
192 // potentially many objects are created
193 return IntStream.range(0, MAX_MPLS_LABEL)
194 .mapToObj(MplsLabel::mplsLabel)
195 .collect(Collectors.toList());
196 }
197
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200198 private static List<TributarySlot> getEntireOdu2TributarySlots() {
199 return IntStream.rangeClosed(1, TOTAL_ODU2_TRIBUTARY_SLOTS)
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800200 .mapToObj(TributarySlot::of)
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200201 .collect(Collectors.toList());
202 }
203 private static List<TributarySlot> getEntireOdu4TributarySlots() {
204 return IntStream.rangeClosed(1, TOTAL_ODU4_TRIBUTARY_SLOTS)
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800205 .mapToObj(TributarySlot::of)
Rimon Ashkenazye2410ff2015-11-10 14:11:08 +0200206 .collect(Collectors.toList());
207 }
Sho SHIMIZUd97a9502015-08-18 10:02:30 -0700208}