Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 8e57fd5 | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 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 | */ |
alshabib | b4d3171 | 2016-06-01 18:51:03 -0700 | [diff] [blame] | 16 | package org.opencord.cordvtn.impl; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 17 | |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 18 | import com.google.common.base.Strings; |
Hyunsun Moon | 3fc0cbc | 2016-11-22 18:29:12 -0800 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 20 | import com.jcraft.jsch.Session; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 26 | import org.apache.felix.scr.annotations.Service; |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 27 | import org.onlab.packet.IpAddress; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 28 | import org.onlab.util.KryoNamespace; |
| 29 | import org.onosproject.cluster.ClusterService; |
Hyunsun Moon | 7004fcf | 2016-03-08 04:36:02 -0800 | [diff] [blame] | 30 | import org.onosproject.cluster.LeadershipService; |
| 31 | import org.onosproject.cluster.NodeId; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 32 | import org.onosproject.core.ApplicationId; |
| 33 | import org.onosproject.core.CoreService; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 34 | import org.onosproject.net.ConnectPoint; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 35 | import org.onosproject.net.Device; |
| 36 | import org.onosproject.net.DeviceId; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 37 | import org.onosproject.net.Port; |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 38 | import org.onosproject.net.PortNumber; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 39 | import org.onosproject.net.behaviour.BridgeConfig; |
Hyunsun Moon | 0984cbd | 2016-12-01 17:34:11 -0800 | [diff] [blame] | 40 | import org.onosproject.net.behaviour.BridgeDescription; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 41 | import org.onosproject.net.behaviour.BridgeName; |
| 42 | import org.onosproject.net.behaviour.ControllerInfo; |
Hyunsun Moon | 0984cbd | 2016-12-01 17:34:11 -0800 | [diff] [blame] | 43 | import org.onosproject.net.behaviour.DefaultBridgeDescription; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 44 | import org.onosproject.net.behaviour.DefaultTunnelDescription; |
Hyunsun Moon | 0984cbd | 2016-12-01 17:34:11 -0800 | [diff] [blame] | 45 | import org.onosproject.net.behaviour.InterfaceConfig; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 46 | import org.onosproject.net.behaviour.TunnelDescription; |
Hyunsun Moon | 0984cbd | 2016-12-01 17:34:11 -0800 | [diff] [blame] | 47 | import org.onosproject.net.behaviour.TunnelEndPoints; |
| 48 | import org.onosproject.net.behaviour.TunnelKeys; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 49 | import org.onosproject.net.config.NetworkConfigEvent; |
| 50 | import org.onosproject.net.config.NetworkConfigListener; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 51 | import org.onosproject.net.config.NetworkConfigService; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 52 | import org.onosproject.net.device.DeviceAdminService; |
| 53 | import org.onosproject.net.device.DeviceEvent; |
| 54 | import org.onosproject.net.device.DeviceListener; |
| 55 | import org.onosproject.net.device.DeviceService; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 56 | import org.onosproject.net.host.HostService; |
| 57 | import org.onosproject.ovsdb.controller.OvsdbClientService; |
| 58 | import org.onosproject.ovsdb.controller.OvsdbController; |
| 59 | import org.onosproject.ovsdb.controller.OvsdbNodeId; |
| 60 | import org.onosproject.store.serializers.KryoNamespaces; |
| 61 | import org.onosproject.store.service.ConsistentMap; |
Hyunsun Moon | ff55e81 | 2016-03-10 12:40:16 -0800 | [diff] [blame] | 62 | import org.onosproject.store.service.MapEvent; |
| 63 | import org.onosproject.store.service.MapEventListener; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 64 | import org.onosproject.store.service.Serializer; |
| 65 | import org.onosproject.store.service.StorageService; |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 66 | import org.onosproject.store.service.Versioned; |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 67 | import org.opencord.cordvtn.api.CordVtnConfig; |
| 68 | import org.opencord.cordvtn.api.core.InstanceService; |
| 69 | import org.opencord.cordvtn.api.net.CidrAddr; |
Hyunsun Moon | 0984cbd | 2016-12-01 17:34:11 -0800 | [diff] [blame] | 70 | import org.opencord.cordvtn.api.node.ConnectionHandler; |
| 71 | import org.opencord.cordvtn.api.node.CordVtnNode; |
| 72 | import org.opencord.cordvtn.api.node.CordVtnNodeState; |
Hyunsun Moon | 0984cbd | 2016-12-01 17:34:11 -0800 | [diff] [blame] | 73 | import org.opencord.cordvtn.api.node.SshAccessInfo; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 74 | import org.slf4j.Logger; |
| 75 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 76 | import java.util.List; |
Hyunsun Moon | 7004fcf | 2016-03-08 04:36:02 -0800 | [diff] [blame] | 77 | import java.util.Objects; |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 78 | import java.util.Optional; |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 79 | import java.util.Set; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 80 | import java.util.concurrent.ExecutorService; |
Hyunsun Moon | 58ddbdc | 2016-03-07 16:37:17 -0800 | [diff] [blame] | 81 | import java.util.stream.Collectors; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 82 | |
| 83 | import static com.google.common.base.Preconditions.checkNotNull; |
Hyunsun Moon | 537018f | 2016-07-27 18:51:00 -0700 | [diff] [blame] | 84 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 85 | import static org.onlab.util.Tools.groupedThreads; |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 86 | import static org.onosproject.net.AnnotationKeys.PORT_NAME; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 87 | import static org.onosproject.net.Device.Type.SWITCH; |
| 88 | import static org.onosproject.net.behaviour.TunnelDescription.Type.VXLAN; |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 89 | import static org.opencord.cordvtn.api.Constants.*; |
alshabib | b4d3171 | 2016-06-01 18:51:03 -0700 | [diff] [blame] | 90 | import static org.opencord.cordvtn.impl.RemoteIpCommandUtil.*; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 91 | import static org.slf4j.LoggerFactory.getLogger; |
| 92 | |
| 93 | /** |
| 94 | * Reads node information from the network config file and handles the config |
| 95 | * update events. |
| 96 | * Only a leader controller performs the node addition or deletion. |
| 97 | */ |
| 98 | @Component(immediate = true) |
| 99 | @Service(value = CordVtnNodeManager.class) |
| 100 | public class CordVtnNodeManager { |
| 101 | |
| 102 | protected final Logger log = getLogger(getClass()); |
| 103 | |
| 104 | private static final KryoNamespace.Builder NODE_SERIALIZER = KryoNamespace.newBuilder() |
| 105 | .register(KryoNamespaces.API) |
| 106 | .register(CordVtnNode.class) |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 107 | .register(NodeState.class) |
| 108 | .register(SshAccessInfo.class) |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 109 | .register(CidrAddr.class); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 110 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 111 | private static final int DPID_BEGIN = 3; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 112 | |
| 113 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 114 | protected CoreService coreService; |
| 115 | |
| 116 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 117 | protected NetworkConfigService configService; |
| 118 | |
| 119 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 120 | protected StorageService storageService; |
| 121 | |
| 122 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 123 | protected DeviceAdminService adminService; |
| 124 | |
| 125 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 126 | protected OvsdbController ovsdbController; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 127 | |
| 128 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 129 | protected ClusterService clusterService; |
| 130 | |
| 131 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 132 | protected DeviceService deviceService; |
| 133 | |
| 134 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 135 | protected HostService hostService; |
| 136 | |
| 137 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | 7004fcf | 2016-03-08 04:36:02 -0800 | [diff] [blame] | 138 | protected LeadershipService leadershipService; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 139 | |
| 140 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 141 | protected InstanceService instanceService; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 142 | |
| 143 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 144 | protected CordVtnPipeline pipeline; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 145 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 146 | private final ExecutorService eventExecutor = |
Hyunsun Moon | 537018f | 2016-07-27 18:51:00 -0700 | [diff] [blame] | 147 | newSingleThreadExecutor(groupedThreads("onos/cordvtn-node", "event-handler", log)); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 148 | |
| 149 | private final NetworkConfigListener configListener = new InternalConfigListener(); |
| 150 | private final DeviceListener deviceListener = new InternalDeviceListener(); |
Hyunsun Moon | ff55e81 | 2016-03-10 12:40:16 -0800 | [diff] [blame] | 151 | private final MapEventListener<String, CordVtnNode> nodeStoreListener = new InternalMapListener(); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 152 | |
| 153 | private final OvsdbHandler ovsdbHandler = new OvsdbHandler(); |
| 154 | private final BridgeHandler bridgeHandler = new BridgeHandler(); |
| 155 | |
Hyunsun Moon | 58ddbdc | 2016-03-07 16:37:17 -0800 | [diff] [blame] | 156 | private ConsistentMap<String, CordVtnNode> nodeStore; |
Hyunsun Moon | 3fc0cbc | 2016-11-22 18:29:12 -0800 | [diff] [blame] | 157 | private List<ControllerInfo> controllers = Lists.newArrayList(); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 158 | private ApplicationId appId; |
Hyunsun Moon | 7004fcf | 2016-03-08 04:36:02 -0800 | [diff] [blame] | 159 | private NodeId localNodeId; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 160 | |
Hyunsun Moon | 58ddbdc | 2016-03-07 16:37:17 -0800 | [diff] [blame] | 161 | private enum NodeState implements CordVtnNodeState { |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 162 | |
| 163 | INIT { |
| 164 | @Override |
| 165 | public void process(CordVtnNodeManager nodeManager, CordVtnNode node) { |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 166 | // make sure there is OVSDB connection |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 167 | if (!nodeManager.isOvsdbConnected(node)) { |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 168 | nodeManager.connectOvsdb(node); |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 169 | return; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 170 | } |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 171 | nodeManager.createIntegrationBridge(node); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 172 | } |
| 173 | }, |
| 174 | BRIDGE_CREATED { |
| 175 | @Override |
| 176 | public void process(CordVtnNodeManager nodeManager, CordVtnNode node) { |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 177 | // make sure there is OVSDB connection |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 178 | if (!nodeManager.isOvsdbConnected(node)) { |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 179 | nodeManager.connectOvsdb(node); |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 180 | return; |
| 181 | } |
| 182 | |
| 183 | nodeManager.createTunnelInterface(node); |
| 184 | nodeManager.addSystemInterface(node, node.dataIface()); |
| 185 | if (node.hostMgmtIface().isPresent()) { |
| 186 | nodeManager.addSystemInterface(node, node.hostMgmtIface().get()); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | }, |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 190 | PORTS_ADDED { |
Hyunsun Moon | bd572c1 | 2016-01-21 00:54:52 -0800 | [diff] [blame] | 191 | @Override |
| 192 | public void process(CordVtnNodeManager nodeManager, CordVtnNode node) { |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 193 | nodeManager.setIpAddress(node); |
Hyunsun Moon | bd572c1 | 2016-01-21 00:54:52 -0800 | [diff] [blame] | 194 | } |
Hyunsun Moon | bd572c1 | 2016-01-21 00:54:52 -0800 | [diff] [blame] | 195 | }, |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 196 | COMPLETE { |
| 197 | @Override |
| 198 | public void process(CordVtnNodeManager nodeManager, CordVtnNode node) { |
| 199 | nodeManager.postInit(node); |
| 200 | } |
| 201 | }, |
| 202 | INCOMPLETE { |
| 203 | @Override |
| 204 | public void process(CordVtnNodeManager nodeManager, CordVtnNode node) { |
| 205 | } |
| 206 | }; |
| 207 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 208 | public abstract void process(CordVtnNodeManager nodeManager, CordVtnNode node); |
| 209 | } |
| 210 | |
| 211 | @Activate |
Hyunsun Moon | 479b775 | 2016-05-06 20:13:28 -0700 | [diff] [blame] | 212 | protected void activate() { |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 213 | appId = coreService.registerApplication(CORDVTN_APP_ID); |
Hyunsun Moon | 7004fcf | 2016-03-08 04:36:02 -0800 | [diff] [blame] | 214 | leadershipService.runForLeadership(appId.name()); |
Hyunsun Moon | 3fc0cbc | 2016-11-22 18:29:12 -0800 | [diff] [blame] | 215 | localNodeId = clusterService.getLocalNode().id(); |
Hyunsun Moon | 7004fcf | 2016-03-08 04:36:02 -0800 | [diff] [blame] | 216 | |
Hyunsun Moon | 58ddbdc | 2016-03-07 16:37:17 -0800 | [diff] [blame] | 217 | nodeStore = storageService.<String, CordVtnNode>consistentMapBuilder() |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 218 | .withSerializer(Serializer.using(NODE_SERIALIZER.build())) |
| 219 | .withName("cordvtn-nodestore") |
| 220 | .withApplicationId(appId) |
| 221 | .build(); |
| 222 | |
Hyunsun Moon | ff55e81 | 2016-03-10 12:40:16 -0800 | [diff] [blame] | 223 | nodeStore.addListener(nodeStoreListener); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 224 | deviceService.addListener(deviceListener); |
| 225 | configService.addListener(configListener); |
Hyunsun Moon | 479b775 | 2016-05-06 20:13:28 -0700 | [diff] [blame] | 226 | |
Hyunsun Moon | 3fc0cbc | 2016-11-22 18:29:12 -0800 | [diff] [blame] | 227 | // TODO read nodes as well after more tests |
| 228 | readControllers(); |
Hyunsun Moon | 479b775 | 2016-05-06 20:13:28 -0700 | [diff] [blame] | 229 | log.info("Started"); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | @Deactivate |
| 233 | protected void deactivate() { |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 234 | configService.removeListener(configListener); |
| 235 | deviceService.removeListener(deviceListener); |
Hyunsun Moon | ff55e81 | 2016-03-10 12:40:16 -0800 | [diff] [blame] | 236 | nodeStore.removeListener(nodeStoreListener); |
Hyunsun Moon | ff55e81 | 2016-03-10 12:40:16 -0800 | [diff] [blame] | 237 | |
Hyunsun Moon | 7004fcf | 2016-03-08 04:36:02 -0800 | [diff] [blame] | 238 | leadershipService.withdraw(appId.name()); |
Hyunsun Moon | ff55e81 | 2016-03-10 12:40:16 -0800 | [diff] [blame] | 239 | eventExecutor.shutdown(); |
Hyunsun Moon | 479b775 | 2016-05-06 20:13:28 -0700 | [diff] [blame] | 240 | |
| 241 | log.info("Stopped"); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | /** |
Hyunsun Moon | ff55e81 | 2016-03-10 12:40:16 -0800 | [diff] [blame] | 245 | * Adds or updates a new node to the service. |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 246 | * |
| 247 | * @param node cordvtn node |
| 248 | */ |
Hyunsun Moon | ff55e81 | 2016-03-10 12:40:16 -0800 | [diff] [blame] | 249 | public void addOrUpdateNode(CordVtnNode node) { |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 250 | checkNotNull(node); |
Hyunsun Moon | 58ddbdc | 2016-03-07 16:37:17 -0800 | [diff] [blame] | 251 | nodeStore.put(node.hostname(), CordVtnNode.getUpdatedNode(node, getNodeState(node))); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Deletes a node from the service. |
| 256 | * |
| 257 | * @param node cordvtn node |
| 258 | */ |
| 259 | public void deleteNode(CordVtnNode node) { |
| 260 | checkNotNull(node); |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 261 | OvsdbClientService ovsdbClient = getOvsdbClient(node); |
| 262 | if (ovsdbClient != null && ovsdbClient.isConnected()) { |
| 263 | ovsdbClient.disconnect(); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 264 | } |
Hyunsun Moon | 58ddbdc | 2016-03-07 16:37:17 -0800 | [diff] [blame] | 265 | nodeStore.remove(node.hostname()); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /** |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 269 | * Returns node initialization state. |
| 270 | * |
| 271 | * @param node cordvtn node |
| 272 | * @return true if initial node setup is completed, otherwise false |
| 273 | */ |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 274 | public boolean isNodeInitComplete(CordVtnNode node) { |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 275 | checkNotNull(node); |
Hyunsun Moon | 6066bd3 | 2016-10-24 15:35:34 -0700 | [diff] [blame] | 276 | return isNodeStateComplete(node); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Returns the number of the nodes known to the service. |
| 281 | * |
| 282 | * @return number of nodes |
| 283 | */ |
| 284 | public int getNodeCount() { |
| 285 | return nodeStore.size(); |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Returns all nodes known to the service. |
| 290 | * |
| 291 | * @return list of nodes |
| 292 | */ |
| 293 | public List<CordVtnNode> getNodes() { |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 294 | return nodeStore.values().stream().map(Versioned::value).collect(Collectors.toList()); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | /** |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 298 | * Returns all nodes in complete state. |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 299 | * |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 300 | * @return set of nodes |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 301 | */ |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 302 | public Set<CordVtnNode> completeNodes() { |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 303 | return getNodes().stream().filter(this::isNodeStateComplete) |
| 304 | .collect(Collectors.toSet()); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | /** |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 308 | * Returns physical data plane port number of a given device. |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 309 | * |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 310 | * @param deviceId integration bridge device id |
| 311 | * @return port number; null otherwise |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 312 | */ |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 313 | public PortNumber dataPort(DeviceId deviceId) { |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 314 | CordVtnNode node = nodeByBridgeId(deviceId); |
| 315 | if (node == null) { |
Hyunsun Moon | 3fc0cbc | 2016-11-22 18:29:12 -0800 | [diff] [blame] | 316 | log.debug("Failed to get node for {}", deviceId); |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 317 | return null; |
| 318 | } |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 319 | |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 320 | Optional<PortNumber> port = getPortNumber(deviceId, node.dataIface()); |
| 321 | return port.isPresent() ? port.get() : null; |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | /** |
| 325 | * Returns physical data plane IP address of a given device. |
| 326 | * |
| 327 | * @param deviceId integration bridge device id |
| 328 | * @return ip address; null otherwise |
| 329 | */ |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 330 | public IpAddress dataIp(DeviceId deviceId) { |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 331 | CordVtnNode node = nodeByBridgeId(deviceId); |
| 332 | if (node == null) { |
Hyunsun Moon | 3fc0cbc | 2016-11-22 18:29:12 -0800 | [diff] [blame] | 333 | log.debug("Failed to get node for {}", deviceId); |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 334 | return null; |
| 335 | } |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 336 | return node.dataIp().ip(); |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | /** |
| 340 | * Returns tunnel port number of a given device. |
| 341 | * |
| 342 | * @param deviceId integration bridge device id |
| 343 | * @return port number |
| 344 | */ |
| 345 | public PortNumber tunnelPort(DeviceId deviceId) { |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 346 | Optional<PortNumber> port = getPortNumber(deviceId, DEFAULT_TUNNEL); |
| 347 | return port.isPresent() ? port.get() : null; |
| 348 | } |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 349 | |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 350 | /** |
| 351 | * Returns host management interface port number if exists. |
| 352 | * |
| 353 | * @param deviceId integration bridge device id |
| 354 | * @return port number; null if it does not exist |
| 355 | */ |
| 356 | public PortNumber hostManagementPort(DeviceId deviceId) { |
| 357 | CordVtnNode node = nodeByBridgeId(deviceId); |
| 358 | if (node == null) { |
Hyunsun Moon | 3fc0cbc | 2016-11-22 18:29:12 -0800 | [diff] [blame] | 359 | log.debug("Failed to get node for {}", deviceId); |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 360 | return null; |
| 361 | } |
| 362 | |
| 363 | if (node.hostMgmtIface().isPresent()) { |
| 364 | Optional<PortNumber> port = getPortNumber(deviceId, node.hostMgmtIface().get()); |
| 365 | return port.isPresent() ? port.get() : null; |
| 366 | } else { |
| 367 | return null; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | private Optional<PortNumber> getPortNumber(DeviceId deviceId, String portName) { |
| 372 | PortNumber port = deviceService.getPorts(deviceId).stream() |
Hyunsun Moon | 8e9bc93 | 2017-01-31 21:32:20 +0900 | [diff] [blame^] | 373 | .filter(p -> p.annotations().value(PORT_NAME).equals(portName) && |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 374 | p.isEnabled()) |
| 375 | .map(Port::number) |
| 376 | .findAny() |
| 377 | .orElse(null); |
| 378 | return Optional.ofNullable(port); |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | /** |
| 382 | * Returns if current node state saved in nodeStore is COMPLETE or not. |
| 383 | * |
| 384 | * @param node cordvtn node |
| 385 | * @return true if it's complete state, otherwise false |
| 386 | */ |
| 387 | private boolean isNodeStateComplete(CordVtnNode node) { |
| 388 | checkNotNull(node); |
| 389 | |
| 390 | // the state saved in nodeStore can be wrong if IP address settings are changed |
| 391 | // after the node init has been completed since there's no way to detect it |
| 392 | // getNodeState and checkNodeInitState always return correct answer but can be slow |
| 393 | Versioned<CordVtnNode> versionedNode = nodeStore.get(node.hostname()); |
| 394 | CordVtnNodeState state = versionedNode.value().state(); |
| 395 | return state != null && state.equals(NodeState.COMPLETE); |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * Initiates node to serve virtual tenant network. |
| 400 | * |
| 401 | * @param node cordvtn node |
| 402 | */ |
| 403 | private void initNode(CordVtnNode node) { |
| 404 | checkNotNull(node); |
| 405 | |
| 406 | NodeState state = (NodeState) node.state(); |
| 407 | log.debug("Processing node: {} state: {}", node.hostname(), state); |
| 408 | |
| 409 | state.process(this, node); |
| 410 | } |
| 411 | |
| 412 | /** |
| 413 | * Performs tasks after node initialization. |
| 414 | * It disconnects unnecessary OVSDB connection and installs initial flow |
| 415 | * rules on the device. |
| 416 | * |
| 417 | * @param node cordvtn node |
| 418 | */ |
| 419 | private void postInit(CordVtnNode node) { |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 420 | checkNotNull(node); |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 421 | |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 422 | // disconnect OVSDB session once the node bootstrap is done |
| 423 | OvsdbClientService ovsdbClient = getOvsdbClient(node); |
| 424 | if (ovsdbClient != null && ovsdbClient.isConnected()) { |
| 425 | ovsdbClient.disconnect(); |
| 426 | } |
| 427 | |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 428 | pipeline.initPipeline(node); |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 429 | |
| 430 | // adds existing instances to the host list |
| 431 | deviceService.getPorts(node.integrationBridgeId()).stream() |
Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame] | 432 | .filter(port -> !node.systemIfaces().contains(portName(port)) && |
| 433 | !port.number().equals(PortNumber.LOCAL) && |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 434 | port.isEnabled()) |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 435 | .forEach(port -> instanceService.addInstance(connectPoint(port))); |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 436 | |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 437 | // removes stale instances from the host list |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 438 | hostService.getHosts().forEach(host -> { |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 439 | if (deviceService.getPort( |
| 440 | host.location().deviceId(), |
| 441 | host.location().port()) == null) { |
| 442 | instanceService.removeInstance(host.location()); |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 443 | } |
| 444 | }); |
| 445 | |
| 446 | log.info("Finished init {}", node.hostname()); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | /** |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 450 | * Sets a new state for a given cordvtn node. |
| 451 | * |
| 452 | * @param node cordvtn node |
| 453 | * @param newState new node state |
| 454 | */ |
| 455 | private void setNodeState(CordVtnNode node, NodeState newState) { |
| 456 | checkNotNull(node); |
Hyunsun Moon | ff55e81 | 2016-03-10 12:40:16 -0800 | [diff] [blame] | 457 | log.debug("Changed {} state: {}", node.hostname(), newState); |
Hyunsun Moon | 58ddbdc | 2016-03-07 16:37:17 -0800 | [diff] [blame] | 458 | nodeStore.put(node.hostname(), CordVtnNode.getUpdatedNode(node, newState)); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | /** |
| 462 | * Checks current state of a given cordvtn node and returns it. |
| 463 | * |
| 464 | * @param node cordvtn node |
| 465 | * @return node state |
| 466 | */ |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 467 | private NodeState getNodeState(CordVtnNode node) { |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 468 | checkNotNull(node); |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 469 | if (!isIntegrationBridgeCreated(node)) { |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 470 | return NodeState.INIT; |
| 471 | } |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 472 | for (String iface : node.systemIfaces()) { |
| 473 | if (!isIfaceCreated(node, iface)) { |
| 474 | return NodeState.BRIDGE_CREATED; |
| 475 | } |
| 476 | } |
| 477 | if (!isIpAddressSet(node)) { |
| 478 | return NodeState.PORTS_ADDED; |
| 479 | } |
| 480 | return NodeState.COMPLETE; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | /** |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 484 | * Returns connection state of OVSDB server for a given node. |
| 485 | * |
| 486 | * @param node cordvtn node |
| 487 | * @return true if it is connected, false otherwise |
| 488 | */ |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 489 | private boolean isOvsdbConnected(CordVtnNode node) { |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 490 | checkNotNull(node); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 491 | OvsdbClientService ovsdbClient = getOvsdbClient(node); |
| 492 | return deviceService.isAvailable(node.ovsdbId()) && |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 493 | ovsdbClient != null && |
| 494 | ovsdbClient.isConnected(); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | /** |
| 498 | * Connects to OVSDB server for a given node. |
| 499 | * |
| 500 | * @param node cordvtn node |
| 501 | */ |
| 502 | private void connectOvsdb(CordVtnNode node) { |
| 503 | checkNotNull(node); |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 504 | ovsdbController.connect(node.hostMgmtIp().ip(), node.ovsdbPort()); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | /** |
| 508 | * Returns OVSDB client for a given node. |
| 509 | * |
| 510 | * @param node cordvtn node |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 511 | * @return ovsdb client, or null if there's no ovsdb connection |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 512 | */ |
| 513 | private OvsdbClientService getOvsdbClient(CordVtnNode node) { |
| 514 | checkNotNull(node); |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 515 | OvsdbNodeId ovsdb = new OvsdbNodeId( |
| 516 | node.hostMgmtIp().ip(), node.ovsdbPort().toInt()); |
| 517 | return ovsdbController.getOvsdbClient(ovsdb); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 518 | } |
| 519 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 520 | private void createIntegrationBridge(CordVtnNode node) { |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 521 | Device device = deviceService.getDevice(node.ovsdbId()); |
| 522 | if (device == null || !device.is(BridgeConfig.class)) { |
| 523 | log.error("Failed to create integration bridge on {}", node.ovsdbId()); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 524 | return; |
| 525 | } |
| 526 | |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 527 | String dpid = node.integrationBridgeId().toString().substring(DPID_BEGIN); |
| 528 | BridgeDescription bridgeDesc = DefaultBridgeDescription.builder() |
| 529 | .name(INTEGRATION_BRIDGE) |
| 530 | .failMode(BridgeDescription.FailMode.SECURE) |
| 531 | .datapathId(dpid) |
| 532 | .disableInBand() |
| 533 | .controllers(controllers) |
| 534 | .build(); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 535 | |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 536 | BridgeConfig bridgeConfig = device.as(BridgeConfig.class); |
| 537 | bridgeConfig.addBridge(bridgeDesc); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 538 | } |
| 539 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 540 | private void createTunnelInterface(CordVtnNode node) { |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 541 | Device device = deviceService.getDevice(node.ovsdbId()); |
| 542 | if (device == null || !device.is(InterfaceConfig.class)) { |
| 543 | log.error("Failed to create tunnel interface on {}", node.ovsdbId()); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 544 | return; |
| 545 | } |
| 546 | |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 547 | TunnelDescription tunnelDesc = DefaultTunnelDescription.builder() |
| 548 | .deviceId(INTEGRATION_BRIDGE) |
| 549 | .ifaceName(DEFAULT_TUNNEL) |
| 550 | .type(VXLAN) |
| 551 | .remote(TunnelEndPoints.flowTunnelEndpoint()) |
| 552 | .key(TunnelKeys.flowTunnelKey()) |
| 553 | .build(); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 554 | |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 555 | InterfaceConfig ifaceConfig = device.as(InterfaceConfig.class); |
| 556 | ifaceConfig.addTunnelMode(DEFAULT_TUNNEL, tunnelDesc); |
Hyunsun Moon | bd572c1 | 2016-01-21 00:54:52 -0800 | [diff] [blame] | 557 | } |
| 558 | |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 559 | private void addSystemInterface(CordVtnNode node, String ifaceName) { |
Hyunsun Moon | 479b775 | 2016-05-06 20:13:28 -0700 | [diff] [blame] | 560 | Session session = connect(node.sshInfo()); |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 561 | if (session == null || !isInterfaceUp(session, ifaceName)) { |
| 562 | log.warn("Interface {} is not available on {}", ifaceName, node.hostname()); |
| 563 | disconnect(session); |
| 564 | return; |
| 565 | } else { |
| 566 | disconnect(session); |
| 567 | } |
| 568 | |
| 569 | Device device = deviceService.getDevice(node.ovsdbId()); |
| 570 | if (!device.is(BridgeConfig.class)) { |
| 571 | log.error("BridgeConfig is not supported for {}", node.ovsdbId()); |
Hyunsun Moon | 479b775 | 2016-05-06 20:13:28 -0700 | [diff] [blame] | 572 | return; |
| 573 | } |
| 574 | |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 575 | BridgeConfig bridgeConfig = device.as(BridgeConfig.class); |
| 576 | bridgeConfig.addPort(BridgeName.bridgeName(INTEGRATION_BRIDGE), ifaceName); |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | /** |
| 580 | * Flushes IP address from data plane interface and adds data plane IP address |
| 581 | * to integration bridge. |
| 582 | * |
| 583 | * @param node cordvtn node |
| 584 | */ |
| 585 | private void setIpAddress(CordVtnNode node) { |
Hyunsun Moon | 479b775 | 2016-05-06 20:13:28 -0700 | [diff] [blame] | 586 | Session session = connect(node.sshInfo()); |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 587 | if (session == null) { |
| 588 | log.debug("Failed to SSH to {}", node.hostname()); |
| 589 | return; |
| 590 | } |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 591 | getCurrentIps(session, INTEGRATION_BRIDGE).stream() |
Hyunsun Moon | 0592c3d | 2016-06-23 14:47:52 -0700 | [diff] [blame] | 592 | .filter(ip -> !ip.equals(node.localMgmtIp().ip())) |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 593 | .filter(ip -> !ip.equals(node.dataIp().ip())) |
| 594 | .forEach(ip -> deleteIp(session, ip, INTEGRATION_BRIDGE)); |
Hyunsun Moon | 0592c3d | 2016-06-23 14:47:52 -0700 | [diff] [blame] | 595 | |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 596 | boolean result = flushIp(session, node.dataIface()) && |
| 597 | setInterfaceUp(session, node.dataIface()) && |
| 598 | addIp(session, node.dataIp(), INTEGRATION_BRIDGE) && |
| 599 | addIp(session, node.localMgmtIp(), INTEGRATION_BRIDGE) && |
| 600 | setInterfaceUp(session, INTEGRATION_BRIDGE); |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 601 | |
Hyunsun Moon | 479b775 | 2016-05-06 20:13:28 -0700 | [diff] [blame] | 602 | disconnect(session); |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 603 | if (result) { |
| 604 | setNodeState(node, NodeState.COMPLETE); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 605 | } |
| 606 | } |
| 607 | |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 608 | private boolean isIntegrationBridgeCreated(CordVtnNode node) { |
| 609 | return deviceService.getDevice(node.integrationBridgeId()) != null && |
| 610 | deviceService.isAvailable(node.integrationBridgeId()); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 611 | } |
| 612 | |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 613 | private boolean isIfaceCreated(CordVtnNode node, String ifaceName) { |
| 614 | if (Strings.isNullOrEmpty(ifaceName)) { |
| 615 | return false; |
| 616 | } |
| 617 | return deviceService.getPorts(node.integrationBridgeId()).stream() |
| 618 | .filter(p -> portName(p).contains(ifaceName) && |
| 619 | p.isEnabled()) |
| 620 | .findAny() |
| 621 | .isPresent(); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | /** |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 625 | * Checks if the IP addresses are correctly set. |
| 626 | * |
| 627 | * @param node cordvtn node |
| 628 | * @return true if the IP is set, false otherwise |
| 629 | */ |
| 630 | private boolean isIpAddressSet(CordVtnNode node) { |
Hyunsun Moon | 479b775 | 2016-05-06 20:13:28 -0700 | [diff] [blame] | 631 | Session session = connect(node.sshInfo()); |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 632 | if (session == null) { |
| 633 | log.debug("Failed to SSH to {}", node.hostname()); |
| 634 | return false; |
| 635 | } |
| 636 | |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 637 | Set<IpAddress> intBrIps = getCurrentIps(session, INTEGRATION_BRIDGE); |
| 638 | boolean result = getCurrentIps(session, node.dataIface()).isEmpty() && |
| 639 | isInterfaceUp(session, node.dataIface()) && |
| 640 | intBrIps.contains(node.dataIp().ip()) && |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 641 | intBrIps.contains(node.localMgmtIp().ip()) && |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 642 | isInterfaceUp(session, INTEGRATION_BRIDGE); |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 643 | |
Hyunsun Moon | 479b775 | 2016-05-06 20:13:28 -0700 | [diff] [blame] | 644 | disconnect(session); |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 645 | return result; |
| 646 | } |
| 647 | |
| 648 | /** |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 649 | * Returns connect point of a given port. |
| 650 | * |
| 651 | * @param port port |
| 652 | * @return connect point |
| 653 | */ |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 654 | private ConnectPoint connectPoint(Port port) { |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 655 | return new ConnectPoint(port.element().id(), port.number()); |
| 656 | } |
| 657 | |
| 658 | /** |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 659 | * Returns cordvtn node associated with a given OVSDB device. |
| 660 | * |
| 661 | * @param ovsdbId OVSDB device id |
| 662 | * @return cordvtn node, null if it fails to find the node |
| 663 | */ |
| 664 | private CordVtnNode nodeByOvsdbId(DeviceId ovsdbId) { |
| 665 | return getNodes().stream() |
| 666 | .filter(node -> node.ovsdbId().equals(ovsdbId)) |
| 667 | .findFirst().orElse(null); |
| 668 | } |
| 669 | |
| 670 | /** |
| 671 | * Returns cordvtn node associated with a given integration bridge. |
| 672 | * |
| 673 | * @param bridgeId device id of integration bridge |
| 674 | * @return cordvtn node, null if it fails to find the node |
| 675 | */ |
| 676 | private CordVtnNode nodeByBridgeId(DeviceId bridgeId) { |
| 677 | return getNodes().stream() |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 678 | .filter(node -> node.integrationBridgeId().equals(bridgeId)) |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 679 | .findFirst().orElse(null); |
| 680 | } |
| 681 | |
| 682 | /** |
| 683 | * Returns port name. |
| 684 | * |
| 685 | * @param port port |
| 686 | * @return port name |
| 687 | */ |
| 688 | private String portName(Port port) { |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 689 | return port.annotations().value(PORT_NAME); |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 690 | } |
| 691 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 692 | private class OvsdbHandler implements ConnectionHandler<Device> { |
| 693 | |
| 694 | @Override |
| 695 | public void connected(Device device) { |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 696 | CordVtnNode node = nodeByOvsdbId(device.id()); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 697 | if (node != null) { |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 698 | setNodeState(node, getNodeState(node)); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 699 | } else { |
Hyunsun Moon | 8e9bc93 | 2017-01-31 21:32:20 +0900 | [diff] [blame^] | 700 | log.info("{} is detected on unregistered node, ignore it.", device.id()); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 701 | } |
| 702 | } |
| 703 | |
| 704 | @Override |
| 705 | public void disconnected(Device device) { |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 706 | log.debug("Device {} is disconnected", device.id()); |
| 707 | adminService.removeDevice(device.id()); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 708 | } |
| 709 | } |
| 710 | |
| 711 | private class BridgeHandler implements ConnectionHandler<Device> { |
| 712 | |
| 713 | @Override |
| 714 | public void connected(Device device) { |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 715 | CordVtnNode node = nodeByBridgeId(device.id()); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 716 | if (node != null) { |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 717 | setNodeState(node, getNodeState(node)); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 718 | } else { |
Hyunsun Moon | 8e9bc93 | 2017-01-31 21:32:20 +0900 | [diff] [blame^] | 719 | log.info("{} is detected on unregistered node, ignore it.", device.id()); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 720 | } |
| 721 | } |
| 722 | |
| 723 | @Override |
| 724 | public void disconnected(Device device) { |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 725 | CordVtnNode node = nodeByBridgeId(device.id()); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 726 | if (node != null) { |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 727 | log.warn("Integration Bridge is disconnected from {}", node.hostname()); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 728 | setNodeState(node, NodeState.INCOMPLETE); |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | /** |
| 733 | * Handles port added situation. |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 734 | * If the added port is tunnel or data plane interface, proceed to the remaining |
| 735 | * node initialization. Otherwise, do nothing. |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 736 | * |
| 737 | * @param port port |
| 738 | */ |
| 739 | public void portAdded(Port port) { |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 740 | CordVtnNode node = nodeByBridgeId((DeviceId) port.element().id()); |
| 741 | String portName = portName(port); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 742 | if (node == null) { |
Hyunsun Moon | 8e9bc93 | 2017-01-31 21:32:20 +0900 | [diff] [blame^] | 743 | log.info("{} is added to unregistered node, ignore it.", portName); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 744 | return; |
| 745 | } |
| 746 | |
Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame] | 747 | if (node.systemIfaces().contains(portName)) { |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 748 | setNodeState(node, getNodeState(node)); |
Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame] | 749 | } else if (isNodeStateComplete(node)) { |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 750 | // TODO move this logic to InstanceManager |
Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame] | 751 | instanceService.addInstance(connectPoint(port)); |
| 752 | } else { |
| 753 | log.warn("Instance is detected on incomplete node, ignore it.", portName); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 754 | } |
| 755 | } |
| 756 | |
| 757 | /** |
| 758 | * Handles port removed situation. |
Hyunsun Moon | 126171d | 2016-02-09 01:55:48 -0800 | [diff] [blame] | 759 | * If the removed port is tunnel or data plane interface, proceed to the remaining |
| 760 | * node initialization.Others, do nothing. |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 761 | * |
| 762 | * @param port port |
| 763 | */ |
| 764 | public void portRemoved(Port port) { |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 765 | CordVtnNode node = nodeByBridgeId((DeviceId) port.element().id()); |
| 766 | String portName = portName(port); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 767 | if (node == null) { |
Hyunsun Moon | 8e9bc93 | 2017-01-31 21:32:20 +0900 | [diff] [blame^] | 768 | log.info("{} is removed from unregistered node, ignore it.", portName); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 769 | return; |
| 770 | } |
| 771 | |
Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame] | 772 | if (node.systemIfaces().contains(portName)) { |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 773 | setNodeState(node, NodeState.INCOMPLETE); |
Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame] | 774 | } else if (isNodeStateComplete(node)) { |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 775 | // TODO move this logic to InstanceManager |
Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame] | 776 | instanceService.removeInstance(connectPoint(port)); |
| 777 | } else { |
| 778 | log.warn("VM is vanished from incomplete node, ignore it.", portName); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 779 | } |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | private class InternalDeviceListener implements DeviceListener { |
| 784 | |
| 785 | @Override |
Hyunsun Moon | 8e9bc93 | 2017-01-31 21:32:20 +0900 | [diff] [blame^] | 786 | public boolean isRelevant(DeviceEvent event) { |
| 787 | NodeId leaderNodeId = leadershipService.getLeader(appId.name()); |
| 788 | return Objects.equals(localNodeId, leaderNodeId); |
| 789 | } |
| 790 | |
| 791 | @Override |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 792 | public void event(DeviceEvent event) { |
| 793 | |
| 794 | Device device = event.subject(); |
Hyunsun Moon | 8e9bc93 | 2017-01-31 21:32:20 +0900 | [diff] [blame^] | 795 | ConnectionHandler<Device> handler = device.type().equals(SWITCH) ? |
| 796 | bridgeHandler : ovsdbHandler; |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 797 | |
| 798 | switch (event.type()) { |
| 799 | case PORT_ADDED: |
Hyunsun Moon | 8e9bc93 | 2017-01-31 21:32:20 +0900 | [diff] [blame^] | 800 | eventExecutor.execute(() -> { |
| 801 | log.info("Port {} is added to {}", |
| 802 | event.port().annotations().value(PORT_NAME), |
| 803 | event.subject().id()); |
| 804 | bridgeHandler.portAdded(event.port()); |
| 805 | }); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 806 | break; |
| 807 | case PORT_UPDATED: |
| 808 | if (!event.port().isEnabled()) { |
Hyunsun Moon | 8e9bc93 | 2017-01-31 21:32:20 +0900 | [diff] [blame^] | 809 | eventExecutor.execute(() -> { |
| 810 | log.info("Port {} is removed from {}", |
| 811 | event.port().annotations().value(PORT_NAME), |
| 812 | event.subject().id()); |
| 813 | bridgeHandler.portRemoved(event.port()); |
| 814 | }); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 815 | } |
| 816 | break; |
| 817 | case DEVICE_ADDED: |
| 818 | case DEVICE_AVAILABILITY_CHANGED: |
| 819 | if (deviceService.isAvailable(device.id())) { |
Hyunsun Moon | 8e9bc93 | 2017-01-31 21:32:20 +0900 | [diff] [blame^] | 820 | eventExecutor.execute(() -> { |
| 821 | log.info("Device {} is connected", |
| 822 | event.subject().id()); |
| 823 | handler.connected(device); |
| 824 | }); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 825 | } else { |
Hyunsun Moon | 8e9bc93 | 2017-01-31 21:32:20 +0900 | [diff] [blame^] | 826 | eventExecutor.execute(() -> { |
| 827 | log.info("Device {} is disconnected", |
| 828 | event.subject().id()); |
| 829 | handler.disconnected(device); |
| 830 | }); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 831 | } |
| 832 | break; |
| 833 | default: |
| 834 | break; |
| 835 | } |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | /** |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 840 | * Reads cordvtn nodes from config file. |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 841 | */ |
Hyunsun Moon | 3fc0cbc | 2016-11-22 18:29:12 -0800 | [diff] [blame] | 842 | private void readNodes() { |
| 843 | NodeId leaderNodeId = leadershipService.getLeader(appId.name()); |
| 844 | if (!Objects.equals(localNodeId, leaderNodeId)) { |
| 845 | // do not allow to proceed without leadership |
| 846 | return; |
| 847 | } |
| 848 | |
Hyunsun Moon | 0984cbd | 2016-12-01 17:34:11 -0800 | [diff] [blame] | 849 | CordVtnConfig config = configService.getConfig(appId, CordVtnConfig.class); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 850 | if (config == null) { |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 851 | log.debug("No configuration found"); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 852 | return; |
| 853 | } |
Hyunsun Moon | ff55e81 | 2016-03-10 12:40:16 -0800 | [diff] [blame] | 854 | config.cordVtnNodes().forEach(this::addOrUpdateNode); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 855 | } |
| 856 | |
Hyunsun Moon | 3fc0cbc | 2016-11-22 18:29:12 -0800 | [diff] [blame] | 857 | private void readControllers() { |
Hyunsun Moon | 0984cbd | 2016-12-01 17:34:11 -0800 | [diff] [blame] | 858 | CordVtnConfig config = configService.getConfig(appId, CordVtnConfig.class); |
Hyunsun Moon | 3fc0cbc | 2016-11-22 18:29:12 -0800 | [diff] [blame] | 859 | if (config == null) { |
| 860 | log.debug("No configuration found"); |
| 861 | return; |
| 862 | } |
| 863 | controllers = config.controllers(); |
| 864 | controllers.stream().forEach(ctrl -> { |
| 865 | log.debug("Added controller {}:{}", ctrl.ip(), ctrl.port()); |
| 866 | }); |
| 867 | } |
| 868 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 869 | private class InternalConfigListener implements NetworkConfigListener { |
| 870 | |
| 871 | @Override |
| 872 | public void event(NetworkConfigEvent event) { |
| 873 | if (!event.configClass().equals(CordVtnConfig.class)) { |
| 874 | return; |
| 875 | } |
| 876 | |
| 877 | switch (event.type()) { |
| 878 | case CONFIG_ADDED: |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 879 | case CONFIG_UPDATED: |
Hyunsun Moon | 3fc0cbc | 2016-11-22 18:29:12 -0800 | [diff] [blame] | 880 | eventExecutor.execute(() -> { |
| 881 | readControllers(); |
| 882 | readNodes(); |
| 883 | }); |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 884 | break; |
| 885 | default: |
| 886 | break; |
| 887 | } |
| 888 | } |
| 889 | } |
Hyunsun Moon | ff55e81 | 2016-03-10 12:40:16 -0800 | [diff] [blame] | 890 | |
| 891 | private class InternalMapListener implements MapEventListener<String, CordVtnNode> { |
| 892 | |
| 893 | @Override |
| 894 | public void event(MapEvent<String, CordVtnNode> event) { |
| 895 | NodeId leaderNodeId = leadershipService.getLeader(appId.name()); |
| 896 | if (!Objects.equals(localNodeId, leaderNodeId)) { |
| 897 | // do not allow to proceed without leadership |
| 898 | return; |
| 899 | } |
| 900 | |
| 901 | CordVtnNode oldNode; |
| 902 | CordVtnNode newNode; |
| 903 | |
| 904 | switch (event.type()) { |
| 905 | case UPDATE: |
| 906 | oldNode = event.oldValue().value(); |
| 907 | newNode = event.newValue().value(); |
| 908 | |
Hyunsun Moon | 479b775 | 2016-05-06 20:13:28 -0700 | [diff] [blame] | 909 | log.info("Reloaded {}", newNode.hostname()); |
Hyunsun Moon | ff55e81 | 2016-03-10 12:40:16 -0800 | [diff] [blame] | 910 | if (!newNode.equals(oldNode)) { |
Hyunsun Moon | ff55e81 | 2016-03-10 12:40:16 -0800 | [diff] [blame] | 911 | log.debug("New node: {}", newNode); |
| 912 | } |
Hyunsun Moon | 479b775 | 2016-05-06 20:13:28 -0700 | [diff] [blame] | 913 | // performs init procedure even if the node is not changed |
| 914 | // for robustness since it's no harm to run init procedure |
| 915 | // multiple times |
Hyunsun Moon | ff55e81 | 2016-03-10 12:40:16 -0800 | [diff] [blame] | 916 | eventExecutor.execute(() -> initNode(newNode)); |
| 917 | break; |
| 918 | case INSERT: |
| 919 | newNode = event.newValue().value(); |
| 920 | log.info("Added {}", newNode.hostname()); |
| 921 | eventExecutor.execute(() -> initNode(newNode)); |
| 922 | break; |
| 923 | case REMOVE: |
| 924 | oldNode = event.oldValue().value(); |
Hyunsun Moon | 479b775 | 2016-05-06 20:13:28 -0700 | [diff] [blame] | 925 | log.info("Removed {}", oldNode.hostname()); |
Hyunsun Moon | ff55e81 | 2016-03-10 12:40:16 -0800 | [diff] [blame] | 926 | break; |
| 927 | default: |
| 928 | break; |
| 929 | } |
| 930 | } |
| 931 | } |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 932 | } |