blob: 7c44f2a7e8d50189eee49804c4ada1babf91a1d6 [file] [log] [blame]
Sho SHIMIZU47e7b802015-08-18 08:54:30 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Sho SHIMIZU47e7b802015-08-18 08:54:30 -07003 *
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 */
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080016package org.onosproject.net.resource.impl;
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070017
18import com.google.common.annotations.Beta;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080019import com.google.common.collect.ImmutableList;
20
Ray Milkeyd84f89b2018-08-17 14:54:17 -070021import org.osgi.service.component.annotations.Activate;
22import org.osgi.service.component.annotations.Component;
23import org.osgi.service.component.annotations.Deactivate;
24import org.osgi.service.component.annotations.Reference;
25import org.osgi.service.component.annotations.ReferenceCardinality;
Sho SHIMIZU76b046e2016-02-24 11:45:44 -080026import org.onosproject.mastership.MastershipService;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080027import org.onosproject.net.ConnectPoint;
28import org.onosproject.net.config.ConfigFactory;
29import org.onosproject.net.config.NetworkConfigListener;
30import org.onosproject.net.config.NetworkConfigRegistry;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070031import org.onosproject.net.device.DeviceListener;
32import org.onosproject.net.device.DeviceService;
Sho SHIMIZU4bdd2592015-08-25 15:33:32 -070033import org.onosproject.net.driver.DriverService;
Sho SHIMIZUb6c63a32016-05-26 12:07:19 -070034import org.onosproject.net.config.basics.BandwidthCapacity;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080035import org.onosproject.net.resource.ResourceAdminService;
36import org.onosproject.net.resource.ResourceService;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080037import org.slf4j.Logger;
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070038
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080039import java.util.List;
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070040import java.util.concurrent.ExecutorService;
41import java.util.concurrent.Executors;
42
43import static org.onlab.util.Tools.groupedThreads;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080044import static org.onosproject.net.config.basics.SubjectFactories.CONNECT_POINT_SUBJECT_FACTORY;
45import static org.slf4j.LoggerFactory.getLogger;
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070046
47/**
48 * A class registering resources when they are detected.
49 */
Sho SHIMIZUf1a62792015-11-04 08:15:29 -080050@Component(immediate = true)
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070051@Beta
52public final class ResourceRegistrar {
53
Ray Milkeyd84f89b2018-08-17 14:54:17 -070054 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Ray Milkey9c9cde42018-01-12 14:22:06 -080055 ResourceAdminService adminService;
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070056
Ray Milkeyd84f89b2018-08-17 14:54:17 -070057 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Ray Milkey9c9cde42018-01-12 14:22:06 -080058 ResourceService resourceService;
Naoki Shiotabe394c82016-02-03 16:44:29 -080059
Ray Milkeyd84f89b2018-08-17 14:54:17 -070060 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Ray Milkey9c9cde42018-01-12 14:22:06 -080061 DriverService driverService;
Sho SHIMIZU4bdd2592015-08-25 15:33:32 -070062
Ray Milkeyd84f89b2018-08-17 14:54:17 -070063 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Ray Milkey9c9cde42018-01-12 14:22:06 -080064 DeviceService deviceService;
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070065
Ray Milkeyd84f89b2018-08-17 14:54:17 -070066 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Ray Milkey9c9cde42018-01-12 14:22:06 -080067 MastershipService mastershipService;
Sho SHIMIZU76b046e2016-02-24 11:45:44 -080068
Ray Milkeyd84f89b2018-08-17 14:54:17 -070069 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Ray Milkey9c9cde42018-01-12 14:22:06 -080070 NetworkConfigRegistry cfgRegistry;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080071
72 private final Logger log = getLogger(getClass());
73
74 private final List<ConfigFactory<?, ?>> factories = ImmutableList.of(
75 new ConfigFactory<ConnectPoint, BandwidthCapacity>(CONNECT_POINT_SUBJECT_FACTORY,
76 BandwidthCapacity.class, BandwidthCapacity.CONFIG_KEY) {
77 @Override
78 public BandwidthCapacity createConfig() {
79 return new BandwidthCapacity();
80 }
81 });
82
83
Sho SHIMIZUd97a9502015-08-18 10:02:30 -070084 private DeviceListener deviceListener;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080085
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070086 private final ExecutorService executor =
HIGUCHI Yuta060da9a2016-03-11 19:16:35 -080087 Executors.newSingleThreadExecutor(groupedThreads("onos/resource", "registrar", log));
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070088
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080089 private NetworkConfigListener cfgListener;
90
Sho SHIMIZU47e7b802015-08-18 08:54:30 -070091 @Activate
92 public void activate() {
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080093 factories.forEach(cfgRegistry::registerConfigFactory);
94
Sho SHIMIZU82b9d172016-02-24 16:53:59 -080095 cfgListener = new ResourceNetworkConfigListener(adminService, cfgRegistry, mastershipService, executor);
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080096 cfgRegistry.addListener(cfgListener);
97
Naoki Shiotabe394c82016-02-03 16:44:29 -080098 deviceListener = new ResourceDeviceListener(adminService, resourceService,
Sho SHIMIZU76b046e2016-02-24 11:45:44 -080099 deviceService, mastershipService, driverService, cfgRegistry, executor);
Sho SHIMIZU69dc5842015-11-20 16:31:12 -0800100 deviceService.addListener(deviceListener);
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800101
102 // TODO Attempt initial registration of existing resources?
103
104 log.info("Started");
Sho SHIMIZU47e7b802015-08-18 08:54:30 -0700105 }
106
107 @Deactivate
108 public void deactivate() {
Sho SHIMIZU69dc5842015-11-20 16:31:12 -0800109 deviceService.removeListener(deviceListener);
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800110 cfgRegistry.removeListener(cfgListener);
111
HIGUCHI Yuta11d16092015-12-04 23:35:43 -0800112 executor.shutdownNow();
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -0800113
114 factories.forEach(cfgRegistry::unregisterConfigFactory);
115
116 log.info("Stopped");
Sho SHIMIZU47e7b802015-08-18 08:54:30 -0700117 }
118}