sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.segmentrouting.web; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | |
| 21 | import org.onosproject.rest.AbstractWebResource; |
| 22 | import org.onosproject.segmentrouting.Policy; |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 23 | import org.onosproject.segmentrouting.SegmentRoutingService; |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 24 | |
| 25 | import javax.ws.rs.Consumes; |
| 26 | import javax.ws.rs.DELETE; |
| 27 | import javax.ws.rs.GET; |
| 28 | import javax.ws.rs.POST; |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 29 | import javax.ws.rs.Produces; |
| 30 | import javax.ws.rs.core.MediaType; |
| 31 | import javax.ws.rs.core.Response; |
| 32 | import java.io.IOException; |
| 33 | import java.io.InputStream; |
| 34 | import java.util.List; |
| 35 | |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame^] | 36 | import static org.onlab.util.Tools.readTreeFromStream; |
| 37 | |
Ray Milkey | f2ab6f3 | 2015-08-20 14:25:51 -0700 | [diff] [blame] | 38 | /** |
| 39 | * Query, create and remove segment routing plicies. |
| 40 | */ |
Andreas Pantelopoulos | cd33959 | 2018-02-23 14:18:00 -0800 | [diff] [blame] | 41 | // @Path("policy") |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 42 | public class PolicyWebResource extends AbstractWebResource { |
| 43 | |
| 44 | private static final PolicyCodec POLICY_CODEC = new PolicyCodec(); |
| 45 | |
Ray Milkey | f2ab6f3 | 2015-08-20 14:25:51 -0700 | [diff] [blame] | 46 | /** |
| 47 | * Get all segment routing policies. |
| 48 | * Returns an array of segment routing policies. |
| 49 | * |
| 50 | * @return status of OK |
| 51 | */ |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 52 | @GET |
| 53 | @Produces(MediaType.APPLICATION_JSON) |
| 54 | public Response getPolicy() { |
| 55 | SegmentRoutingService srService = get(SegmentRoutingService.class); |
| 56 | List<Policy> policies = srService.getPolicies(); |
| 57 | ObjectNode result = new ObjectMapper().createObjectNode(); |
| 58 | result.set("policy", new PolicyCodec().encode(policies, this)); |
| 59 | |
| 60 | return ok(result.toString()).build(); |
| 61 | } |
| 62 | |
Ray Milkey | f2ab6f3 | 2015-08-20 14:25:51 -0700 | [diff] [blame] | 63 | /** |
| 64 | * Create a new segment routing policy. |
| 65 | * |
| 66 | * @param input JSON stream for policy to create |
| 67 | * @return status of the request - OK if the policy is created, |
Ray Milkey | 9b36d81 | 2015-09-09 15:24:54 -0700 | [diff] [blame] | 68 | * @throws IOException if JSON processing fails |
Ray Milkey | f2ab6f3 | 2015-08-20 14:25:51 -0700 | [diff] [blame] | 69 | */ |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 70 | @POST |
| 71 | @Consumes(MediaType.APPLICATION_JSON) |
| 72 | public Response createPolicy(InputStream input) throws IOException { |
| 73 | ObjectMapper mapper = new ObjectMapper(); |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame^] | 74 | ObjectNode policyJson = readTreeFromStream(mapper, input); |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 75 | SegmentRoutingService srService = get(SegmentRoutingService.class); |
| 76 | Policy policyInfo = POLICY_CODEC.decode(policyJson, this); |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 77 | |
sangho | 0b2b6d1 | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 78 | if (policyInfo.type() == Policy.Type.TUNNEL_FLOW) { |
| 79 | srService.createPolicy(policyInfo); |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 80 | return Response.ok().build(); |
| 81 | } else { |
| 82 | return Response.serverError().build(); |
| 83 | } |
| 84 | } |
| 85 | |
Ray Milkey | f2ab6f3 | 2015-08-20 14:25:51 -0700 | [diff] [blame] | 86 | /** |
| 87 | * Delete a segment routing policy. |
| 88 | * |
| 89 | * @param input JSON stream for policy to delete |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 90 | * @return 204 NO CONTENT if the policy is removed |
Ray Milkey | f2ab6f3 | 2015-08-20 14:25:51 -0700 | [diff] [blame] | 91 | * @throws IOException if JSON is invalid |
| 92 | */ |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 93 | @DELETE |
| 94 | @Consumes(MediaType.APPLICATION_JSON) |
| 95 | public Response removePolicy(InputStream input) throws IOException { |
| 96 | ObjectMapper mapper = new ObjectMapper(); |
Ray Milkey | 86ee5e8 | 2018-04-02 15:33:07 -0700 | [diff] [blame^] | 97 | ObjectNode policyJson = readTreeFromStream(mapper, input); |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 98 | SegmentRoutingService srService = get(SegmentRoutingService.class); |
| 99 | Policy policyInfo = POLICY_CODEC.decode(policyJson, this); |
| 100 | // TODO: Check the result |
| 101 | srService.removePolicy(policyInfo); |
sangho | 0b2b6d1 | 2015-05-20 22:16:38 -0700 | [diff] [blame] | 102 | |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 103 | return Response.noContent().build(); |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | } |