Support openstack subnet list command in OpenstackNetworking app

Change-Id: Id7fb104a139c024923aecd7439104e2242ce3588
diff --git a/apps/openstacknetworking/app/src/main/java/org/onosproject/openstacknetworking/cli/OpenstackSubnetListCommand.java b/apps/openstacknetworking/app/src/main/java/org/onosproject/openstacknetworking/cli/OpenstackSubnetListCommand.java
new file mode 100644
index 0000000..69573e2
--- /dev/null
+++ b/apps/openstacknetworking/app/src/main/java/org/onosproject/openstacknetworking/cli/OpenstackSubnetListCommand.java
@@ -0,0 +1,74 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.openstacknetworking.cli;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.google.common.collect.Lists;
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.openstacknetworking.api.OpenstackNetworkService;
+import org.openstack4j.model.network.Subnet;
+import org.openstack4j.openstack.networking.domain.NeutronSubnet;
+
+import java.util.Comparator;
+import java.util.List;
+
+import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.modelEntityToJson;
+import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.prettyJson;
+
+/**
+ * Lists OpenStack subnets.
+ */
+@Command(scope = "onos", name = "openstack-subnets",
+        description = "Lists all OpenStack subnets")
+public class OpenstackSubnetListCommand extends AbstractShellCommand {
+
+    private static final String FORMAT = "%-40s%-20s%-20s%-20s%-40s%-20s%-8s";
+
+    @Override
+    protected void execute() {
+        OpenstackNetworkService service = AbstractShellCommand.get(OpenstackNetworkService.class);
+        List<Subnet> subnets = Lists.newArrayList(service.subnets());
+        subnets.sort(Comparator.comparing(Subnet::getName));
+
+        if (outputJson()) {
+            print("%s", json(subnets));
+        } else {
+            print(FORMAT, "ID", "Name", "CIDR", "GatewayIp", "NetworkId", "NetworkName", "HostRoutes");
+
+            for (Subnet subnet: subnets) {
+                print(FORMAT,
+                        subnet.getId(),
+                        subnet.getName(),
+                        subnet.getCidr(),
+                        subnet.getGateway(),
+                        subnet.getNetworkId(),
+                        service.network(subnet.getNetworkId()).getName(),
+                        subnet.getHostRoutes());
+            }
+        }
+    }
+
+    private String json(List<Subnet> subnets) {
+        ObjectMapper mapper = new ObjectMapper();
+        ArrayNode result = mapper.createArrayNode();
+        for (Subnet net: subnets) {
+            result.add(modelEntityToJson(net, NeutronSubnet.class));
+        }
+        return prettyJson(mapper, result.toString());
+    }
+}
diff --git a/apps/openstacknetworking/app/src/main/resources/OSGI-INF/blueprint/shell-config.xml b/apps/openstacknetworking/app/src/main/resources/OSGI-INF/blueprint/shell-config.xml
index bb8bbdd..f21a250 100644
--- a/apps/openstacknetworking/app/src/main/resources/OSGI-INF/blueprint/shell-config.xml
+++ b/apps/openstacknetworking/app/src/main/resources/OSGI-INF/blueprint/shell-config.xml
@@ -19,6 +19,9 @@
             <action class="org.onosproject.openstacknetworking.cli.OpenstackNetworkListCommand"/>
         </command>
         <command>
+            <action class="org.onosproject.openstacknetworking.cli.OpenstackSubnetListCommand"/>
+        </command>
+        <command>
             <action class="org.onosproject.openstacknetworking.cli.OpenstackPortListCommand"/>
         </command>
         <command>