blob: ebba4cd585594c4029616d9a333c630b62a84765 [file] [log] [blame]
Hyunsun Moond0e932a2015-09-15 22:39:16 -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
Hyunsun Moon2b530322015-09-23 13:24:35 -070018import 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;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070024import org.onosproject.cluster.LeadershipService;
25import org.onosproject.cluster.NodeId;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070026import org.onosproject.mastership.MastershipService;
Hyunsun Moon2b530322015-09-23 13:24:35 -070027import org.onosproject.net.Device;
28import org.onosproject.net.device.DeviceEvent;
29import org.onosproject.net.device.DeviceListener;
30import org.onosproject.net.device.DeviceService;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070031import org.slf4j.Logger;
32
33import java.util.concurrent.Executors;
34import java.util.concurrent.ScheduledExecutorService;
35import java.util.concurrent.TimeUnit;
36
37import static org.onlab.util.Tools.groupedThreads;
Hyunsun Moon2b530322015-09-23 13:24:35 -070038import static org.onosproject.cordvtn.OvsdbNode.State.CONNECTED;
39import static org.onosproject.cordvtn.OvsdbNode.State.DISCONNECTED;
40import static org.onosproject.cordvtn.OvsdbNode.State.READY;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070041import static org.slf4j.LoggerFactory.getLogger;
42
43/**
Hyunsun Moon2b530322015-09-23 13:24:35 -070044 * Provides the connection state management of all nodes registered to the service
45 * so that the nodes keep connected unless it is requested to be deleted.
Hyunsun Moond0e932a2015-09-15 22:39:16 -070046 */
Hyunsun Moon2b530322015-09-23 13:24:35 -070047@Component(immediate = true)
Hyunsun Moond0e932a2015-09-15 22:39:16 -070048public class NodeConnectionManager {
49 protected final Logger log = getLogger(getClass());
50
Hyunsun Moon2b530322015-09-23 13:24:35 -070051 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
52 MastershipService mastershipService;
53
54 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
55 LeadershipService leadershipService;
56
57 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
58 ClusterService clusterService;
59
60 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
61 DeviceService deviceService;
62
63 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
64 CordVtnService cordVtnService;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070065
66 private static final int DELAY_SEC = 5;
Hyunsun Moond0e932a2015-09-15 22:39:16 -070067
Hyunsun Moon2b530322015-09-23 13:24:35 -070068 private final DeviceListener deviceListener = new InternalDeviceListener();
69 private final ScheduledExecutorService connectionExecutor = Executors
70 .newSingleThreadScheduledExecutor(groupedThreads("onos/cordvtn", "connection-manager"));
Hyunsun Moond0e932a2015-09-15 22:39:16 -070071
Hyunsun Moon2b530322015-09-23 13:24:35 -070072 private NodeId localId;
73
74 @Activate
75 protected void activate() {
76 localId = clusterService.getLocalNode().id();
77 deviceService.addListener(deviceListener);
78
79 connectionExecutor.scheduleWithFixedDelay(() -> cordVtnService.getNodes()
Hyunsun Moond0e932a2015-09-15 22:39:16 -070080 .stream()
81 .filter(node -> localId.equals(getMaster(node)))
Hyunsun Moon2b530322015-09-23 13:24:35 -070082 .forEach(node -> {
83 connect(node);
84 disconnect(node);
85 }), 0, DELAY_SEC, TimeUnit.SECONDS);
Hyunsun Moond0e932a2015-09-15 22:39:16 -070086 }
87
Hyunsun Moon2b530322015-09-23 13:24:35 -070088 @Deactivate
Hyunsun Moond0e932a2015-09-15 22:39:16 -070089 public void stop() {
90 connectionExecutor.shutdown();
Hyunsun Moon2b530322015-09-23 13:24:35 -070091 deviceService.removeListener(deviceListener);
Hyunsun Moond0e932a2015-09-15 22:39:16 -070092 }
93
Hyunsun Moon2b530322015-09-23 13:24:35 -070094 public void connect(OvsdbNode ovsdbNode) {
Hyunsun Moond0e932a2015-09-15 22:39:16 -070095 switch (ovsdbNode.state()) {
96 case INIT:
97 case DISCONNECTED:
Hyunsun Moon2b530322015-09-23 13:24:35 -070098 setPassiveMode(ovsdbNode);
Hyunsun Moond0e932a2015-09-15 22:39:16 -070099 case READY:
Hyunsun Moon2b530322015-09-23 13:24:35 -0700100 setupConnection(ovsdbNode);
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700101 break;
102 default:
Hyunsun Moon2b530322015-09-23 13:24:35 -0700103 break;
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700104 }
105 }
106
Hyunsun Moon2b530322015-09-23 13:24:35 -0700107 public void disconnect(OvsdbNode ovsdbNode) {
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700108 switch (ovsdbNode.state()) {
Hyunsun Moon2b530322015-09-23 13:24:35 -0700109 case DISCONNECT:
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700110 // TODO: disconnect
111 break;
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700112 default:
Hyunsun Moon2b530322015-09-23 13:24:35 -0700113 break;
114 }
115 }
116
117 private class InternalDeviceListener implements DeviceListener {
118
119 @Override
120 public void event(DeviceEvent event) {
121 Device device = event.subject();
122 if (device.type() != Device.Type.CONTROLLER) {
123 return;
124 }
125
126 DefaultOvsdbNode node;
127 switch (event.type()) {
128 case DEVICE_ADDED:
129 node = (DefaultOvsdbNode) cordVtnService.getNode(device.id());
130 if (node != null) {
131 cordVtnService.updateNode(node, CONNECTED);
132 }
133 break;
134 case DEVICE_AVAILABILITY_CHANGED:
135 node = (DefaultOvsdbNode) cordVtnService.getNode(device.id());
136 if (node != null) {
137 cordVtnService.updateNode(node, DISCONNECTED);
138 }
139 break;
140 default:
141 break;
142 }
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700143 }
144 }
145
146 private NodeId getMaster(OvsdbNode ovsdbNode) {
Hyunsun Moon2b530322015-09-23 13:24:35 -0700147 NodeId master = mastershipService.getMasterFor(ovsdbNode.intBrId());
148
149 // master is null if there's no such device
150 if (master == null) {
151 master = leadershipService.getLeader(CordVtnService.CORDVTN_APP_ID);
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700152 }
Hyunsun Moon2b530322015-09-23 13:24:35 -0700153 return master;
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700154 }
155
156 private void setPassiveMode(OvsdbNode ovsdbNode) {
157 // TODO: need ovsdb client implementation first
158 // TODO: set the remove ovsdb server passive mode
Hyunsun Moon2b530322015-09-23 13:24:35 -0700159 cordVtnService.updateNode(ovsdbNode, READY);
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700160 }
161
Hyunsun Moon2b530322015-09-23 13:24:35 -0700162 private void setupConnection(OvsdbNode ovsdbNode) {
163 // TODO initiate connection
Hyunsun Moond0e932a2015-09-15 22:39:16 -0700164 }
165}