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 org.onosproject.net.Device;
19 import org.onosproject.net.Port;
20 import org.onosproject.net.device.DeviceEvent;
21 import org.onosproject.net.device.DeviceListener;
22 import org.onosproject.net.newresource.ResourceAdminService;
23 import org.onosproject.net.newresource.ResourcePath;
25 import java.util.concurrent.ExecutorService;
27 import static com.google.common.base.Preconditions.checkNotNull;
30 * An implementation of DeviceListener registering devices as resources.
32 final class ResourceDeviceListener implements DeviceListener {
34 private final ResourceAdminService adminService;
35 private final ExecutorService executor;
38 * Creates an instance with the specified ResourceAdminService and ExecutorService.
40 * @param adminService instance invoked to register resources
41 * @param executor executor used for processing resource registration
43 ResourceDeviceListener(ResourceAdminService adminService, ExecutorService executor) {
44 this.adminService = checkNotNull(adminService);
45 this.executor = checkNotNull(executor);
49 public void event(DeviceEvent event) {
50 Device device = event.subject();
51 switch (event.type()) {
53 registerDeviceResource(device);
56 unregisterDeviceResource(device);
59 registerPortResource(device, event.port());
62 unregisterPortResource(device, event.port());
69 private void registerDeviceResource(Device device) {
70 executor.submit(() -> adminService.registerResources(ResourcePath.ROOT, device.id()));
73 private void unregisterDeviceResource(Device device) {
74 executor.submit(() -> adminService.unregisterResources(ResourcePath.ROOT, device.id()));
77 private void registerPortResource(Device device, Port port) {
78 ResourcePath parent = ResourcePath.discrete(device.id());
79 executor.submit(() -> adminService.registerResources(parent, port.number()));
82 private void unregisterPortResource(Device device, Port port) {
83 ResourcePath parent = ResourcePath.discrete(device.id());
84 executor.submit(() -> adminService.unregisterResources(parent, port.number()));