Allow to query openstack nodes details through REST API

Change-Id: Ic2e1d3684f457d00ebaf971aeb357e4f7e84e74f
diff --git a/apps/openstacknode/app/BUILD b/apps/openstacknode/app/BUILD
index dc99f64..8c4fe3d 100644
--- a/apps/openstacknode/app/BUILD
+++ b/apps/openstacknode/app/BUILD
@@ -27,6 +27,7 @@
     "//apps/openstacknode/api:onos-apps-openstacknode-api-tests",
     "//core/common:onos-core-common-tests",
     "//web/api:onos-rest-tests",
+    "@minimal_json//jar",
 ]
 
 osgi_jar_with_tests(
diff --git a/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/web/OpenstackNodeWebResource.java b/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/web/OpenstackNodeWebResource.java
index ae85af0..41d3f33 100644
--- a/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/web/OpenstackNodeWebResource.java
+++ b/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/web/OpenstackNodeWebResource.java
@@ -28,6 +28,7 @@
 
 import javax.ws.rs.Consumes;
 import javax.ws.rs.DELETE;
+import javax.ws.rs.GET;
 import javax.ws.rs.POST;
 import javax.ws.rs.PUT;
 import javax.ws.rs.Path;
@@ -60,10 +61,14 @@
     private static final String UPDATE = "UPDATE";
     private static final String NODE_ID = "NODE_ID";
     private static final String DELETE = "DELETE";
+    private static final String QUERY = "QUERY";
 
     private static final String HOST_NAME = "hostname";
     private static final String ERROR_MESSAGE = " cannot be null";
 
+    private final ObjectNode root = mapper().createObjectNode();
+    private final ArrayNode osJsonNodes = root.putArray("nodes");
+
     private final OpenstackNodeAdminService osNodeAdminService =
                                             get(OpenstackNodeAdminService.class);
     private final OpenstackNodeService osNodeService =
@@ -156,6 +161,25 @@
         return Response.noContent().build();
     }
 
