From e9bb60be43af477f17b30ee1f2ba205565b7fa15 Mon Sep 17 00:00:00 2001 From: Ashlee Young Date: Mon, 19 Oct 2015 10:14:31 -0700 Subject: Updated onos src tree to commit id 1e60f97ae50c05b94fcb6a10520738bfb5efdfd1 --- framework/src/onos/apps/cordvtn/pom.xml | 5 + .../main/java/org/onosproject/cordvtn/CordVtn.java | 194 ++++++++++++++++----- .../org/onosproject/cordvtn/CordVtnConfig.java | 13 +- .../onosproject/cordvtn/CordVtnConfigManager.java | 46 +---- .../org/onosproject/cordvtn/CordVtnService.java | 32 ++-- .../org/onosproject/cordvtn/DefaultOvsdbNode.java | 20 +-- .../onosproject/cordvtn/NodeConnectionManager.java | 165 ------------------ .../java/org/onosproject/cordvtn/OvsdbNode.java | 13 -- 8 files changed, 199 insertions(+), 289 deletions(-) delete mode 100644 framework/src/onos/apps/cordvtn/src/main/java/org/onosproject/cordvtn/NodeConnectionManager.java (limited to 'framework/src/onos/apps/cordvtn') diff --git a/framework/src/onos/apps/cordvtn/pom.xml b/framework/src/onos/apps/cordvtn/pom.xml index a019bec2..b8e913d4 100644 --- a/framework/src/onos/apps/cordvtn/pom.xml +++ b/framework/src/onos/apps/cordvtn/pom.xml @@ -49,6 +49,11 @@ onos-core-serializers ${project.version} + + org.onosproject + onos-ovsdb-api + ${project.version} + diff --git a/framework/src/onos/apps/cordvtn/src/main/java/org/onosproject/cordvtn/CordVtn.java b/framework/src/onos/apps/cordvtn/src/main/java/org/onosproject/cordvtn/CordVtn.java index cb8acab2..ba707800 100644 --- a/framework/src/onos/apps/cordvtn/src/main/java/org/onosproject/cordvtn/CordVtn.java +++ b/framework/src/onos/apps/cordvtn/src/main/java/org/onosproject/cordvtn/CordVtn.java @@ -15,6 +15,8 @@ */ package org.onosproject.cordvtn; +import com.google.common.collect.Collections2; +import com.google.common.collect.Sets; import org.apache.felix.scr.annotations.Activate; import org.apache.felix.scr.annotations.Component; import org.apache.felix.scr.annotations.Deactivate; @@ -22,31 +24,39 @@ import org.apache.felix.scr.annotations.Reference; import org.apache.felix.scr.annotations.ReferenceCardinality; import org.apache.felix.scr.annotations.Service; import org.onlab.util.KryoNamespace; +import org.onosproject.cluster.ClusterService; +import org.onosproject.core.ApplicationId; import org.onosproject.core.CoreService; +import org.onosproject.mastership.MastershipService; import org.onosproject.net.Device; import org.onosproject.net.DeviceId; import org.onosproject.net.Host; +import org.onosproject.net.behaviour.ControllerInfo; import org.onosproject.net.device.DeviceEvent; import org.onosproject.net.device.DeviceListener; import org.onosproject.net.device.DeviceService; import org.onosproject.net.host.HostEvent; import org.onosproject.net.host.HostListener; import org.onosproject.net.host.HostService; +import org.onosproject.ovsdb.controller.OvsdbClientService; +import org.onosproject.ovsdb.controller.OvsdbController; +import org.onosproject.ovsdb.controller.OvsdbNodeId; import org.onosproject.store.serializers.KryoNamespaces; -import org.onosproject.store.service.EventuallyConsistentMap; -import org.onosproject.store.service.LogicalClockService; +import org.onosproject.store.service.ConsistentMap; +import org.onosproject.store.service.Serializer; import org.onosproject.store.service.StorageService; +import org.onosproject.store.service.Versioned; import org.slf4j.Logger; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -import java.util.stream.Collectors; +import static com.google.common.base.Preconditions.checkNotNull; import static org.onlab.util.Tools.groupedThreads; -import static org.onosproject.cordvtn.OvsdbNode.State; -import static org.onosproject.cordvtn.OvsdbNode.State.INIT; -import static org.onosproject.cordvtn.OvsdbNode.State.DISCONNECT; import static org.onosproject.net.Device.Type.SWITCH; import static org.slf4j.LoggerFactory.getLogger; @@ -63,7 +73,17 @@ public class CordVtn implements CordVtnService { private static final int NUM_THREADS = 1; private static final KryoNamespace.Builder NODE_SERIALIZER = KryoNamespace.newBuilder() .register(KryoNamespaces.API) - .register(OvsdbNode.class); + .register(DefaultOvsdbNode.class); + private static final String DEFAULT_BRIDGE_NAME = "br-int"; + private static final Map VXLAN_OPTIONS = new HashMap() { + { + put("key", "flow"); + put("local_ip", "flow"); + put("remote_ip", "flow"); + } + }; + private static final int DPID_BEGIN = 3; + private static final int OFPORT = 6653; @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) protected CoreService coreService; @@ -71,15 +91,21 @@ public class CordVtn implements CordVtnService { @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) protected StorageService storageService; - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) - protected LogicalClockService clockService; - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) protected DeviceService deviceService; @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) protected HostService hostService; + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) + protected MastershipService mastershipService; + + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) + protected OvsdbController controller; + + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) + protected ClusterService clusterService; + private final ExecutorService eventExecutor = Executors .newFixedThreadPool(NUM_THREADS, groupedThreads("onos/cordvtn", "event-handler")); @@ -90,15 +116,16 @@ public class CordVtn implements CordVtnService { private final BridgeHandler bridgeHandler = new BridgeHandler(); private final VmHandler vmHandler = new VmHandler(); - private EventuallyConsistentMap nodeStore; + private ConsistentMap nodeStore; + private ApplicationId appId; @Activate protected void activate() { - coreService.registerApplication("org.onosproject.cordvtn"); - nodeStore = storageService.eventuallyConsistentMapBuilder() + appId = coreService.registerApplication("org.onosproject.cordvtn"); + nodeStore = storageService.consistentMapBuilder() + .withSerializer(Serializer.using(NODE_SERIALIZER.build())) .withName("cordvtn-nodestore") - .withSerializer(NODE_SERIALIZER) - .withTimestampProvider((k, v) -> clockService.getTimestamp()) + .withApplicationId(appId) .build(); deviceService.addListener(deviceListener); @@ -113,43 +140,59 @@ public class CordVtn implements CordVtnService { hostService.removeListener(hostListener); eventExecutor.shutdown(); - nodeStore.destroy(); + nodeStore.clear(); log.info("Stopped"); } @Override - public void addNode(OvsdbNode ovsdbNode) { - if (nodeStore.containsKey(ovsdbNode.deviceId())) { - log.warn("Node {} already exists", ovsdbNode.host()); + public void addNode(OvsdbNode ovsdb) { + checkNotNull(ovsdb); + nodeStore.put(ovsdb.deviceId(), ovsdb); + } + + @Override + public void deleteNode(OvsdbNode ovsdb) { + checkNotNull(ovsdb); + + if (!nodeStore.containsKey(ovsdb.deviceId())) { return; } - nodeStore.put(ovsdbNode.deviceId(), ovsdbNode); - if (ovsdbNode.state() != INIT) { - updateNode(ovsdbNode, INIT); + + // check ovsdb and integration bridge connection state first + if (isNodeConnected(ovsdb)) { + log.warn("Cannot delete connected node {}", ovsdb.host()); + } else { + nodeStore.remove(ovsdb.deviceId()); } } @Override - public void deleteNode(OvsdbNode ovsdbNode) { - if (!nodeStore.containsKey(ovsdbNode.deviceId())) { - log.warn("Node {} does not exist", ovsdbNode.host()); + public void connect(OvsdbNode ovsdb) { + checkNotNull(ovsdb); + + if (!nodeStore.containsKey(ovsdb.deviceId())) { + log.warn("Node {} does not exist", ovsdb.host()); return; } - updateNode(ovsdbNode, DISCONNECT); + controller.connect(ovsdb.ip(), ovsdb.port()); } @Override - public void updateNode(OvsdbNode ovsdbNode, State state) { - if (!nodeStore.containsKey(ovsdbNode.deviceId())) { - log.warn("Node {} does not exist", ovsdbNode.host()); + public void disconnect(OvsdbNode ovsdb) { + checkNotNull(ovsdb); + + if (!nodeStore.containsKey(ovsdb.deviceId())) { + log.warn("Node {} does not exist", ovsdb.host()); return; } - DefaultOvsdbNode updatedNode = new DefaultOvsdbNode(ovsdbNode.host(), - ovsdbNode.ip(), - ovsdbNode.port(), - state); - nodeStore.put(ovsdbNode.deviceId(), updatedNode); + + OvsdbClientService ovsdbClient = getOvsdbClient(ovsdb); + checkNotNull(ovsdbClient); + + if (ovsdbClient.isConnected()) { + ovsdbClient.disconnect(); + } } @Override @@ -159,14 +202,42 @@ public class CordVtn implements CordVtnService { @Override public OvsdbNode getNode(DeviceId deviceId) { - return nodeStore.get(deviceId); + Versioned ovsdb = nodeStore.get(deviceId); + if (ovsdb != null) { + return ovsdb.value(); + } else { + return null; + } } @Override public List getNodes() { - return nodeStore.values() - .stream() - .collect(Collectors.toList()); + List ovsdbs = new ArrayList<>(); + ovsdbs.addAll(Collections2.transform(nodeStore.values(), Versioned::value)); + return ovsdbs; + } + + @Override + public boolean isNodeConnected(OvsdbNode ovsdb) { + checkNotNull(ovsdb); + + OvsdbClientService ovsdbClient = getOvsdbClient(ovsdb); + if (ovsdbClient == null) { + return false; + } else { + return ovsdbClient.isConnected(); + } + } + + private OvsdbClientService getOvsdbClient(OvsdbNode ovsdb) { + checkNotNull(ovsdb); + + OvsdbClientService ovsdbClient = controller.getOvsdbClient( + new OvsdbNodeId(ovsdb.ip(), ovsdb.port().toInt())); + if (ovsdbClient == null) { + log.warn("Couldn't find ovsdb client of node {}", ovsdb.host()); + } + return ovsdbClient; } private class InternalDeviceListener implements DeviceListener { @@ -182,6 +253,7 @@ public class CordVtn implements CordVtnService { break; case DEVICE_AVAILABILITY_CHANGED: eventExecutor.submit(() -> handler.disconnected(device)); + // TODO handle the case that the device is recovered break; default: break; @@ -212,14 +284,27 @@ public class CordVtn implements CordVtnService { @Override public void connected(Device device) { - // create bridge and set bridgeId - // set node state connected + log.info("Ovsdb {} is connected", device.id()); + + if (!mastershipService.isLocalMaster(device.id())) { + return; + } + + // TODO change to use bridge config + OvsdbNode ovsdb = getNode(device.id()); + OvsdbClientService ovsdbClient = getOvsdbClient(ovsdb); + + List controllers = new ArrayList<>(); + Sets.newHashSet(clusterService.getNodes()).forEach(controller -> + controllers.add(new ControllerInfo(controller.ip(), OFPORT, "tcp"))); + String dpid = ovsdb.intBrId().toString().substring(DPID_BEGIN); + + ovsdbClient.createBridge(DEFAULT_BRIDGE_NAME, dpid, controllers); } @Override public void disconnected(Device device) { - // set node state disconnected if the node exists - // which means that the node is not deleted explicitly + log.warn("Ovsdb {} is disconnected", device.id()); } } @@ -227,12 +312,29 @@ public class CordVtn implements CordVtnService { @Override public void connected(Device device) { - // create vxlan port + log.info("Integration Bridge {} is detected", device.id()); + + OvsdbNode ovsdb = getNodes().stream() + .filter(node -> node.intBrId().equals(device.id())) + .findFirst().get(); + + if (ovsdb == null) { + log.warn("Couldn't find OVSDB associated with {}", device.id()); + return; + } + + if (!mastershipService.isLocalMaster(ovsdb.deviceId())) { + return; + } + + // TODO change to use tunnel config and tunnel description + OvsdbClientService ovsdbClient = getOvsdbClient(ovsdb); + ovsdbClient.createTunnel(DEFAULT_BRIDGE_NAME, "vxlan", "vxlan", VXLAN_OPTIONS); } @Override public void disconnected(Device device) { - + log.info("Integration Bridge {} is vanished", device.id()); } } @@ -240,12 +342,12 @@ public class CordVtn implements CordVtnService { @Override public void connected(Host host) { - // install flow rules for this vm + log.info("VM {} is detected", host.id()); } @Override public void disconnected(Host host) { - // uninstall flow rules associated with this vm + log.info("VM {} is vanished", host.id()); } } } diff --git a/framework/src/onos/apps/cordvtn/src/main/java/org/onosproject/cordvtn/CordVtnConfig.java b/framework/src/onos/apps/cordvtn/src/main/java/org/onosproject/cordvtn/CordVtnConfig.java index fdaf752a..550452ce 100644 --- a/framework/src/onos/apps/cordvtn/src/main/java/org/onosproject/cordvtn/CordVtnConfig.java +++ b/framework/src/onos/apps/cordvtn/src/main/java/org/onosproject/cordvtn/CordVtnConfig.java @@ -20,6 +20,7 @@ import com.google.common.collect.Sets; import org.onlab.packet.IpAddress; import org.onlab.packet.TpPort; import org.onosproject.core.ApplicationId; +import org.onosproject.net.DeviceId; import org.onosproject.net.config.Config; import java.util.Set; @@ -35,6 +36,7 @@ public class CordVtnConfig extends Config { public static final String HOST = "host"; public static final String IP = "ip"; public static final String PORT = "port"; + public static final String BRIDGE_ID = "bridgeId"; /** * Returns the set of ovsdb nodes read from network config. @@ -51,7 +53,8 @@ public class CordVtnConfig extends Config { nodes.forEach(jsonNode -> ovsdbNodes.add(new OvsdbNodeConfig( jsonNode.path(HOST).asText(), IpAddress.valueOf(jsonNode.path(IP).asText()), - TpPort.tpPort(jsonNode.path(PORT).asInt())))); + TpPort.tpPort(jsonNode.path(PORT).asInt()), + DeviceId.deviceId(jsonNode.path(BRIDGE_ID).asText())))); return ovsdbNodes; } @@ -64,11 +67,13 @@ public class CordVtnConfig extends Config { private final String host; private final IpAddress ip; private final TpPort port; + private final DeviceId bridgeId; - public OvsdbNodeConfig(String host, IpAddress ip, TpPort port) { + public OvsdbNodeConfig(String host, IpAddress ip, TpPort port, DeviceId bridgeId) { this.host = checkNotNull(host); this.ip = checkNotNull(ip); this.port = checkNotNull(port); + this.bridgeId = checkNotNull(bridgeId); } /** @@ -97,5 +102,9 @@ public class CordVtnConfig extends Config { public TpPort port() { return this.port; } + + public DeviceId bridgeId() { + return this.bridgeId; + } } } diff --git a/framework/src/onos/apps/cordvtn/src/main/java/org/onosproject/cordvtn/CordVtnConfigManager.java b/framework/src/onos/apps/cordvtn/src/main/java/org/onosproject/cordvtn/CordVtnConfigManager.java index 043b3760..f276c7ca 100644 --- a/framework/src/onos/apps/cordvtn/src/main/java/org/onosproject/cordvtn/CordVtnConfigManager.java +++ b/framework/src/onos/apps/cordvtn/src/main/java/org/onosproject/cordvtn/CordVtnConfigManager.java @@ -20,11 +20,6 @@ import org.apache.felix.scr.annotations.Component; import org.apache.felix.scr.annotations.Deactivate; import org.apache.felix.scr.annotations.Reference; import org.apache.felix.scr.annotations.ReferenceCardinality; -import org.onosproject.cluster.ClusterService; -import org.onosproject.cluster.LeadershipEvent; -import org.onosproject.cluster.LeadershipEventListener; -import org.onosproject.cluster.LeadershipService; -import org.onosproject.cluster.NodeId; import org.onosproject.core.ApplicationId; import org.onosproject.core.CoreService; import org.onosproject.net.config.ConfigFactory; @@ -35,7 +30,6 @@ import org.onosproject.net.config.NetworkConfigService; import org.onosproject.net.config.basics.SubjectFactories; import org.slf4j.Logger; -import static org.onosproject.cordvtn.OvsdbNode.State.INIT; import static org.slf4j.LoggerFactory.getLogger; /** @@ -57,12 +51,6 @@ public class CordVtnConfigManager { @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) protected NetworkConfigService configService; - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) - protected LeadershipService leadershipService; - - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) - protected ClusterService clusterService; - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) protected CordVtnService cordVtnService; @@ -74,29 +62,22 @@ public class CordVtnConfigManager { } }; - private final LeadershipEventListener leadershipListener = new InternalLeadershipListener(); private final NetworkConfigListener configListener = new InternalConfigListener(); - private NodeId local; private ApplicationId appId; @Activate protected void active() { - local = clusterService.getLocalNode().id(); appId = coreService.getAppId(CordVtnService.CORDVTN_APP_ID); configService.addListener(configListener); configRegistry.registerConfigFactory(configFactory); - leadershipService.addListener(leadershipListener); - leadershipService.runForLeadership(CordVtnService.CORDVTN_APP_ID); + readConfiguration(); } @Deactivate protected void deactivate() { - leadershipService.removeListener(leadershipListener); - leadershipService.withdraw(appId.name()); - configRegistry.unregisterConfigFactory(configFactory); configService.removeListener(configListener); } @@ -110,30 +91,13 @@ public class CordVtnConfigManager { } config.ovsdbNodes().forEach(node -> { - DefaultOvsdbNode ovsdbNode = - new DefaultOvsdbNode(node.host(), node.ip(), node.port(), INIT); - cordVtnService.addNode(ovsdbNode); - log.info("Add new node {}", node.host()); + DefaultOvsdbNode ovsdb = new DefaultOvsdbNode( + node.host(), node.ip(), node.port(), node.bridgeId()); + cordVtnService.addNode(ovsdb); + cordVtnService.connect(ovsdb); }); } - private synchronized void processLeadershipChange(NodeId leader) { - if (leader == null || !leader.equals(local)) { - return; - } - readConfiguration(); - } - - private class InternalLeadershipListener implements LeadershipEventListener { - - @Override - public void event(LeadershipEvent event) { - if (event.subject().topic().equals(appId.name())) { - processLeadershipChange(event.subject().leader()); - } - } - } - private class InternalConfigListener implements NetworkConfigListener { @Override diff --git a/framework/src/onos/apps/cordvtn/src/main/java/org/onosproject/cordvtn/CordVtnService.java b/framework/src/onos/apps/cordvtn/src/main/java/org/onosproject/cordvtn/CordVtnService.java index 1f75dceb..7e01a452 100644 --- a/framework/src/onos/apps/cordvtn/src/main/java/org/onosproject/cordvtn/CordVtnService.java +++ b/framework/src/onos/apps/cordvtn/src/main/java/org/onosproject/cordvtn/CordVtnService.java @@ -15,7 +15,6 @@ */ package org.onosproject.cordvtn; -import org.onosproject.cordvtn.OvsdbNode.State; import org.onosproject.net.DeviceId; import java.util.List; @@ -29,25 +28,30 @@ public interface CordVtnService { /** * Adds a new node to the service. * - * @param ovsdbNode ovsdb node + * @param ovsdb ovsdb node */ - void addNode(OvsdbNode ovsdbNode); + void addNode(OvsdbNode ovsdb); /** * Deletes a node from the service. * - * @param ovsdbNode ovsdb node + * @param ovsdb ovsdb node */ - void deleteNode(OvsdbNode ovsdbNode); + void deleteNode(OvsdbNode ovsdb); /** - * Updates ovsdb node. - * It only used for updating node's connection state. + * Connect to a node. * - * @param ovsdbNode ovsdb node - * @param state ovsdb connection state + * @param ovsdb ovsdb node */ - void updateNode(OvsdbNode ovsdbNode, State state); + void connect(OvsdbNode ovsdb); + + /** + * Disconnect a node. + * + * @param ovsdb ovsdb node + */ + void disconnect(OvsdbNode ovsdb); /** * Returns the number of the nodes known to the service. @@ -64,6 +68,14 @@ public interface CordVtnService { */ OvsdbNode getNode(DeviceId deviceId); + /** + * Returns connection state of the node. + * + * @param ovsdb ovsdb node + * @return true if the node is connected, false otherwise + */ + boolean isNodeConnected(OvsdbNode ovsdb); + /** * Returns all nodes known to the service. * diff --git a/framework/src/onos/apps/cordvtn/src/main/java/org/onosproject/cordvtn/DefaultOvsdbNode.java b/framework/src/onos/apps/cordvtn/src/main/java/org/onosproject/cordvtn/DefaultOvsdbNode.java index ce8b0f1d..eba52108 100644 --- a/framework/src/onos/apps/cordvtn/src/main/java/org/onosproject/cordvtn/DefaultOvsdbNode.java +++ b/framework/src/onos/apps/cordvtn/src/main/java/org/onosproject/cordvtn/DefaultOvsdbNode.java @@ -30,13 +30,13 @@ public class DefaultOvsdbNode implements OvsdbNode { private final String host; private final IpAddress ip; private final TpPort port; - private final State state; + private final DeviceId brId; - public DefaultOvsdbNode(String host, IpAddress ip, TpPort port, State state) { + public DefaultOvsdbNode(String host, IpAddress ip, TpPort port, DeviceId brId) { this.host = host; this.ip = ip; this.port = port; - this.state = state; + this.brId = brId; } @Override @@ -55,8 +55,8 @@ public class DefaultOvsdbNode implements OvsdbNode { } @Override - public State state() { - return this.state; + public DeviceId intBrId() { + return this.brId; } @Override @@ -64,11 +64,6 @@ public class DefaultOvsdbNode implements OvsdbNode { return DeviceId.deviceId("ovsdb:" + this.ip.toString() + ":" + this.port.toString()); } - @Override - public DeviceId intBrId() { - return DeviceId.deviceId("of:" + this.host); - } - @Override public boolean equals(Object o) { if (this == o) { @@ -79,7 +74,8 @@ public class DefaultOvsdbNode implements OvsdbNode { DefaultOvsdbNode that = (DefaultOvsdbNode) o; if (this.host.equals(that.host) && this.ip.equals(that.ip) && - this.port.equals(that.port)) { + this.port.equals(that.port) && + this.brId.equals(that.brId)) { return true; } } @@ -97,7 +93,7 @@ public class DefaultOvsdbNode implements OvsdbNode { .add("host", host) .add("ip", ip) .add("port", port) - .add("state", state) + .add("bridgeId", brId) .toString(); } } diff --git a/framework/src/onos/apps/cordvtn/src/main/java/org/onosproject/cordvtn/NodeConnectionManager.java b/framework/src/onos/apps/cordvtn/src/main/java/org/onosproject/cordvtn/NodeConnectionManager.java deleted file mode 100644 index ebba4cd5..00000000 --- a/framework/src/onos/apps/cordvtn/src/main/java/org/onosproject/cordvtn/NodeConnectionManager.java +++ /dev/null @@ -1,165 +0,0 @@ -/* - * Copyright 2014-2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.onosproject.cordvtn; - -import org.apache.felix.scr.annotations.Activate; -import org.apache.felix.scr.annotations.Component; -import org.apache.felix.scr.annotations.Deactivate; -import org.apache.felix.scr.annotations.Reference; -import org.apache.felix.scr.annotations.ReferenceCardinality; -import org.onosproject.cluster.ClusterService; -import org.onosproject.cluster.LeadershipService; -import org.onosproject.cluster.NodeId; -import org.onosproject.mastership.MastershipService; -import org.onosproject.net.Device; -import org.onosproject.net.device.DeviceEvent; -import org.onosproject.net.device.DeviceListener; -import org.onosproject.net.device.DeviceService; -import org.slf4j.Logger; - -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; - -import static org.onlab.util.Tools.groupedThreads; -import static org.onosproject.cordvtn.OvsdbNode.State.CONNECTED; -import static org.onosproject.cordvtn.OvsdbNode.State.DISCONNECTED; -import static org.onosproject.cordvtn.OvsdbNode.State.READY; -import static org.slf4j.LoggerFactory.getLogger; - -/** - * Provides the connection state management of all nodes registered to the service - * so that the nodes keep connected unless it is requested to be deleted. - */ -@Component(immediate = true) -public class NodeConnectionManager { - protected final Logger log = getLogger(getClass()); - - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) - MastershipService mastershipService; - - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) - LeadershipService leadershipService; - - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) - ClusterService clusterService; - - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) - DeviceService deviceService; - - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) - CordVtnService cordVtnService; - - private static final int DELAY_SEC = 5; - - private final DeviceListener deviceListener = new InternalDeviceListener(); - private final ScheduledExecutorService connectionExecutor = Executors - .newSingleThreadScheduledExecutor(groupedThreads("onos/cordvtn", "connection-manager")); - - private NodeId localId; - - @Activate - protected void activate() { - localId = clusterService.getLocalNode().id(); - deviceService.addListener(deviceListener); - - connectionExecutor.scheduleWithFixedDelay(() -> cordVtnService.getNodes() - .stream() - .filter(node -> localId.equals(getMaster(node))) - .forEach(node -> { - connect(node); - disconnect(node); - }), 0, DELAY_SEC, TimeUnit.SECONDS); - } - - @Deactivate - public void stop() { - connectionExecutor.shutdown(); - deviceService.removeListener(deviceListener); - } - - public void connect(OvsdbNode ovsdbNode) { - switch (ovsdbNode.state()) { - case INIT: - case DISCONNECTED: - setPassiveMode(ovsdbNode); - case READY: - setupConnection(ovsdbNode); - break; - default: - break; - } - } - - public void disconnect(OvsdbNode ovsdbNode) { - switch (ovsdbNode.state()) { - case DISCONNECT: - // TODO: disconnect - break; - default: - break; - } - } - - private class InternalDeviceListener implements DeviceListener { - - @Override - public void event(DeviceEvent event) { - Device device = event.subject(); - if (device.type() != Device.Type.CONTROLLER) { - return; - } - - DefaultOvsdbNode node; - switch (event.type()) { - case DEVICE_ADDED: - node = (DefaultOvsdbNode) cordVtnService.getNode(device.id()); - if (node != null) { - cordVtnService.updateNode(node, CONNECTED); - } - break; - case DEVICE_AVAILABILITY_CHANGED: - node = (DefaultOvsdbNode) cordVtnService.getNode(device.id()); - if (node != null) { - cordVtnService.updateNode(node, DISCONNECTED); - } - break; - default: - break; - } - } - } - - private NodeId getMaster(OvsdbNode ovsdbNode) { - NodeId master = mastershipService.getMasterFor(ovsdbNode.intBrId()); - - // master is null if there's no such device - if (master == null) { - master = leadershipService.getLeader(CordVtnService.CORDVTN_APP_ID); - } - return master; - } - - private void setPassiveMode(OvsdbNode ovsdbNode) { - // TODO: need ovsdb client implementation first - // TODO: set the remove ovsdb server passive mode - cordVtnService.updateNode(ovsdbNode, READY); - } - - private void setupConnection(OvsdbNode ovsdbNode) { - // TODO initiate connection - } -} diff --git a/framework/src/onos/apps/cordvtn/src/main/java/org/onosproject/cordvtn/OvsdbNode.java b/framework/src/onos/apps/cordvtn/src/main/java/org/onosproject/cordvtn/OvsdbNode.java index 296bd439..c5b7a078 100644 --- a/framework/src/onos/apps/cordvtn/src/main/java/org/onosproject/cordvtn/OvsdbNode.java +++ b/framework/src/onos/apps/cordvtn/src/main/java/org/onosproject/cordvtn/OvsdbNode.java @@ -23,12 +23,6 @@ import org.onosproject.net.DeviceId; * Representation of a node with ovsdb server. */ public interface OvsdbNode { - /** - * Ovsdb connection state. - */ - enum State { - INIT, READY, CONNECTED, DISCONNECT, DISCONNECTED - } /** * Returns the IP address of the ovsdb server. @@ -52,13 +46,6 @@ public interface OvsdbNode { */ String host(); - /** - * Returns the connection state of the ovsdb server. - * - * @return connection state - */ - State state(); - /** * Returns the device id of the ovsdb server. * -- cgit 1.2.3-korg