blob: b46a99f585ac1acd744a3909224c6b11f0d19ecc [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;
19import org.onosproject.net.DeviceId;
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -070020import org.onosproject.net.Port;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070021import org.onosproject.net.device.DeviceEvent;
22import org.onosproject.net.device.DeviceListener;
23import org.onosproject.net.newresource.ResourceAdminService;
24import org.onosproject.net.newresource.ResourcePath;
25
26import java.util.concurrent.ExecutorService;
27
28import static com.google.common.base.Preconditions.checkNotNull;
29
30/**
31 * An implementation of DeviceListener registering devices as resources.
32 */
33final class ResourceDeviceListener implements DeviceListener {
34
35 private final ResourceAdminService adminService;
36 private final ExecutorService executor;
37
38 /**
39 * Creates an instance with the specified ResourceAdminService and ExecutorService.
40 *
41 * @param adminService instance invoked to register resources
42 * @param executor executor used for processing resource registration
43 */
44 ResourceDeviceListener(ResourceAdminService adminService, ExecutorService executor) {
45 this.adminService = checkNotNull(adminService);
46 this.executor = checkNotNull(executor);
47 }
48
49 @Override
50 public void event(DeviceEvent event) {
51 Device device = event.subject();
52 switch (event.type()) {
53 case DEVICE_ADDED:
54 registerDeviceResource(device);
55 break;
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -070056 case PORT_ADDED:
57 registerPortResource(device, event.port());
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070058 default:
59 break;
60 }
61 }
62
63 private void registerDeviceResource(Device device) {
64 DeviceId deviceId = device.id();
65 executor.submit(() -> adminService.registerResources(new ResourcePath(ResourcePath.ROOT, deviceId)));
66 }
Sho SHIMIZU08fdbd22015-08-19 16:59:35 -070067
68 private void registerPortResource(Device device, Port port) {
69 ResourcePath parent = new ResourcePath(device.id());
70 executor.submit(() -> adminService.registerResources(parent, port.number()));
71 }
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070072}