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 | |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame^] | 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 23 | import org.onosproject.cluster.ClusterService; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 24 | import org.onosproject.cluster.LeadershipService; |
| 25 | import org.onosproject.cluster.NodeId; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 26 | import org.onosproject.mastership.MastershipService; |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame^] | 27 | import org.onosproject.net.Device; |
| 28 | import org.onosproject.net.device.DeviceEvent; |
| 29 | import org.onosproject.net.device.DeviceListener; |
| 30 | import org.onosproject.net.device.DeviceService; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 31 | import org.slf4j.Logger; |
| 32 | |
| 33 | import java.util.concurrent.Executors; |
| 34 | import java.util.concurrent.ScheduledExecutorService; |
| 35 | import java.util.concurrent.TimeUnit; |
| 36 | |
| 37 | import static org.onlab.util.Tools.groupedThreads; |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame^] | 38 | import static org.onosproject.cordvtn.OvsdbNode.State.CONNECTED; |
| 39 | import static org.onosproject.cordvtn.OvsdbNode.State.DISCONNECTED; |
| 40 | import static org.onosproject.cordvtn.OvsdbNode.State.READY; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 41 | import static org.slf4j.LoggerFactory.getLogger; |
| 42 | |
| 43 | /** |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame^] | 44 | * 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 Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 46 | */ |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame^] | 47 | @Component(immediate = true) |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 48 | public class NodeConnectionManager { |
| 49 | protected final Logger log = getLogger(getClass()); |
| 50 | |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame^] | 51 | @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 Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 65 | |
| 66 | private static final int DELAY_SEC = 5; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 67 | |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame^] | 68 | private final DeviceListener deviceListener = new InternalDeviceListener(); |
| 69 | private final ScheduledExecutorService connectionExecutor = Executors |
| 70 | .newSingleThreadScheduledExecutor(groupedThreads("onos/cordvtn", "connection-manager")); |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 71 | |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame^] | 72 | 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 Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 80 | .stream() |
| 81 | .filter(node -> localId.equals(getMaster(node))) |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame^] | 82 | .forEach(node -> { |
| 83 | connect(node); |
| 84 | disconnect(node); |
| 85 | }), 0, DELAY_SEC, TimeUnit.SECONDS); |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame^] | 88 | @Deactivate |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 89 | public void stop() { |
| 90 | connectionExecutor.shutdown(); |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame^] | 91 | deviceService.removeListener(deviceListener); |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame^] | 94 | public void connect(OvsdbNode ovsdbNode) { |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 95 | switch (ovsdbNode.state()) { |
| 96 | case INIT: |
| 97 | case DISCONNECTED: |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame^] | 98 | setPassiveMode(ovsdbNode); |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 99 | case READY: |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame^] | 100 | setupConnection(ovsdbNode); |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 101 | break; |
| 102 | default: |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame^] | 103 | break; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame^] | 107 | public void disconnect(OvsdbNode ovsdbNode) { |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 108 | switch (ovsdbNode.state()) { |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame^] | 109 | case DISCONNECT: |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 110 | // TODO: disconnect |
| 111 | break; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 112 | default: |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame^] | 113 | 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 Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
| 146 | private NodeId getMaster(OvsdbNode ovsdbNode) { |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame^] | 147 | 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 Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 152 | } |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame^] | 153 | return master; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | private void setPassiveMode(OvsdbNode ovsdbNode) { |
| 157 | // TODO: need ovsdb client implementation first |
| 158 | // TODO: set the remove ovsdb server passive mode |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame^] | 159 | cordVtnService.updateNode(ovsdbNode, READY); |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame^] | 162 | private void setupConnection(OvsdbNode ovsdbNode) { |
| 163 | // TODO initiate connection |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 164 | } |
| 165 | } |