Thomas Vachuska
Committed by Gerrit Code Review

ONOS-1470 Implemented component config REST API.

Change-Id: I637456a1dfd364ff3184cded8a5e8b251afc9987
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.rest;
17 +
18 +import com.fasterxml.jackson.databind.node.ObjectNode;
19 +import org.onosproject.cfg.ComponentConfigService;
20 +import org.onosproject.cfg.ConfigProperty;
21 +
22 +import javax.ws.rs.DELETE;
23 +import javax.ws.rs.GET;
24 +import javax.ws.rs.POST;
25 +import javax.ws.rs.Path;
26 +import javax.ws.rs.PathParam;
27 +import javax.ws.rs.core.Response;
28 +import java.io.IOException;
29 +import java.io.InputStream;
30 +import java.util.Set;
31 +
32 +/**
33 + * REST resource for cluster-wide component configuration.
34 + */
35 +@Path("configuration")
36 +public class ComponentConfigWebResource extends AbstractWebResource {
37 +
38 + @GET
39 + public Response getComponentConfigs() {
40 + ComponentConfigService service = get(ComponentConfigService.class);
41 + Set<String> components = service.getComponentNames();
42 + ObjectNode root = mapper().createObjectNode();
43 + components.forEach(c -> encodeConfigs(c, service.getProperties(c), root));
44 + return ok(root).build();
45 + }
46 +
47 + @GET
48 + @Path("{component}")
49 + public Response getComponentConfigs(@PathParam("component") String component) {
50 + ComponentConfigService service = get(ComponentConfigService.class);
51 + ObjectNode root = mapper().createObjectNode();
52 + encodeConfigs(component, nullIsNotFound(service.getProperties(component),
53 + "No such component"), root);
54 + return ok(root).build();
55 + }
56 +
57 + // Encodes the specified properties as an object in the given node.
58 + private void encodeConfigs(String component, Set<ConfigProperty> props,
59 + ObjectNode node) {
60 + ObjectNode compNode = mapper().createObjectNode();
61 + node.set(component, compNode);
62 + props.forEach(p -> compNode.put(p.name(), p.value()));
63 + }
64 +
65 + @POST
66 + @Path("{component}")
67 + public Response setComponentConfigs(@PathParam("component") String component,
68 + InputStream request) throws IOException {
69 + ComponentConfigService service = get(ComponentConfigService.class);
70 + ObjectNode props = (ObjectNode) mapper().readTree(request);
71 + props.fieldNames().forEachRemaining(k -> service.setProperty(component, k,
72 + props.path(k).asText()));
73 + return Response.noContent().build();
74 + }
75 +
76 + @DELETE
77 + @Path("{component}")
78 + public Response unsetComponentConfigs(@PathParam("component") String component,
79 + InputStream request) throws IOException {
80 + ComponentConfigService service = get(ComponentConfigService.class);
81 + ObjectNode props = (ObjectNode) mapper().readTree(request);
82 + props.fieldNames().forEachRemaining(k -> service.unsetProperty(component, k));
83 + return Response.noContent().build();
84 + }
85 +}
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.rest.exceptions;
17 +
18 +import javax.ws.rs.core.Response;
19 +import java.io.IOException;
20 +
21 +/**
22 + * Mapper for IO exceptions to the BAD_REQUEST response code.
23 + */
24 +public class BadRequestMapper extends AbstractMapper<IOException> {
25 + @Override
26 + protected Response.Status responseStatus() {
27 + return Response.Status.BAD_REQUEST;
28 + }
29 +}
...@@ -59,9 +59,11 @@ ...@@ -59,9 +59,11 @@
59 org.onosproject.rest.exceptions.ServiceNotFoundMapper, 59 org.onosproject.rest.exceptions.ServiceNotFoundMapper,
60 org.onosproject.rest.exceptions.NotFoundMapper, 60 org.onosproject.rest.exceptions.NotFoundMapper,
61 org.onosproject.rest.exceptions.ServerErrorMapper, 61 org.onosproject.rest.exceptions.ServerErrorMapper,
62 + org.onosproject.rest.exceptions.BadRequestMapper,
62 org.onosproject.rest.JsonBodyWriter, 63 org.onosproject.rest.JsonBodyWriter,
63 64
64 org.onosproject.rest.ApplicationsWebResource, 65 org.onosproject.rest.ApplicationsWebResource,
66 + org.onosproject.rest.ComponentConfigWebResource,
65 org.onosproject.rest.ClusterWebResource, 67 org.onosproject.rest.ClusterWebResource,
66 org.onosproject.rest.DevicesWebResource, 68 org.onosproject.rest.DevicesWebResource,
67 org.onosproject.rest.LinksWebResource, 69 org.onosproject.rest.LinksWebResource,
......