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