blob: bbe53d39964cf85311236905a75ae5328c202e19 [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;
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 Milkeyb784adb2018-04-02 15:33:07 -070037import static org.onlab.util.Tools.readTreeFromStream;
38
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070039/**
40 * Query, create and remove segment routing plicies.
41 */
sangho1e575652015-05-14 00:39:53 -070042@Path("policy")
43public class PolicyWebResource extends AbstractWebResource {
44
45 private static final PolicyCodec POLICY_CODEC = new PolicyCodec();
46
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070047 /**
48 * Get all segment routing policies.
49 * Returns an array of segment routing policies.
50 *
51 * @return status of OK
52 */
sangho1e575652015-05-14 00:39:53 -070053 @GET
54 @Produces(MediaType.APPLICATION_JSON)
55 public Response getPolicy() {
56 SegmentRoutingService srService = get(SegmentRoutingService.class);
57 List<Policy> policies = srService.getPolicies();
58 ObjectNode result = new ObjectMapper().createObjectNode();
59 result.set("policy", new PolicyCodec().encode(policies, this));
60
61 return ok(result.toString()).build();
62 }
63
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070064 /**
65 * Create a new segment routing policy.
66 *
67 * @param input JSON stream for policy to create
68 * @return status of the request - OK if the policy is created,
Ray Milkey9b36d812015-09-09 15:24:54 -070069 * @throws IOException if JSON processing fails
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070070 */
sangho1e575652015-05-14 00:39:53 -070071 @POST
72 @Consumes(MediaType.APPLICATION_JSON)
73 public Response createPolicy(InputStream input) throws IOException {
74 ObjectMapper mapper = new ObjectMapper();
Ray Milkeyb784adb2018-04-02 15:33:07 -070075 ObjectNode policyJson = readTreeFromStream(mapper, input);
sangho1e575652015-05-14 00:39:53 -070076 SegmentRoutingService srService = get(SegmentRoutingService.class);
77 Policy policyInfo = POLICY_CODEC.decode(policyJson, this);
sangho1e575652015-05-14 00:39:53 -070078
sangho0b2b6d12015-05-20 22:16:38 -070079 if (policyInfo.type() == Policy.Type.TUNNEL_FLOW) {
80 srService.createPolicy(policyInfo);
sangho1e575652015-05-14 00:39:53 -070081 return Response.ok().build();
82 } else {
83 return Response.serverError().build();
84 }
85 }
86
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070087 /**
88 * Delete a segment routing policy.
89 *
90 * @param input JSON stream for policy to delete
Jian Lic2a542b2016-05-10 11:48:19 -070091 * @return 204 NO CONTENT if the policy is removed
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070092 * @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();
Ray Milkeyb784adb2018-04-02 15:33:07 -070098 ObjectNode policyJson = readTreeFromStream(mapper, input);
sangho1e575652015-05-14 00:39:53 -070099 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
Jian Lic2a542b2016-05-10 11:48:19 -0700104 return Response.noContent().build();
sangho1e575652015-05-14 00:39:53 -0700105 }
106
107}