blob: 384ff0f102a0b52feffc549720c6c2bfa6d25651 [file] [log] [blame]
sangho1e575652015-05-14 00:39:53 -07001/*
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 */
16package org.onosproject.segmentrouting.web;
17
18import com.fasterxml.jackson.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20
21import org.onosproject.rest.AbstractWebResource;
22import org.onosproject.segmentrouting.Policy;
sangho1e575652015-05-14 00:39:53 -070023import org.onosproject.segmentrouting.SegmentRoutingService;
sangho1e575652015-05-14 00:39:53 -070024
25import javax.ws.rs.Consumes;
26import javax.ws.rs.DELETE;
27import javax.ws.rs.GET;
28import javax.ws.rs.POST;
29import javax.ws.rs.Path;
30import javax.ws.rs.Produces;
31import javax.ws.rs.core.MediaType;
32import javax.ws.rs.core.Response;
33import java.io.IOException;
34import java.io.InputStream;
35import java.util.List;
36
37@Path("policy")
38public class PolicyWebResource extends AbstractWebResource {
39
40 private static final PolicyCodec POLICY_CODEC = new PolicyCodec();
41
42 @GET
43 @Produces(MediaType.APPLICATION_JSON)
44 public Response getPolicy() {
45 SegmentRoutingService srService = get(SegmentRoutingService.class);
46 List<Policy> policies = srService.getPolicies();
47 ObjectNode result = new ObjectMapper().createObjectNode();
48 result.set("policy", new PolicyCodec().encode(policies, this));
49
50 return ok(result.toString()).build();
51 }
52
53 @POST
54 @Consumes(MediaType.APPLICATION_JSON)
55 public Response createPolicy(InputStream input) throws IOException {
56 ObjectMapper mapper = new ObjectMapper();
57 ObjectNode policyJson = (ObjectNode) mapper.readTree(input);
58 SegmentRoutingService srService = get(SegmentRoutingService.class);
59 Policy policyInfo = POLICY_CODEC.decode(policyJson, this);
sangho1e575652015-05-14 00:39:53 -070060
sangho0b2b6d12015-05-20 22:16:38 -070061 if (policyInfo.type() == Policy.Type.TUNNEL_FLOW) {
62 srService.createPolicy(policyInfo);
sangho1e575652015-05-14 00:39:53 -070063 return Response.ok().build();
64 } else {
65 return Response.serverError().build();
66 }
67 }
68
69 @DELETE
70 @Consumes(MediaType.APPLICATION_JSON)
71 public Response removePolicy(InputStream input) throws IOException {
72 ObjectMapper mapper = new ObjectMapper();
73 ObjectNode policyJson = (ObjectNode) mapper.readTree(input);
74 SegmentRoutingService srService = get(SegmentRoutingService.class);
75 Policy policyInfo = POLICY_CODEC.decode(policyJson, this);
76 // TODO: Check the result
77 srService.removePolicy(policyInfo);
sangho0b2b6d12015-05-20 22:16:38 -070078
sangho1e575652015-05-14 00:39:53 -070079 return Response.ok().build();
80
81 }
82
83}