Sho SHIMIZU | 47e7b80 | 2015-08-18 08:54:30 -0700 | [diff] [blame] | 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; |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
| 20 | |
Sho SHIMIZU | 47e7b80 | 2015-08-18 08:54:30 -0700 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 26 | import org.onosproject.net.ConnectPoint; |
| 27 | import org.onosproject.net.config.ConfigFactory; |
| 28 | import org.onosproject.net.config.NetworkConfigListener; |
| 29 | import org.onosproject.net.config.NetworkConfigRegistry; |
Sho SHIMIZU | d97a950 | 2015-08-18 10:02:30 -0700 | [diff] [blame] | 30 | import org.onosproject.net.device.DeviceListener; |
| 31 | import org.onosproject.net.device.DeviceService; |
Sho SHIMIZU | 4bdd259 | 2015-08-25 15:33:32 -0700 | [diff] [blame] | 32 | import org.onosproject.net.driver.DriverService; |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 33 | import org.onosproject.net.newresource.BandwidthCapacity; |
Sho SHIMIZU | 47e7b80 | 2015-08-18 08:54:30 -0700 | [diff] [blame] | 34 | import org.onosproject.net.newresource.ResourceAdminService; |
Naoki Shiota | be394c8 | 2016-02-03 16:44:29 -0800 | [diff] [blame] | 35 | import org.onosproject.net.newresource.ResourceService; |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 36 | import org.slf4j.Logger; |
Sho SHIMIZU | 47e7b80 | 2015-08-18 08:54:30 -0700 | [diff] [blame] | 37 | |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 38 | import java.util.List; |
Sho SHIMIZU | 47e7b80 | 2015-08-18 08:54:30 -0700 | [diff] [blame] | 39 | import java.util.concurrent.ExecutorService; |
| 40 | import java.util.concurrent.Executors; |
| 41 | |
| 42 | import static org.onlab.util.Tools.groupedThreads; |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 43 | import static org.onosproject.net.config.basics.SubjectFactories.CONNECT_POINT_SUBJECT_FACTORY; |
| 44 | import static org.slf4j.LoggerFactory.getLogger; |
Sho SHIMIZU | 47e7b80 | 2015-08-18 08:54:30 -0700 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * A class registering resources when they are detected. |
| 48 | */ |
Sho SHIMIZU | f1a6279 | 2015-11-04 08:15:29 -0800 | [diff] [blame] | 49 | @Component(immediate = true) |
Sho SHIMIZU | 47e7b80 | 2015-08-18 08:54:30 -0700 | [diff] [blame] | 50 | @Beta |
| 51 | public final class ResourceRegistrar { |
| 52 | |
| 53 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 54 | protected ResourceAdminService adminService; |
| 55 | |
| 56 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Naoki Shiota | be394c8 | 2016-02-03 16:44:29 -0800 | [diff] [blame] | 57 | protected ResourceService resourceService; |
| 58 | |
| 59 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Sho SHIMIZU | 4bdd259 | 2015-08-25 15:33:32 -0700 | [diff] [blame] | 60 | protected DriverService driverService; |
| 61 | |
| 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Sho SHIMIZU | d97a950 | 2015-08-18 10:02:30 -0700 | [diff] [blame] | 63 | protected DeviceService deviceService; |
| 64 | |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 65 | @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 SHIMIZU | d97a950 | 2015-08-18 10:02:30 -0700 | [diff] [blame] | 80 | private DeviceListener deviceListener; |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 81 | |
Sho SHIMIZU | 47e7b80 | 2015-08-18 08:54:30 -0700 | [diff] [blame] | 82 | private final ExecutorService executor = |
| 83 | Executors.newSingleThreadExecutor(groupedThreads("onos/resource", "registrar")); |
| 84 | |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 85 | private NetworkConfigListener cfgListener; |
| 86 | |
Sho SHIMIZU | 47e7b80 | 2015-08-18 08:54:30 -0700 | [diff] [blame] | 87 | @Activate |
| 88 | public void activate() { |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 89 | factories.forEach(cfgRegistry::registerConfigFactory); |
| 90 | |
| 91 | cfgListener = new ResourceNetworkConfigListener(adminService, cfgRegistry, executor); |
| 92 | cfgRegistry.addListener(cfgListener); |
| 93 | |
Naoki Shiota | be394c8 | 2016-02-03 16:44:29 -0800 | [diff] [blame] | 94 | deviceListener = new ResourceDeviceListener(adminService, resourceService, |
| 95 | deviceService, driverService, cfgRegistry, executor); |
Sho SHIMIZU | 69dc584 | 2015-11-20 16:31:12 -0800 | [diff] [blame] | 96 | deviceService.addListener(deviceListener); |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 97 | |
| 98 | // TODO Attempt initial registration of existing resources? |
| 99 | |
| 100 | log.info("Started"); |
Sho SHIMIZU | 47e7b80 | 2015-08-18 08:54:30 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Deactivate |
| 104 | public void deactivate() { |
Sho SHIMIZU | 69dc584 | 2015-11-20 16:31:12 -0800 | [diff] [blame] | 105 | deviceService.removeListener(deviceListener); |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 106 | cfgRegistry.removeListener(cfgListener); |
| 107 | |
HIGUCHI Yuta | 11d1609 | 2015-12-04 23:35:43 -0800 | [diff] [blame] | 108 | executor.shutdownNow(); |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 109 | |
| 110 | factories.forEach(cfgRegistry::unregisterConfigFactory); |
| 111 | |
| 112 | log.info("Stopped"); |
Sho SHIMIZU | 47e7b80 | 2015-08-18 08:54:30 -0700 | [diff] [blame] | 113 | } |
| 114 | } |