blob: e6d922530891a13b7ee92311ad837fceb6e4c6e1 [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;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070020import org.onosproject.net.device.DeviceEvent;
21import org.onosproject.net.device.DeviceListener;
22import org.onosproject.net.newresource.ResourceAdminService;
23import org.onosproject.net.newresource.ResourcePath;
24
25import java.util.concurrent.ExecutorService;
26
27import static com.google.common.base.Preconditions.checkNotNull;
28
29/**
30 * An implementation of DeviceListener registering devices as resources.
31 */
32final class ResourceDeviceListener implements DeviceListener {
33
34 private final ResourceAdminService adminService;
35 private final ExecutorService executor;
36
37 /**
38 * Creates an instance with the specified ResourceAdminService and ExecutorService.
39 *
40 * @param adminService instance invoked to register resources
41 * @param executor executor used for processing resource registration
42 */
43 ResourceDeviceListener(ResourceAdminService adminService, ExecutorService executor) {
44 this.adminService = checkNotNull(adminService);
45 this.executor = checkNotNull(executor);
46 }
47
48 @Override
49 public void event(DeviceEvent event) {
50 Device device = event.subject();
51 switch (event.type()) {
52 case DEVICE_ADDED:
53 registerDeviceResource(device);
54 break;
Sho SHIMIZUe60a5ab2015-08-20 11:51:49 -070055 case DEVICE_REMOVED:
56 unregisterDeviceResource(device);
57 break;
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -070058 case PORT_ADDED:
59 registerPortResource(device, event.port());
Sho SHIMIZUc2ddedd2015-08-20 11:54:29 -070060 break;
Sho SHIMIZUe2292842015-08-20 11:59:23 -070061 case PORT_REMOVED:
62 unregisterPortResource(device, event.port());
63 break;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070064 default:
65 break;
66 }
67 }
68
69 private void registerDeviceResource(Device device) {
Sho SHIMIZUea17b832015-08-19 17:08:08 -070070 executor.submit(() -> adminService.registerResources(ResourcePath.ROOT, device.id()));
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070071 }
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -070072
Sho SHIMIZUe60a5ab2015-08-20 11:51:49 -070073 private void unregisterDeviceResource(Device device) {
74 executor.submit(() -> adminService.unregisterResources(ResourcePath.ROOT, device.id()));
75 }
76
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -070077 private void registerPortResource(Device device, Port port) {
78 ResourcePath parent = new ResourcePath(device.id());
79 executor.submit(() -> adminService.registerResources(parent, port.number()));
80 }
Sho SHIMIZUe2292842015-08-20 11:59:23 -070081
82 private void unregisterPortResource(Device device, Port port) {
83 ResourcePath parent = new ResourcePath(device.id());
84 executor.submit(() -> adminService.unregisterResources(parent, port.number()));
85 }
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070086}