SureshBR
Committed by Gerrit Code Review

[ONOS-3114] Classifier web resource added

Change-Id: I5e0cafd09ee0ed8f03b7d0be195989a0fddf9bde
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.vtnweb.resources;
17 +
18 +import javax.ws.rs.GET;
19 +import javax.ws.rs.Path;
20 +import javax.ws.rs.Produces;
21 +import javax.ws.rs.core.MediaType;
22 +import javax.ws.rs.core.Response;
23 +
24 +import org.onosproject.net.DeviceId;
25 +import org.onosproject.rest.AbstractWebResource;
26 +import org.onosproject.vtnrsc.classifier.ClassifierService;
27 +import org.slf4j.Logger;
28 +import org.slf4j.LoggerFactory;
29 +
30 +import com.fasterxml.jackson.databind.node.ArrayNode;
31 +import com.fasterxml.jackson.databind.node.ObjectNode;
32 +
33 +/**
34 + * Query and program classifiers.
35 + */
36 +@Path("classifiers")
37 +public class ClassifierWebResource extends AbstractWebResource {
38 +
39 + private final Logger log = LoggerFactory.getLogger(ClassifierWebResource.class);
40 +
41 + /**
42 + * Get the list of classifier devices.
43 + *
44 + * @return 200 OK
45 + */
46 + @GET
47 + @Produces(MediaType.APPLICATION_JSON)
48 + public Response getClassifiers() {
49 +
50 + ObjectNode result = mapper().createObjectNode();
51 +
52 + Iterable<DeviceId> classifierDevices = get(ClassifierService.class).getClassifiers();
53 + ArrayNode classifier = result.putArray("classifiers");
54 + if (classifierDevices != null) {
55 + for (final DeviceId deviceId : classifierDevices) {
56 + ObjectNode dev = mapper().createObjectNode()
57 + .put("DeviceId", deviceId.toString());
58 + classifier.add(dev);
59 + }
60 + }
61 + return ok(result.toString()).build();
62 + }
63 +}
1 +/*
2 + * Copyright 2014-2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.vtnweb.resources;
17 +
18 +import static org.easymock.EasyMock.createMock;
19 +import static org.easymock.EasyMock.expect;
20 +import static org.easymock.EasyMock.replay;
21 +import static org.hamcrest.Matchers.is;
22 +import static org.hamcrest.Matchers.notNullValue;
23 +import static org.junit.Assert.assertThat;
24 +import static org.onosproject.net.NetTestTools.device;
25 +import static org.onosproject.net.NetTestTools.did;
26 +
27 +import org.junit.After;
28 +import org.junit.Before;
29 +import org.junit.Test;
30 +import org.onlab.osgi.ServiceDirectory;
31 +import org.onlab.osgi.TestServiceDirectory;
32 +import org.onlab.rest.BaseResource;
33 +import org.onosproject.codec.CodecService;
34 +import org.onosproject.net.Device;
35 +import org.onosproject.net.DeviceId;
36 +import org.onosproject.vtnrsc.classifier.ClassifierService;
37 +import org.onosproject.vtnweb.web.SfcCodecContext;
38 +
39 +import com.eclipsesource.json.JsonObject;
40 +import com.google.common.collect.ImmutableList;
41 +import com.sun.jersey.api.client.WebResource;
42 +/**
43 + * Unit tests for classifier REST APIs.
44 + */
45 +public class ClassifierResourceTest extends VtnResourceTest {
46 +
47 + final ClassifierService classifierService = createMock(ClassifierService.class);
48 +
49 + /**
50 + * Sets up the global values for all the tests.
51 + */
52 + @Before
53 + public void setUpTest() {
54 +
55 + SfcCodecContext context = new SfcCodecContext();
56 + ServiceDirectory testDirectory = new TestServiceDirectory().add(ClassifierService.class, classifierService)
57 + .add(CodecService.class, context.codecManager());
58 + BaseResource.setServiceDirectory(testDirectory);
59 +
60 + }
61 +
62 + /**
63 + * Cleans up.
64 + */
65 + @After
66 + public void tearDownTest() {
67 + }
68 +
69 + /**
70 + * Tests the result of the rest api GET when there are no classifiers.
71 + */
72 + @Test
73 + public void testClassifiersEmpty() {
74 +
75 + expect(classifierService.getClassifiers()).andReturn(null).anyTimes();
76 + replay(classifierService);
77 + final WebResource rs = resource();
78 + final String response = rs.path("classifiers").get(String.class);
79 + assertThat(response, is("{\"classifiers\":[]}"));
80 + }
81 +
82 + /**
83 + * Tests the result of a rest api GET for classifiers.
84 + */
85 + @Test
86 + public void testClassifiers() {
87 +
88 + DeviceId devId1 = did("dev1");
89 + Device device1 = device("dev1");
90 +
91 + expect(classifierService.getClassifiers()).andReturn(ImmutableList.of(devId1)).anyTimes();
92 + replay(classifierService);
93 +
94 + final WebResource rs = resource();
95 + final String response = rs.path("classifiers").get(String.class);
96 + final JsonObject result = JsonObject.readFrom(response);
97 + assertThat(result, notNullValue());
98 + }
99 +}