[ONOS-3114] Classifier web resource added
Change-Id: I5e0cafd09ee0ed8f03b7d0be195989a0fddf9bde
diff --git a/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/resources/ClassifierWebResource.java b/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/resources/ClassifierWebResource.java
new file mode 100644
index 0000000..cfed29a
--- /dev/null
+++ b/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/resources/ClassifierWebResource.java
@@ -0,0 +1,63 @@
+/*
+ * 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.vtnweb.resources;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+import org.onosproject.net.DeviceId;
+import org.onosproject.rest.AbstractWebResource;
+import org.onosproject.vtnrsc.classifier.ClassifierService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+/**
+ * Query and program classifiers.
+ */
+@Path("classifiers")
+public class ClassifierWebResource extends AbstractWebResource {
+
+ private final Logger log = LoggerFactory.getLogger(ClassifierWebResource.class);
+
+ /**
+ * Get the list of classifier devices.
+ *
+ * @return 200 OK
+ */
+ @GET
+ @Produces(MediaType.APPLICATION_JSON)
+ public Response getClassifiers() {
+
+ ObjectNode result = mapper().createObjectNode();
+
+ Iterable<DeviceId> classifierDevices = get(ClassifierService.class).getClassifiers();
+ ArrayNode classifier = result.putArray("classifiers");
+ if (classifierDevices != null) {
+ for (final DeviceId deviceId : classifierDevices) {
+ ObjectNode dev = mapper().createObjectNode()
+ .put("DeviceId", deviceId.toString());
+ classifier.add(dev);
+ }
+ }
+ return ok(result.toString()).build();
+ }
+}
diff --git a/apps/vtn/vtnweb/src/test/java/org/onosproject/vtnweb/resources/ClassifierResourceTest.java b/apps/vtn/vtnweb/src/test/java/org/onosproject/vtnweb/resources/ClassifierResourceTest.java
new file mode 100644
index 0000000..f63cab0
--- /dev/null
+++ b/apps/vtn/vtnweb/src/test/java/org/onosproject/vtnweb/resources/ClassifierResourceTest.java
@@ -0,0 +1,99 @@
+/*
+ * Copyright 2014-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.vtnweb.resources;
+
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.junit.Assert.assertThat;
+import static org.onosproject.net.NetTestTools.device;
+import static org.onosproject.net.NetTestTools.did;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.osgi.ServiceDirectory;
+import org.onlab.osgi.TestServiceDirectory;
+import org.onlab.rest.BaseResource;
+import org.onosproject.codec.CodecService;
+import org.onosproject.net.Device;
+import org.onosproject.net.DeviceId;
+import org.onosproject.vtnrsc.classifier.ClassifierService;
+import org.onosproject.vtnweb.web.SfcCodecContext;
+
+import com.eclipsesource.json.JsonObject;
+import com.google.common.collect.ImmutableList;
+import com.sun.jersey.api.client.WebResource;
+/**
+ * Unit tests for classifier REST APIs.
+ */
+public class ClassifierResourceTest extends VtnResourceTest {
+
+ final ClassifierService classifierService = createMock(ClassifierService.class);
+
+ /**
+ * Sets up the global values for all the tests.
+ */
+ @Before
+ public void setUpTest() {
+
+ SfcCodecContext context = new SfcCodecContext();
+ ServiceDirectory testDirectory = new TestServiceDirectory().add(ClassifierService.class, classifierService)
+ .add(CodecService.class, context.codecManager());
+ BaseResource.setServiceDirectory(testDirectory);
+
+ }
+
+ /**
+ * Cleans up.
+ */
+ @After
+ public void tearDownTest() {
+ }
+
+ /**
+ * Tests the result of the rest api GET when there are no classifiers.
+ */
+ @Test
+ public void testClassifiersEmpty() {
+
+ expect(classifierService.getClassifiers()).andReturn(null).anyTimes();
+ replay(classifierService);
+ final WebResource rs = resource();
+ final String response = rs.path("classifiers").get(String.class);
+ assertThat(response, is("{\"classifiers\":[]}"));
+ }
+
+ /**
+ * Tests the result of a rest api GET for classifiers.
+ */
+ @Test
+ public void testClassifiers() {
+
+ DeviceId devId1 = did("dev1");
+ Device device1 = device("dev1");
+
+ expect(classifierService.getClassifiers()).andReturn(ImmutableList.of(devId1)).anyTimes();
+ replay(classifierService);
+
+ final WebResource rs = resource();
+ final String response = rs.path("classifiers").get(String.class);
+ final JsonObject result = JsonObject.readFrom(response);
+ assertThat(result, notNullValue());
+ }
+}