blob: cdc53a021e0e5270196e58c0f682ef31087295ed [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
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070037/**
38 * Query, create and remove segment routing plicies.
39 */
sangho1e575652015-05-14 00:39:53 -070040@Path("policy")
41public class PolicyWebResource extends AbstractWebResource {
42
43 private static final PolicyCodec POLICY_CODEC = new PolicyCodec();
44
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070045 /**
46 * Get all segment routing policies.
47 * Returns an array of segment routing policies.
48 *
49 * @return status of OK
50 */
sangho1e575652015-05-14 00:39:53 -070051 @GET
52 @Produces(MediaType.APPLICATION_JSON)
53 public Response getPolicy() {
54 SegmentRoutingService srService = get(SegmentRoutingService.class);
55 List<Policy> policies = srService.getPolicies();
56 ObjectNode result = new ObjectMapper().createObjectNode();
57 result.set("policy", new PolicyCodec().encode(policies, this));
58
59 return ok(result.toString()).build();
60 }
61
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070062 /**
63 * Create a new segment routing policy.
64 *
65 * @param input JSON stream for policy to create
66 * @return status of the request - OK if the policy is created,
67 * INTERNAL_SERVER_ERROR if the JSON is invalid or the policy cannot be created
Ray Milkey9b36d812015-09-09 15:24:54 -070068 * @throws IOException if JSON processing fails
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070069 */
sangho1e575652015-05-14 00:39:53 -070070 @POST
71 @Consumes(MediaType.APPLICATION_JSON)
72 public Response createPolicy(InputStream input) throws IOException {
73 ObjectMapper mapper = new ObjectMapper();
74 ObjectNode policyJson = (ObjectNode) mapper.readTree(input);
75 SegmentRoutingService srService = get(SegmentRoutingService.class);
76 Policy policyInfo = POLICY_CODEC.decode(policyJson, this);
sangho1e575652015-05-14 00:39:53 -070077
sangho0b2b6d12015-05-20 22:16:38 -070078 if (policyInfo.type() == Policy.Type.TUNNEL_FLOW) {
79 srService.createPolicy(policyInfo);
sangho1e575652015-05-14 00:39:53 -070080 return Response.ok().build();
81 } else {
82 return Response.serverError().build();
83 }
84 }
85
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070086 /**
87 * Delete a segment routing policy.
88 *
89 * @param input JSON stream for policy to delete
90 * @return status of the request - OK if the policy is removed,
91 * INTERNAL_SERVER_ERROR otherwise
92 * @throws IOException if JSON is invalid
93 */
sangho1e575652015-05-14 00:39:53 -070094 @DELETE
95 @Consumes(MediaType.APPLICATION_JSON)
96 public Response removePolicy(InputStream input) throws IOException {
97 ObjectMapper mapper = new ObjectMapper();
98 ObjectNode policyJson = (ObjectNode) mapper.readTree(input);
99 SegmentRoutingService srService = get(SegmentRoutingService.class);
100 Policy policyInfo = POLICY_CODEC.decode(policyJson, this);
101 // TODO: Check the result
102 srService.removePolicy(policyInfo);
sangho0b2b6d12015-05-20 22:16:38 -0700103
sangho1e575652015-05-14 00:39:53 -0700104 return Response.ok().build();
105
106 }
107
108}