blob: 908aff27a9b2b2407a1f62efa493836fde01a036 [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
18import org.onosproject.cluster.LeadershipService;
19import org.onosproject.cluster.NodeId;
20import org.onosproject.core.ApplicationId;
21import org.onosproject.mastership.MastershipService;
22import org.onosproject.net.DeviceId;
23import org.onosproject.store.service.EventuallyConsistentMap;
24import org.slf4j.Logger;
25
26import java.util.concurrent.Executors;
27import java.util.concurrent.ScheduledExecutorService;
28import java.util.concurrent.TimeUnit;
29
30import static org.onlab.util.Tools.groupedThreads;
31import static org.slf4j.LoggerFactory.getLogger;
32
33/**
34 * Node connection manager.
35 */
36public class NodeConnectionManager {
37 protected final Logger log = getLogger(getClass());
38
39 private final ApplicationId appId;
40 private final NodeId localId;
41 private final EventuallyConsistentMap<DeviceId, OvsdbNode> nodeStore;
42 private final MastershipService mastershipService;
43 private final LeadershipService leadershipService;
44
45 private static final int DELAY_SEC = 5;
46 private ScheduledExecutorService connectionExecutor;
47
48 /**
49 * Creates a new NodeConnectionManager.
50 *
Thomas Vachuska0ae45602015-09-22 17:06:19 -070051 * @param appId app id
52 * @param localId local id
53 * @param nodeStore node store
Hyunsun Moond0e932a2015-09-15 22:39:16 -070054 * @param mastershipService mastership service
Thomas Vachuska0ae45602015-09-22 17:06:19 -070055 * @param leadershipService leadership service
Hyunsun Moond0e932a2015-09-15 22:39:16 -070056 */
57 public NodeConnectionManager(ApplicationId appId, NodeId localId,
58 EventuallyConsistentMap<DeviceId, OvsdbNode> nodeStore,
59 MastershipService mastershipService,
60 LeadershipService leadershipService) {
61 this.appId = appId;
62 this.localId = localId;
63 this.nodeStore = nodeStore;
64 this.mastershipService = mastershipService;
65 this.leadershipService = leadershipService;
66 }
67
68 /**
69 * Starts the node connection manager.
70 */
71 public void start() {
72 connectionExecutor = Executors.newSingleThreadScheduledExecutor(
Thomas Vachuska0ae45602015-09-22 17:06:19 -070073 groupedThreads("onos/cordvtn", "connection-executor"));
Hyunsun Moond0e932a2015-09-15 22:39:16 -070074 connectionExecutor.scheduleWithFixedDelay(() -> nodeStore.values()
75 .stream()
76 .filter(node -> localId.equals(getMaster(node)))
Thomas Vachuska0ae45602015-09-22 17:06:19 -070077 .forEach(this::connectNode), 0, DELAY_SEC, TimeUnit.SECONDS);
Hyunsun Moond0e932a2015-09-15 22:39:16 -070078 }
79
80 /**
81 * Stops the node connection manager.
82 */
83 public void stop() {
84 connectionExecutor.shutdown();
85 }
86
87 /**
88 * Adds a new node to the system.
89 *
90 * @param ovsdbNode ovsdb node
91 */
92 public void connectNode(OvsdbNode ovsdbNode) {
93 switch (ovsdbNode.state()) {
94 case INIT:
95 case DISCONNECTED:
96 // TODO: set the node to passive mode
97 case READY:
98 // TODO: initiate connection
99 break;
100 case CONNECTED:
101 break;
102 default:
103 }
104 }
105
106 /**
107 * Deletes the ovsdb node.
108 *
109 * @param ovsdbNode ovsdb node
110 */
111 public void disconnectNode(OvsdbNode ovsdbNode) {
112 switch (ovsdbNode.state()) {
113 case CONNECTED:
114 // TODO: disconnect
115 break;
116 case INIT:
117 case READY:
118 case DISCONNECTED:
119 break;
120 default:
121 }
122 }
123
124 private NodeId getMaster(OvsdbNode ovsdbNode) {
125 // Return the master of the bridge(switch) if it exist or
126 // return the current leader
127 if (ovsdbNode.bridgeId() == DeviceId.NONE) {
128 return leadershipService.getLeader(this.appId.name());
129 } else {
130 return mastershipService.getMasterFor(ovsdbNode.bridgeId());
131 }
132 }
133
134 private void setPassiveMode(OvsdbNode ovsdbNode) {
135 // TODO: need ovsdb client implementation first
136 // TODO: set the remove ovsdb server passive mode
137 // TODO: set the node state READY if it succeed
138 }
139
140 private void connect(OvsdbNode ovsdbNode) {
141 // TODO: need ovsdb client implementation first
142 }
143
144 private void disconnect(OvsdbNode ovsdbNode) {
145 // TODO: need ovsdb client implementation first
146 }
147}