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