CORD-280 Added CLIs for cordvtn

Change-Id: I76e0358ccf3f71ad7cb1f9ba048616f0177d7666
diff --git a/src/main/java/org/onosproject/cordvtn/OvsdbNode.java b/src/main/java/org/onosproject/cordvtn/OvsdbNode.java
index c5b7a07..7a9a06a 100644
--- a/src/main/java/org/onosproject/cordvtn/OvsdbNode.java
+++ b/src/main/java/org/onosproject/cordvtn/OvsdbNode.java
@@ -19,11 +19,20 @@
 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.
      *
diff --git a/src/main/java/org/onosproject/cordvtn/cli/OvsdbNodeAddCommand.java b/src/main/java/org/onosproject/cordvtn/cli/OvsdbNodeAddCommand.java
new file mode 100644
index 0000000..88d1634
--- /dev/null
+++ b/src/main/java/org/onosproject/cordvtn/cli/OvsdbNodeAddCommand.java
@@ -0,0 +1,65 @@
+/*
+ * 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);
+    }
+}
diff --git a/src/main/java/org/onosproject/cordvtn/cli/OvsdbNodeConnectCommand.java b/src/main/java/org/onosproject/cordvtn/cli/OvsdbNodeConnectCommand.java
new file mode 100644
index 0000000..e4ca0f3
--- /dev/null
+++ b/src/main/java/org/onosproject/cordvtn/cli/OvsdbNodeConnectCommand.java
@@ -0,0 +1,60 @@
+/*
+ * 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);
+            }
+        }
+    }
+}
diff --git a/src/main/java/org/onosproject/cordvtn/cli/OvsdbNodeDeleteCommand.java b/src/main/java/org/onosproject/cordvtn/cli/OvsdbNodeDeleteCommand.java
new file mode 100644
index 0000000..a500d0d
--- /dev/null
+++ b/src/main/java/org/onosproject/cordvtn/cli/OvsdbNodeDeleteCommand.java
@@ -0,0 +1,57 @@
+/*
+ * 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);
+        }
+    }
+}
diff --git a/src/main/java/org/onosproject/cordvtn/cli/OvsdbNodeDisconnectCommand.java b/src/main/java/org/onosproject/cordvtn/cli/OvsdbNodeDisconnectCommand.java
new file mode 100644
index 0000000..14e44e0
--- /dev/null
+++ b/src/main/java/org/onosproject/cordvtn/cli/OvsdbNodeDisconnectCommand.java
@@ -0,0 +1,60 @@
+/*
+ * 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);
+            }
+        }
+    }
+}
diff --git a/src/main/java/org/onosproject/cordvtn/cli/OvsdbNodeListCommand.java b/src/main/java/org/onosproject/cordvtn/cli/OvsdbNodeListCommand.java
new file mode 100644
index 0000000..7d125ca
--- /dev/null
+++ b/src/main/java/org/onosproject/cordvtn/cli/OvsdbNodeListCommand.java
@@ -0,0 +1,74 @@
+/*
+ * 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";
+    }
+}
diff --git a/src/main/java/org/onosproject/cordvtn/cli/package-info.java b/src/main/java/org/onosproject/cordvtn/cli/package-info.java
new file mode 100644
index 0000000..686172c
--- /dev/null
+++ b/src/main/java/org/onosproject/cordvtn/cli/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * 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
diff --git a/src/main/java/org/onosproject/cordvtn/package-info.java b/src/main/java/org/onosproject/cordvtn/package-info.java
new file mode 100644
index 0000000..1c13737
--- /dev/null
+++ b/src/main/java/org/onosproject/cordvtn/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * 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
diff --git a/src/main/resources/OSGI-INF/blueprint/shell-config.xml b/src/main/resources/OSGI-INF/blueprint/shell-config.xml
new file mode 100644
index 0000000..6e17238
--- /dev/null
+++ b/src/main/resources/OSGI-INF/blueprint/shell-config.xml
@@ -0,0 +1,35 @@
+<!--
+  ~ 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>