blob: 4ff133b6814f4028e102d5fd247166b81278687a [file] [log] [blame]
Hyunsun Moon2b530322015-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 Moon2b530322015-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 Moonbaf70542015-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 Moon2b530322015-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 Moon2b530322015-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 Moon2b530322015-09-23 13:24:35 -070069 private final NetworkConfigListener configListener = new InternalConfigListener();
Hyunsun Moon2b530322015-09-23 13:24:35 -070070 private ApplicationId appId;
Hyunsun Moonbaf70542015-12-16 14:29:36 -080071 protected ExecutorService eventExecutor;
72
Hyunsun Moon2b530322015-09-23 13:24:35 -070073
74 @Activate
75 protected void active() {
Hyunsun Moon2b530322015-09-23 13:24:35 -070076 appId = coreService.getAppId(CordVtnService.CORDVTN_APP_ID);
77
Hyunsun Moonbaf70542015-12-16 14:29:36 -080078 eventExecutor = newSingleThreadScheduledExecutor(groupedThreads("onos/cordvtncfg", "event-handler"));
Hyunsun Moon2b530322015-09-23 13:24:35 -070079 configService.addListener(configListener);
80 configRegistry.registerConfigFactory(configFactory);
Hyunsun Moon2b530322015-09-23 13:24:35 -070081 }
82
83 @Deactivate
84 protected void deactivate() {
Hyunsun Moon2b530322015-09-23 13:24:35 -070085 configRegistry.unregisterConfigFactory(configFactory);
86 configService.removeListener(configListener);
Hyunsun Moonbaf70542015-12-16 14:29:36 -080087 eventExecutor.shutdown();
Hyunsun Moon2b530322015-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 Moon8539b042015-11-07 22:08:43 -080098 config.cordVtnNodes().forEach(node -> {
99 CordVtnNode cordVtnNode = new CordVtnNode(
Hyunsun Moonb219fc42016-01-14 03:42:47 -0800100 node.hostname(),
101 node.ovsdbIp(),
102 node.ovsdbPort(),
103 node.bridgeId(),
104 node.phyPortName(),
105 node.localIp());
106
Hyunsun Moon8539b042015-11-07 22:08:43 -0800107 cordVtnService.addNode(cordVtnNode);
Hyunsun Moon2b530322015-09-23 13:24:35 -0700108 });
109 }
110
Hyunsun Moon2b530322015-09-23 13:24:35 -0700111 private class InternalConfigListener implements NetworkConfigListener {
112
113 @Override
114 public void event(NetworkConfigEvent event) {
Hyunsun Moonfae11ee2015-10-27 15:40:22 -0700115 if (!event.configClass().equals(CordVtnConfig.class)) {
116 return;
117 }
118
119 switch (event.type()) {
120 case CONFIG_ADDED:
121 log.info("Network configuration added");
Hyunsun Moonbaf70542015-12-16 14:29:36 -0800122 eventExecutor.execute(CordVtnConfigManager.this::readConfiguration);
Hyunsun Moonfae11ee2015-10-27 15:40:22 -0700123 break;
124 case CONFIG_UPDATED:
125 log.info("Network configuration updated");
Hyunsun Moonbaf70542015-12-16 14:29:36 -0800126 eventExecutor.execute(CordVtnConfigManager.this::readConfiguration);
Hyunsun Moonfae11ee2015-10-27 15:40:22 -0700127 break;
128 default:
129 break;
130 }
Hyunsun Moon2b530322015-09-23 13:24:35 -0700131 }
132 }
133}