Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014-2015 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.cordvtn; |
| 17 | |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame^] | 18 | import com.google.common.collect.Lists; |
| 19 | import com.google.common.collect.Maps; |
Hyunsun Moon | 1f14555 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 20 | import com.google.common.collect.Sets; |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [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 | d772f34 | 2015-10-28 20:28:16 -0700 | [diff] [blame] | 27 | import org.onlab.util.ItemNotFoundException; |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame^] | 28 | import org.onlab.packet.IpAddress; |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 29 | import org.onlab.util.KryoNamespace; |
Hyunsun Moon | 1f14555 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 30 | import org.onosproject.cluster.ClusterService; |
| 31 | import org.onosproject.core.ApplicationId; |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 32 | import org.onosproject.core.CoreService; |
Hyunsun Moon | d772f34 | 2015-10-28 20:28:16 -0700 | [diff] [blame] | 33 | import org.onosproject.net.DefaultAnnotations; |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 34 | import org.onosproject.net.Device; |
| 35 | import org.onosproject.net.DeviceId; |
| 36 | import org.onosproject.net.Host; |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame^] | 37 | import org.onosproject.net.HostId; |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 38 | import org.onosproject.net.Port; |
Hyunsun Moon | d772f34 | 2015-10-28 20:28:16 -0700 | [diff] [blame] | 39 | import org.onosproject.net.behaviour.BridgeConfig; |
| 40 | import org.onosproject.net.behaviour.BridgeName; |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame^] | 41 | import org.onosproject.net.ConnectPoint; |
Hyunsun Moon | 1f14555 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 42 | import org.onosproject.net.behaviour.ControllerInfo; |
Hyunsun Moon | d772f34 | 2015-10-28 20:28:16 -0700 | [diff] [blame] | 43 | import org.onosproject.net.behaviour.DefaultTunnelDescription; |
| 44 | import org.onosproject.net.behaviour.TunnelConfig; |
| 45 | import org.onosproject.net.behaviour.TunnelDescription; |
| 46 | import org.onosproject.net.behaviour.TunnelName; |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 47 | import org.onosproject.net.device.DeviceAdminService; |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 48 | import org.onosproject.net.device.DeviceEvent; |
| 49 | import org.onosproject.net.device.DeviceListener; |
| 50 | import org.onosproject.net.device.DeviceService; |
Hyunsun Moon | d772f34 | 2015-10-28 20:28:16 -0700 | [diff] [blame] | 51 | import org.onosproject.net.driver.DriverHandler; |
| 52 | import org.onosproject.net.driver.DriverService; |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame^] | 53 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 54 | import org.onosproject.net.host.HostEvent; |
| 55 | import org.onosproject.net.host.HostListener; |
| 56 | import org.onosproject.net.host.HostService; |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame^] | 57 | import org.onosproject.openstackswitching.OpenstackNetwork; |
| 58 | import org.onosproject.openstackswitching.OpenstackPort; |
| 59 | import org.onosproject.openstackswitching.OpenstackSwitchingService; |
Hyunsun Moon | 1f14555 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 60 | import org.onosproject.ovsdb.controller.OvsdbClientService; |
| 61 | import org.onosproject.ovsdb.controller.OvsdbController; |
| 62 | import org.onosproject.ovsdb.controller.OvsdbNodeId; |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 63 | import org.onosproject.store.serializers.KryoNamespaces; |
Hyunsun Moon | 1f14555 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 64 | import org.onosproject.store.service.ConsistentMap; |
| 65 | import org.onosproject.store.service.Serializer; |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 66 | import org.onosproject.store.service.StorageService; |
| 67 | import org.slf4j.Logger; |
| 68 | |
Hyunsun Moon | 1f14555 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 69 | import java.util.ArrayList; |
| 70 | import java.util.HashMap; |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 71 | import java.util.List; |
Hyunsun Moon | 1f14555 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 72 | import java.util.Map; |
Hyunsun Moon | 523d976 | 2015-10-19 12:38:21 -0700 | [diff] [blame] | 73 | import java.util.NoSuchElementException; |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame^] | 74 | import java.util.Set; |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 75 | import java.util.concurrent.ExecutorService; |
| 76 | import java.util.concurrent.Executors; |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame^] | 77 | import java.util.stream.Collectors; |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 78 | |
Hyunsun Moon | 1f14555 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 79 | import static com.google.common.base.Preconditions.checkNotNull; |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 80 | import static org.onlab.util.Tools.groupedThreads; |
Hyunsun Moon | 2b53032 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 81 | import static org.onosproject.net.Device.Type.SWITCH; |
Hyunsun Moon | d772f34 | 2015-10-28 20:28:16 -0700 | [diff] [blame] | 82 | import static org.onosproject.net.behaviour.TunnelDescription.Type.VXLAN; |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 83 | import static org.slf4j.LoggerFactory.getLogger; |
| 84 | |
| 85 | /** |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame^] | 86 | * Provisions virtual tenant networks with service chaining capability |
| 87 | * in OpenStack environment. |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 88 | */ |
| 89 | @Component(immediate = true) |
| 90 | @Service |
| 91 | public class CordVtn implements CordVtnService { |
| 92 | |
| 93 | protected final Logger log = getLogger(getClass()); |
| 94 | |
Hyunsun Moon | 2b53032 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 95 | private static final int NUM_THREADS = 1; |
| 96 | private static final KryoNamespace.Builder NODE_SERIALIZER = KryoNamespace.newBuilder() |
| 97 | .register(KryoNamespaces.API) |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 98 | .register(CordVtnNode.class) |
| 99 | .register(NodeState.class); |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame^] | 100 | private static final String DEFAULT_BRIDGE = "br-int"; |
| 101 | private static final String VPORT_PREFIX = "tap"; |
Hyunsun Moon | 523d976 | 2015-10-19 12:38:21 -0700 | [diff] [blame] | 102 | private static final String DEFAULT_TUNNEL = "vxlan"; |
| 103 | private static final Map<String, String> DEFAULT_TUNNEL_OPTIONS = new HashMap<String, String>() { |
Hyunsun Moon | 1f14555 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 104 | { |
| 105 | put("key", "flow"); |
Hyunsun Moon | 1f14555 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 106 | put("remote_ip", "flow"); |
| 107 | } |
| 108 | }; |
| 109 | private static final int DPID_BEGIN = 3; |
| 110 | private static final int OFPORT = 6653; |
Hyunsun Moon | 2b53032 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 111 | |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 112 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 113 | protected CoreService coreService; |
| 114 | |
| 115 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 116 | protected StorageService storageService; |
| 117 | |
| 118 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 119 | protected DeviceService deviceService; |
| 120 | |
| 121 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 122 | protected HostService hostService; |
| 123 | |
Hyunsun Moon | 1f14555 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 124 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | d772f34 | 2015-10-28 20:28:16 -0700 | [diff] [blame] | 125 | protected DriverService driverService; |
| 126 | |
| 127 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 128 | protected DeviceAdminService adminService; |
| 129 | |
| 130 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame^] | 131 | protected FlowObjectiveService flowObjectiveService; |
| 132 | |
| 133 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | 1f14555 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 134 | protected OvsdbController controller; |
| 135 | |
| 136 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 137 | protected ClusterService clusterService; |
| 138 | |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame^] | 139 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 140 | protected OpenstackSwitchingService openstackService; |
| 141 | |
Hyunsun Moon | 2b53032 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 142 | private final ExecutorService eventExecutor = Executors |
| 143 | .newFixedThreadPool(NUM_THREADS, groupedThreads("onos/cordvtn", "event-handler")); |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 144 | |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 145 | private final DeviceListener deviceListener = new InternalDeviceListener(); |
| 146 | private final HostListener hostListener = new InternalHostListener(); |
Hyunsun Moon | 2b53032 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 147 | |
| 148 | private final OvsdbHandler ovsdbHandler = new OvsdbHandler(); |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 149 | private final BridgeHandler bridgeHandler = new BridgeHandler(); |
Hyunsun Moon | 2b53032 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 150 | private final VmHandler vmHandler = new VmHandler(); |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 151 | |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 152 | private ConsistentMap<CordVtnNode, NodeState> nodeStore; |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame^] | 153 | private Map<HostId, String> hostNetworkMap = Maps.newHashMap(); |
| 154 | private CordVtnRuleInstaller ruleInstaller; |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 155 | |
| 156 | private enum NodeState { |
| 157 | |
| 158 | INIT { |
| 159 | @Override |
| 160 | public void process(CordVtn cordVtn, CordVtnNode node) { |
| 161 | cordVtn.connect(node); |
| 162 | } |
| 163 | }, |
| 164 | OVSDB_CONNECTED { |
| 165 | @Override |
| 166 | public void process(CordVtn cordVtn, CordVtnNode node) { |
| 167 | if (!cordVtn.getOvsdbConnectionState(node)) { |
| 168 | cordVtn.connect(node); |
| 169 | } else { |
| 170 | cordVtn.createIntegrationBridge(node); |
| 171 | } |
| 172 | } |
| 173 | }, |
| 174 | BRIDGE_CREATED { |
| 175 | @Override |
| 176 | public void process(CordVtn cordVtn, CordVtnNode node) { |
| 177 | if (!cordVtn.getOvsdbConnectionState(node)) { |
| 178 | cordVtn.connect(node); |
| 179 | } else { |
| 180 | cordVtn.createTunnelInterface(node); |
| 181 | } |
| 182 | } |
| 183 | }, |
| 184 | COMPLETE { |
| 185 | @Override |
| 186 | public void process(CordVtn cordVtn, CordVtnNode node) { |
| 187 | cordVtn.postInit(node); |
| 188 | } |
| 189 | }, |
| 190 | INCOMPLETE { |
| 191 | @Override |
| 192 | public void process(CordVtn cordVtn, CordVtnNode node) { |
| 193 | } |
| 194 | }; |
| 195 | |
| 196 | public abstract void process(CordVtn cordVtn, CordVtnNode node); |
| 197 | } |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 198 | |
| 199 | @Activate |
| 200 | protected void activate() { |
Hyunsun Moon | 523d976 | 2015-10-19 12:38:21 -0700 | [diff] [blame] | 201 | ApplicationId appId = coreService.registerApplication("org.onosproject.cordvtn"); |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 202 | nodeStore = storageService.<CordVtnNode, NodeState>consistentMapBuilder() |
Hyunsun Moon | 1f14555 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 203 | .withSerializer(Serializer.using(NODE_SERIALIZER.build())) |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 204 | .withName("cordvtn-nodestore") |
Hyunsun Moon | 1f14555 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 205 | .withApplicationId(appId) |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 206 | .build(); |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 207 | |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame^] | 208 | ruleInstaller = new CordVtnRuleInstaller(appId, flowObjectiveService, |
| 209 | driverService, DEFAULT_TUNNEL); |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 210 | deviceService.addListener(deviceListener); |
| 211 | hostService.addListener(hostListener); |
Hyunsun Moon | 2b53032 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 212 | |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 213 | log.info("Started"); |
| 214 | } |
| 215 | |
| 216 | @Deactivate |
| 217 | protected void deactivate() { |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 218 | deviceService.removeListener(deviceListener); |
| 219 | hostService.removeListener(hostListener); |
Hyunsun Moon | 2b53032 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 220 | |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 221 | eventExecutor.shutdown(); |
Hyunsun Moon | 1f14555 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 222 | nodeStore.clear(); |
Hyunsun Moon | 2b53032 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 223 | |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 224 | log.info("Stopped"); |
| 225 | } |
| 226 | |
| 227 | @Override |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 228 | public void addNode(CordVtnNode node) { |
| 229 | checkNotNull(node); |
Hyunsun Moon | 523d976 | 2015-10-19 12:38:21 -0700 | [diff] [blame] | 230 | |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 231 | nodeStore.putIfAbsent(node, checkNodeState(node)); |
| 232 | initNode(node); |
Hyunsun Moon | 1f14555 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | @Override |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 236 | public void deleteNode(CordVtnNode node) { |
| 237 | checkNotNull(node); |
Hyunsun Moon | 1f14555 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 238 | |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 239 | if (getOvsdbConnectionState(node)) { |
| 240 | disconnect(node); |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 241 | } |
Hyunsun Moon | 523d976 | 2015-10-19 12:38:21 -0700 | [diff] [blame] | 242 | |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 243 | nodeStore.remove(node); |
Hyunsun Moon | 523d976 | 2015-10-19 12:38:21 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 246 | @Override |
| 247 | public int getNodeCount() { |
| 248 | return nodeStore.size(); |
| 249 | } |
| 250 | |
| 251 | @Override |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 252 | public List<CordVtnNode> getNodes() { |
| 253 | List<CordVtnNode> nodes = new ArrayList<>(); |
| 254 | nodes.addAll(nodeStore.keySet()); |
| 255 | return nodes; |
| 256 | } |
| 257 | |
| 258 | @Override |
| 259 | public void initNode(CordVtnNode node) { |
| 260 | checkNotNull(node); |
| 261 | |
| 262 | if (!nodeStore.containsKey(node)) { |
| 263 | log.warn("Node {} does not exist, add node first", node.hostname()); |
| 264 | return; |
| 265 | } |
| 266 | |
| 267 | NodeState state = getNodeState(node); |
| 268 | if (state == null) { |
| 269 | return; |
| 270 | } else if (state.equals(NodeState.INCOMPLETE)) { |
| 271 | state = checkNodeState(node); |
| 272 | } |
| 273 | |
| 274 | state.process(this, node); |
| 275 | } |
| 276 | |
| 277 | @Override |
| 278 | public boolean getNodeInitState(CordVtnNode node) { |
| 279 | checkNotNull(node); |
| 280 | |
| 281 | NodeState state = getNodeState(node); |
| 282 | return state != null && state.equals(NodeState.COMPLETE); |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Returns state of a given cordvtn node. |
| 287 | * |
| 288 | * @param node cordvtn node |
| 289 | * @return node state, or null if no such node exists |
| 290 | */ |
| 291 | private NodeState getNodeState(CordVtnNode node) { |
| 292 | checkNotNull(node); |
| 293 | |
| 294 | try { |
| 295 | return nodeStore.get(node).value(); |
| 296 | } catch (NullPointerException e) { |
| 297 | log.error("Failed to get state of {}", node.hostname()); |
Hyunsun Moon | 1f14555 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 298 | return null; |
| 299 | } |
Hyunsun Moon | 2b53032 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 302 | /** |
| 303 | * Sets a new state for a given cordvtn node. |
| 304 | * |
| 305 | * @param node cordvtn node |
| 306 | * @param newState new node state |
| 307 | */ |
| 308 | private void setNodeState(CordVtnNode node, NodeState newState) { |
| 309 | checkNotNull(node); |
| 310 | |
| 311 | log.info("Changed {} state: {}", node.hostname(), newState.toString()); |
| 312 | |
| 313 | nodeStore.put(node, newState); |
| 314 | newState.process(this, node); |
Hyunsun Moon | 1f14555 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 317 | /** |
| 318 | * Checks current state of a given cordvtn node and returns it. |
| 319 | * |
| 320 | * @param node cordvtn node |
| 321 | * @return node state |
| 322 | */ |
| 323 | private NodeState checkNodeState(CordVtnNode node) { |
| 324 | checkNotNull(node); |
Hyunsun Moon | 1f14555 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 325 | |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 326 | if (checkIntegrationBridge(node) && checkTunnelInterface(node)) { |
| 327 | return NodeState.COMPLETE; |
| 328 | } else if (checkIntegrationBridge(node)) { |
| 329 | return NodeState.BRIDGE_CREATED; |
| 330 | } else if (getOvsdbConnectionState(node)) { |
| 331 | return NodeState.OVSDB_CONNECTED; |
Hyunsun Moon | 1f14555 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 332 | } else { |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 333 | return NodeState.INIT; |
Hyunsun Moon | 1f14555 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 337 | /** |
| 338 | * Performs tasks after node initialization. |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame^] | 339 | * First disconnect unnecessary OVSDB connection and then installs flow rules |
| 340 | * for existing VMs if there are any. |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 341 | * |
| 342 | * @param node cordvtn node |
| 343 | */ |
| 344 | private void postInit(CordVtnNode node) { |
| 345 | disconnect(node); |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame^] | 346 | |
| 347 | Set<OpenstackNetwork> vNets = Sets.newHashSet(); |
| 348 | hostService.getConnectedHosts(node.intBrId()) |
| 349 | .stream() |
| 350 | .forEach(host -> { |
| 351 | OpenstackNetwork vNet = getOpenstackNetworkByHost(host); |
| 352 | if (vNet != null) { |
| 353 | log.info("VM {} is detected", host.id()); |
| 354 | |
| 355 | hostNetworkMap.put(host.id(), vNet.id()); |
| 356 | vNets.add(vNet); |
| 357 | } |
| 358 | }); |
| 359 | vNets.stream().forEach(this::installFlowRules); |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | /** |
| 363 | * Returns connection state of OVSDB server for a given node. |
| 364 | * |
| 365 | * @param node cordvtn node |
| 366 | * @return true if it is connected, false otherwise |
| 367 | */ |
| 368 | private boolean getOvsdbConnectionState(CordVtnNode node) { |
| 369 | checkNotNull(node); |
| 370 | |
| 371 | OvsdbClientService ovsdbClient = getOvsdbClient(node); |
| 372 | return deviceService.isAvailable(node.ovsdbId()) && |
| 373 | ovsdbClient != null && ovsdbClient.isConnected(); |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Connects to OVSDB server for a given node. |
| 378 | * |
| 379 | * @param node cordvtn node |
| 380 | */ |
| 381 | private void connect(CordVtnNode node) { |
| 382 | checkNotNull(node); |
| 383 | |
| 384 | if (!nodeStore.containsKey(node)) { |
| 385 | log.warn("Node {} does not exist", node.hostname()); |
| 386 | return; |
| 387 | } |
| 388 | |
| 389 | if (!getOvsdbConnectionState(node)) { |
| 390 | // FIXME remove existing OVSDB device to work around OVSDB device re-connect issue |
| 391 | if (deviceService.getDevice(node.ovsdbId()) != null) { |
| 392 | adminService.removeDevice(node.ovsdbId()); |
| 393 | } |
| 394 | controller.connect(node.ovsdbIp(), node.ovsdbPort()); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * Disconnects OVSDB server for a given node. |
| 400 | * |
| 401 | * @param node cordvtn node |
| 402 | */ |
| 403 | private void disconnect(CordVtnNode node) { |
| 404 | checkNotNull(node); |
| 405 | |
| 406 | if (!nodeStore.containsKey(node)) { |
| 407 | log.warn("Node {} does not exist", node.hostname()); |
| 408 | return; |
| 409 | } |
| 410 | |
| 411 | if (getOvsdbConnectionState(node)) { |
| 412 | OvsdbClientService ovsdbClient = getOvsdbClient(node); |
| 413 | ovsdbClient.disconnect(); |
| 414 | } |
| 415 | |
| 416 | // FIXME remove existing OVSDB device to work around OVSDB device re-connect issue |
| 417 | if (deviceService.getDevice(node.ovsdbId()) != null) { |
| 418 | adminService.removeDevice(node.ovsdbId()); |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | /** |
| 423 | * Returns cordvtn node associated with a given OVSDB device. |
| 424 | * |
| 425 | * @param ovsdbId OVSDB device id |
| 426 | * @return cordvtn node, null if it fails to find the node |
| 427 | */ |
| 428 | private CordVtnNode getNodeByOvsdbId(DeviceId ovsdbId) { |
| 429 | try { |
| 430 | return getNodes().stream() |
| 431 | .filter(node -> node.ovsdbId().equals(ovsdbId)) |
| 432 | .findFirst().get(); |
| 433 | } catch (NoSuchElementException e) { |
| 434 | log.debug("Couldn't find node information for {}", ovsdbId); |
| 435 | return null; |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | /** |
| 440 | * Returns cordvtn node associated with a given integration bridge. |
| 441 | * |
| 442 | * @param bridgeId device id of integration bridge |
| 443 | * @return cordvtn node, null if it fails to find the node |
| 444 | */ |
| 445 | private CordVtnNode getNodeByBridgeId(DeviceId bridgeId) { |
| 446 | try { |
| 447 | return getNodes().stream() |
| 448 | .filter(node -> node.intBrId().equals(bridgeId)) |
| 449 | .findFirst().get(); |
| 450 | } catch (NoSuchElementException e) { |
| 451 | log.debug("Couldn't find node information for {}", bridgeId); |
| 452 | return null; |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | /** |
| 457 | * Returns OVSDB client for a given node. |
| 458 | * |
| 459 | * @param node cordvtn node |
| 460 | * @return OVSDB client, or null if it fails to get OVSDB client |
| 461 | */ |
| 462 | private OvsdbClientService getOvsdbClient(CordVtnNode node) { |
| 463 | checkNotNull(node); |
Hyunsun Moon | 1f14555 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 464 | |
| 465 | OvsdbClientService ovsdbClient = controller.getOvsdbClient( |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 466 | new OvsdbNodeId(node.ovsdbIp(), node.ovsdbPort().toInt())); |
Hyunsun Moon | 1f14555 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 467 | if (ovsdbClient == null) { |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 468 | log.debug("Couldn't find OVSDB client for {}", node.hostname()); |
Hyunsun Moon | 1f14555 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 469 | } |
| 470 | return ovsdbClient; |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 471 | } |
| 472 | |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 473 | /** |
| 474 | * Creates an integration bridge for a given node. |
| 475 | * |
| 476 | * @param node cordvtn node |
| 477 | */ |
| 478 | private void createIntegrationBridge(CordVtnNode node) { |
| 479 | if (checkIntegrationBridge(node)) { |
| 480 | return; |
| 481 | } |
| 482 | |
Hyunsun Moon | 523d976 | 2015-10-19 12:38:21 -0700 | [diff] [blame] | 483 | List<ControllerInfo> controllers = new ArrayList<>(); |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame^] | 484 | Sets.newHashSet(clusterService.getNodes()).stream() |
Hyunsun Moon | 523d976 | 2015-10-19 12:38:21 -0700 | [diff] [blame] | 485 | .forEach(controller -> { |
| 486 | ControllerInfo ctrlInfo = new ControllerInfo(controller.ip(), OFPORT, "tcp"); |
| 487 | controllers.add(ctrlInfo); |
| 488 | }); |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 489 | String dpid = node.intBrId().toString().substring(DPID_BEGIN); |
Hyunsun Moon | 523d976 | 2015-10-19 12:38:21 -0700 | [diff] [blame] | 490 | |
Hyunsun Moon | 523d976 | 2015-10-19 12:38:21 -0700 | [diff] [blame] | 491 | try { |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 492 | DriverHandler handler = driverService.createHandler(node.ovsdbId()); |
Hyunsun Moon | d772f34 | 2015-10-28 20:28:16 -0700 | [diff] [blame] | 493 | BridgeConfig bridgeConfig = handler.behaviour(BridgeConfig.class); |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame^] | 494 | bridgeConfig.addBridge(BridgeName.bridgeName(DEFAULT_BRIDGE), dpid, controllers); |
Hyunsun Moon | d772f34 | 2015-10-28 20:28:16 -0700 | [diff] [blame] | 495 | } catch (ItemNotFoundException e) { |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 496 | log.warn("Failed to create integration bridge on {}", node.ovsdbId()); |
Hyunsun Moon | d772f34 | 2015-10-28 20:28:16 -0700 | [diff] [blame] | 497 | } |
| 498 | } |
| 499 | |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 500 | /** |
| 501 | * Creates tunnel interface to the integration bridge for a given node. |
| 502 | * |
| 503 | * @param node cordvtn node |
| 504 | */ |
| 505 | private void createTunnelInterface(CordVtnNode node) { |
| 506 | if (checkTunnelInterface(node)) { |
| 507 | return; |
| 508 | } |
| 509 | |
Hyunsun Moon | d772f34 | 2015-10-28 20:28:16 -0700 | [diff] [blame] | 510 | DefaultAnnotations.Builder optionBuilder = DefaultAnnotations.builder(); |
| 511 | for (String key : DEFAULT_TUNNEL_OPTIONS.keySet()) { |
| 512 | optionBuilder.set(key, DEFAULT_TUNNEL_OPTIONS.get(key)); |
| 513 | } |
| 514 | TunnelDescription description = |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame^] | 515 | new DefaultTunnelDescription(null, null, VXLAN, TunnelName.tunnelName(DEFAULT_TUNNEL), |
Hyunsun Moon | d772f34 | 2015-10-28 20:28:16 -0700 | [diff] [blame] | 516 | optionBuilder.build()); |
| 517 | try { |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 518 | DriverHandler handler = driverService.createHandler(node.ovsdbId()); |
Hyunsun Moon | d772f34 | 2015-10-28 20:28:16 -0700 | [diff] [blame] | 519 | TunnelConfig tunnelConfig = handler.behaviour(TunnelConfig.class); |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame^] | 520 | tunnelConfig.createTunnelInterface(BridgeName.bridgeName(DEFAULT_BRIDGE), description); |
Hyunsun Moon | d772f34 | 2015-10-28 20:28:16 -0700 | [diff] [blame] | 521 | } catch (ItemNotFoundException e) { |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 522 | log.warn("Failed to create tunnel interface on {}", node.ovsdbId()); |
Hyunsun Moon | d772f34 | 2015-10-28 20:28:16 -0700 | [diff] [blame] | 523 | } |
| 524 | } |
| 525 | |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 526 | /** |
| 527 | * Checks if integration bridge exists and available. |
| 528 | * |
| 529 | * @param node cordvtn node |
| 530 | * @return true if the bridge is available, false otherwise |
| 531 | */ |
| 532 | private boolean checkIntegrationBridge(CordVtnNode node) { |
| 533 | return (deviceService.getDevice(node.intBrId()) != null |
| 534 | && deviceService.isAvailable(node.intBrId())); |
| 535 | } |
| 536 | |
| 537 | /** |
| 538 | * Checks if tunnel interface exists. |
| 539 | * |
| 540 | * @param node cordvtn node |
| 541 | * @return true if the interface exists, false otherwise |
| 542 | */ |
| 543 | private boolean checkTunnelInterface(CordVtnNode node) { |
Hyunsun Moon | d772f34 | 2015-10-28 20:28:16 -0700 | [diff] [blame] | 544 | try { |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 545 | deviceService.getPorts(node.intBrId()) |
| 546 | .stream() |
| 547 | .filter(p -> p.annotations().value("portName").contains(DEFAULT_TUNNEL) |
| 548 | && p.isEnabled()) |
Hyunsun Moon | d772f34 | 2015-10-28 20:28:16 -0700 | [diff] [blame] | 549 | .findAny().get(); |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 550 | return true; |
| 551 | } catch (NoSuchElementException e) { |
Hyunsun Moon | 523d976 | 2015-10-19 12:38:21 -0700 | [diff] [blame] | 552 | return false; |
| 553 | } |
Hyunsun Moon | 523d976 | 2015-10-19 12:38:21 -0700 | [diff] [blame] | 554 | } |
| 555 | |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame^] | 556 | /** |
| 557 | * Returns tunnel port of the device. |
| 558 | * |
| 559 | * @param bridgeId device id |
| 560 | * @return port, null if no tunnel port exists on a given device |
| 561 | */ |
| 562 | private Port getTunnelPort(DeviceId bridgeId) { |
| 563 | try { |
| 564 | return deviceService.getPorts(bridgeId).stream() |
| 565 | .filter(p -> p.annotations().value("portName").contains(DEFAULT_TUNNEL) |
| 566 | && p.isEnabled()) |
| 567 | .findFirst().get(); |
| 568 | } catch (NoSuchElementException e) { |
| 569 | return null; |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | /** |
| 574 | * Returns remote ip address for tunneling. |
| 575 | * |
| 576 | * @param bridgeId device id |
| 577 | * @return ip address, null if no such device exists |
| 578 | */ |
| 579 | private IpAddress getRemoteIp(DeviceId bridgeId) { |
| 580 | CordVtnNode node = getNodeByBridgeId(bridgeId); |
| 581 | if (node != null) { |
| 582 | // TODO get data plane IP for tunneling |
| 583 | return node.ovsdbIp(); |
| 584 | } else { |
| 585 | return null; |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | /** |
| 590 | * Returns destination information of all ports associated with a given |
| 591 | * OpenStack network. Output of the destination information is set to local |
| 592 | * port or tunnel port according to a given device id. |
| 593 | * |
| 594 | * @param deviceId device id to install flow rules |
| 595 | * @param vNet OpenStack network |
| 596 | * @return list of flow information, empty list if no flow information exists |
| 597 | */ |
| 598 | private List<DestinationInfo> getSameNetworkPortsInfo(DeviceId deviceId, OpenstackNetwork vNet) { |
| 599 | List<DestinationInfo> dstInfos = Lists.newArrayList(); |
| 600 | long tunnelId = Long.valueOf(vNet.segmentId()); |
| 601 | |
| 602 | for (OpenstackPort vPort : openstackService.ports(vNet.id())) { |
| 603 | ConnectPoint cp = getConnectPoint(vPort); |
| 604 | if (cp == null) { |
| 605 | log.debug("Couldn't find connection point for OpenStack port {}", vPort.id()); |
| 606 | continue; |
| 607 | } |
| 608 | |
| 609 | DestinationInfo.Builder dBuilder = cp.deviceId().equals(deviceId) ? |
| 610 | DestinationInfo.builder(deviceService.getPort(cp.deviceId(), cp.port())) : |
| 611 | DestinationInfo.builder(getTunnelPort(deviceId)) |
| 612 | .setRemoteIp(getRemoteIp(cp.deviceId())); |
| 613 | |
| 614 | dBuilder.setMac(vPort.macAddress()) |
| 615 | .setTunnelId(tunnelId); |
| 616 | dstInfos.add(dBuilder.build()); |
| 617 | } |
| 618 | return dstInfos; |
| 619 | } |
| 620 | |
| 621 | /** |
| 622 | * Returns local ports associated with a given OpenStack network. |
| 623 | * |
| 624 | * @param bridgeId device id |
| 625 | * @param vNet OpenStack network |
| 626 | * @return port list, empty list if no port exists |
| 627 | */ |
| 628 | private List<Port> getLocalSameNetworkPorts(DeviceId bridgeId, OpenstackNetwork vNet) { |
| 629 | List<Port> ports = new ArrayList<>(); |
| 630 | openstackService.ports(vNet.id()).stream().forEach(port -> { |
| 631 | ConnectPoint cp = getConnectPoint(port); |
| 632 | if (cp != null && cp.deviceId().equals(bridgeId)) { |
| 633 | ports.add(deviceService.getPort(cp.deviceId(), cp.port())); |
| 634 | } |
| 635 | }); |
| 636 | return ports; |
| 637 | } |
| 638 | |
| 639 | /** |
| 640 | * Returns OpenStack port associated with a given host. |
| 641 | * |
| 642 | * @param host host |
| 643 | * @return OpenStack port, or null if no port has been found |
| 644 | */ |
| 645 | private OpenstackPort getOpenstackPortByHost(Host host) { |
| 646 | Port port = deviceService.getPort(host.location().deviceId(), |
| 647 | host.location().port()); |
| 648 | return openstackService.port(port); |
| 649 | } |
| 650 | |
| 651 | /** |
| 652 | * Returns OpenStack network associated with a given host. |
| 653 | * |
| 654 | * @param host host |
| 655 | * @return OpenStack network, or null if no network has been found |
| 656 | */ |
| 657 | private OpenstackNetwork getOpenstackNetworkByHost(Host host) { |
| 658 | OpenstackPort vPort = getOpenstackPortByHost(host); |
| 659 | if (vPort != null) { |
| 660 | return openstackService.network(vPort.networkId()); |
| 661 | } else { |
| 662 | return null; |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | /** |
| 667 | * Returns port name with OpenStack port information. |
| 668 | * |
| 669 | * @param vPort OpenStack port |
| 670 | * @return port name |
| 671 | */ |
| 672 | private String getPortName(OpenstackPort vPort) { |
| 673 | checkNotNull(vPort); |
| 674 | return VPORT_PREFIX + vPort.id().substring(0, 10); |
| 675 | } |
| 676 | |
| 677 | /** |
| 678 | * Returns connect point of a given OpenStack port. |
| 679 | * It assumes there's only one physical port associated with an OpenStack port. |
| 680 | * |
| 681 | * @param vPort openstack port |
| 682 | * @return connect point, null if no such port exists |
| 683 | */ |
| 684 | private ConnectPoint getConnectPoint(OpenstackPort vPort) { |
| 685 | try { |
| 686 | Host host = hostService.getHostsByMac(vPort.macAddress()) |
| 687 | .stream() |
| 688 | .findFirst() |
| 689 | .get(); |
| 690 | return new ConnectPoint(host.location().deviceId(), host.location().port()); |
| 691 | } catch (NoSuchElementException e) { |
| 692 | log.debug("Not a valid host with {}", vPort.macAddress()); |
| 693 | return null; |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | /** |
| 698 | * Installs flow rules for a given OpenStack network. |
| 699 | * |
| 700 | * @param vNet OpenStack network |
| 701 | */ |
| 702 | private void installFlowRules(OpenstackNetwork vNet) { |
| 703 | checkNotNull(vNet, "Tenant network should not be null"); |
| 704 | |
| 705 | for (Device device : deviceService.getAvailableDevices(SWITCH)) { |
| 706 | List<DestinationInfo> dstInfos = getSameNetworkPortsInfo(device.id(), vNet); |
| 707 | |
| 708 | for (Port inPort : getLocalSameNetworkPorts(device.id(), vNet)) { |
| 709 | List<DestinationInfo> localInInfos = dstInfos.stream() |
| 710 | .filter(info -> !info.output().equals(inPort)) |
| 711 | .collect(Collectors.toList()); |
| 712 | ruleInstaller.installFlowRulesLocalIn(device.id(), inPort, localInInfos); |
| 713 | } |
| 714 | |
| 715 | Port tunPort = getTunnelPort(device.id()); |
| 716 | List<DestinationInfo> tunnelInInfos = dstInfos.stream() |
| 717 | .filter(info -> !info.output().equals(tunPort)) |
| 718 | .collect(Collectors.toList()); |
| 719 | ruleInstaller.installFlowRulesTunnelIn(device.id(), tunPort, tunnelInInfos); |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | /** |
| 724 | * Uninstalls flow rules associated with a given host for a given OpenStack network. |
| 725 | * |
| 726 | * @param vNet OpenStack network |
| 727 | * @param host removed host |
| 728 | */ |
| 729 | private void uninstallFlowRules(OpenstackNetwork vNet, Host host) { |
| 730 | checkNotNull(vNet, "Tenant network should not be null"); |
| 731 | |
| 732 | Port removedPort = deviceService.getPort(host.location().deviceId(), |
| 733 | host.location().port()); |
| 734 | |
| 735 | for (Device device : deviceService.getAvailableDevices(SWITCH)) { |
| 736 | List<DestinationInfo> dstInfos = getSameNetworkPortsInfo(device.id(), vNet); |
| 737 | |
| 738 | for (Port inPort : getLocalSameNetworkPorts(device.id(), vNet)) { |
| 739 | List<DestinationInfo> localInInfos = Lists.newArrayList( |
| 740 | DestinationInfo.builder(getTunnelPort(device.id())) |
| 741 | .setTunnelId(Long.valueOf(vNet.segmentId())) |
| 742 | .setMac(host.mac()) |
| 743 | .setRemoteIp(getRemoteIp(host.location().deviceId())) |
| 744 | .build()); |
| 745 | ruleInstaller.uninstallFlowRules(device.id(), inPort, localInInfos); |
| 746 | } |
| 747 | |
| 748 | if (device.id().equals(host.location().deviceId())) { |
| 749 | Port tunPort = getTunnelPort(device.id()); |
| 750 | List<DestinationInfo> tunnelInInfo = Lists.newArrayList( |
| 751 | DestinationInfo.builder(removedPort) |
| 752 | .setTunnelId(Long.valueOf(vNet.segmentId())) |
| 753 | .setMac(host.mac()) |
| 754 | .build()); |
| 755 | |
| 756 | ruleInstaller.uninstallFlowRules(device.id(), tunPort, tunnelInInfo); |
| 757 | ruleInstaller.uninstallFlowRules(device.id(), removedPort, dstInfos); |
| 758 | } |
| 759 | } |
| 760 | } |
| 761 | |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 762 | private class InternalDeviceListener implements DeviceListener { |
| 763 | |
| 764 | @Override |
| 765 | public void event(DeviceEvent event) { |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 766 | |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 767 | Device device = event.subject(); |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 768 | ConnectionHandler<Device> handler = |
| 769 | (device.type().equals(SWITCH) ? bridgeHandler : ovsdbHandler); |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 770 | |
| 771 | switch (event.type()) { |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 772 | case PORT_ADDED: |
| 773 | eventExecutor.submit(() -> bridgeHandler.portAdded(event.port())); |
Hyunsun Moon | 2b53032 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 774 | break; |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 775 | case PORT_UPDATED: |
| 776 | if (!event.port().isEnabled()) { |
| 777 | eventExecutor.submit(() -> bridgeHandler.portRemoved(event.port())); |
| 778 | } |
| 779 | break; |
| 780 | case DEVICE_ADDED: |
Hyunsun Moon | 2b53032 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 781 | case DEVICE_AVAILABILITY_CHANGED: |
Hyunsun Moon | 523d976 | 2015-10-19 12:38:21 -0700 | [diff] [blame] | 782 | if (deviceService.isAvailable(device.id())) { |
| 783 | eventExecutor.submit(() -> handler.connected(device)); |
| 784 | } else { |
| 785 | eventExecutor.submit(() -> handler.disconnected(device)); |
| 786 | } |
Hyunsun Moon | 2b53032 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 787 | break; |
| 788 | default: |
| 789 | break; |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 790 | } |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | private class InternalHostListener implements HostListener { |
| 795 | |
| 796 | @Override |
| 797 | public void event(HostEvent event) { |
| 798 | Host vm = event.subject(); |
| 799 | |
| 800 | switch (event.type()) { |
| 801 | case HOST_ADDED: |
| 802 | eventExecutor.submit(() -> vmHandler.connected(vm)); |
| 803 | break; |
| 804 | case HOST_REMOVED: |
| 805 | eventExecutor.submit(() -> vmHandler.disconnected(vm)); |
| 806 | break; |
| 807 | default: |
| 808 | break; |
| 809 | } |
| 810 | } |
| 811 | } |
| 812 | |
Hyunsun Moon | 2b53032 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 813 | private class OvsdbHandler implements ConnectionHandler<Device> { |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 814 | |
| 815 | @Override |
| 816 | public void connected(Device device) { |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 817 | CordVtnNode node = getNodeByOvsdbId(device.id()); |
| 818 | if (node != null) { |
| 819 | setNodeState(node, checkNodeState(node)); |
Hyunsun Moon | 523d976 | 2015-10-19 12:38:21 -0700 | [diff] [blame] | 820 | } |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | @Override |
| 824 | public void disconnected(Device device) { |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 825 | log.info("OVSDB {} is disconnected", device.id()); |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 826 | } |
| 827 | } |
| 828 | |
| 829 | private class BridgeHandler implements ConnectionHandler<Device> { |
| 830 | |
| 831 | @Override |
| 832 | public void connected(Device device) { |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 833 | CordVtnNode node = getNodeByBridgeId(device.id()); |
| 834 | if (node != null) { |
| 835 | setNodeState(node, checkNodeState(node)); |
Hyunsun Moon | 1f14555 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 836 | } |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | @Override |
| 840 | public void disconnected(Device device) { |
Hyunsun Moon | 8539b04 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 841 | CordVtnNode node = getNodeByBridgeId(device.id()); |
| 842 | if (node != null) { |
| 843 | log.info("Integration Bridge is disconnected from {}", node.hostname()); |
| 844 | setNodeState(node, NodeState.INCOMPLETE); |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | /** |
| 849 | * Handles port added situation. |
| 850 | * If the added port is tunnel port, proceed remaining node initialization. |
| 851 | * Otherwise, do nothing. |
| 852 | * |
| 853 | * @param port port |
| 854 | */ |
| 855 | public void portAdded(Port port) { |
| 856 | if (!port.annotations().value("portName").contains(DEFAULT_TUNNEL)) { |
| 857 | return; |
| 858 | } |
| 859 | |
| 860 | CordVtnNode node = getNodeByBridgeId((DeviceId) port.element().id()); |
| 861 | if (node != null) { |
| 862 | setNodeState(node, checkNodeState(node)); |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | /** |
| 867 | * Handles port removed situation. |
| 868 | * If the removed port is tunnel port, proceed remaining node initialization. |
| 869 | * Others, do nothing. |
| 870 | * |
| 871 | * @param port port |
| 872 | */ |
| 873 | public void portRemoved(Port port) { |
| 874 | if (!port.annotations().value("portName").contains(DEFAULT_TUNNEL)) { |
| 875 | return; |
| 876 | } |
| 877 | |
| 878 | CordVtnNode node = getNodeByBridgeId((DeviceId) port.element().id()); |
| 879 | if (node != null) { |
| 880 | log.info("Tunnel interface is removed from {}", node.hostname()); |
| 881 | setNodeState(node, NodeState.INCOMPLETE); |
| 882 | } |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 883 | } |
| 884 | } |
| 885 | |
Hyunsun Moon | 2b53032 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 886 | private class VmHandler implements ConnectionHandler<Host> { |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 887 | |
| 888 | @Override |
| 889 | public void connected(Host host) { |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame^] | 890 | CordVtnNode node = getNodeByBridgeId(host.location().deviceId()); |
| 891 | if (node == null || !getNodeState(node).equals(NodeState.COMPLETE)) { |
| 892 | // do nothing for the host on unregistered or unprepared device |
| 893 | return; |
| 894 | } |
| 895 | |
| 896 | OpenstackNetwork vNet = getOpenstackNetworkByHost(host); |
| 897 | if (vNet == null) { |
| 898 | return; |
| 899 | } |
| 900 | |
Hyunsun Moon | 1f14555 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 901 | log.info("VM {} is detected", host.id()); |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame^] | 902 | |
| 903 | hostNetworkMap.put(host.id(), vNet.id()); |
| 904 | installFlowRules(vNet); |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | @Override |
| 908 | public void disconnected(Host host) { |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame^] | 909 | CordVtnNode node = getNodeByBridgeId(host.location().deviceId()); |
| 910 | if (node == null || !getNodeState(node).equals(NodeState.COMPLETE)) { |
| 911 | // do nothing for the host on unregistered or unprepared device |
| 912 | return; |
| 913 | } |
| 914 | |
| 915 | OpenstackNetwork vNet = openstackService.network(hostNetworkMap.get(host.id())); |
| 916 | if (vNet == null) { |
| 917 | return; |
| 918 | } |
| 919 | |
Hyunsun Moon | 1f14555 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 920 | log.info("VM {} is vanished", host.id()); |
Hyunsun Moon | 9f0814b | 2015-11-04 17:34:35 -0800 | [diff] [blame^] | 921 | |
| 922 | uninstallFlowRules(vNet, host); |
| 923 | hostNetworkMap.remove(host.id()); |
Hyunsun Moon | d0e932a | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 924 | } |
| 925 | } |
| 926 | } |