blob: cebcf84a6bc71c878156b13976853e837e8c4dd6 [file] [log] [blame]
sangho1e575652015-05-14 00:39:53 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
sangho1e575652015-05-14 00:39:53 -07003 *
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;
sangho1e575652015-05-14 00:39:53 -070029import javax.ws.rs.Produces;
30import javax.ws.rs.core.MediaType;
31import javax.ws.rs.core.Response;
32import java.io.IOException;
33import java.io.InputStream;
34import java.util.List;
35
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070036/**
37 * Query, create and remove segment routing plicies.
38 */
Andreas Pantelopouloscd339592018-02-23 14:18:00 -080039// @Path("policy")
sangho1e575652015-05-14 00:39:53 -070040public class PolicyWebResource extends AbstractWebResource {
41
42 private static final PolicyCodec POLICY_CODEC = new PolicyCodec();
43
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070044 /**
45 * Get all segment routing policies.
46 * Returns an array of segment routing policies.
47 *
48 * @return status of OK
49 */
sangho1e575652015-05-14 00:39:53 -070050 @GET
51 @Produces(MediaType.APPLICATION_JSON)
52 public Response getPolicy() {
53 SegmentRoutingService srService = get(SegmentRoutingService.class);
54 List<Policy> policies = srService.getPolicies();
55 ObjectNode result = new ObjectMapper().createObjectNode();
56 result.set("policy", new PolicyCodec().encode(policies, this));
57
58 return ok(result.toString()).build();
59 }
60
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070061 /**
62 * Create a new segment routing policy.
63 *
64 * @param input JSON stream for policy to create
65 * @return status of the request - OK if the policy is created,
Ray Milkey9b36d812015-09-09 15:24:54 -070066 * @throws IOException if JSON processing fails
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070067 */
sangho1e575652015-05-14 00:39:53 -070068 @POST
69 @Consumes(MediaType.APPLICATION_JSON)
70 public Response createPolicy(InputStream input) throws IOException {
71 ObjectMapper mapper = new ObjectMapper();
72 ObjectNode policyJson = (ObjectNode) mapper.readTree(input);
73 SegmentRoutingService srService = get(SegmentRoutingService.class);
74 Policy policyInfo = POLICY_CODEC.decode(policyJson, this);
sangho1e575652015-05-14 00:39:53 -070075
sangho0b2b6d12015-05-20 22:16:38 -070076 if (policyInfo.type() == Policy.Type.TUNNEL_FLOW) {
77 srService.createPolicy(policyInfo);
sangho1e575652015-05-14 00:39:53 -070078 return Response.ok().build();
79 } else {
80 return Response.serverError().build();
81 }
82 }
83
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070084 /**
85 * Delete a segment routing policy.
86 *
87 * @param input JSON stream for policy to delete
Jian Lic2a542b2016-05-10 11:48:19 -070088 * @return 204 NO CONTENT if the policy is removed
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070089 * @throws IOException if JSON is invalid
90 */
sangho1e575652015-05-14 00:39:53 -070091 @DELETE
92 @Consumes(MediaType.APPLICATION_JSON)
93 public Response removePolicy(InputStream input) throws IOException {
94 ObjectMapper mapper = new ObjectMapper();
95 ObjectNode policyJson = (ObjectNode) mapper.readTree(input);
96 SegmentRoutingService srService = get(SegmentRoutingService.class);
97 Policy policyInfo = POLICY_CODEC.decode(policyJson, this);
98 // TODO: Check the result
99 srService.removePolicy(policyInfo);
sangho0b2b6d12015-05-20 22:16:38 -0700100
Jian Lic2a542b2016-05-10 11:48:19 -0700101 return Response.noContent().build();
sangho1e575652015-05-14 00:39:53 -0700102 }
103
104}