143f8c2b9dacf1db9f9e33c1e651ce9644e663d0
[onosfw.git] /
1 /*
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  */
16 package org.onosproject.net.newresource.impl;
17
18 import com.google.common.annotations.Beta;
19 import org.apache.felix.scr.annotations.Activate;
20 import org.apache.felix.scr.annotations.Component;
21 import org.apache.felix.scr.annotations.Deactivate;
22 import org.apache.felix.scr.annotations.Reference;
23 import org.apache.felix.scr.annotations.ReferenceCardinality;
24 import org.onosproject.net.device.DeviceListener;
25 import org.onosproject.net.device.DeviceService;
26 import org.onosproject.net.driver.DriverService;
27 import org.onosproject.net.link.LinkListener;
28 import org.onosproject.net.link.LinkService;
29 import org.onosproject.net.newresource.ResourceAdminService;
30
31 import java.util.concurrent.ExecutorService;
32 import java.util.concurrent.Executors;
33
34 import static org.onlab.util.Tools.groupedThreads;
35
36 /**
37  * A class registering resources when they are detected.
38  */
39 @Component(immediate = true)
40 @Beta
41 public final class ResourceRegistrar {
42
43     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
44     protected ResourceAdminService adminService;
45
46     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
47     protected DriverService driverService;
48
49     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
50     protected DeviceService deviceService;
51
52     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
53     protected LinkService linkService;
54
55     private DeviceListener deviceListener;
56     private LinkListener linkListener;
57     private final ExecutorService executor =
58             Executors.newSingleThreadExecutor(groupedThreads("onos/resource", "registrar"));
59
60     @Activate
61     public void activate() {
62         deviceListener = new ResourceDeviceListener(adminService, executor);
63         deviceService.addListener(deviceListener);
64         linkListener = new ResourceLinkListener(adminService, driverService, executor);
65         linkService.addListener(linkListener);
66     }
67
68     @Deactivate
69     public void deactivate() {
70         deviceService.removeListener(deviceListener);
71         linkService.removeListener(linkListener);
72     }
73 }