Hyunsun Moon
Committed by Gerrit Code Review

CORD-380 Added host provider for cordvtn service

- Renamed CordVtnConfigManager to CordVtnNodeManager
- Moved all node bootstrap logic to CordVtnNodeManager
- CordVtnService now provides VM add/remove and service dependency create/remove
- Made CordVtn implement HostProvider so that it can inject/eject VM to the system

Change-Id: I0011ac692ecea240d2d7fe48b3e7a1db4973b76e
/*
* 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.core.ApplicationId;
import org.onosproject.core.CoreService;
import org.onosproject.net.config.ConfigFactory;
import org.onosproject.net.config.NetworkConfigEvent;
import org.onosproject.net.config.NetworkConfigListener;
import org.onosproject.net.config.NetworkConfigRegistry;
import org.onosproject.net.config.NetworkConfigService;
import org.onosproject.net.config.basics.SubjectFactories;
import org.slf4j.Logger;
import java.util.concurrent.ExecutorService;
import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
import static org.onlab.util.Tools.groupedThreads;
import static org.slf4j.LoggerFactory.getLogger;
/**
* Reads node information from the network config file and handles the config
* update events.
* Only a leader controller performs the node addition or deletion.
*/
@Component(immediate = true)
public class CordVtnConfigManager {
protected final Logger log = getLogger(getClass());
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected CoreService coreService;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected NetworkConfigRegistry configRegistry;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected NetworkConfigService configService;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected CordVtnService cordVtnService;
private final ConfigFactory configFactory =
new ConfigFactory(SubjectFactories.APP_SUBJECT_FACTORY, CordVtnConfig.class, "cordvtn") {
@Override
public CordVtnConfig createConfig() {
return new CordVtnConfig();
}
};
private final NetworkConfigListener configListener = new InternalConfigListener();
private ApplicationId appId;
protected ExecutorService eventExecutor;
@Activate
protected void active() {
appId = coreService.getAppId(CordVtnService.CORDVTN_APP_ID);
eventExecutor = newSingleThreadScheduledExecutor(groupedThreads("onos/cordvtncfg", "event-handler"));
configService.addListener(configListener);
configRegistry.registerConfigFactory(configFactory);
}
@Deactivate
protected void deactivate() {
configRegistry.unregisterConfigFactory(configFactory);
configService.removeListener(configListener);
eventExecutor.shutdown();
}
private void readConfiguration() {
CordVtnConfig config = configRegistry.getConfig(appId, CordVtnConfig.class);
if (config == null) {
log.warn("No configuration found");
return;
}
config.cordVtnNodes().forEach(node -> {
CordVtnNode cordVtnNode = new CordVtnNode(
node.hostname(),
node.ovsdbIp(),
node.ovsdbPort(),
node.bridgeId(),
node.phyPortName(),
node.localIp());
cordVtnService.addNode(cordVtnNode);
});
}
private class InternalConfigListener implements NetworkConfigListener {
@Override
public void event(NetworkConfigEvent event) {
if (!event.configClass().equals(CordVtnConfig.class)) {
return;
}
switch (event.type()) {
case CONFIG_ADDED:
log.info("Network configuration added");
eventExecutor.execute(CordVtnConfigManager.this::readConfiguration);
break;
case CONFIG_UPDATED:
log.info("Network configuration updated");
eventExecutor.execute(CordVtnConfigManager.this::readConfiguration);
break;
default:
break;
}
}
}
}
......@@ -165,13 +165,10 @@ public class CordVtnRuleInstaller {
* Populates basic rules that connect a VM to the other VMs in the system.
*
* @param host host
* @param hostIp host ip
* @param tunnelIp tunnel ip
* @param vNet openstack network
*/
public void populateBasicConnectionRules(Host host, IpAddress hostIp, IpAddress tunnelIp,
OpenstackNetwork vNet) {
// TODO we can get host ip from host.ip() after applying NetworkConfig host provider
public void populateBasicConnectionRules(Host host, IpAddress tunnelIp, OpenstackNetwork vNet) {
checkNotNull(host);
checkNotNull(vNet);
......@@ -182,6 +179,7 @@ public class CordVtnRuleInstaller {
PortNumber inPort = host.location().port();
MacAddress dstMac = host.mac();
IpAddress hostIp = host.ipAddresses().stream().findFirst().get();
long tunnelId = Long.parseLong(vNet.segmentId());
OpenstackSubnet subnet = vNet.subnets().stream()
......@@ -352,7 +350,7 @@ public class CordVtnRuleInstaller {
Group group = groupService.getGroup(deviceId, groupKey);
if (group == null) {
log.debug("No group exists for service {} in {}", service.id(), deviceId);
log.trace("No group exists for service {} in {}, do nothing.", service.id(), deviceId);
continue;
}
......
......@@ -15,7 +15,7 @@
*/
package org.onosproject.cordvtn;
import java.util.List;
import org.onosproject.net.ConnectPoint;
/**
* Service for provisioning overlay virtual networks on compute nodes.
......@@ -23,61 +23,21 @@ import java.util.List;
public interface CordVtnService {
String CORDVTN_APP_ID = "org.onosproject.cordvtn";
/**
* Adds a new node to the service.
*
* @param node cordvtn node
*/
void addNode(CordVtnNode node);
/**
* Deletes a node from the service.
*
* @param node cordvtn node
*/
void deleteNode(CordVtnNode node);
/**
* Initiates node to serve virtual tenant network.
*
* @param node cordvtn node
*/
void initNode(CordVtnNode node);
/**
* Returns the number of the nodes known to the service.
*
* @return number of nodes
*/
int getNodeCount();
/**
* Returns node initialization state.
*
* @param node cordvtn node
* @return true if initial node setup is completed, otherwise false
*/
boolean getNodeInitState(CordVtnNode node);
/**
* Returns detailed node initialization state.
* Return string includes the following information.
*
* Integration bridge created/connected: OK or NO
* VXLAN interface created: OK or NO
* Physical interface added: OK or NO
* Adds a new VM on a given node and connect point.
*
* @param node cordvtn node
* @return string including detailed node init state
* @param connectPoint connect point
*/
String checkNodeInitState(CordVtnNode node);
void addServiceVm(CordVtnNode node, ConnectPoint connectPoint);
/**
* Returns all nodes known to the service.
* Removes a VM from a given node and connect point.
*
* @return list of nodes
* @param connectPoint connect point
*/
List<CordVtnNode> getNodes();
void removeServiceVm(ConnectPoint connectPoint);
/**
* Creates dependencies for a given tenant service.
......
......@@ -21,7 +21,7 @@ import org.apache.karaf.shell.commands.Command;
import org.onlab.packet.IpAddress;
import org.onlab.packet.TpPort;
import org.onosproject.cli.AbstractShellCommand;
import org.onosproject.cordvtn.CordVtnService;
import org.onosproject.cordvtn.CordVtnNodeManager;
import org.onosproject.cordvtn.CordVtnNode;
import org.onosproject.net.DeviceId;
......@@ -63,7 +63,7 @@ public class CordVtnNodeAddCommand extends AbstractShellCommand {
checkArgument(ovsdb.contains(":"), "OVSDB address should be ip:port format");
checkArgument(bridgeId.startsWith("of:"), "bridgeId should be of:dpid format");
CordVtnService service = AbstractShellCommand.get(CordVtnService.class);
CordVtnNodeManager nodeManager = AbstractShellCommand.get(CordVtnNodeManager.class);
String[] ipPort = ovsdb.split(":");
CordVtnNode node = new CordVtnNode(hostname,
IpAddress.valueOf(ipPort[0]),
......@@ -71,6 +71,6 @@ public class CordVtnNodeAddCommand extends AbstractShellCommand {
DeviceId.deviceId(bridgeId),
phyPortName,
IpAddress.valueOf(localIp));
service.addNode(node);
nodeManager.addNode(node);
}
}
......
......@@ -20,7 +20,7 @@ import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command;
import org.onosproject.cli.AbstractShellCommand;
import org.onosproject.cordvtn.CordVtnNode;
import org.onosproject.cordvtn.CordVtnService;
import org.onosproject.cordvtn.CordVtnNodeManager;
/**
* Checks detailed node init state.
......@@ -35,8 +35,8 @@ public class CordVtnNodeCheckCommand extends AbstractShellCommand {
@Override
protected void execute() {
CordVtnService service = AbstractShellCommand.get(CordVtnService.class);
CordVtnNode node = service.getNodes()
CordVtnNodeManager nodeManager = AbstractShellCommand.get(CordVtnNodeManager.class);
CordVtnNode node = nodeManager.getNodes()
.stream()
.filter(n -> n.hostname().equals(hostname))
.findFirst()
......@@ -47,6 +47,6 @@ public class CordVtnNodeCheckCommand extends AbstractShellCommand {
return;
}
print(service.checkNodeInitState(node));
print(nodeManager.checkNodeInitState(node));
}
}
......
......@@ -19,7 +19,7 @@ package org.onosproject.cordvtn.cli;
import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command;
import org.onosproject.cli.AbstractShellCommand;
import org.onosproject.cordvtn.CordVtnService;
import org.onosproject.cordvtn.CordVtnNodeManager;
import org.onosproject.cordvtn.CordVtnNode;
import java.util.NoSuchElementException;
......@@ -37,12 +37,12 @@ public class CordVtnNodeDeleteCommand extends AbstractShellCommand {
@Override
protected void execute() {
CordVtnService service = AbstractShellCommand.get(CordVtnService.class);
CordVtnNodeManager nodeManager = AbstractShellCommand.get(CordVtnNodeManager.class);
for (String hostname : hostnames) {
CordVtnNode node;
try {
node = service.getNodes()
node = nodeManager.getNodes()
.stream()
.filter(n -> n.hostname().equals(hostname))
.findFirst().get();
......@@ -51,7 +51,7 @@ public class CordVtnNodeDeleteCommand extends AbstractShellCommand {
continue;
}
service.deleteNode(node);
nodeManager.deleteNode(node);
}
}
}
......
......@@ -19,7 +19,7 @@ package org.onosproject.cordvtn.cli;
import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command;
import org.onosproject.cli.AbstractShellCommand;
import org.onosproject.cordvtn.CordVtnService;
import org.onosproject.cordvtn.CordVtnNodeManager;
import org.onosproject.cordvtn.CordVtnNode;
import java.util.NoSuchElementException;
......@@ -37,12 +37,12 @@ public class CordVtnNodeInitCommand extends AbstractShellCommand {
@Override
protected void execute() {
CordVtnService service = AbstractShellCommand.get(CordVtnService.class);
CordVtnNodeManager nodeManager = AbstractShellCommand.get(CordVtnNodeManager.class);
for (String hostname : hostnames) {
CordVtnNode node;
try {
node = service.getNodes()
node = nodeManager.getNodes()
.stream()
.filter(n -> n.hostname().equals(hostname))
.findFirst().get();
......@@ -51,7 +51,7 @@ public class CordVtnNodeInitCommand extends AbstractShellCommand {
continue;
}
service.initNode(node);
nodeManager.initNode(node);
}
}
}
......
......@@ -21,7 +21,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import org.apache.karaf.shell.commands.Command;
import org.onosproject.cli.AbstractShellCommand;
import org.onosproject.cordvtn.CordVtnService;
import org.onosproject.cordvtn.CordVtnNodeManager;
import org.onosproject.cordvtn.CordVtnNode;
import java.util.Collections;
......@@ -36,12 +36,12 @@ public class CordVtnNodeListCommand extends AbstractShellCommand {
@Override
protected void execute() {
CordVtnService service = AbstractShellCommand.get(CordVtnService.class);
List<CordVtnNode> nodes = service.getNodes();
CordVtnNodeManager nodeManager = AbstractShellCommand.get(CordVtnNodeManager.class);
List<CordVtnNode> nodes = nodeManager.getNodes();
Collections.sort(nodes, CordVtnNode.CORDVTN_NODE_COMPARATOR);
if (outputJson()) {
print("%s", json(service, nodes));
print("%s", json(nodeManager, nodes));
} else {
for (CordVtnNode node : nodes) {
print("hostname=%s, ovsdb=%s, br-int=%s, phyPort=%s, localIp=%s, init=%s",
......@@ -50,13 +50,13 @@ public class CordVtnNodeListCommand extends AbstractShellCommand {
node.intBrId().toString(),
node.phyPortName(),
node.localIp().toString(),
getState(service, node));
getState(nodeManager, node));
}
print("Total %s nodes", service.getNodeCount());
print("Total %s nodes", nodeManager.getNodeCount());
}
}
private JsonNode json(CordVtnService service, List<CordVtnNode> nodes) {
private JsonNode json(CordVtnNodeManager nodeManager, List<CordVtnNode> nodes) {
ObjectMapper mapper = new ObjectMapper();
ArrayNode result = mapper.createArrayNode();
for (CordVtnNode node : nodes) {
......@@ -67,12 +67,12 @@ public class CordVtnNodeListCommand extends AbstractShellCommand {
.put("brInt", node.intBrId().toString())
.put("phyPort", node.phyPortName())
.put("localIp", node.localIp().toString())
.put("init", getState(service, node)));
.put("init", getState(nodeManager, node)));
}
return result;
}
private String getState(CordVtnService service, CordVtnNode node) {
return service.getNodeInitState(node) ? "COMPLETE" : "INCOMPLETE";
private String getState(CordVtnNodeManager nodeManager, CordVtnNode node) {
return nodeManager.getNodeInitState(node) ? "COMPLETE" : "INCOMPLETE";
}
}
......