[ONOS-2253] The CLIs of subnet resource.

Change-Id: I4e499ecfef33681bc78c6f865e3d0e85f7a85ee9
diff --git a/apps/vtnrsc/src/main/java/org/onosproject/vtnrsc/cli/subnet/SubnetCreateCommand.java b/apps/vtnrsc/src/main/java/org/onosproject/vtnrsc/cli/subnet/SubnetCreateCommand.java
new file mode 100644
index 0000000..a673d17
--- /dev/null
+++ b/apps/vtnrsc/src/main/java/org/onosproject/vtnrsc/cli/subnet/SubnetCreateCommand.java
@@ -0,0 +1,118 @@
+/*
+ * 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.subnet;
+
+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.onlab.packet.IpAddress.Version;
+import org.onlab.packet.IpPrefix;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.vtnrsc.AllocationPool;
+import org.onosproject.vtnrsc.DefaultSubnet;
+import org.onosproject.vtnrsc.HostRoute;
+import org.onosproject.vtnrsc.Subnet;
+import org.onosproject.vtnrsc.Subnet.Mode;
+import org.onosproject.vtnrsc.SubnetId;
+import org.onosproject.vtnrsc.TenantId;
+import org.onosproject.vtnrsc.TenantNetworkId;
+import org.onosproject.vtnrsc.subnet.SubnetService;
+
+import com.google.common.collect.Sets;
+
+/**
+ * Supports for creating a subnet.
+ */
+@Command(scope = "onos", name = "subnet-create", description = "Supports for creating a subnet")
+public class SubnetCreateCommand extends AbstractShellCommand {
+
+    @Argument(index = 0, name = "id", description = "Subnet Id", required = true,
+            multiValued = false)
+    String id = null;
+
+    @Argument(index = 1, name = "subnetName", description = "Subnet String name", required = true,
+            multiValued = false)
+    String subnetName = null;
+
+    @Argument(index = 2, name = "networkId", description = "Subnet Network Id", required = true,
+            multiValued = false)
+    String networkId = null;
+
+    @Argument(index = 3, name = "tenantId", description = "Subnet Tenant Id", required = true,
+            multiValued = false)
+    String tenantId = null;
+
+    @Option(name = "-i", aliases = "--ipVersion", description = "Subnet Version ipVersion",
+            required = false, multiValued = false)
+    Version ipVersion = null;
+
+    @Option(name = "-c", aliases = "--cidr", description = "Subnet IpPrefix cidr",
+            required = false, multiValued = false)
+    String cidr = "0.0.0.0/0";
+
+    @Option(name = "-g", aliases = "--gatewayIp", description = "Subnet IpAddress gatewayIp",
+            required = false, multiValued = false)
+    String gatewayIp = "0.0.0.0";
+
+    @Option(name = "-d", aliases = "--dhcpEnabled", description = "Subnet boolean dhcpEnabled",
+            required = false, multiValued = false)
+    boolean dhcpEnabled = false;
+
+    @Option(name = "-s", aliases = "--shared", description = "Subnet boolean shared",
+            required = false, multiValued = false)
+    boolean shared = false;
+
+    @Option(name = "-m", aliases = "--ipV6AddressMode",
+            description = "Subnet Mode ipV6AddressMode", required = false, multiValued = false)
+    String ipV6AddressMode = null;
+
+    @Option(name = "-r", aliases = "--ipV6RaMode", description = "Subnet Mode ipV6RaMode",
+            required = false, multiValued = false)
+    String ipV6RaMode = null;
+
+    @Option(name = "-h", aliases = "--hostRoutes", description = "Subnet jsonnode hostRoutes",
+            required = false, multiValued = false)
+    Iterable<HostRoute> hostRoutes = null;
+
+    @Option(name = "-a", aliases = "--allocationPools",
+            description = "Subnet jsonnode allocationPools", required = false, multiValued = false)
+    Iterable<AllocationPool> allocationPools = Sets.newHashSet();
+
+    @Override
+    protected void execute() {
+        SubnetService service = get(SubnetService.class);
+        if (id == null || networkId == null || tenantId == null) {
+            print(null, "id,networkId,tenantId can not be null");
+            return;
+        }
+        Subnet subnet = new DefaultSubnet(SubnetId.subnetId(id), subnetName,
+                                          TenantNetworkId.networkId(networkId),
+                                          TenantId.tenantId(tenantId), ipVersion,
+                                          cidr == null ? null : IpPrefix.valueOf(cidr),
+                                          gatewayIp == null ? null : IpAddress.valueOf(gatewayIp),
+                                          dhcpEnabled, shared, hostRoutes,
+                                          ipV6AddressMode == null ? null : Mode.valueOf(ipV6AddressMode),
+                                          ipV6RaMode == null ? null : Mode.valueOf(ipV6RaMode),
+                                          allocationPools);
+
+        Set<Subnet> subnetsSet = Sets.newHashSet(subnet);
+        service.createSubnets(subnetsSet);
+    }
+
+}
diff --git a/apps/vtnrsc/src/main/java/org/onosproject/vtnrsc/cli/subnet/SubnetQueryCommand.java b/apps/vtnrsc/src/main/java/org/onosproject/vtnrsc/cli/subnet/SubnetQueryCommand.java
new file mode 100644
index 0000000..f5a94f0
--- /dev/null
+++ b/apps/vtnrsc/src/main/java/org/onosproject/vtnrsc/cli/subnet/SubnetQueryCommand.java
@@ -0,0 +1,61 @@
+/*
+ * 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.subnet;
+
+import org.apache.karaf.shell.commands.Command;
+import org.apache.karaf.shell.commands.Option;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.vtnrsc.Subnet;
+import org.onosproject.vtnrsc.SubnetId;
+import org.onosproject.vtnrsc.subnet.SubnetService;
+
+/**
+ * Supports for querying a subnet.
+ */
+@Command(scope = "onos", name = "subnets", description = "Supports for querying a subnet")
+public class SubnetQueryCommand extends AbstractShellCommand {
+
+    @Option(name = "-i", aliases = "--id", description = "Subnet id", required = false,
+            multiValued = false)
+    String id = null;
+
+    private static final String FMT = "subnetId=%s, networkId=%s, subnetName=%s,"
+            + "tenantId=%s, cidr=%s, dhcpEnabled=%s, gatewayIp=%s," + "ipVersion=%s";
+
+    @Override
+    protected void execute() {
+        SubnetService service = get(SubnetService.class);
+        if (id != null) {
+            Subnet subnet = service.getSubnet(SubnetId.subnetId(id));
+            printSubnet(subnet);
+        } else {
+            Iterable<Subnet> subnets = service.getSubnets();
+            if (subnets == null) {
+                return;
+            }
+            for (Subnet subnet : subnets) {
+                printSubnet(subnet);
+            }
+        }
+    }
+
+    private void printSubnet(Subnet subnet) {
+        print(FMT, subnet.id(), subnet.networkId(), subnet.subnetName(),
+              subnet.tenantId(), subnet.cidr(), subnet.dhcpEnabled(), subnet
+                      .gatewayIp(), subnet.ipVersion());
+
+    }
+}
diff --git a/apps/vtnrsc/src/main/java/org/onosproject/vtnrsc/cli/subnet/SubnetRemoveCommand.java b/apps/vtnrsc/src/main/java/org/onosproject/vtnrsc/cli/subnet/SubnetRemoveCommand.java
new file mode 100644
index 0000000..241af87
--- /dev/null
+++ b/apps/vtnrsc/src/main/java/org/onosproject/vtnrsc/cli/subnet/SubnetRemoveCommand.java
@@ -0,0 +1,46 @@
+/*
+ * 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.subnet;
+
+import java.util.Set;
+
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.vtnrsc.SubnetId;
+import org.onosproject.vtnrsc.subnet.SubnetService;
+
+import com.google.common.collect.Sets;
+
+/**
+ * Supports for removing a subnet.
+ */
+@Command(scope = "onos", name = "subnet-remove", description = "Supports for removing a subnet")
+public class SubnetRemoveCommand extends AbstractShellCommand {
+
+    @Argument(index = 0, name = "id", description = "Subnet SubnetId Id", required = true,
+            multiValued = false)
+    String id = null;
+
+    @Override
+    protected void execute() {
+        SubnetService service = get(SubnetService.class);
+        Set<SubnetId> subnetsSet = Sets.newHashSet();
+        subnetsSet.add(SubnetId.subnetId(id));
+        service.removeSubnets(subnetsSet);
+    }
+
+}
diff --git a/apps/vtnrsc/src/main/java/org/onosproject/vtnrsc/cli/subnet/SubnetUpdateCommand.java b/apps/vtnrsc/src/main/java/org/onosproject/vtnrsc/cli/subnet/SubnetUpdateCommand.java
new file mode 100644
index 0000000..2e8fc20
--- /dev/null
+++ b/apps/vtnrsc/src/main/java/org/onosproject/vtnrsc/cli/subnet/SubnetUpdateCommand.java
@@ -0,0 +1,118 @@
+/*
+ * 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.subnet;
+
+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.onlab.packet.IpAddress.Version;
+import org.onlab.packet.IpPrefix;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.vtnrsc.AllocationPool;
+import org.onosproject.vtnrsc.DefaultSubnet;
+import org.onosproject.vtnrsc.HostRoute;
+import org.onosproject.vtnrsc.Subnet;
+import org.onosproject.vtnrsc.Subnet.Mode;
+import org.onosproject.vtnrsc.SubnetId;
+import org.onosproject.vtnrsc.TenantId;
+import org.onosproject.vtnrsc.TenantNetworkId;
+import org.onosproject.vtnrsc.subnet.SubnetService;
+
+import com.google.common.collect.Sets;
+
+/**
+ * Supports for updating a subnet.
+ */
+@Command(scope = "onos", name = "subnet-update", description = "Supports for updating a subnet")
+public class SubnetUpdateCommand extends AbstractShellCommand {
+
+    @Argument(index = 0, name = "id", description = "Subnet Id", required = true,
+            multiValued = false)
+    String id = null;
+
+    @Argument(index = 1, name = "subnetName", description = "Subnet String name", required = true,
+            multiValued = false)
+    String subnetName = null;
+
+    @Argument(index = 2, name = "networkId", description = "Subnet Network Id", required = true,
+            multiValued = false)
+    String networkId = null;
+
+    @Argument(index = 3, name = "tenantId", description = "Subnet Tenant Id", required = true,
+            multiValued = false)
+    String tenantId = null;
+
+    @Option(name = "-i", aliases = "--ipVersion", description = "Subnet Version ipVersion",
+            required = false, multiValued = false)
+    Version ipVersion = null;
+
+    @Option(name = "-c", aliases = "--cidr", description = "Subnet IpPrefix cidr", required = false,
+            multiValued = false)
+    String cidr = "0.0.0.0/0";
+
+    @Option(name = "-g", aliases = "--gatewayIp", description = "Subnet IpAddress gatewayIp",
+            required = false, multiValued = false)
+    String gatewayIp = "0.0.0.0";
+
+    @Option(name = "-d", aliases = "--dhcpEnabled", description = "Subnet boolean dhcpEnabled",
+            required = false, multiValued = false)
+    boolean dhcpEnabled = false;
+
+    @Option(name = "-s", aliases = "--shared", description = "Subnet boolean shared", required = false,
+            multiValued = false)
+    boolean shared = false;
+
+    @Option(name = "-m", aliases = "--ipV6AddressMode", description = "Subnet Mode ipV6AddressMode",
+            required = false, multiValued = false)
+    String ipV6AddressMode = null;
+
+    @Option(name = "-r", aliases = "--ipV6RaMode", description = "Subnet Mode ipV6RaMode",
+            required = false, multiValued = false)
+    String ipV6RaMode = null;
+
+    @Option(name = "-h", aliases = "--hostRoutes", description = "Subnet jsonnode hostRoutes",
+            required = false, multiValued = false)
+    Iterable<HostRoute> hostRoutes = null;
+
+    @Option(name = "-a", aliases = "--allocationPools",
+            description = "Subnet jsonnode allocationPools", required = false, multiValued = false)
+    Iterable<AllocationPool> allocationPools = Sets.newHashSet();;
+
+    @Override
+    protected void execute() {
+        SubnetService service = get(SubnetService.class);
+        if (id == null || networkId == null || tenantId == null) {
+            print(null, "id,networkId,tenantId can not be null");
+            return;
+        }
+        Subnet subnet = new DefaultSubnet(SubnetId.subnetId(id), subnetName,
+                                          TenantNetworkId.networkId(networkId),
+                                          TenantId.tenantId(tenantId), ipVersion,
+                                          cidr == null ? null : IpPrefix.valueOf(cidr),
+                                          gatewayIp == null ? null : IpAddress.valueOf(gatewayIp),
+                                          dhcpEnabled, shared, hostRoutes,
+                                          ipV6AddressMode == null ? null : Mode.valueOf(ipV6AddressMode),
+                                          ipV6RaMode == null ? null : Mode.valueOf(ipV6RaMode),
+                                          allocationPools);
+        Set<Subnet> subnetsSet = Sets.newHashSet();
+        subnetsSet.add(subnet);
+        service.updateSubnets(subnetsSet);
+    }
+
+}