blob: 274ca9b4f51abacc8a5423155cdcc83e51cb40ca [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 Moon2b530322015-09-23 13:24:35 -070033import static org.slf4j.LoggerFactory.getLogger;
34
35/**
36 * Reads node information from the network config file and handles the config
37 * update events.
38 * Only a leader controller performs the node addition or deletion.
39 */
40@Component(immediate = true)
41public class CordVtnConfigManager {
42
43 protected final Logger log = getLogger(getClass());
44
45 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
46 protected CoreService coreService;
47
48 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
49 protected NetworkConfigRegistry configRegistry;
50
51 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
52 protected NetworkConfigService configService;
53
54 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Hyunsun Moon2b530322015-09-23 13:24:35 -070055 protected CordVtnService cordVtnService;
56
57 private final ConfigFactory configFactory =
58 new ConfigFactory(SubjectFactories.APP_SUBJECT_FACTORY, CordVtnConfig.class, "cordvtn") {
59 @Override
60 public CordVtnConfig createConfig() {
61 return new CordVtnConfig();
62 }
63 };
64
Hyunsun Moon2b530322015-09-23 13:24:35 -070065 private final NetworkConfigListener configListener = new InternalConfigListener();
66
Hyunsun Moon2b530322015-09-23 13:24:35 -070067 private ApplicationId appId;
68
69 @Activate
70 protected void active() {
Hyunsun Moon2b530322015-09-23 13:24:35 -070071 appId = coreService.getAppId(CordVtnService.CORDVTN_APP_ID);
72
73 configService.addListener(configListener);
74 configRegistry.registerConfigFactory(configFactory);
Hyunsun Moon2b530322015-09-23 13:24:35 -070075 }
76
77 @Deactivate
78 protected void deactivate() {
Hyunsun Moon2b530322015-09-23 13:24:35 -070079 configRegistry.unregisterConfigFactory(configFactory);
80 configService.removeListener(configListener);
81 }
82
83 private void readConfiguration() {
84 CordVtnConfig config = configRegistry.getConfig(appId, CordVtnConfig.class);
85
86 if (config == null) {
87 log.warn("No configuration found");
88 return;
89 }
90
91 config.ovsdbNodes().forEach(node -> {
Hyunsun Moon1f145552015-10-08 22:25:30 -070092 DefaultOvsdbNode ovsdb = new DefaultOvsdbNode(
93 node.host(), node.ip(), node.port(), node.bridgeId());
94 cordVtnService.addNode(ovsdb);
Hyunsun Moon2b530322015-09-23 13:24:35 -070095 });
96 }
97
Hyunsun Moon2b530322015-09-23 13:24:35 -070098 private class InternalConfigListener implements NetworkConfigListener {
99
100 @Override
101 public void event(NetworkConfigEvent event) {
Hyunsun Moonfae11ee2015-10-27 15:40:22 -0700102 if (!event.configClass().equals(CordVtnConfig.class)) {
103 return;
104 }
105
106 switch (event.type()) {
107 case CONFIG_ADDED:
108 log.info("Network configuration added");
109 readConfiguration();
110 break;
111 case CONFIG_UPDATED:
112 log.info("Network configuration updated");
Hyunsun Moond772f342015-10-28 20:28:16 -0700113 readConfiguration();
Hyunsun Moonfae11ee2015-10-27 15:40:22 -0700114 break;
115 default:
116 break;
117 }
Hyunsun Moon2b530322015-09-23 13:24:35 -0700118 }
119 }
120}