[ONOS-2826] add floatingIp service cli

Change-Id: I5681397f8b0fd28f361517df1349916abb4df0a2
diff --git a/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/cli/floatingip/FloatingIpCreateCommand.java b/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/cli/floatingip/FloatingIpCreateCommand.java
new file mode 100644
index 0000000..00758dd
--- /dev/null
+++ b/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/cli/floatingip/FloatingIpCreateCommand.java
@@ -0,0 +1,95 @@
+/*
+ * 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.vtnrsc.cli.floatingip;
+
+import java.util.Set;
+
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.apache.karaf.shell.commands.Option;
+import org.onlab.packet.IpAddress;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.vtnrsc.DefaultFloatingIp;
+import org.onosproject.vtnrsc.FloatingIpId;
+import org.onosproject.vtnrsc.FloatingIp;
+import org.onosproject.vtnrsc.FloatingIp.Status;
+import org.onosproject.vtnrsc.RouterId;
+import org.onosproject.vtnrsc.TenantId;
+import org.onosproject.vtnrsc.TenantNetworkId;
+import org.onosproject.vtnrsc.VirtualPortId;
+import org.onosproject.vtnrsc.floatingip.FloatingIpService;
+
+import com.google.common.collect.Sets;
+
+/**
+ * Supports for create a floating IP.
+ */
+@Command(scope = "onos", name = "floatingip-create",
+        description = "Supports for creating a floating IP")
+public class FloatingIpCreateCommand extends AbstractShellCommand {
+    @Argument(index = 0, name = "id", description = "The floating IP identifier",
+            required = true, multiValued = false)
+    String id = null;
+
+    @Argument(index = 1, name = "networkId", description = "The network identifier of floating IP",
+            required = true, multiValued = false)
+    String networkId = null;
+
+    @Argument(index = 2, name = "tenantId", description = "The tenant identifier of floating IP",
+            required = true, multiValued = false)
+    String tenantId = null;
+
+    @Argument(index = 3, name = "routerId", description = "The router identifier of floating IP",
+            required = true, multiValued = false)
+    String routerId = null;
+
+    @Argument(index = 4, name = "fixedIp", description = "The fixed IP of floating IP",
+            required = true, multiValued = false)
+    String fixedIp = null;
+
+    @Argument(index = 5, name = "floatingIp", description = "The floating IP of floating IP",
+            required = true, multiValued = false)
+    String floatingIp = null;
+
+    @Option(name = "-p", aliases = "--portId", description = "The port identifier of floating IP",
+            required = false, multiValued = false)
+    String portId = null;
+
+    @Option(name = "-s", aliases = "--status", description = "The status of floating IP",
+            required = false, multiValued = false)
+    String status = null;
+
+    @Override
+    protected void execute() {
+        FloatingIpService service = get(FloatingIpService.class);
+        try {
+            FloatingIp floatingIpObj = new DefaultFloatingIp(
+                                                          FloatingIpId.of(id),
+                                                          TenantId.tenantId(tenantId),
+                                                          TenantNetworkId.networkId(networkId),
+                                                          VirtualPortId.portId(portId),
+                                                          RouterId.valueOf(routerId),
+                                                          floatingIp == null ? null : IpAddress.valueOf(floatingIp),
+                                                          fixedIp == null ? null : IpAddress.valueOf(fixedIp),
+                                                          status == null ? Status.ACTIVE
+                                                                         : Status.valueOf(status));
+            Set<FloatingIp> floatingIpSet = Sets.newHashSet(floatingIpObj);
+            service.createFloatingIps(floatingIpSet);
+        } catch (Exception e) {
+            print(null, e.getMessage());
+        }
+    }
+}
diff --git a/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/cli/floatingip/FloatingIpQueryCommand.java b/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/cli/floatingip/FloatingIpQueryCommand.java
new file mode 100644
index 0000000..c441d53
--- /dev/null
+++ b/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/cli/floatingip/FloatingIpQueryCommand.java
@@ -0,0 +1,92 @@
+/*
+ * 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.vtnrsc.cli.floatingip;
+
+import org.apache.karaf.shell.commands.Command;
+import org.apache.karaf.shell.commands.Option;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.vtnrsc.FloatingIpId;
+import org.onosproject.vtnrsc.FloatingIp;
+import org.onosproject.vtnrsc.floatingip.FloatingIpService;
+
+/**
+ * Supports for query a floating IP.
+ */
+@Command(scope = "onos", name = "floatingips", description = "Supports for querying a floating IP")
+public class FloatingIpQueryCommand extends AbstractShellCommand {
+    @Option(name = "-I", aliases = "--id", description = "The floating IP identifier",
+            required = false, multiValued = false)
+    String id = null;
+
+    @Option(name = "-i", aliases = "--fixedIp", description = "The fixed IP of floating IP",
+            required = false, multiValued = false)
+    String fixedIp = null;
+
+    @Option(name = "-l", aliases = "--floatingIp", description = "The floating IP of floating IP",
+            required = false, multiValued = false)
+    String floatingIp = null;
+
+    private static final String FMT = "floatingIpId=%s, networkId=%s, tenantId=%s, portId=%s,"
+            + "routerId=%s, fixedIp=%s, floatingIp=%s, status=%s";
+
+    @Override
+    protected void execute() {
+        FloatingIpService service = get(FloatingIpService.class);
+        if (id != null) {
+            FloatingIp floatingIp = service.getFloatingIp(FloatingIpId
+                    .of(id));
+            printFloatingIp(floatingIp);
+        } else if (fixedIp != null || floatingIp != null) {
+            Iterable<FloatingIp> floatingIps = service.getFloatingIps();
+            if (floatingIps == null) {
+                return;
+            }
+            if (fixedIp != null) {
+                for (FloatingIp floatingIp : floatingIps) {
+                    if (floatingIp.fixedIp().toString().equals(fixedIp)) {
+                        printFloatingIp(floatingIp);
+                        return;
+                    }
+                }
+                print(null, "The fixedIp is not existed");
+            }
+            if (floatingIp != null) {
+                for (FloatingIp floatingIpObj : floatingIps) {
+                    if (floatingIpObj.fixedIp().toString().equals(floatingIp)) {
+                        printFloatingIp(floatingIpObj);
+                        return;
+                    }
+                }
+                print(null, "The floatingIp is not existed");
+            }
+        } else {
+            Iterable<FloatingIp> floatingIps = service.getFloatingIps();
+            if (floatingIps == null) {
+                return;
+            }
+            for (FloatingIp floatingIp : floatingIps) {
+                printFloatingIp(floatingIp);
+            }
+        }
+    }
+
+    private void printFloatingIp(FloatingIp floatingIp) {
+        print(FMT, floatingIp.id(), floatingIp.networkId(),
+              floatingIp.tenantId(), floatingIp.portId(),
+              floatingIp.routerId(), floatingIp.fixedIp(),
+              floatingIp.floatingIp(), floatingIp.status());
+    }
+}
diff --git a/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/cli/floatingip/FloatingIpRemoveCommand.java b/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/cli/floatingip/FloatingIpRemoveCommand.java
new file mode 100644
index 0000000..a413503
--- /dev/null
+++ b/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/cli/floatingip/FloatingIpRemoveCommand.java
@@ -0,0 +1,90 @@
+/*
+ * 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.vtnrsc.cli.floatingip;
+
+import java.util.Set;
+
+import org.apache.karaf.shell.commands.Command;
+import org.apache.karaf.shell.commands.Option;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.vtnrsc.FloatingIp;
+import org.onosproject.vtnrsc.FloatingIpId;
+import org.onosproject.vtnrsc.floatingip.FloatingIpService;
+
+import com.google.common.collect.Sets;
+
+/**
+ * Supports for remove a floating IP.
+ */
+@Command(scope = "onos", name = "floatingip-remove", description = "Supports for removing a floating IP")
+public class FloatingIpRemoveCommand extends AbstractShellCommand {
+    @Option(name = "-I", aliases = "--id", description = "The floating IP identifier",
+            required = false, multiValued = false)
+    String id = null;
+
+    @Option(name = "-i", aliases = "--fixedIp", description = "The fixed IP of floating IP",
+            required = false, multiValued = false)
+    String fixedIp = null;
+
+    @Option(name = "-l", aliases = "--floatingIp", description = "The floating IP of floating IP",
+            required = false, multiValued = false)
+    String floatingIp = null;
+
+    @Override
+    protected void execute() {
+        FloatingIpService service = get(FloatingIpService.class);
+        if (id == null && fixedIp == null && floatingIp == null) {
+            print(null, "one of id, fixedIp, floatingIp should not be null");
+        }
+        try {
+            Set<FloatingIpId> floatingIpSet = Sets.newHashSet();
+            if (id != null) {
+                floatingIpSet.add(FloatingIpId.of(id));
+                service.removeFloatingIps(floatingIpSet);
+            } else {
+                Iterable<FloatingIp> floatingIps = service.getFloatingIps();
+                if (floatingIps == null) {
+                    return;
+                }
+                if (fixedIp != null) {
+                    for (FloatingIp floatingIp : floatingIps) {
+                        if (floatingIp.fixedIp().toString().equals(fixedIp)) {
+                            floatingIpSet.add(floatingIp.id());
+                            service.removeFloatingIps(floatingIpSet);
+                            return;
+                        }
+                    }
+                    print(null, "The fixedIp is not existed");
+                    return;
+                }
+                if (floatingIp != null) {
+                    for (FloatingIp floatingIpObj : floatingIps) {
+                        if (floatingIpObj.fixedIp().toString()
+                                .equals(floatingIp)) {
+                            floatingIpSet.add(floatingIpObj.id());
+                            service.removeFloatingIps(floatingIpSet);
+                            return;
+                        }
+                    }
+                    print(null, "The floatingIp is not existed");
+                    return;
+                }
+            }
+        } catch (Exception e) {
+            print(null, e.getMessage());
+        }
+    }
+}
diff --git a/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/cli/floatingip/FloatingIpUpdateCommand.java b/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/cli/floatingip/FloatingIpUpdateCommand.java
new file mode 100644
index 0000000..413b3bd
--- /dev/null
+++ b/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/cli/floatingip/FloatingIpUpdateCommand.java
@@ -0,0 +1,103 @@
+/*
+ * 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.vtnrsc.cli.floatingip;
+
+import java.util.Set;
+
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.apache.karaf.shell.commands.Option;
+import org.onlab.packet.IpAddress;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.vtnrsc.DefaultFloatingIp;
+import org.onosproject.vtnrsc.FloatingIpId;
+import org.onosproject.vtnrsc.FloatingIp;
+import org.onosproject.vtnrsc.FloatingIp.Status;
+import org.onosproject.vtnrsc.RouterId;
+import org.onosproject.vtnrsc.TenantId;
+import org.onosproject.vtnrsc.TenantNetworkId;
+import org.onosproject.vtnrsc.VirtualPortId;
+import org.onosproject.vtnrsc.floatingip.FloatingIpService;
+
+import com.google.common.collect.Sets;
+
+/**
+ * Supports for update a floating IP.
+ */
+@Command(scope = "onos", name = "floatingip-update",
+        description = "Supports for updating a floating IP")
+public class FloatingIpUpdateCommand extends AbstractShellCommand {
+    @Argument(index = 0, name = "id", description = "The floating IP identifier",
+            required = true, multiValued = false)
+    String id = null;
+
+    @Option(name = "-n", aliases = "--networkId", description = "The network identifier of floating IP",
+            required = false, multiValued = false)
+    String networkId = null;
+
+    @Option(name = "-t", aliases = "--tenantId", description = "The tenant identifier of floating IP",
+            required = false, multiValued = false)
+    String tenantId = null;
+
+    @Option(name = "-r", aliases = "--routerId", description = "The router identifier of floating IP",
+            required = false, multiValued = false)
+    String routerId = null;
+
+    @Option(name = "-p", aliases = "--portId", description = "The port identifier of floating IP",
+            required = false, multiValued = false)
+    String portId = null;
+
+    @Option(name = "-s", aliases = "--status", description = "The status of floating IP",
+            required = false, multiValued = false)
+    String status = null;
+
+    @Option(name = "-i", aliases = "--fixedIp", description = "The fixed IP of floating IP",
+            required = false, multiValued = false)
+    String fixedIp = null;
+
+    @Option(name = "-l", aliases = "--floatingIp", description = "The floating IP of floating IP",
+            required = false, multiValued = false)
+    String floatingIp = null;
+
+    @Override
+    protected void execute() {
+        FloatingIpService service = get(FloatingIpService.class);
+        FloatingIpId floatingIpId = FloatingIpId.of(id);
+        FloatingIp floatingIpStore = get(FloatingIpService.class).getFloatingIp(floatingIpId);
+        try {
+            FloatingIp floatingIpObj = new DefaultFloatingIp(
+                                                          floatingIpId,
+                                                          tenantId == null ? floatingIpStore.tenantId()
+                                                                           :  TenantId.tenantId(tenantId),
+                                                          networkId == null ? floatingIpStore.networkId()
+                                                                            : TenantNetworkId.networkId(networkId),
+                                                          portId == null ? floatingIpStore.portId()
+                                                                         : VirtualPortId.portId(portId),
+                                                          routerId == null ? floatingIpStore.routerId()
+                                                                           : RouterId.valueOf(routerId),
+                                                          floatingIp == null ? floatingIpStore.floatingIp()
+                                                                             : IpAddress.valueOf(floatingIp),
+                                                          fixedIp == null ? floatingIpStore.fixedIp()
+                                                                          : IpAddress.valueOf(fixedIp),
+                                                          status == null ? floatingIpStore.status()
+                                                                         : Status.valueOf(status));
+            Set<FloatingIp> floatingIpSet = Sets.newHashSet(floatingIpObj);
+            service.updateFloatingIps(floatingIpSet);
+        } catch (Exception e) {
+            print(null, e.getMessage());
+        }
+    }
+}
diff --git a/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/cli/floatingip/package-info.java b/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/cli/floatingip/package-info.java
new file mode 100644
index 0000000..ac56077
--- /dev/null
+++ b/apps/vtn/vtnrsc/src/main/java/org/onosproject/vtnrsc/cli/floatingip/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.
+ */
+
+/**
+ * Command line interface for floatingIP.
+ */
+package org.onosproject.vtnrsc.cli.floatingip;