blob: c714b05a2336b1ca2e5f7ccc404a071419d40725 [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
slowrdb071b22017-07-07 11:10:25 -070019import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.apache.felix.scr.annotations.Service;
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070025import org.onosproject.artemis.ArtemisEventListener;
26import org.onosproject.artemis.ArtemisService;
slowrdb071b22017-07-07 11:10:25 -070027import org.onosproject.core.ApplicationId;
28import org.onosproject.core.CoreService;
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070029import org.onosproject.event.AbstractListenerManager;
slowrdb071b22017-07-07 11:10:25 -070030import org.onosproject.net.config.ConfigFactory;
31import org.onosproject.net.config.NetworkConfigEvent;
32import org.onosproject.net.config.NetworkConfigListener;
33import org.onosproject.net.config.NetworkConfigRegistry;
34import org.onosproject.net.config.NetworkConfigService;
35import org.onosproject.net.config.basics.SubjectFactories;
slowrdb071b22017-07-07 11:10:25 -070036import org.slf4j.Logger;
37import org.slf4j.LoggerFactory;
38
slowrdb071b22017-07-07 11:10:25 -070039import java.util.Optional;
slowrdb071b22017-07-07 11:10:25 -070040
slowrdb071b22017-07-07 11:10:25 -070041@Component(immediate = true)
42@Service
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070043public class ArtemisManager
44 extends AbstractListenerManager<ArtemisEvent, ArtemisEventListener>
45 implements ArtemisService {
46
slowrdb071b22017-07-07 11:10:25 -070047 private static final String ARTEMIS_APP_ID = "org.onosproject.artemis";
48 private static final Class<ArtemisConfig> CONFIG_CLASS = ArtemisConfig.class;
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070049 private final Logger log = LoggerFactory.getLogger(getClass());
slowrdb071b22017-07-07 11:10:25 -070050
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070051 private final InternalNetworkConfigListener configListener =
52 new InternalNetworkConfigListener();
53 /* Services */
slowrdb071b22017-07-07 11:10:25 -070054 @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 Mavrommatisf0c06322017-10-31 23:49:04 -070063 /* Variables */
slowrdb071b22017-07-07 11:10:25 -070064 private ApplicationId appId;
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070065 private ArtemisConfig artemisConfig;
slowrdb071b22017-07-07 11:10:25 -070066
slowrdb071b22017-07-07 11:10:25 -070067
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070068 /* Config */
slowrdb071b22017-07-07 11:10:25 -070069 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 Mavrommatisf0c06322017-10-31 23:49:04 -070083
84 eventDispatcher.addSink(ArtemisEvent.class, listenerRegistry);
85
86 log.info("Artemis Service Started");
slowrdb071b22017-07-07 11:10:25 -070087 }
88
89 @Deactivate
90 protected void deactivate() {
91 configService.removeListener(configListener);
92 registry.unregisterConfigFactory(artemisConfigFactory);
slowrdb071b22017-07-07 11:10:25 -070093
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070094 eventDispatcher.removeSink(ArtemisEvent.class);
slowrdb071b22017-07-07 11:10:25 -070095
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070096 log.info("Artemis Service Stopped");
slowrdb071b22017-07-07 11:10:25 -070097 }
98
99 @Override
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -0700100 public Optional<ArtemisConfig> getConfig() {
101 return Optional.ofNullable(artemisConfig);
slowrdb071b22017-07-07 11:10:25 -0700102 }
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 Mavrommatisf0c06322017-10-31 23:49:04 -0700110 case CONFIG_UNREGISTERED: {
slowrdb071b22017-07-07 11:10:25 -0700111 break;
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -0700112 }
113 case CONFIG_REMOVED: {
slowrdb071b22017-07-07 11:10:25 -0700114 if (event.configClass() == CONFIG_CLASS) {
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -0700115 artemisConfig = null;
slowrdb071b22017-07-07 11:10:25 -0700116 }
117 break;
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -0700118 }
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 }
slowrdb071b22017-07-07 11:10:25 -0700126 default:
127 break;
128 }
129 }
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -0700130
slowrdb071b22017-07-07 11:10:25 -0700131 }
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -0700132
slowrdb071b22017-07-07 11:10:25 -0700133}