blob: 27d5333e1dc2ad1882b86bb13e3137ecacb3e274 [file] [log] [blame]
Sho SHIMIZU47e7b802015-08-18 08:54: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 com.google.common.annotations.Beta;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080019import com.google.common.collect.ImmutableList;
20
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070021import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
24import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080026import org.onosproject.net.ConnectPoint;
27import org.onosproject.net.config.ConfigFactory;
28import org.onosproject.net.config.NetworkConfigListener;
29import org.onosproject.net.config.NetworkConfigRegistry;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070030import org.onosproject.net.device.DeviceListener;
31import org.onosproject.net.device.DeviceService;
Sho SHIMIZU4bdd2592015-08-25 15:33:32 -070032import org.onosproject.net.driver.DriverService;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080033import org.onosproject.net.newresource.BandwidthCapacity;
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070034import org.onosproject.net.newresource.ResourceAdminService;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080035import org.slf4j.Logger;
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070036
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080037import java.util.List;
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070038import java.util.concurrent.ExecutorService;
39import java.util.concurrent.Executors;
40
41import static org.onlab.util.Tools.groupedThreads;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080042import static org.onosproject.net.config.basics.SubjectFactories.CONNECT_POINT_SUBJECT_FACTORY;
43import static org.slf4j.LoggerFactory.getLogger;
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070044
45/**
46 * A class registering resources when they are detected.
47 */
Sho SHIMIZUf1a62792015-11-04 08:15:29 -080048@Component(immediate = true)
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070049@Beta
50public final class ResourceRegistrar {
51
52 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
53 protected ResourceAdminService adminService;
54
55 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sho SHIMIZU4bdd2592015-08-25 15:33:32 -070056 protected DriverService driverService;
57
58 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070059 protected DeviceService deviceService;
60
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080061 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
62 protected NetworkConfigRegistry cfgRegistry;
63
64 private final Logger log = getLogger(getClass());
65
66 private final List<ConfigFactory<?, ?>> factories = ImmutableList.of(
67 new ConfigFactory<ConnectPoint, BandwidthCapacity>(CONNECT_POINT_SUBJECT_FACTORY,
68 BandwidthCapacity.class, BandwidthCapacity.CONFIG_KEY) {
69 @Override
70 public BandwidthCapacity createConfig() {
71 return new BandwidthCapacity();
72 }
73 });
74
75
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070076 private DeviceListener deviceListener;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080077
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070078 private final ExecutorService executor =
79 Executors.newSingleThreadExecutor(groupedThreads("onos/resource", "registrar"));
80
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080081 private NetworkConfigListener cfgListener;
82
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070083 @Activate
84 public void activate() {
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080085 factories.forEach(cfgRegistry::registerConfigFactory);
86
87 cfgListener = new ResourceNetworkConfigListener(adminService, cfgRegistry, executor);
88 cfgRegistry.addListener(cfgListener);
89
90 deviceListener = new ResourceDeviceListener(adminService, deviceService, driverService, cfgRegistry, executor);
Sho SHIMIZU69dc5842015-11-20 16:31:12 -080091 deviceService.addListener(deviceListener);
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080092
93 // TODO Attempt initial registration of existing resources?
94
95 log.info("Started");
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070096 }
97
98 @Deactivate
99 public void deactivate() {
Sho SHIMIZU69dc5842015-11-20 16:31:12 -0800100 deviceService.removeListener(deviceListener);
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800101 cfgRegistry.removeListener(cfgListener);
102
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800103 executor.shutdownNow();
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800104
105 factories.forEach(cfgRegistry::unregisterConfigFactory);
106
107 log.info("Stopped");
Sho SHIMIZU47e7b802015-08-18 08:54:30 -0700108 }
109}