David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 180c109 | 2017-08-03 22:46:14 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -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 | */ |
Amit Ghosh | c29c7a9 | 2017-08-01 09:59:13 +0100 | [diff] [blame] | 16 | package org.opencord.sadis.impl; |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 17 | |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame^] | 18 | import com.google.common.collect.Lists; |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 19 | import org.apache.felix.scr.annotations.Component; |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 20 | import org.apache.felix.scr.annotations.Reference; |
| 21 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 22 | import org.apache.felix.scr.annotations.Service; |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame^] | 23 | import org.apache.felix.scr.annotations.Activate; |
| 24 | import org.apache.felix.scr.annotations.Deactivate; |
| 25 | import org.onosproject.codec.CodecService; |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 26 | import org.onosproject.core.ApplicationId; |
| 27 | import org.onosproject.core.CoreService; |
| 28 | import org.onosproject.net.config.ConfigFactory; |
| 29 | import org.onosproject.net.config.NetworkConfigEvent; |
| 30 | import org.onosproject.net.config.NetworkConfigListener; |
| 31 | import org.onosproject.net.config.NetworkConfigRegistry; |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame^] | 32 | import org.opencord.sadis.BaseInformationService; |
| 33 | import org.opencord.sadis.BandwidthProfileInformation; |
| 34 | import org.opencord.sadis.SadisService; |
Deepa vaddireddy | 386f38b | 2017-08-02 06:24:01 +0000 | [diff] [blame] | 35 | import org.opencord.sadis.SubscriberAndDeviceInformation; |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 36 | import org.slf4j.Logger; |
| 37 | import org.slf4j.LoggerFactory; |
| 38 | |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame^] | 39 | import java.util.List; |
| 40 | import java.util.Set; |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 41 | |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 42 | @Service |
| 43 | @Component(immediate = true) |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame^] | 44 | public class SadisManager implements SadisService { |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 45 | private final Logger log = LoggerFactory.getLogger(this.getClass()); |
| 46 | |
| 47 | private static final String SADIS_APP = "org.opencord.sadis"; |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 48 | |
| 49 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 50 | protected CoreService coreService; |
| 51 | |
| 52 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 53 | protected NetworkConfigRegistry cfgService; |
| 54 | |
Deepa vaddireddy | 386f38b | 2017-08-02 06:24:01 +0000 | [diff] [blame] | 55 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 56 | protected CodecService codecService; |
| 57 | |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame^] | 58 | private final InternalConfigListener cfgListener = new InternalConfigListener(); |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 59 | |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame^] | 60 | private SubscriberManager subscriberManager; |
| 61 | private BandwidthProfileManager bandwidthProfileManager; |
| 62 | |
| 63 | private List<InformationAdapter> internalServices = Lists.newArrayList(); |
| 64 | |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 65 | @Activate |
| 66 | protected void activate() { |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame^] | 67 | ApplicationId appId = this.coreService.registerApplication(SADIS_APP); |
| 68 | cfgService.addListener(this.cfgListener); |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 69 | |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame^] | 70 | subscriberManager = new SubscriberManager(appId); |
| 71 | bandwidthProfileManager = new BandwidthProfileManager(appId); |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 72 | |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame^] | 73 | internalServices.add(subscriberManager); |
| 74 | internalServices.add(bandwidthProfileManager); |
| 75 | |
| 76 | registerAdapters(); |
| 77 | |
| 78 | log.info("Started"); |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame^] | 81 | private void registerAdapters() { |
| 82 | internalServices.forEach(service -> { |
| 83 | registerConfigFactory(service.getConfigFactories()); |
| 84 | registerCodec(service); |
| 85 | service.updateConfig(cfgService); |
| 86 | }); |
| 87 | } |
| 88 | |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 89 | @Deactivate |
| 90 | protected void deactivate() { |
Deepa Vaddireddy | fc67bdf | 2017-09-21 12:50:56 +0530 | [diff] [blame] | 91 | cfgService.removeListener(cfgListener); |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame^] | 92 | log.info("Stopped"); |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame^] | 95 | private void registerConfigFactory(Set<ConfigFactory> factories) { |
| 96 | factories.forEach(cfgService::registerConfigFactory); |
| 97 | } |
Amit Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 98 | |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame^] | 99 | private void registerCodec(InformationAdapter service) { |
| 100 | codecService.registerCodec(service.getInformationClass(), service.getCodec()); |
| 101 | } |
| 102 | |
| 103 | |
| 104 | @Override |
| 105 | public BaseInformationService<SubscriberAndDeviceInformation> getSubscriberInfoService() { |
| 106 | return subscriberManager; |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public BaseInformationService<BandwidthProfileInformation> getBandwidthProfileService() { |
| 111 | return bandwidthProfileManager; |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Listener for SADIS configuration events. |
| 116 | */ |
| 117 | private class InternalConfigListener implements NetworkConfigListener { |
| 118 | |
| 119 | @Override |
| 120 | public void event(final NetworkConfigEvent event) { |
| 121 | |
| 122 | if ((event.type() == NetworkConfigEvent.Type.CONFIG_ADDED |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame^] | 123 | || event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED)) { |
| 124 | |
| 125 | internalServices.forEach(adapter -> { |
| 126 | if (event.configClass().equals(adapter.getConfigClass())) { |
| 127 | adapter.updateConfig(cfgService); |
| 128 | log.info("Reconfigured"); |
| 129 | } |
| 130 | }); |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 131 | } |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame^] | 132 | |
| 133 | |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | } |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame^] | 137 | |