blob: 525135d76ea5243e2c3f348dd25fd3138dccf113 [file] [log] [blame]
Hyunsun Moon9661d642015-09-23 13:24:35 -07001/*
2 * Copyright 2014-2015 Open Networking Laboratory
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 */
16package org.onosproject.cordvtn;
17
18import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
21import org.apache.felix.scr.annotations.Reference;
22import org.apache.felix.scr.annotations.ReferenceCardinality;
Hyunsun Moon9661d642015-09-23 13:24:35 -070023import org.onosproject.core.ApplicationId;
24import org.onosproject.core.CoreService;
25import org.onosproject.net.config.ConfigFactory;
26import org.onosproject.net.config.NetworkConfigEvent;
27import org.onosproject.net.config.NetworkConfigListener;
28import org.onosproject.net.config.NetworkConfigRegistry;
29import org.onosproject.net.config.NetworkConfigService;
30import org.onosproject.net.config.basics.SubjectFactories;
31import org.slf4j.Logger;
32
Hyunsun Moonca464562015-12-16 14:29:36 -080033import java.util.concurrent.ExecutorService;
34
35import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
36import static org.onlab.util.Tools.groupedThreads;
Hyunsun Moon9661d642015-09-23 13:24:35 -070037import static org.slf4j.LoggerFactory.getLogger;
38
39/**
40 * Reads node information from the network config file and handles the config
41 * update events.
42 * Only a leader controller performs the node addition or deletion.
43 */
44@Component(immediate = true)
45public class CordVtnConfigManager {
46
47 protected final Logger log = getLogger(getClass());
48
49 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
50 protected CoreService coreService;
51
52 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
53 protected NetworkConfigRegistry configRegistry;
54
55 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
56 protected NetworkConfigService configService;
57
58 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Hyunsun Moon9661d642015-09-23 13:24:35 -070059 protected CordVtnService cordVtnService;
60
61 private final ConfigFactory configFactory =
62 new ConfigFactory(SubjectFactories.APP_SUBJECT_FACTORY, CordVtnConfig.class, "cordvtn") {
63 @Override
64 public CordVtnConfig createConfig() {
65 return new CordVtnConfig();
66 }
67 };
68
Hyunsun Moon9661d642015-09-23 13:24:35 -070069 private final NetworkConfigListener configListener = new InternalConfigListener();
Hyunsun Moon9661d642015-09-23 13:24:35 -070070 private ApplicationId appId;
Hyunsun Moonca464562015-12-16 14:29:36 -080071 protected ExecutorService eventExecutor;
72
Hyunsun Moon9661d642015-09-23 13:24:35 -070073
74 @Activate
75 protected void active() {
Hyunsun Moon9661d642015-09-23 13:24:35 -070076 appId = coreService.getAppId(CordVtnService.CORDVTN_APP_ID);
77
Hyunsun Moonca464562015-12-16 14:29:36 -080078 eventExecutor = newSingleThreadScheduledExecutor(groupedThreads("onos/cordvtncfg", "event-handler"));
Hyunsun Moon9661d642015-09-23 13:24:35 -070079 configService.addListener(configListener);
80 configRegistry.registerConfigFactory(configFactory);
Hyunsun Moon9661d642015-09-23 13:24:35 -070081 }
82
83 @Deactivate
84 protected void deactivate() {
Hyunsun Moon9661d642015-09-23 13:24:35 -070085 configRegistry.unregisterConfigFactory(configFactory);
86 configService.removeListener(configListener);
Hyunsun Moonca464562015-12-16 14:29:36 -080087 eventExecutor.shutdown();
Hyunsun Moon9661d642015-09-23 13:24:35 -070088 }
89
90 private void readConfiguration() {
91 CordVtnConfig config = configRegistry.getConfig(appId, CordVtnConfig.class);
92
93 if (config == null) {
94 log.warn("No configuration found");
95 return;
96 }
97
Hyunsun Moon4edb0172015-11-07 22:08:43 -080098 config.cordVtnNodes().forEach(node -> {
99 CordVtnNode cordVtnNode = new CordVtnNode(
100 node.hostname(), node.ovsdbIp(), node.ovsdbPort(), node.bridgeId());
101 cordVtnService.addNode(cordVtnNode);
Hyunsun Moon9661d642015-09-23 13:24:35 -0700102 });
103 }
104
Hyunsun Moon9661d642015-09-23 13:24:35 -0700105 private class InternalConfigListener implements NetworkConfigListener {
106
107 @Override
108 public void event(NetworkConfigEvent event) {
Hyunsun Moon61c98382015-10-27 15:40:22 -0700109 if (!event.configClass().equals(CordVtnConfig.class)) {
110 return;
111 }
112
113 switch (event.type()) {
114 case CONFIG_ADDED:
115 log.info("Network configuration added");
Hyunsun Moonca464562015-12-16 14:29:36 -0800116 eventExecutor.execute(CordVtnConfigManager.this::readConfiguration);
Hyunsun Moon61c98382015-10-27 15:40:22 -0700117 break;
118 case CONFIG_UPDATED:
119 log.info("Network configuration updated");
Hyunsun Moonca464562015-12-16 14:29:36 -0800120 eventExecutor.execute(CordVtnConfigManager.this::readConfiguration);
Hyunsun Moon61c98382015-10-27 15:40:22 -0700121 break;
122 default:
123 break;
124 }
Hyunsun Moon9661d642015-09-23 13:24:35 -0700125 }
126 }
127}