blob: f276c7cae524bd33bff34c67d3452b2d9b66fdcf [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 Moon9661d642015-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 Moon9661d642015-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 Moon9661d642015-09-23 13:24:35 -070065 private final NetworkConfigListener configListener = new InternalConfigListener();
66
Hyunsun Moon9661d642015-09-23 13:24:35 -070067 private ApplicationId appId;
68
69 @Activate
70 protected void active() {
Hyunsun Moon9661d642015-09-23 13:24:35 -070071 appId = coreService.getAppId(CordVtnService.CORDVTN_APP_ID);
72
73 configService.addListener(configListener);
74 configRegistry.registerConfigFactory(configFactory);
75
Hyunsun Moon7dca9b32015-10-08 22:25:30 -070076 readConfiguration();
Hyunsun Moon9661d642015-09-23 13:24:35 -070077 }
78
79 @Deactivate
80 protected void deactivate() {
Hyunsun Moon9661d642015-09-23 13:24:35 -070081 configRegistry.unregisterConfigFactory(configFactory);
82 configService.removeListener(configListener);
83 }
84
85 private void readConfiguration() {
86 CordVtnConfig config = configRegistry.getConfig(appId, CordVtnConfig.class);
87
88 if (config == null) {
89 log.warn("No configuration found");
90 return;
91 }
92
93 config.ovsdbNodes().forEach(node -> {
Hyunsun Moon7dca9b32015-10-08 22:25:30 -070094 DefaultOvsdbNode ovsdb = new DefaultOvsdbNode(
95 node.host(), node.ip(), node.port(), node.bridgeId());
96 cordVtnService.addNode(ovsdb);
97 cordVtnService.connect(ovsdb);
Hyunsun Moon9661d642015-09-23 13:24:35 -070098 });
99 }
100
Hyunsun Moon9661d642015-09-23 13:24:35 -0700101 private class InternalConfigListener implements NetworkConfigListener {
102
103 @Override
104 public void event(NetworkConfigEvent event) {
105 // TODO handle update event
106 }
107 }
108}