blob: e2666dd5db1d8622d83e85daa48072edc9e66a4f [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;
Naoki Shiotabe394c82016-02-03 16:44:29 -080035import org.onosproject.net.newresource.ResourceService;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080036import org.slf4j.Logger;
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070037
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080038import java.util.List;
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070039import java.util.concurrent.ExecutorService;
40import java.util.concurrent.Executors;
41
42import static org.onlab.util.Tools.groupedThreads;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080043import static org.onosproject.net.config.basics.SubjectFactories.CONNECT_POINT_SUBJECT_FACTORY;
44import static org.slf4j.LoggerFactory.getLogger;
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070045
46/**
47 * A class registering resources when they are detected.
48 */
Sho SHIMIZUf1a62792015-11-04 08:15:29 -080049@Component(immediate = true)
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070050@Beta
51public final class ResourceRegistrar {
52
53 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
54 protected ResourceAdminService adminService;
55
56 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Naoki Shiotabe394c82016-02-03 16:44:29 -080057 protected ResourceService resourceService;
58
59 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sho SHIMIZU4bdd2592015-08-25 15:33:32 -070060 protected DriverService driverService;
61
62 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070063 protected DeviceService deviceService;
64
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080065 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
66 protected NetworkConfigRegistry cfgRegistry;
67
68 private final Logger log = getLogger(getClass());
69
70 private final List<ConfigFactory<?, ?>> factories = ImmutableList.of(
71 new ConfigFactory<ConnectPoint, BandwidthCapacity>(CONNECT_POINT_SUBJECT_FACTORY,
72 BandwidthCapacity.class, BandwidthCapacity.CONFIG_KEY) {
73 @Override
74 public BandwidthCapacity createConfig() {
75 return new BandwidthCapacity();
76 }
77 });
78
79
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070080 private DeviceListener deviceListener;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080081
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070082 private final ExecutorService executor =
83 Executors.newSingleThreadExecutor(groupedThreads("onos/resource", "registrar"));
84
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080085 private NetworkConfigListener cfgListener;
86
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070087 @Activate
88 public void activate() {
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080089 factories.forEach(cfgRegistry::registerConfigFactory);
90
91 cfgListener = new ResourceNetworkConfigListener(adminService, cfgRegistry, executor);
92 cfgRegistry.addListener(cfgListener);
93
Naoki Shiotabe394c82016-02-03 16:44:29 -080094 deviceListener = new ResourceDeviceListener(adminService, resourceService,
95 deviceService, driverService, cfgRegistry, executor);
Sho SHIMIZU69dc5842015-11-20 16:31:12 -080096 deviceService.addListener(deviceListener);
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080097
98 // TODO Attempt initial registration of existing resources?
99
100 log.info("Started");
Sho SHIMIZU47e7b802015-08-18 08:54:30 -0700101 }
102
103 @Deactivate
104 public void deactivate() {
Sho SHIMIZU69dc5842015-11-20 16:31:12 -0800105 deviceService.removeListener(deviceListener);
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800106 cfgRegistry.removeListener(cfgListener);
107
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800108 executor.shutdownNow();
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800109
110 factories.forEach(cfgRegistry::unregisterConfigFactory);
111
112 log.info("Stopped");
Sho SHIMIZU47e7b802015-08-18 08:54:30 -0700113 }
114}