slowr | db071b2 | 2017-07-07 11:10:25 -0700 | [diff] [blame] | 1 | /* |
Dimitrios Mavrommatis | f0c0632 | 2017-10-31 23:49:04 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
slowr | db071b2 | 2017-07-07 11:10:25 -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 | */ |
Dimitrios Mavrommatis | f0c0632 | 2017-10-31 23:49:04 -0700 | [diff] [blame] | 16 | |
slowr | db071b2 | 2017-07-07 11:10:25 -0700 | [diff] [blame] | 17 | package org.onosproject.artemis.impl; |
| 18 | |
Dimitrios Mavrommatis | f0c0632 | 2017-10-31 23:49:04 -0700 | [diff] [blame] | 19 | import org.onosproject.artemis.ArtemisEventListener; |
| 20 | import org.onosproject.artemis.ArtemisService; |
slowr | db071b2 | 2017-07-07 11:10:25 -0700 | [diff] [blame] | 21 | import org.onosproject.core.ApplicationId; |
| 22 | import org.onosproject.core.CoreService; |
Dimitrios Mavrommatis | f0c0632 | 2017-10-31 23:49:04 -0700 | [diff] [blame] | 23 | import org.onosproject.event.AbstractListenerManager; |
slowr | db071b2 | 2017-07-07 11:10:25 -0700 | [diff] [blame] | 24 | import org.onosproject.net.config.ConfigFactory; |
| 25 | import org.onosproject.net.config.NetworkConfigEvent; |
| 26 | import org.onosproject.net.config.NetworkConfigListener; |
| 27 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 28 | import org.onosproject.net.config.NetworkConfigService; |
| 29 | import org.onosproject.net.config.basics.SubjectFactories; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 30 | import org.osgi.service.component.annotations.Activate; |
| 31 | import org.osgi.service.component.annotations.Component; |
| 32 | import org.osgi.service.component.annotations.Deactivate; |
| 33 | import org.osgi.service.component.annotations.Reference; |
| 34 | import org.osgi.service.component.annotations.ReferenceCardinality; |
slowr | db071b2 | 2017-07-07 11:10:25 -0700 | [diff] [blame] | 35 | import org.slf4j.Logger; |
| 36 | import org.slf4j.LoggerFactory; |
| 37 | |
slowr | db071b2 | 2017-07-07 11:10:25 -0700 | [diff] [blame] | 38 | import java.util.Optional; |
slowr | db071b2 | 2017-07-07 11:10:25 -0700 | [diff] [blame] | 39 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 40 | @Component(immediate = true, service = ArtemisService.class) |
Dimitrios Mavrommatis | f0c0632 | 2017-10-31 23:49:04 -0700 | [diff] [blame] | 41 | public class ArtemisManager |
| 42 | extends AbstractListenerManager<ArtemisEvent, ArtemisEventListener> |
| 43 | implements ArtemisService { |
| 44 | |
slowr | db071b2 | 2017-07-07 11:10:25 -0700 | [diff] [blame] | 45 | private static final String ARTEMIS_APP_ID = "org.onosproject.artemis"; |
| 46 | private static final Class<ArtemisConfig> CONFIG_CLASS = ArtemisConfig.class; |
Dimitrios Mavrommatis | f0c0632 | 2017-10-31 23:49:04 -0700 | [diff] [blame] | 47 | private final Logger log = LoggerFactory.getLogger(getClass()); |
slowr | db071b2 | 2017-07-07 11:10:25 -0700 | [diff] [blame] | 48 | |
Dimitrios Mavrommatis | f0c0632 | 2017-10-31 23:49:04 -0700 | [diff] [blame] | 49 | private final InternalNetworkConfigListener configListener = |
| 50 | new InternalNetworkConfigListener(); |
| 51 | /* Services */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 52 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
slowr | db071b2 | 2017-07-07 11:10:25 -0700 | [diff] [blame] | 53 | private NetworkConfigRegistry registry; |
| 54 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 55 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
slowr | db071b2 | 2017-07-07 11:10:25 -0700 | [diff] [blame] | 56 | private NetworkConfigService configService; |
| 57 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 58 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
slowr | db071b2 | 2017-07-07 11:10:25 -0700 | [diff] [blame] | 59 | private CoreService coreService; |
| 60 | |
Dimitrios Mavrommatis | f0c0632 | 2017-10-31 23:49:04 -0700 | [diff] [blame] | 61 | /* Variables */ |
slowr | db071b2 | 2017-07-07 11:10:25 -0700 | [diff] [blame] | 62 | private ApplicationId appId; |
Dimitrios Mavrommatis | f0c0632 | 2017-10-31 23:49:04 -0700 | [diff] [blame] | 63 | private ArtemisConfig artemisConfig; |
slowr | db071b2 | 2017-07-07 11:10:25 -0700 | [diff] [blame] | 64 | |
slowr | db071b2 | 2017-07-07 11:10:25 -0700 | [diff] [blame] | 65 | |
Dimitrios Mavrommatis | f0c0632 | 2017-10-31 23:49:04 -0700 | [diff] [blame] | 66 | /* Config */ |
slowr | db071b2 | 2017-07-07 11:10:25 -0700 | [diff] [blame] | 67 | private ConfigFactory<ApplicationId, ArtemisConfig> artemisConfigFactory = |
| 68 | new ConfigFactory<ApplicationId, ArtemisConfig>( |
| 69 | SubjectFactories.APP_SUBJECT_FACTORY, ArtemisConfig.class, "artemis") { |
| 70 | @Override |
| 71 | public ArtemisConfig createConfig() { |
| 72 | return new ArtemisConfig(); |
| 73 | } |
| 74 | }; |
| 75 | |
| 76 | @Activate |
| 77 | protected void activate() { |
| 78 | appId = coreService.registerApplication(ARTEMIS_APP_ID); |
| 79 | configService.addListener(configListener); |
| 80 | registry.registerConfigFactory(artemisConfigFactory); |
Dimitrios Mavrommatis | f0c0632 | 2017-10-31 23:49:04 -0700 | [diff] [blame] | 81 | |
| 82 | eventDispatcher.addSink(ArtemisEvent.class, listenerRegistry); |
| 83 | |
| 84 | log.info("Artemis Service Started"); |
slowr | db071b2 | 2017-07-07 11:10:25 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Deactivate |
| 88 | protected void deactivate() { |
| 89 | configService.removeListener(configListener); |
| 90 | registry.unregisterConfigFactory(artemisConfigFactory); |
slowr | db071b2 | 2017-07-07 11:10:25 -0700 | [diff] [blame] | 91 | |
Dimitrios Mavrommatis | f0c0632 | 2017-10-31 23:49:04 -0700 | [diff] [blame] | 92 | eventDispatcher.removeSink(ArtemisEvent.class); |
slowr | db071b2 | 2017-07-07 11:10:25 -0700 | [diff] [blame] | 93 | |
Dimitrios Mavrommatis | f0c0632 | 2017-10-31 23:49:04 -0700 | [diff] [blame] | 94 | log.info("Artemis Service Stopped"); |
slowr | db071b2 | 2017-07-07 11:10:25 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Override |
Dimitrios Mavrommatis | f0c0632 | 2017-10-31 23:49:04 -0700 | [diff] [blame] | 98 | public Optional<ArtemisConfig> getConfig() { |
| 99 | return Optional.ofNullable(artemisConfig); |
slowr | db071b2 | 2017-07-07 11:10:25 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
| 103 | |
| 104 | @Override |
| 105 | public void event(NetworkConfigEvent event) { |
| 106 | switch (event.type()) { |
| 107 | case CONFIG_REGISTERED: |
Dimitrios Mavrommatis | f0c0632 | 2017-10-31 23:49:04 -0700 | [diff] [blame] | 108 | case CONFIG_UNREGISTERED: { |
slowr | db071b2 | 2017-07-07 11:10:25 -0700 | [diff] [blame] | 109 | break; |
Dimitrios Mavrommatis | f0c0632 | 2017-10-31 23:49:04 -0700 | [diff] [blame] | 110 | } |
| 111 | case CONFIG_REMOVED: { |
slowr | db071b2 | 2017-07-07 11:10:25 -0700 | [diff] [blame] | 112 | if (event.configClass() == CONFIG_CLASS) { |
Dimitrios Mavrommatis | f0c0632 | 2017-10-31 23:49:04 -0700 | [diff] [blame] | 113 | artemisConfig = null; |
slowr | db071b2 | 2017-07-07 11:10:25 -0700 | [diff] [blame] | 114 | } |
| 115 | break; |
Dimitrios Mavrommatis | f0c0632 | 2017-10-31 23:49:04 -0700 | [diff] [blame] | 116 | } |
| 117 | case CONFIG_UPDATED: |
| 118 | case CONFIG_ADDED: { |
| 119 | if (event.configClass() == CONFIG_CLASS) { |
| 120 | event.config().ifPresent(config -> artemisConfig = (ArtemisConfig) config); |
| 121 | } |
| 122 | break; |
| 123 | } |
slowr | db071b2 | 2017-07-07 11:10:25 -0700 | [diff] [blame] | 124 | default: |
| 125 | break; |
| 126 | } |
| 127 | } |
Dimitrios Mavrommatis | f0c0632 | 2017-10-31 23:49:04 -0700 | [diff] [blame] | 128 | |
slowr | db071b2 | 2017-07-07 11:10:25 -0700 | [diff] [blame] | 129 | } |
Dimitrios Mavrommatis | f0c0632 | 2017-10-31 23:49:04 -0700 | [diff] [blame] | 130 | |
slowr | db071b2 | 2017-07-07 11:10:25 -0700 | [diff] [blame] | 131 | } |