sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame^] | 1 | /* |
| 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 | */ |
| 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; |
| 23 | import org.onosproject.segmentrouting.SegmentRoutingManager; |
| 24 | import org.onosproject.segmentrouting.SegmentRoutingService; |
| 25 | import org.onosproject.segmentrouting.TunnelPolicy; |
| 26 | |
| 27 | import javax.ws.rs.Consumes; |
| 28 | import javax.ws.rs.DELETE; |
| 29 | import javax.ws.rs.GET; |
| 30 | import javax.ws.rs.POST; |
| 31 | import javax.ws.rs.Path; |
| 32 | import javax.ws.rs.Produces; |
| 33 | import javax.ws.rs.core.MediaType; |
| 34 | import javax.ws.rs.core.Response; |
| 35 | import java.io.IOException; |
| 36 | import java.io.InputStream; |
| 37 | import java.util.List; |
| 38 | |
| 39 | @Path("policy") |
| 40 | public 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 | } |