blob: 043b376094e268c023b7d2f3534c1ba477dce4dc [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;
23import org.onosproject.cluster.ClusterService;
24import org.onosproject.cluster.LeadershipEvent;
25import org.onosproject.cluster.LeadershipEventListener;
26import org.onosproject.cluster.LeadershipService;
27import org.onosproject.cluster.NodeId;
28import org.onosproject.core.ApplicationId;
29import org.onosproject.core.CoreService;
30import 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;
36import org.slf4j.Logger;
37
38import static org.onosproject.cordvtn.OvsdbNode.State.INIT;
39import static org.slf4j.LoggerFactory.getLogger;
40
41/**
42 * Reads node information from the network config file and handles the config
43 * update events.
44 * Only a leader controller performs the node addition or deletion.
45 */
46@Component(immediate = true)
47public class CordVtnConfigManager {
48
49 protected final Logger log = getLogger(getClass());
50
51 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
52 protected CoreService coreService;
53
54 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
55 protected NetworkConfigRegistry configRegistry;
56
57 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
58 protected NetworkConfigService configService;
59
60 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
61 protected LeadershipService leadershipService;
62
63 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
64 protected ClusterService clusterService;
65
66 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
67 protected CordVtnService cordVtnService;
68
69 private final ConfigFactory configFactory =
70 new ConfigFactory(SubjectFactories.APP_SUBJECT_FACTORY, CordVtnConfig.class, "cordvtn") {
71 @Override
72 public CordVtnConfig createConfig() {
73 return new CordVtnConfig();
74 }
75 };
76
77 private final LeadershipEventListener leadershipListener = new InternalLeadershipListener();
78 private final NetworkConfigListener configListener = new InternalConfigListener();
79
80 private NodeId local;
81 private ApplicationId appId;
82
83 @Activate
84 protected void active() {
85 local = clusterService.getLocalNode().id();
86 appId = coreService.getAppId(CordVtnService.CORDVTN_APP_ID);
87
88 configService.addListener(configListener);
89 configRegistry.registerConfigFactory(configFactory);
90
91 leadershipService.addListener(leadershipListener);
92 leadershipService.runForLeadership(CordVtnService.CORDVTN_APP_ID);
93 }
94
95 @Deactivate
96 protected void deactivate() {
97 leadershipService.removeListener(leadershipListener);
98 leadershipService.withdraw(appId.name());
99
100 configRegistry.unregisterConfigFactory(configFactory);
101 configService.removeListener(configListener);
102 }
103
104 private void readConfiguration() {
105 CordVtnConfig config = configRegistry.getConfig(appId, CordVtnConfig.class);
106
107 if (config == null) {
108 log.warn("No configuration found");
109 return;
110 }
111
112 config.ovsdbNodes().forEach(node -> {
113 DefaultOvsdbNode ovsdbNode =
114 new DefaultOvsdbNode(node.host(), node.ip(), node.port(), INIT);
115 cordVtnService.addNode(ovsdbNode);
116 log.info("Add new node {}", node.host());
117 });
118 }
119
120 private synchronized void processLeadershipChange(NodeId leader) {
121 if (leader == null || !leader.equals(local)) {
122 return;
123 }
124 readConfiguration();
125 }
126
127 private class InternalLeadershipListener implements LeadershipEventListener {
128
129 @Override
130 public void event(LeadershipEvent event) {
131 if (event.subject().topic().equals(appId.name())) {
132 processLeadershipChange(event.subject().leader());
133 }
134 }
135 }
136
137 private class InternalConfigListener implements NetworkConfigListener {
138
139 @Override
140 public void event(NetworkConfigEvent event) {
141 // TODO handle update event
142 }
143 }
144}