+    /**
+     * Obtains a set of openstack node's.
+     *
+     * @return 200 OK with array of all the openstack nodes stored in the system
+     * @onos.rsModel OpenstackNode
+     */
+    @GET
+    @Produces(MediaType.APPLICATION_JSON)
+    public Response nodes() {
+        log.trace(String.format(MESSAGE_NODE, QUERY));
+
+        Set<OpenstackNode> osNodes = osNodeService.nodes();
+
+        for (OpenstackNode osNode : osNodes) {
+            osJsonNodes.add(codec(OpenstackNode.class).encode(osNode, this));
+        }
+        return ok(root).build();
+    }
+
     private Set<OpenstackNode> readNodeConfiguration(InputStream input) {
         Set<OpenstackNode> nodeSet = Sets.newHashSet();
         try {
diff --git a/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/codec/OpenstackNodeJsonArrayMatcher.java b/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/codec/OpenstackNodeJsonArrayMatcher.java
new file mode 100644
index 0000000..ece9167
--- /dev/null
+++ b/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/codec/OpenstackNodeJsonArrayMatcher.java
@@ -0,0 +1,68 @@
+/*
+ * Copyright 2019-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.openstacknode.codec;
+
+import com.eclipsesource.json.JsonArray;
+import com.eclipsesource.json.JsonObject;
+import org.hamcrest.Description;
+import org.hamcrest.TypeSafeMatcher;
+import org.onosproject.openstacknode.api.OpenstackNode;
+
+public class OpenstackNodeJsonArrayMatcher extends TypeSafeMatcher<JsonArray> {
+
+    private final OpenstackNode node;
+    private String reason = "";
+
+    public OpenstackNodeJsonArrayMatcher(OpenstackNode node) {
+        this.node = node;
+    }
+
+    @Override
+    protected boolean matchesSafely(JsonArray json) {
+        boolean nodeFound = false;
+        for (int jsonNodeIndex = 0; jsonNodeIndex < json.size(); jsonNodeIndex++) {
+            final JsonObject jsonNode = json.get(jsonNodeIndex).asObject();
+
+            final String hostname = node.hostname();
+            final String jsonHostname = jsonNode.get("hostname").asString();
+            if (jsonHostname.equals(hostname)) {
+                nodeFound = true;
+            }
+        }
+
+        if (!nodeFound) {
+            reason = "Node with hostname " + node.hostname() + " not found";
+            return false;
+        } else {
+            return true;
+        }
+    }
+
+    @Override
+    public void describeTo(Description description) {
+        description.appendText(reason);
+    }
+
+    /**
+     * Factory to allocate a node array matcher.
+     *
+     * @param node node object we are looking for
+     * @return matcher
+     */
+    public static OpenstackNodeJsonArrayMatcher hasNode(OpenstackNode node) {
+        return new OpenstackNodeJsonArrayMatcher(node);
+    }
+}
diff --git a/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/web/OpenstackNodeWebResourceTest.java b/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/web/OpenstackNodeWebResourceTest.java
index d912013..4f7c843 100644
--- a/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/web/OpenstackNodeWebResourceTest.java
+++ b/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/web/OpenstackNodeWebResourceTest.java
@@ -15,6 +15,10 @@
  */
 package org.onosproject.openstacknode.web;
 
+import com.eclipsesource.json.Json;
+import com.eclipsesource.json.JsonArray;
+import com.eclipsesource.json.JsonObject;
+import com.google.common.collect.ImmutableSet;
 import org.glassfish.jersey.server.ResourceConfig;
 import org.junit.Before;
 import org.junit.Test;
@@ -24,12 +28,12 @@
 import org.onosproject.codec.CodecService;
 import org.onosproject.codec.impl.CodecManager;
 import org.onosproject.net.DeviceId;
+import org.onosproject.openstacknode.api.DefaultOpenstackNode;
 import org.onosproject.openstacknode.api.NodeState;
 import org.onosproject.openstacknode.api.OpenstackNode;
 import org.onosproject.openstacknode.api.OpenstackNodeAdminService;
 import org.onosproject.openstacknode.api.OpenstackNodeService;
 import org.onosproject.openstacknode.codec.OpenstackNodeCodec;
-import org.onosproject.openstacknode.api.DefaultOpenstackNode;
 import org.onosproject.rest.resources.ResourceTest;
 
 import javax.ws.rs.client.Entity;
@@ -44,8 +48,11 @@
 import static org.easymock.EasyMock.expect;
 import static org.easymock.EasyMock.replay;
 import static org.easymock.EasyMock.verify;
+import static org.hamcrest.Matchers.hasSize;
 import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
 import static org.junit.Assert.assertThat;
+import static org.onosproject.openstacknode.codec.OpenstackNodeJsonArrayMatcher.hasNode;
 
 /**
  * Unit test for Openstack node REST API.
@@ -226,4 +233,28 @@
 
         verify(mockOpenstackNodeService);
     }
+
+    /**
+     * Tests the result of the REST API GET when there are valid nodes.
+     */
+    @Test
+    public void testGetNodesPopulatedArray() {
+        expect(mockOpenstackNodeService.nodes()).
+                andReturn(ImmutableSet.of(openstackNode)).anyTimes();
+        replay(mockOpenstackNodeService);
+
+        final WebTarget wt = target();
+        final String response = wt.path(PATH).request().get(String.class);
+        final JsonObject result = Json.parse(response).asObject();
+
+        assertThat(result, notNullValue());
+        assertThat(result.names(), hasSize(1));
+        assertThat(result.names().get(0), is("nodes"));
+        final JsonArray jsonNodes = result.get("nodes").asArray();
+        assertThat(jsonNodes, notNullValue());
+        assertThat(jsonNodes.size(), is(1));
+        assertThat(jsonNodes, hasNode(openstackNode));
+
+        verify(mockOpenstackNodeService);
+    }
 }