blob: 0b7029ef1e129d8d75d586d3eb45112fa9015fac [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 *
51 * @param localId local id
52 * @param nodeStore node store
53 * @param mastershipService mastership service
54 */
55 public NodeConnectionManager(ApplicationId appId, NodeId localId,
56 EventuallyConsistentMap<DeviceId, OvsdbNode> nodeStore,
57 MastershipService mastershipService,
58 LeadershipService leadershipService) {
59 this.appId = appId;
60 this.localId = localId;
61 this.nodeStore = nodeStore;
62 this.mastershipService = mastershipService;
63 this.leadershipService = leadershipService;
64 }
65
66 /**
67 * Starts the node connection manager.
68 */
69 public void start() {
70 connectionExecutor = Executors.newSingleThreadScheduledExecutor(
71 groupedThreads("onos/cordvtn", "connection-executor"));
72 connectionExecutor.scheduleWithFixedDelay(() -> nodeStore.values()
73 .stream()
74 .filter(node -> localId.equals(getMaster(node)))
75 .forEach(node -> connectNode(node)), 0, DELAY_SEC, TimeUnit.SECONDS);
76 }
77
78 /**
79 * Stops the node connection manager.
80 */
81 public void stop() {
82 connectionExecutor.shutdown();
83 }
84
85 /**
86 * Adds a new node to the system.
87 *
88 * @param ovsdbNode ovsdb node
89 */
90 public void connectNode(OvsdbNode ovsdbNode) {
91 switch (ovsdbNode.state()) {
92 case INIT:
93 case DISCONNECTED:
94 // TODO: set the node to passive mode
95 case READY:
96 // TODO: initiate connection
97 break;
98 case CONNECTED:
99 break;
100 default:
101 }
102 }
103
104 /**
105 * Deletes the ovsdb node.
106 *
107 * @param ovsdbNode ovsdb node
108 */
109 public void disconnectNode(OvsdbNode ovsdbNode) {
110 switch (ovsdbNode.state()) {
111 case CONNECTED:
112 // TODO: disconnect
113 break;
114 case INIT:
115 case READY:
116 case DISCONNECTED:
117 break;
118 default:
119 }
120 }
121
122 private NodeId getMaster(OvsdbNode ovsdbNode) {
123 // Return the master of the bridge(switch) if it exist or
124 // return the current leader
125 if (ovsdbNode.bridgeId() == DeviceId.NONE) {
126 return leadershipService.getLeader(this.appId.name());
127 } else {
128 return mastershipService.getMasterFor(ovsdbNode.bridgeId());
129 }
130 }
131
132 private void setPassiveMode(OvsdbNode ovsdbNode) {
133 // TODO: need ovsdb client implementation first
134 // TODO: set the remove ovsdb server passive mode
135 // TODO: set the node state READY if it succeed
136 }
137
138 private void connect(OvsdbNode ovsdbNode) {
139 // TODO: need ovsdb client implementation first
140 }
141
142 private void disconnect(OvsdbNode ovsdbNode) {
143 // TODO: need ovsdb client implementation first
144 }
145}