blob: 97e836af74b50c011b9522d5fd19ad0ee0e33258 [file] [log] [blame]
sangho27462c62015-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;
23import org.onosproject.segmentrouting.SegmentRoutingManager;
24import org.onosproject.segmentrouting.SegmentRoutingService;
25import org.onosproject.segmentrouting.TunnelPolicy;
26
27import javax.ws.rs.Consumes;
28import javax.ws.rs.DELETE;
29import javax.ws.rs.GET;
30import javax.ws.rs.POST;
31import javax.ws.rs.Path;
32import javax.ws.rs.Produces;
33import javax.ws.rs.core.MediaType;
34import javax.ws.rs.core.Response;
35import java.io.IOException;
36import java.io.InputStream;
37import java.util.List;
38
39@Path("policy")
40public class PolicyWebResource extends AbstractWebResource {
41
42 private static final PolicyCodec POLICY_CODEC = new PolicyCodec();
43
44 @GET
45 @Produces(MediaType.APPLICATION_JSON)
46 public Response getPolicy() {
47 SegmentRoutingService srService = get(SegmentRoutingService.class);
48 List<Policy> policies = srService.getPolicies();
49 ObjectNode result = new ObjectMapper().createObjectNode();
50 result.set("policy", new PolicyCodec().encode(policies, this));
51
52 return ok(result.toString()).build();
53 }
54
55 @POST
56 @Consumes(MediaType.APPLICATION_JSON)
57 public Response createPolicy(InputStream input) throws IOException {
58 ObjectMapper mapper = new ObjectMapper();
59 ObjectNode policyJson = (ObjectNode) mapper.readTree(input);
60 SegmentRoutingService srService = get(SegmentRoutingService.class);
61 Policy policyInfo = POLICY_CODEC.decode(policyJson, this);
62 if (policyInfo.type() == Policy.Type.TUNNEL_FLOW) {
63 TunnelPolicy policy = new TunnelPolicy((SegmentRoutingManager) srService, (TunnelPolicy) policyInfo);
64 srService.createPolicy(policy);
65
66 return Response.ok().build();
67 } else {
68 return Response.serverError().build();
69 }
70 }
71
72 @DELETE
73 @Consumes(MediaType.APPLICATION_JSON)
74 public Response removePolicy(InputStream input) throws IOException {
75 ObjectMapper mapper = new ObjectMapper();
76 ObjectNode policyJson = (ObjectNode) mapper.readTree(input);
77 SegmentRoutingService srService = get(SegmentRoutingService.class);
78 Policy policyInfo = POLICY_CODEC.decode(policyJson, this);
79 // TODO: Check the result
80 srService.removePolicy(policyInfo);
81 return Response.ok().build();
82
83 }
84
85}