Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.cordvtn; |
| 17 | |
| 18 | import org.onosproject.cluster.LeadershipService; |
| 19 | import org.onosproject.cluster.NodeId; |
| 20 | import org.onosproject.core.ApplicationId; |
| 21 | import org.onosproject.mastership.MastershipService; |
| 22 | import org.onosproject.net.DeviceId; |
| 23 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 24 | import org.slf4j.Logger; |
| 25 | |
| 26 | import java.util.concurrent.Executors; |
| 27 | import java.util.concurrent.ScheduledExecutorService; |
| 28 | import java.util.concurrent.TimeUnit; |
| 29 | |
| 30 | import static org.onlab.util.Tools.groupedThreads; |
| 31 | import static org.slf4j.LoggerFactory.getLogger; |
| 32 | |
| 33 | /** |
| 34 | * Node connection manager. |
| 35 | */ |
| 36 | public 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 Vachuska | bf5b4c8 | 2015-09-22 17:06:19 -0700 | [diff] [blame^] | 51 | * @param appId app id |
| 52 | * @param localId local id |
| 53 | * @param nodeStore node store |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 54 | * @param mastershipService mastership service |
Thomas Vachuska | bf5b4c8 | 2015-09-22 17:06:19 -0700 | [diff] [blame^] | 55 | * @param leadershipService leadership service |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 56 | */ |
| 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 Vachuska | bf5b4c8 | 2015-09-22 17:06:19 -0700 | [diff] [blame^] | 73 | groupedThreads("onos/cordvtn", "connection-executor")); |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 74 | connectionExecutor.scheduleWithFixedDelay(() -> nodeStore.values() |
| 75 | .stream() |
| 76 | .filter(node -> localId.equals(getMaster(node))) |
Thomas Vachuska | bf5b4c8 | 2015-09-22 17:06:19 -0700 | [diff] [blame^] | 77 | .forEach(this::connectNode), 0, DELAY_SEC, TimeUnit.SECONDS); |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 78 | } |
| 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 | } |