blob: 52dbeb18b9ec84f5b8b52cc9adcbc9f32affe999 [file] [log] [blame]
Hyunsun Moonb77b60f2016-01-15 20:03:18 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Hyunsun Moonb77b60f2016-01-15 20:03:18 -08003 *
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 */
16package org.onosproject.cordvtn;
17
18import com.google.common.collect.Sets;
Hyunsun Moon133fd792016-02-09 01:55:48 -080019import com.jcraft.jsch.Session;
Hyunsun Moonb77b60f2016-01-15 20:03:18 -080020import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
23import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
25import org.apache.felix.scr.annotations.Service;
Hyunsun Moon133fd792016-02-09 01:55:48 -080026import org.onlab.packet.IpAddress;
Hyunsun Moonb77b60f2016-01-15 20:03:18 -080027import org.onlab.util.ItemNotFoundException;
28import org.onlab.util.KryoNamespace;
29import org.onosproject.cluster.ClusterService;
Hyunsun Moon98025542016-03-08 04:36:02 -080030import org.onosproject.cluster.LeadershipService;
31import org.onosproject.cluster.NodeId;
Hyunsun Moonb77b60f2016-01-15 20:03:18 -080032import org.onosproject.core.ApplicationId;
33import org.onosproject.core.CoreService;
Hyunsun Moonb77b60f2016-01-15 20:03:18 -080034import org.onosproject.net.ConnectPoint;
35import org.onosproject.net.DefaultAnnotations;
36import org.onosproject.net.Device;
37import org.onosproject.net.DeviceId;
38import org.onosproject.net.Host;
39import org.onosproject.net.Port;
40import org.onosproject.net.behaviour.BridgeConfig;
41import org.onosproject.net.behaviour.BridgeName;
42import org.onosproject.net.behaviour.ControllerInfo;
43import org.onosproject.net.behaviour.DefaultTunnelDescription;
44import org.onosproject.net.behaviour.TunnelConfig;
45import org.onosproject.net.behaviour.TunnelDescription;
46import org.onosproject.net.behaviour.TunnelName;
Hyunsun Moonb77b60f2016-01-15 20:03:18 -080047import org.onosproject.net.config.NetworkConfigEvent;
48import org.onosproject.net.config.NetworkConfigListener;
49import org.onosproject.net.config.NetworkConfigRegistry;
50import org.onosproject.net.config.NetworkConfigService;
Hyunsun Moonb77b60f2016-01-15 20:03:18 -080051import org.onosproject.net.device.DeviceAdminService;
52import org.onosproject.net.device.DeviceEvent;
53import org.onosproject.net.device.DeviceListener;
54import org.onosproject.net.device.DeviceService;
55import org.onosproject.net.driver.DriverHandler;
56import org.onosproject.net.driver.DriverService;
57import org.onosproject.net.flow.FlowRuleService;
58import org.onosproject.net.group.GroupService;
59import org.onosproject.net.host.HostService;
60import org.onosproject.ovsdb.controller.OvsdbClientService;
61import org.onosproject.ovsdb.controller.OvsdbController;
62import org.onosproject.ovsdb.controller.OvsdbNodeId;
63import org.onosproject.store.serializers.KryoNamespaces;
64import org.onosproject.store.service.ConsistentMap;
Hyunsun Moon80b03872016-03-10 12:40:16 -080065import org.onosproject.store.service.MapEvent;
66import org.onosproject.store.service.MapEventListener;
Hyunsun Moonb77b60f2016-01-15 20:03:18 -080067import org.onosproject.store.service.Serializer;
68import org.onosproject.store.service.StorageService;
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -080069import org.onosproject.store.service.Versioned;
Hyunsun Moonb77b60f2016-01-15 20:03:18 -080070import org.slf4j.Logger;
71
72import java.util.ArrayList;
73import java.util.HashMap;
74import java.util.List;
75import java.util.Map;
Hyunsun Moon98025542016-03-08 04:36:02 -080076import java.util.Objects;
Hyunsun Moon133fd792016-02-09 01:55:48 -080077import java.util.Set;
Hyunsun Moonb77b60f2016-01-15 20:03:18 -080078import java.util.concurrent.ExecutorService;
Hyunsun Moonaf520d32016-03-07 16:37:17 -080079import java.util.stream.Collectors;
Hyunsun Moonb77b60f2016-01-15 20:03:18 -080080
81import static com.google.common.base.Preconditions.checkNotNull;
82import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
83import static org.onlab.util.Tools.groupedThreads;
84import static org.onosproject.net.Device.Type.SWITCH;
85import static org.onosproject.net.behaviour.TunnelDescription.Type.VXLAN;
86import static org.slf4j.LoggerFactory.getLogger;
87
88/**
89 * Reads node information from the network config file and handles the config
90 * update events.
91 * Only a leader controller performs the node addition or deletion.
92 */
93@Component(immediate = true)
94@Service(value = CordVtnNodeManager.class)
95public class CordVtnNodeManager {
96
97 protected final Logger log = getLogger(getClass());
98
99 private static final KryoNamespace.Builder NODE_SERIALIZER = KryoNamespace.newBuilder()
100 .register(KryoNamespaces.API)
Hyunsun Moon133fd792016-02-09 01:55:48 -0800101 .register(KryoNamespaces.MISC)
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800102 .register(CordVtnNode.class)
Hyunsun Moon133fd792016-02-09 01:55:48 -0800103 .register(NodeState.class)
104 .register(SshAccessInfo.class)
105 .register(NetworkAddress.class);
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800106
107 private static final String DEFAULT_BRIDGE = "br-int";
108 private static final String DEFAULT_TUNNEL = "vxlan";
109 private static final String VPORT_PREFIX = "tap";
110 private static final String OK = "OK";
111 private static final String NO = "NO";
112
113 private static final Map<String, String> DEFAULT_TUNNEL_OPTIONS = new HashMap<String, String>() {
114 {
115 put("key", "flow");
116 put("remote_ip", "flow");
117 }
118 };
119 private static final int DPID_BEGIN = 3;
120 private static final int OFPORT = 6653;
121
122 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
123 protected CoreService coreService;
124
125 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
126 protected NetworkConfigRegistry configRegistry;
127
128 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
129 protected NetworkConfigService configService;
130
131 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
132 protected StorageService storageService;
133
134 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
135 protected DeviceAdminService adminService;
136
137 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
138 protected OvsdbController controller;
139
140 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
141 protected ClusterService clusterService;
142
143 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
144 protected DriverService driverService;
145
146 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
147 protected DeviceService deviceService;
148
149 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
150 protected HostService hostService;
151
152 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
153 protected FlowRuleService flowRuleService;
154
155 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Hyunsun Moon98025542016-03-08 04:36:02 -0800156 protected LeadershipService leadershipService;
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800157
158 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
159 protected GroupService groupService;
160
161 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
162 protected CordVtnService cordVtnService;
163
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800164 private final ExecutorService eventExecutor =
165 newSingleThreadScheduledExecutor(groupedThreads("onos/cordvtncfg", "event-handler"));
166
167 private final NetworkConfigListener configListener = new InternalConfigListener();
168 private final DeviceListener deviceListener = new InternalDeviceListener();
Hyunsun Moon80b03872016-03-10 12:40:16 -0800169 private final MapEventListener<String, CordVtnNode> nodeStoreListener = new InternalMapListener();
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800170
171 private final OvsdbHandler ovsdbHandler = new OvsdbHandler();
172 private final BridgeHandler bridgeHandler = new BridgeHandler();
173
Hyunsun Moonaf520d32016-03-07 16:37:17 -0800174 private ConsistentMap<String, CordVtnNode> nodeStore;
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800175 private CordVtnRuleInstaller ruleInstaller;
176 private ApplicationId appId;
Hyunsun Moon98025542016-03-08 04:36:02 -0800177 private NodeId localNodeId;
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800178
Hyunsun Moonaf520d32016-03-07 16:37:17 -0800179 private enum NodeState implements CordVtnNodeState {
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800180
181 INIT {
182 @Override
183 public void process(CordVtnNodeManager nodeManager, CordVtnNode node) {
Hyunsun Moon133fd792016-02-09 01:55:48 -0800184 if (!nodeManager.isOvsdbConnected(node)) {
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800185 nodeManager.connectOvsdb(node);
186 } else {
187 nodeManager.createIntegrationBridge(node);
188 }
189 }
190 },
191 BRIDGE_CREATED {
192 @Override
193 public void process(CordVtnNodeManager nodeManager, CordVtnNode node) {
Hyunsun Moon133fd792016-02-09 01:55:48 -0800194 if (!nodeManager.isOvsdbConnected(node)) {
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800195 nodeManager.connectOvsdb(node);
196 } else {
197 nodeManager.createTunnelInterface(node);
Hyunsun Moon133fd792016-02-09 01:55:48 -0800198 nodeManager.addDataPlaneInterface(node);
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800199 }
200 }
201 },
Hyunsun Moon133fd792016-02-09 01:55:48 -0800202 PORTS_ADDED {
Hyunsun Moon177506f2016-01-21 00:54:52 -0800203 @Override
204 public void process(CordVtnNodeManager nodeManager, CordVtnNode node) {
Hyunsun Moon133fd792016-02-09 01:55:48 -0800205 nodeManager.setIpAddress(node);
Hyunsun Moon177506f2016-01-21 00:54:52 -0800206 }
Hyunsun Moon177506f2016-01-21 00:54:52 -0800207 },
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800208 COMPLETE {
209 @Override
210 public void process(CordVtnNodeManager nodeManager, CordVtnNode node) {
211 nodeManager.postInit(node);
212 }
213 },
214 INCOMPLETE {
215 @Override
216 public void process(CordVtnNodeManager nodeManager, CordVtnNode node) {
217 }
218 };
219
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800220 public abstract void process(CordVtnNodeManager nodeManager, CordVtnNode node);
221 }
222
223 @Activate
224 protected void active() {
225 appId = coreService.getAppId(CordVtnService.CORDVTN_APP_ID);
Hyunsun Moon98025542016-03-08 04:36:02 -0800226 localNodeId = clusterService.getLocalNode().id();
227 leadershipService.runForLeadership(appId.name());
228
Hyunsun Moonaf520d32016-03-07 16:37:17 -0800229 nodeStore = storageService.<String, CordVtnNode>consistentMapBuilder()
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800230 .withSerializer(Serializer.using(NODE_SERIALIZER.build()))
231 .withName("cordvtn-nodestore")
232 .withApplicationId(appId)
233 .build();
234
235 ruleInstaller = new CordVtnRuleInstaller(appId, flowRuleService,
236 deviceService,
237 driverService,
238 groupService,
Hyunsun Moonfae776d2016-03-08 18:07:52 -0800239 configRegistry,
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800240 DEFAULT_TUNNEL);
241
Hyunsun Moon80b03872016-03-10 12:40:16 -0800242 nodeStore.addListener(nodeStoreListener);
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800243 deviceService.addListener(deviceListener);
244 configService.addListener(configListener);
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800245 }
246
247 @Deactivate
248 protected void deactivate() {
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800249 configService.removeListener(configListener);
250 deviceService.removeListener(deviceListener);
251
Hyunsun Moon80b03872016-03-10 12:40:16 -0800252 nodeStore.removeListener(nodeStoreListener);
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800253 nodeStore.clear();
Hyunsun Moon80b03872016-03-10 12:40:16 -0800254
Hyunsun Moon98025542016-03-08 04:36:02 -0800255 leadershipService.withdraw(appId.name());
Hyunsun Moon80b03872016-03-10 12:40:16 -0800256 eventExecutor.shutdown();
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800257 }
258
259 /**
Hyunsun Moon80b03872016-03-10 12:40:16 -0800260 * Adds or updates a new node to the service.
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800261 *
262 * @param node cordvtn node
263 */
Hyunsun Moon80b03872016-03-10 12:40:16 -0800264 public void addOrUpdateNode(CordVtnNode node) {
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800265 checkNotNull(node);
Hyunsun Moonaf520d32016-03-07 16:37:17 -0800266 nodeStore.put(node.hostname(), CordVtnNode.getUpdatedNode(node, getNodeState(node)));
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800267 }
268
269 /**
270 * Deletes a node from the service.
271 *
272 * @param node cordvtn node
273 */
274 public void deleteNode(CordVtnNode node) {
275 checkNotNull(node);
276
Hyunsun Moon133fd792016-02-09 01:55:48 -0800277 if (isOvsdbConnected(node)) {
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800278 disconnectOvsdb(node);
279 }
280
Hyunsun Moonaf520d32016-03-07 16:37:17 -0800281 nodeStore.remove(node.hostname());
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800282 }
283
284 /**
285 * Initiates node to serve virtual tenant network.
286 *
287 * @param node cordvtn node
288 */
Hyunsun Moon80b03872016-03-10 12:40:16 -0800289 private void initNode(CordVtnNode node) {
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800290 checkNotNull(node);
291
Hyunsun Moon80b03872016-03-10 12:40:16 -0800292 NodeState state = (NodeState) node.state();
293 log.debug("Processing node: {} state: {}", node.hostname(), state);
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800294
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800295 state.process(this, node);
296 }
297
298 /**
299 * Returns node initialization state.
300 *
301 * @param node cordvtn node
302 * @return true if initial node setup is completed, otherwise false
303 */
Hyunsun Moon133fd792016-02-09 01:55:48 -0800304 public boolean isNodeInitComplete(CordVtnNode node) {
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800305 checkNotNull(node);
Hyunsun Moonaf520d32016-03-07 16:37:17 -0800306 return nodeStore.containsKey(node.hostname()) && getNodeState(node).equals(NodeState.COMPLETE);
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800307 }
308
309 /**
Hyunsun Moon2062e7e2016-03-08 15:37:57 -0800310 * Flush flows installed by cordvtn.
311 */
312 public void flushRules() {
313 ruleInstaller.flushRules();
314 }
315
316 /**
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -0800317 * Returns if current node state saved in nodeStore is COMPLETE or not.
318 *
319 * @param node cordvtn node
320 * @return true if it's complete state, otherwise false
321 */
322 private boolean isNodeStateComplete(CordVtnNode node) {
323 checkNotNull(node);
324
325 // the state saved in nodeStore can be wrong if IP address settings are changed
326 // after the node init has been completed since there's no way to detect it
327 // getNodeState and checkNodeInitState always return correct answer but can be slow
Hyunsun Moonaf520d32016-03-07 16:37:17 -0800328 Versioned<CordVtnNode> versionedNode = nodeStore.get(node.hostname());
329 CordVtnNodeState state = versionedNode.value().state();
330 return state != null && state.equals(NodeState.COMPLETE);
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -0800331 }
332
333 /**
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800334 * Returns detailed node initialization state.
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800335 *
336 * @param node cordvtn node
337 * @return string including detailed node init state
338 */
339 public String checkNodeInitState(CordVtnNode node) {
340 checkNotNull(node);
341
Hyunsun Moonaf520d32016-03-07 16:37:17 -0800342 if (!nodeStore.containsKey(node.hostname())) {
Hyunsun Moon133fd792016-02-09 01:55:48 -0800343 log.warn("Node {} does not exist, add node first", node.hostname());
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800344 return null;
345 }
346
Hyunsun Moon133fd792016-02-09 01:55:48 -0800347 Session session = RemoteIpCommandUtil.connect(node.sshInfo());
348 if (session == null) {
349 log.debug("Failed to SSH to {}", node.hostname());
350 return null;
351 }
352
353 Set<IpAddress> intBrIps = RemoteIpCommandUtil.getCurrentIps(session, DEFAULT_BRIDGE);
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800354 String result = String.format(
Hyunsun Moonaf520d32016-03-07 16:37:17 -0800355 "br-int created and connected : %s (%s)%n" +
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800356 "VXLAN interface created : %s%n" +
Hyunsun Moon133fd792016-02-09 01:55:48 -0800357 "Data plane interface added : %s (%s)%n" +
358 "IP flushed from %s : %s%n" +
359 "Data plane IP added to br-int : %s (%s)%n" +
360 "Local management IP added to br-int : %s (%s)",
Hyunsun Moonaf520d32016-03-07 16:37:17 -0800361 isBrIntCreated(node) ? OK : NO, node.intBrId(),
Hyunsun Moon133fd792016-02-09 01:55:48 -0800362 isTunnelIntfCreated(node) ? OK : NO,
363 isDataPlaneIntfAdded(node) ? OK : NO, node.dpIntf(),
364 node.dpIntf(),
365 RemoteIpCommandUtil.getCurrentIps(session, node.dpIntf()).isEmpty() ? OK : NO,
366 intBrIps.contains(node.dpIp().ip()) ? OK : NO, node.dpIp().cidr(),
367 intBrIps.contains(node.localMgmtIp().ip()) ? OK : NO, node.localMgmtIp().cidr());
368
369 RemoteIpCommandUtil.disconnect(session);
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800370
371 return result;
372 }
373
374 /**
375 * Returns the number of the nodes known to the service.
376 *
377 * @return number of nodes
378 */
379 public int getNodeCount() {
380 return nodeStore.size();
381 }
382
383 /**
384 * Returns all nodes known to the service.
385 *
386 * @return list of nodes
387 */
388 public List<CordVtnNode> getNodes() {
Hyunsun Moonaf520d32016-03-07 16:37:17 -0800389 return nodeStore.values().stream()
390 .map(Versioned::value)
391 .collect(Collectors.toList());
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800392 }
393
394 /**
395 * Returns cordvtn node associated with a given OVSDB device.
396 *
397 * @param ovsdbId OVSDB device id
398 * @return cordvtn node, null if it fails to find the node
399 */
400 private CordVtnNode getNodeByOvsdbId(DeviceId ovsdbId) {
401 return getNodes().stream()
402 .filter(node -> node.ovsdbId().equals(ovsdbId))
403 .findFirst().orElse(null);
404 }
405
406 /**
407 * Returns cordvtn node associated with a given integration bridge.
408 *
409 * @param bridgeId device id of integration bridge
410 * @return cordvtn node, null if it fails to find the node
411 */
412 private CordVtnNode getNodeByBridgeId(DeviceId bridgeId) {
413 return getNodes().stream()
414 .filter(node -> node.intBrId().equals(bridgeId))
415 .findFirst().orElse(null);
416 }
417
418 /**
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800419 * Sets a new state for a given cordvtn node.
420 *
421 * @param node cordvtn node
422 * @param newState new node state
423 */
424 private void setNodeState(CordVtnNode node, NodeState newState) {
425 checkNotNull(node);
426
Hyunsun Moon80b03872016-03-10 12:40:16 -0800427 log.debug("Changed {} state: {}", node.hostname(), newState);
Hyunsun Moonaf520d32016-03-07 16:37:17 -0800428 nodeStore.put(node.hostname(), CordVtnNode.getUpdatedNode(node, newState));
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800429 }
430
431 /**
432 * Checks current state of a given cordvtn node and returns it.
433 *
434 * @param node cordvtn node
435 * @return node state
436 */
Hyunsun Moon133fd792016-02-09 01:55:48 -0800437 private NodeState getNodeState(CordVtnNode node) {
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800438 checkNotNull(node);
439
Hyunsun Moon133fd792016-02-09 01:55:48 -0800440 if (isBrIntCreated(node) && isTunnelIntfCreated(node) &&
441 isDataPlaneIntfAdded(node) && isIpAddressSet(node)) {
Hyunsun Moon177506f2016-01-21 00:54:52 -0800442 return NodeState.COMPLETE;
Hyunsun Moon133fd792016-02-09 01:55:48 -0800443 } else if (isDataPlaneIntfAdded(node) && isTunnelIntfCreated(node)) {
444 return NodeState.PORTS_ADDED;
445 } else if (isBrIntCreated(node)) {
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800446 return NodeState.BRIDGE_CREATED;
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800447 } else {
448 return NodeState.INIT;
449 }
450 }
451
452 /**
453 * Performs tasks after node initialization.
454 * It disconnects unnecessary OVSDB connection and installs initial flow
455 * rules on the device.
456 *
457 * @param node cordvtn node
458 */
459 private void postInit(CordVtnNode node) {
460 disconnectOvsdb(node);
461
Hyunsun Moon133fd792016-02-09 01:55:48 -0800462 ruleInstaller.init(node.intBrId(), node.dpIntf(), node.dpIp().ip());
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800463
464 // add existing hosts to the service
465 deviceService.getPorts(node.intBrId()).stream()
466 .filter(port -> getPortName(port).startsWith(VPORT_PREFIX) &&
467 port.isEnabled())
468 .forEach(port -> cordVtnService.addServiceVm(node, getConnectPoint(port)));
469
470 // remove stale hosts from the service
471 hostService.getHosts().forEach(host -> {
472 Port port = deviceService.getPort(host.location().deviceId(), host.location().port());
473 if (port == null) {
474 cordVtnService.removeServiceVm(getConnectPoint(host));
475 }
476 });
477
478 log.info("Finished init {}", node.hostname());
479 }
480
481 /**
482 * Returns port name.
483 *
484 * @param port port
485 * @return port name
486 */
487 private String getPortName(Port port) {
488 return port.annotations().value("portName");
489 }
490
491 /**
492 * Returns connection state of OVSDB server for a given node.
493 *
494 * @param node cordvtn node
495 * @return true if it is connected, false otherwise
496 */
Hyunsun Moon133fd792016-02-09 01:55:48 -0800497 private boolean isOvsdbConnected(CordVtnNode node) {
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800498 checkNotNull(node);
499
500 OvsdbClientService ovsdbClient = getOvsdbClient(node);
501 return deviceService.isAvailable(node.ovsdbId()) &&
502 ovsdbClient != null && ovsdbClient.isConnected();
503 }
504
505 /**
506 * Connects to OVSDB server for a given node.
507 *
508 * @param node cordvtn node
509 */
510 private void connectOvsdb(CordVtnNode node) {
511 checkNotNull(node);
512
Hyunsun Moonaf520d32016-03-07 16:37:17 -0800513 if (!nodeStore.containsKey(node.hostname())) {
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800514 log.warn("Node {} does not exist", node.hostname());
515 return;
516 }
517
Hyunsun Moon133fd792016-02-09 01:55:48 -0800518 if (!isOvsdbConnected(node)) {
519 controller.connect(node.hostMgmtIp().ip(), node.ovsdbPort());
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800520 }
521 }
522
523 /**
524 * Disconnects OVSDB server for a given node.
525 *
526 * @param node cordvtn node
527 */
528 private void disconnectOvsdb(CordVtnNode node) {
529 checkNotNull(node);
530
Hyunsun Moonaf520d32016-03-07 16:37:17 -0800531 if (!nodeStore.containsKey(node.hostname())) {
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800532 log.warn("Node {} does not exist", node.hostname());
533 return;
534 }
535
Hyunsun Moon133fd792016-02-09 01:55:48 -0800536 if (isOvsdbConnected(node)) {
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800537 OvsdbClientService ovsdbClient = getOvsdbClient(node);
538 ovsdbClient.disconnect();
539 }
540 }
541
542 /**
543 * Returns OVSDB client for a given node.
544 *
545 * @param node cordvtn node
546 * @return OVSDB client, or null if it fails to get OVSDB client
547 */
548 private OvsdbClientService getOvsdbClient(CordVtnNode node) {
549 checkNotNull(node);
550
551 OvsdbClientService ovsdbClient = controller.getOvsdbClient(
Hyunsun Moon133fd792016-02-09 01:55:48 -0800552 new OvsdbNodeId(node.hostMgmtIp().ip(), node.ovsdbPort().toInt()));
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800553 if (ovsdbClient == null) {
554 log.trace("Couldn't find OVSDB client for {}", node.hostname());
555 }
556 return ovsdbClient;
557 }
558
559 /**
560 * Creates an integration bridge for a given node.
561 *
562 * @param node cordvtn node
563 */
564 private void createIntegrationBridge(CordVtnNode node) {
Hyunsun Moon133fd792016-02-09 01:55:48 -0800565 if (isBrIntCreated(node)) {
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800566 return;
567 }
568
569 List<ControllerInfo> controllers = new ArrayList<>();
570 Sets.newHashSet(clusterService.getNodes()).stream()
571 .forEach(controller -> {
572 ControllerInfo ctrlInfo = new ControllerInfo(controller.ip(), OFPORT, "tcp");
573 controllers.add(ctrlInfo);
574 });
575
576 String dpid = node.intBrId().toString().substring(DPID_BEGIN);
577
578 try {
579 DriverHandler handler = driverService.createHandler(node.ovsdbId());
580 BridgeConfig bridgeConfig = handler.behaviour(BridgeConfig.class);
581 bridgeConfig.addBridge(BridgeName.bridgeName(DEFAULT_BRIDGE), dpid, controllers);
582 } catch (ItemNotFoundException e) {
Hyunsun Moon177506f2016-01-21 00:54:52 -0800583 log.warn("Failed to create integration bridge on {}", node.hostname());
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800584 }
585 }
586
587 /**
588 * Creates tunnel interface to the integration bridge for a given node.
589 *
590 * @param node cordvtn node
591 */
592 private void createTunnelInterface(CordVtnNode node) {
Hyunsun Moon133fd792016-02-09 01:55:48 -0800593 if (isTunnelIntfCreated(node)) {
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800594 return;
595 }
596
597 DefaultAnnotations.Builder optionBuilder = DefaultAnnotations.builder();
598 for (String key : DEFAULT_TUNNEL_OPTIONS.keySet()) {
599 optionBuilder.set(key, DEFAULT_TUNNEL_OPTIONS.get(key));
600 }
601
602 TunnelDescription description = new DefaultTunnelDescription(
603 null, null, VXLAN, TunnelName.tunnelName(DEFAULT_TUNNEL),
604 optionBuilder.build());
605
606 try {
607 DriverHandler handler = driverService.createHandler(node.ovsdbId());
608 TunnelConfig tunnelConfig = handler.behaviour(TunnelConfig.class);
609 tunnelConfig.createTunnelInterface(BridgeName.bridgeName(DEFAULT_BRIDGE), description);
610 } catch (ItemNotFoundException e) {
Hyunsun Moon177506f2016-01-21 00:54:52 -0800611 log.warn("Failed to create tunnel interface on {}", node.hostname());
612 }
613 }
614
615 /**
Hyunsun Moon133fd792016-02-09 01:55:48 -0800616 * Adds data plane interface to a given node.
Hyunsun Moon177506f2016-01-21 00:54:52 -0800617 *
618 * @param node cordvtn node
619 */
Hyunsun Moon133fd792016-02-09 01:55:48 -0800620 private void addDataPlaneInterface(CordVtnNode node) {
621 if (isDataPlaneIntfAdded(node)) {
Hyunsun Moon177506f2016-01-21 00:54:52 -0800622 return;
623 }
624
625 try {
626 DriverHandler handler = driverService.createHandler(node.ovsdbId());
627 BridgeConfig bridgeConfig = handler.behaviour(BridgeConfig.class);
Hyunsun Moon133fd792016-02-09 01:55:48 -0800628 bridgeConfig.addPort(BridgeName.bridgeName(DEFAULT_BRIDGE), node.dpIntf());
Hyunsun Moon177506f2016-01-21 00:54:52 -0800629 } catch (ItemNotFoundException e) {
Hyunsun Moon133fd792016-02-09 01:55:48 -0800630 log.warn("Failed to add {} on {}", node.dpIntf(), node.hostname());
631 }
632 }
633
634 /**
635 * Flushes IP address from data plane interface and adds data plane IP address
636 * to integration bridge.
637 *
638 * @param node cordvtn node
639 */
640 private void setIpAddress(CordVtnNode node) {
641 Session session = RemoteIpCommandUtil.connect(node.sshInfo());
642 if (session == null) {
643 log.debug("Failed to SSH to {}", node.hostname());
644 return;
645 }
646
Hyunsun Moone9d4f4a2016-03-04 19:24:08 -0800647 RemoteIpCommandUtil.getCurrentIps(session, DEFAULT_BRIDGE).stream()
648 .filter(ip -> !ip.equals(node.localMgmtIp().ip()))
649 .filter(ip -> !ip.equals(node.dpIp().ip()))
650 .forEach(ip -> RemoteIpCommandUtil.deleteIp(session, ip, DEFAULT_BRIDGE));
651
Hyunsun Moon133fd792016-02-09 01:55:48 -0800652 boolean result = RemoteIpCommandUtil.flushIp(session, node.dpIntf()) &&
653 RemoteIpCommandUtil.setInterfaceUp(session, node.dpIntf()) &&
654 RemoteIpCommandUtil.addIp(session, node.dpIp(), DEFAULT_BRIDGE) &&
655 RemoteIpCommandUtil.addIp(session, node.localMgmtIp(), DEFAULT_BRIDGE) &&
656 RemoteIpCommandUtil.setInterfaceUp(session, DEFAULT_BRIDGE);
657
658 RemoteIpCommandUtil.disconnect(session);
659
660 if (result) {
661 setNodeState(node, NodeState.COMPLETE);
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800662 }
663 }
664
665 /**
666 * Checks if integration bridge exists and available.
667 *
668 * @param node cordvtn node
669 * @return true if the bridge is available, false otherwise
670 */
Hyunsun Moon133fd792016-02-09 01:55:48 -0800671 private boolean isBrIntCreated(CordVtnNode node) {
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800672 return (deviceService.getDevice(node.intBrId()) != null
673 && deviceService.isAvailable(node.intBrId()));
674 }
675
676 /**
677 * Checks if tunnel interface exists.
678 *
679 * @param node cordvtn node
680 * @return true if the interface exists, false otherwise
681 */
Hyunsun Moon133fd792016-02-09 01:55:48 -0800682 private boolean isTunnelIntfCreated(CordVtnNode node) {
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800683 return deviceService.getPorts(node.intBrId())
684 .stream()
685 .filter(p -> getPortName(p).contains(DEFAULT_TUNNEL) &&
686 p.isEnabled())
687 .findAny().isPresent();
688 }
689
690 /**
Hyunsun Moon133fd792016-02-09 01:55:48 -0800691 * Checks if data plane interface exists.
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800692 *
693 * @param node cordvtn node
694 * @return true if the interface exists, false otherwise
695 */
Hyunsun Moon133fd792016-02-09 01:55:48 -0800696 private boolean isDataPlaneIntfAdded(CordVtnNode node) {
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800697 return deviceService.getPorts(node.intBrId())
698 .stream()
Hyunsun Moon133fd792016-02-09 01:55:48 -0800699 .filter(p -> getPortName(p).contains(node.dpIntf()) &&
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800700 p.isEnabled())
701 .findAny().isPresent();
702 }
703
704 /**
Hyunsun Moon133fd792016-02-09 01:55:48 -0800705 * Checks if the IP addresses are correctly set.
706 *
707 * @param node cordvtn node
708 * @return true if the IP is set, false otherwise
709 */
710 private boolean isIpAddressSet(CordVtnNode node) {
711 Session session = RemoteIpCommandUtil.connect(node.sshInfo());
712 if (session == null) {
713 log.debug("Failed to SSH to {}", node.hostname());
714 return false;
715 }
716
717 Set<IpAddress> intBrIps = RemoteIpCommandUtil.getCurrentIps(session, DEFAULT_BRIDGE);
718 boolean result = RemoteIpCommandUtil.getCurrentIps(session, node.dpIntf()).isEmpty() &&
719 RemoteIpCommandUtil.isInterfaceUp(session, node.dpIntf()) &&
720 intBrIps.contains(node.dpIp().ip()) &&
721 intBrIps.contains(node.localMgmtIp().ip()) &&
722 RemoteIpCommandUtil.isInterfaceUp(session, DEFAULT_BRIDGE);
723
724 RemoteIpCommandUtil.disconnect(session);
725 return result;
726 }
727
728 /**
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800729 * Returns connect point of a given port.
730 *
731 * @param port port
732 * @return connect point
733 */
734 private ConnectPoint getConnectPoint(Port port) {
735 return new ConnectPoint(port.element().id(), port.number());
736 }
737
738 /**
739 * Returns connect point of a given host.
740 *
741 * @param host host
742 * @return connect point
743 */
744 private ConnectPoint getConnectPoint(Host host) {
745 return new ConnectPoint(host.location().deviceId(), host.location().port());
746 }
747
748 private class OvsdbHandler implements ConnectionHandler<Device> {
749
750 @Override
751 public void connected(Device device) {
752 CordVtnNode node = getNodeByOvsdbId(device.id());
753 if (node != null) {
Hyunsun Moon133fd792016-02-09 01:55:48 -0800754 setNodeState(node, getNodeState(node));
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800755 } else {
756 log.debug("{} is detected on unregistered node, ignore it.", device.id());
757 }
758 }
759
760 @Override
761 public void disconnected(Device device) {
762 if (!deviceService.isAvailable(device.id())) {
Hyunsun Moonaf520d32016-03-07 16:37:17 -0800763 log.debug("Device {} is disconnected", device.id());
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800764 adminService.removeDevice(device.id());
765 }
766 }
767 }
768
769 private class BridgeHandler implements ConnectionHandler<Device> {
770
771 @Override
772 public void connected(Device device) {
773 CordVtnNode node = getNodeByBridgeId(device.id());
774 if (node != null) {
Hyunsun Moon133fd792016-02-09 01:55:48 -0800775 setNodeState(node, getNodeState(node));
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800776 } else {
777 log.debug("{} is detected on unregistered node, ignore it.", device.id());
778 }
779 }
780
781 @Override
782 public void disconnected(Device device) {
783 CordVtnNode node = getNodeByBridgeId(device.id());
784 if (node != null) {
785 log.debug("Integration Bridge is disconnected from {}", node.hostname());
786 setNodeState(node, NodeState.INCOMPLETE);
787 }
788 }
789
790 /**
791 * Handles port added situation.
Hyunsun Moon133fd792016-02-09 01:55:48 -0800792 * If the added port is tunnel or data plane interface, proceed to the remaining
793 * node initialization. Otherwise, do nothing.
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800794 *
795 * @param port port
796 */
797 public void portAdded(Port port) {
798 CordVtnNode node = getNodeByBridgeId((DeviceId) port.element().id());
799 String portName = getPortName(port);
800
801 if (node == null) {
802 log.debug("{} is added to unregistered node, ignore it.", portName);
803 return;
804 }
805
Hyunsun Moon80b03872016-03-10 12:40:16 -0800806 log.info("Port {} is added to {}", portName, node.hostname());
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800807
808 if (portName.startsWith(VPORT_PREFIX)) {
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -0800809 if (isNodeStateComplete(node)) {
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800810 cordVtnService.addServiceVm(node, getConnectPoint(port));
811 } else {
812 log.debug("VM is detected on incomplete node, ignore it.", portName);
813 }
Hyunsun Moon133fd792016-02-09 01:55:48 -0800814 } else if (portName.contains(DEFAULT_TUNNEL) || portName.equals(node.dpIntf())) {
815 setNodeState(node, getNodeState(node));
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800816 }
817 }
818
819 /**
820 * Handles port removed situation.
Hyunsun Moon133fd792016-02-09 01:55:48 -0800821 * If the removed port is tunnel or data plane interface, proceed to the remaining
822 * node initialization.Others, do nothing.
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800823 *
824 * @param port port
825 */
826 public void portRemoved(Port port) {
827 CordVtnNode node = getNodeByBridgeId((DeviceId) port.element().id());
828 String portName = getPortName(port);
829
830 if (node == null) {
831 return;
832 }
833
Hyunsun Moon80b03872016-03-10 12:40:16 -0800834 log.info("Port {} is removed from {}", portName, node.hostname());
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800835
836 if (portName.startsWith(VPORT_PREFIX)) {
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -0800837 if (isNodeStateComplete(node)) {
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800838 cordVtnService.removeServiceVm(getConnectPoint(port));
839 } else {
840 log.debug("VM is vanished from incomplete node, ignore it.", portName);
841 }
Hyunsun Moon133fd792016-02-09 01:55:48 -0800842 } else if (portName.contains(DEFAULT_TUNNEL) || portName.equals(node.dpIntf())) {
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800843 setNodeState(node, NodeState.INCOMPLETE);
844 }
845 }
846 }
847
848 private class InternalDeviceListener implements DeviceListener {
849
850 @Override
851 public void event(DeviceEvent event) {
852
Hyunsun Moon98025542016-03-08 04:36:02 -0800853 NodeId leaderNodeId = leadershipService.getLeader(appId.name());
854 if (!Objects.equals(localNodeId, leaderNodeId)) {
Hyunsun Moon80b03872016-03-10 12:40:16 -0800855 // do not allow to proceed without leadership
Hyunsun Moon98025542016-03-08 04:36:02 -0800856 return;
857 }
858
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800859 Device device = event.subject();
860 ConnectionHandler<Device> handler =
861 (device.type().equals(SWITCH) ? bridgeHandler : ovsdbHandler);
862
863 switch (event.type()) {
864 case PORT_ADDED:
Hyunsun Moon80b03872016-03-10 12:40:16 -0800865 eventExecutor.execute(() -> bridgeHandler.portAdded(event.port()));
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800866 break;
867 case PORT_UPDATED:
868 if (!event.port().isEnabled()) {
Hyunsun Moon80b03872016-03-10 12:40:16 -0800869 eventExecutor.execute(() -> bridgeHandler.portRemoved(event.port()));
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800870 }
871 break;
872 case DEVICE_ADDED:
873 case DEVICE_AVAILABILITY_CHANGED:
874 if (deviceService.isAvailable(device.id())) {
Hyunsun Moon80b03872016-03-10 12:40:16 -0800875 eventExecutor.execute(() -> handler.connected(device));
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800876 } else {
Hyunsun Moon80b03872016-03-10 12:40:16 -0800877 eventExecutor.execute(() -> handler.disconnected(device));
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800878 }
879 break;
880 default:
881 break;
882 }
883 }
884 }
885
886 /**
Hyunsun Moon746956f2016-01-24 21:47:06 -0800887 * Reads cordvtn nodes from config file.
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800888 */
889 private void readConfiguration() {
890 CordVtnConfig config = configRegistry.getConfig(appId, CordVtnConfig.class);
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800891 if (config == null) {
Hyunsun Moon746956f2016-01-24 21:47:06 -0800892 log.debug("No configuration found");
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800893 return;
894 }
895
Hyunsun Moon80b03872016-03-10 12:40:16 -0800896 config.cordVtnNodes().forEach(this::addOrUpdateNode);
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800897 }
898
899 private class InternalConfigListener implements NetworkConfigListener {
900
901 @Override
902 public void event(NetworkConfigEvent event) {
Hyunsun Moon80b03872016-03-10 12:40:16 -0800903 NodeId leaderNodeId = leadershipService.getLeader(appId.name());
904 if (!Objects.equals(localNodeId, leaderNodeId)) {
905 // do not allow to proceed without leadership
906 return;
907 }
908
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800909 if (!event.configClass().equals(CordVtnConfig.class)) {
910 return;
911 }
912
913 switch (event.type()) {
914 case CONFIG_ADDED:
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800915 case CONFIG_UPDATED:
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800916 eventExecutor.execute(CordVtnNodeManager.this::readConfiguration);
917 break;
918 default:
919 break;
920 }
921 }
922 }
Hyunsun Moon80b03872016-03-10 12:40:16 -0800923
924 private class InternalMapListener implements MapEventListener<String, CordVtnNode> {
925
926 @Override
927 public void event(MapEvent<String, CordVtnNode> event) {
928 NodeId leaderNodeId = leadershipService.getLeader(appId.name());
929 if (!Objects.equals(localNodeId, leaderNodeId)) {
930 // do not allow to proceed without leadership
931 return;
932 }
933
934 CordVtnNode oldNode;
935 CordVtnNode newNode;
936
937 switch (event.type()) {
938 case UPDATE:
939 oldNode = event.oldValue().value();
940 newNode = event.newValue().value();
941
942 if (!newNode.equals(oldNode)) {
943 log.info("{} has been updated", newNode.hostname());
944 log.debug("New node: {}", newNode);
945 }
946 // perform init procedure based on current state on any updates,
947 // insert, or even if the node is the same for robustness since
948 // it's no harm to run the init procedure multiple times
949 eventExecutor.execute(() -> initNode(newNode));
950 break;
951 case INSERT:
952 newNode = event.newValue().value();
953 log.info("Added {}", newNode.hostname());
954 eventExecutor.execute(() -> initNode(newNode));
955 break;
956 case REMOVE:
957 oldNode = event.oldValue().value();
958 log.info("{} is removed", oldNode.hostname());
959 break;
960 default:
961 break;
962 }
963 }
964 }
Hyunsun Moonb77b60f2016-01-15 20:03:18 -0800965}