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 | |
| 18 | import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY; |
| 19 | |
| 20 | import java.util.Set; |
| 21 | |
| 22 | import org.apache.felix.scr.annotations.Activate; |
| 23 | import org.apache.felix.scr.annotations.Component; |
| 24 | import org.apache.felix.scr.annotations.Deactivate; |
| 25 | import org.apache.felix.scr.annotations.Reference; |
| 26 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 27 | import org.apache.felix.scr.annotations.Service; |
| 28 | import org.onosproject.core.ApplicationId; |
| 29 | import org.onosproject.core.CoreService; |
| 30 | import org.onosproject.net.config.ConfigFactory; |
| 31 | import org.onosproject.net.config.NetworkConfigEvent; |
| 32 | import org.onosproject.net.config.NetworkConfigListener; |
| 33 | import org.onosproject.net.config.NetworkConfigRegistry; |
Deepa vaddireddy | 386f38b | 2017-08-02 06:24:01 +0000 | [diff] [blame] | 34 | import org.onosproject.codec.CodecService; |
| 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 | |
| 39 | import com.google.common.collect.ImmutableSet; |
| 40 | |
| 41 | /** |
| 42 | * Subscriber And Device Information Service application component. Component |
| 43 | * that manages the integration of ONOS into a deployment providing a bridge |
| 44 | * between ONOS and deployment specific information about subscribers and access |
| 45 | * devices. |
| 46 | */ |
| 47 | @Service |
| 48 | @Component(immediate = true) |
| 49 | public class SadisManager extends SubscriberAndDeviceInformationAdapter { |
| 50 | private final Logger log = LoggerFactory.getLogger(this.getClass()); |
| 51 | |
| 52 | private static final String SADIS_APP = "org.opencord.sadis"; |
| 53 | private ApplicationId appId; |
| 54 | private final InternalConfigListener cfgListener = new InternalConfigListener(); |
| 55 | |
| 56 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 57 | protected CoreService coreService; |
| 58 | |
| 59 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 60 | protected NetworkConfigRegistry cfgService; |
| 61 | |
Deepa vaddireddy | 386f38b | 2017-08-02 06:24:01 +0000 | [diff] [blame] | 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 63 | protected CodecService codecService; |
| 64 | |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 65 | @SuppressWarnings("rawtypes") |
| 66 | private final Set<ConfigFactory> factories = ImmutableSet |
| 67 | .of(new ConfigFactory<ApplicationId, SadisConfig>(APP_SUBJECT_FACTORY, SadisConfig.class, "sadis") { |
| 68 | @Override |
| 69 | public SadisConfig createConfig() { |
| 70 | return new SadisConfig(); |
| 71 | } |
| 72 | }); |
| 73 | |
| 74 | /** |
| 75 | * Initialize the SADIS ONOS application. |
| 76 | */ |
| 77 | @Activate |
| 78 | protected void activate() { |
| 79 | |
| 80 | this.appId = this.coreService.registerApplication(SADIS_APP); |
Deepa vaddireddy | 386f38b | 2017-08-02 06:24:01 +0000 | [diff] [blame] | 81 | codecService.registerCodec(SubscriberAndDeviceInformation.class, new SubscriberAndDeviceInformationCodec()); |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 82 | this.cfgService.addListener(this.cfgListener); |
| 83 | this.factories.forEach(this.cfgService::registerConfigFactory); |
| 84 | this.updateConfig(); |
| 85 | |
| 86 | this.log.info("Started"); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Cleans up resources utilized by the SADIS ONOS application. |
| 91 | */ |
| 92 | @Deactivate |
| 93 | protected void deactivate() { |
Deepa Vaddireddy | fc67bdf | 2017-09-21 12:50:56 +0530 | [diff] [blame] | 94 | cfgService.removeListener(cfgListener); |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 95 | this.log.info("Stopped"); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Validates the configuration and updates any operational settings that are |
| 100 | * affected by configuration changes. |
| 101 | */ |
| 102 | private void updateConfig() { |
| 103 | final SadisConfig cfg = this.cfgService.getConfig(this.appId, SadisConfig.class); |
| 104 | if (cfg == null) { |
| 105 | this.log.warn("Subscriber And Device Information Service (SADIS) configuration not available"); |
| 106 | return; |
| 107 | } |
| 108 | this.log.info("Cache Enabled: {}", cfg.getCacheEnabled()); |
| 109 | this.log.info("Cache Mac Size: {}", cfg.getCacheMaxSize()); |
| 110 | this.log.info("Cache TTL: {}", cfg.getCacheTtl().getSeconds()); |
| 111 | this.log.info("Entries: {}", cfg.getEntries()); |
Amit Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 112 | |
| 113 | configure(cfg); |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Listener for SADIS configuration events. |
| 118 | */ |
| 119 | private class InternalConfigListener implements NetworkConfigListener { |
| 120 | |
| 121 | @Override |
| 122 | public void event(final NetworkConfigEvent event) { |
| 123 | |
| 124 | if ((event.type() == NetworkConfigEvent.Type.CONFIG_ADDED |
| 125 | || event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) |
| 126 | && event.configClass().equals(SadisConfig.class)) { |
| 127 | SadisManager.this.updateConfig(); |
| 128 | SadisManager.this.log.info("Reconfigured"); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | } |