Hyunsun Moon
Committed by Gerrit Code Review

CORD-280 Added CLIs for cordvtn

Change-Id: I76e0358ccf3f71ad7cb1f9ba048616f0177d7666
......@@ -54,6 +54,16 @@
<artifactId>onos-ovsdb-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.onosproject</groupId>
<artifactId>onos-cli</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.karaf.shell</groupId>
<artifactId>org.apache.karaf.shell.console</artifactId>
<version>3.0.3</version>
</dependency>
</dependencies>
</project>
......
......@@ -19,11 +19,20 @@ import org.onlab.packet.IpAddress;
import org.onlab.packet.TpPort;
import org.onosproject.net.DeviceId;
import java.util.Comparator;
/**
* Representation of a node with ovsdb server.
*/
public interface OvsdbNode {
Comparator<OvsdbNode> OVSDB_NODE_COMPARATOR = new Comparator<OvsdbNode>() {
@Override
public int compare(OvsdbNode ovsdb1, OvsdbNode ovsdb2) {
return ovsdb1.host().compareTo(ovsdb2.host());
}
};
/**
* Returns the IP address of the ovsdb server.
*
......
/*
* Copyright 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.cli;
import org.apache.karaf.shell.commands.Argument;
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.DefaultOvsdbNode;
import org.onosproject.cordvtn.OvsdbNode;
import org.onosproject.net.DeviceId;
import static com.google.common.base.Preconditions.checkArgument;
/**
* Adds a new OVSDB nodes.
*/
@Command(scope = "onos", name = "ovsdb-add",
description = "Adds a new OVSDB node to cordvtn")
public class OvsdbNodeAddCommand extends AbstractShellCommand {
@Argument(index = 0, name = "host", description = "Hostname or IP",
required = true, multiValued = false)
private String host = null;
@Argument(index = 1, name = "address",
description = "OVSDB server listening address (ip:port)",
required = true, multiValued = false)
private String address = null;
@Argument(index = 2, name = "bridgeId",
description = "Device ID of integration bridge",
required = true, multiValued = false)
private String bridgeId = null;
@Override
protected void execute() {
checkArgument(address.contains(":"), "address should be ip:port format");
checkArgument(bridgeId.startsWith("of:"), "bridgeId should be of:dpid format");
CordVtnService service = AbstractShellCommand.get(CordVtnService.class);
String[] ipPort = address.split(":");
OvsdbNode ovsdb = new DefaultOvsdbNode(host,
IpAddress.valueOf(ipPort[0]),
TpPort.tpPort(Integer.parseInt(ipPort[1])),
DeviceId.deviceId(bridgeId));
service.addNode(ovsdb);
}
}
/*
* Copyright 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.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.OvsdbNode;
import java.util.NoSuchElementException;
/**
* Connects to OVSDBs.
*/
@Command(scope = "onos", name = "ovsdb-connect",
description = "Connects to OVSDBs")
public class OvsdbNodeConnectCommand extends AbstractShellCommand {
@Argument(index = 0, name = "hosts", description = "Hostname(s) or IP(s)",
required = true, multiValued = true)
private String[] hosts = null;
@Override
protected void execute() {
CordVtnService service = AbstractShellCommand.get(CordVtnService.class);
for (String host : hosts) {
OvsdbNode ovsdb;
try {
ovsdb = service.getNodes().stream()
.filter(node -> node.host().equals(host))
.findFirst().get();
} catch (NoSuchElementException e) {
print("Unable to find %s", host);
continue;
}
if (service.isNodeConnected(ovsdb)) {
print("OVSDB %s is already in connected state, do nothing", host);
} else {
service.connect(ovsdb);
}
}
}
}
/*
* Copyright 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.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.OvsdbNode;
import java.util.NoSuchElementException;
/**
* Deletes OVSDB nodes from cordvtn.
*/
@Command(scope = "onos", name = "ovsdb-delete",
description = "Deletes OVSDB nodes from cordvtn")
public class OvsdbNodeDeleteCommand extends AbstractShellCommand {
@Argument(index = 0, name = "hosts", description = "Hostname(s) or IP(s)",
required = true, multiValued = true)
private String[] hosts = null;
@Override
protected void execute() {
CordVtnService service = AbstractShellCommand.get(CordVtnService.class);
for (String host : hosts) {
OvsdbNode ovsdb;
try {
ovsdb = service.getNodes().stream()
.filter(node -> node.host().equals(host))
.findFirst().get();
} catch (NoSuchElementException e) {
print("Unable to find %s", host);
continue;
}
service.deleteNode(ovsdb);
}
}
}
/*
* Copyright 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.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.OvsdbNode;
import java.util.NoSuchElementException;
/**
* Disconnects OVSDBs.
*/
@Command(scope = "onos", name = "ovsdb-disconnect",
description = "Disconnects OVSDBs")
public class OvsdbNodeDisconnectCommand extends AbstractShellCommand {
@Argument(index = 0, name = "hosts", description = "Hostname(s) or IP(s)",
required = true, multiValued = true)
private String[] hosts = null;
@Override
protected void execute() {
CordVtnService service = AbstractShellCommand.get(CordVtnService.class);
for (String host : hosts) {
OvsdbNode ovsdb;
try {
ovsdb = service.getNodes().stream()
.filter(node -> node.host().equals(host))
.findFirst().get();
} catch (NoSuchElementException e) {
print("Unable to find %s", host);
continue;
}
if (!service.isNodeConnected(ovsdb)) {
print("OVSDB %s is already in disconnected state, do nothing", host);
} else {
service.disconnect(ovsdb);
}
}
}
}
/*
* Copyright 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.cli;
import com.fasterxml.jackson.databind.JsonNode;
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.OvsdbNode;
import java.util.Collections;
import java.util.List;
/**
* Lists all OVSDB nodes.
*/
@Command(scope = "onos", name = "ovsdbs",
description = "Lists all OVSDB nodes registered in cordvtn application")
public class OvsdbNodeListCommand extends AbstractShellCommand {
@Override
protected void execute() {
CordVtnService service = AbstractShellCommand.get(CordVtnService.class);
List<OvsdbNode> ovsdbs = service.getNodes();
Collections.sort(ovsdbs, OvsdbNode.OVSDB_NODE_COMPARATOR);
if (outputJson()) {
print("%s", json(service, ovsdbs));
} else {
for (OvsdbNode ovsdb : ovsdbs) {
print("host=%s, address=%s, br-int=%s, state=%s",
ovsdb.host(),
ovsdb.ip().toString() + ":" + ovsdb.port().toString(),
ovsdb.intBrId().toString(),
getState(service, ovsdb));
}
print("Total %s nodes", service.getNodeCount());
}
}
private JsonNode json(CordVtnService service, List<OvsdbNode> ovsdbs) {
ObjectMapper mapper = new ObjectMapper();
ArrayNode result = mapper.createArrayNode();
for (OvsdbNode ovsdb : ovsdbs) {
String ipPort = ovsdb.ip().toString() + ":" + ovsdb.port().toString();
result.add(mapper.createObjectNode()
.put("host", ovsdb.host())
.put("address", ipPort)
.put("brInt", ovsdb.intBrId().toString())
.put("state", getState(service, ovsdb)));
}
return result;
}
private String getState(CordVtnService service, OvsdbNode ovsdb) {
return service.isNodeConnected(ovsdb) ? "CONNECTED" : "DISCONNECTED";
}
}
/*
* Copyright 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.
*/
/**
* Console commands to manage OVSDB nodes for cordvtn.
*/
package org.onosproject.cordvtn.cli;
\ No newline at end of file
/*
* Copyright 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.
*/
/**
* Application for provisioning virtual tenant networks.
*/
package org.onosproject.cordvtn;
\ No newline at end of file
<!--
~ Copyright 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.
-->
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
<command-bundle xmlns="http://karaf.apache.org/xmlns/shell/v1.1.0">
<command>
<action class="org.onosproject.cordvtn.cli.OvsdbNodeListCommand"/>
</command>
<command>
<action class="org.onosproject.cordvtn.cli.OvsdbNodeAddCommand"/>
</command>
<command>
<action class="org.onosproject.cordvtn.cli.OvsdbNodeDeleteCommand"/>
</command>
<command>
<action class="org.onosproject.cordvtn.cli.OvsdbNodeConnectCommand"/>
</command>
<command>
<action class="org.onosproject.cordvtn.cli.OvsdbNodeDisconnectCommand"/>
</command>
</command-bundle>
</blueprint>