blob: a0872e5a8d41fcbe22ebf585a0b216940f1fb16e [file] [log] [blame]
slowrdb071b22017-07-07 11:10:25 -07001/*
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -07002 * Copyright 2017-present Open Networking Foundation
slowrdb071b22017-07-07 11:10:25 -07003 *
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 Mavrommatisf0c06322017-10-31 23:49:04 -070016
slowrdb071b22017-07-07 11:10:25 -070017package org.onosproject.artemis.impl;
18
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070019import org.onosproject.artemis.ArtemisEventListener;
20import org.onosproject.artemis.ArtemisService;
slowrdb071b22017-07-07 11:10:25 -070021import org.onosproject.core.ApplicationId;
22import org.onosproject.core.CoreService;
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070023import org.onosproject.event.AbstractListenerManager;
slowrdb071b22017-07-07 11:10:25 -070024import org.onosproject.net.config.ConfigFactory;
25import org.onosproject.net.config.NetworkConfigEvent;
26import org.onosproject.net.config.NetworkConfigListener;
27import org.onosproject.net.config.NetworkConfigRegistry;
28import org.onosproject.net.config.NetworkConfigService;
29import org.onosproject.net.config.basics.SubjectFactories;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070030import org.osgi.service.component.annotations.Activate;
31import org.osgi.service.component.annotations.Component;
32import org.osgi.service.component.annotations.Deactivate;
33import org.osgi.service.component.annotations.Reference;
34import org.osgi.service.component.annotations.ReferenceCardinality;
slowrdb071b22017-07-07 11:10:25 -070035import org.slf4j.Logger;
36import org.slf4j.LoggerFactory;
37
slowrdb071b22017-07-07 11:10:25 -070038import java.util.Optional;
slowrdb071b22017-07-07 11:10:25 -070039
Ray Milkeyd84f89b2018-08-17 14:54:17 -070040@Component(immediate = true, service = ArtemisService.class)
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070041public class ArtemisManager
42 extends AbstractListenerManager<ArtemisEvent, ArtemisEventListener>
43 implements ArtemisService {
44
slowrdb071b22017-07-07 11:10:25 -070045 private static final String ARTEMIS_APP_ID = "org.onosproject.artemis";
46 private static final Class<ArtemisConfig> CONFIG_CLASS = ArtemisConfig.class;
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070047 private final Logger log = LoggerFactory.getLogger(getClass());
slowrdb071b22017-07-07 11:10:25 -070048
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070049 private final InternalNetworkConfigListener configListener =
50 new InternalNetworkConfigListener();
51 /* Services */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070052 @Reference(cardinality = ReferenceCardinality.MANDATORY)
slowrdb071b22017-07-07 11:10:25 -070053 private NetworkConfigRegistry registry;
54
Ray Milkeyd84f89b2018-08-17 14:54:17 -070055 @Reference(cardinality = ReferenceCardinality.MANDATORY)
slowrdb071b22017-07-07 11:10:25 -070056 private NetworkConfigService configService;
57
Ray Milkeyd84f89b2018-08-17 14:54:17 -070058 @Reference(cardinality = ReferenceCardinality.MANDATORY)
slowrdb071b22017-07-07 11:10:25 -070059 private CoreService coreService;
60
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070061 /* Variables */
slowrdb071b22017-07-07 11:10:25 -070062 private ApplicationId appId;
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070063 private ArtemisConfig artemisConfig;
slowrdb071b22017-07-07 11:10:25 -070064
slowrdb071b22017-07-07 11:10:25 -070065
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070066 /* Config */
slowrdb071b22017-07-07 11:10:25 -070067 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 Mavrommatisf0c06322017-10-31 23:49:04 -070081
82 eventDispatcher.addSink(ArtemisEvent.class, listenerRegistry);
83
84 log.info("Artemis Service Started");
slowrdb071b22017-07-07 11:10:25 -070085 }
86
87 @Deactivate
88 protected void deactivate() {
89 configService.removeListener(configListener);
90 registry.unregisterConfigFactory(artemisConfigFactory);
slowrdb071b22017-07-07 11:10:25 -070091
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070092 eventDispatcher.removeSink(ArtemisEvent.class);
slowrdb071b22017-07-07 11:10:25 -070093
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070094 log.info("Artemis Service Stopped");
slowrdb071b22017-07-07 11:10:25 -070095 }
96
97 @Override
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070098 public Optional<ArtemisConfig> getConfig() {
99 return Optional.ofNullable(artemisConfig);
slowrdb071b22017-07-07 11:10:25 -0700100 }
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 Mavrommatisf0c06322017-10-31 23:49:04 -0700108 case CONFIG_UNREGISTERED: {
slowrdb071b22017-07-07 11:10:25 -0700109 break;
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -0700110 }
111 case CONFIG_REMOVED: {
slowrdb071b22017-07-07 11:10:25 -0700112 if (event.configClass() == CONFIG_CLASS) {
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -0700113 artemisConfig = null;
slowrdb071b22017-07-07 11:10:25 -0700114 }
115 break;
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -0700116 }
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 }
slowrdb071b22017-07-07 11:10:25 -0700124 default:
125 break;
126 }
127 }
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -0700128
slowrdb071b22017-07-07 11:10:25 -0700129 }
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -0700130
slowrdb071b22017-07-07 11:10:25 -0700131}