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