Andreas Pantelopoulos | cd33959 | 2018-02-23 14:18:00 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015-present Open Networking Foundation |
| 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 | import org.onosproject.rest.AbstractWebResource; |
| 21 | import org.onosproject.segmentrouting.SegmentRoutingService; |
| 22 | import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelDescription; |
| 23 | import org.onosproject.segmentrouting.pwaas.L2TunnelPolicy; |
| 24 | import org.onosproject.segmentrouting.pwaas.L2Tunnel; |
| 25 | import org.onosproject.segmentrouting.pwaas.L2TunnelHandler; |
| 26 | import org.slf4j.Logger; |
| 27 | import org.slf4j.LoggerFactory; |
| 28 | |
| 29 | import javax.ws.rs.Consumes; |
| 30 | import javax.ws.rs.DELETE; |
| 31 | import javax.ws.rs.GET; |
| 32 | import javax.ws.rs.POST; |
| 33 | import javax.ws.rs.Path; |
| 34 | import javax.ws.rs.Produces; |
| 35 | import javax.ws.rs.core.MediaType; |
| 36 | import javax.ws.rs.core.Response; |
| 37 | import java.io.IOException; |
| 38 | import java.io.InputStream; |
| 39 | import java.util.List; |
| 40 | import java.util.stream.Collectors; |
| 41 | |
| 42 | /** |
| 43 | * Query, create and remove pseudowires. |
| 44 | */ |
| 45 | @Path("pseudowire") |
| 46 | public class PseudowireWebResource extends AbstractWebResource { |
| 47 | |
| 48 | private static final PseudowireCodec PSEUDOWIRE_CODEC = new PseudowireCodec(); |
| 49 | |
| 50 | private static Logger log = LoggerFactory |
| 51 | .getLogger(PseudowireWebResource.class); |
| 52 | |
| 53 | /** |
| 54 | * Get all pseudowires. |
| 55 | * Returns an array of pseudowires. |
| 56 | * |
| 57 | * @return status of OK |
| 58 | */ |
| 59 | @GET |
| 60 | @Produces(MediaType.APPLICATION_JSON) |
| 61 | public Response getPseudowire() { |
| 62 | SegmentRoutingService srService = get(SegmentRoutingService.class); |
| 63 | |
| 64 | log.debug("Fetching pseudowires form rest api!"); |
| 65 | |
| 66 | List<L2TunnelPolicy> policies = srService.getL2Policies(); |
| 67 | List<L2Tunnel> tunnels = srService.getL2Tunnels(); |
| 68 | List<DefaultL2TunnelDescription> pseudowires = tunnels.stream() |
| 69 | .map(l2Tunnel -> { |
| 70 | L2TunnelPolicy policy = null; |
| 71 | for (L2TunnelPolicy l2Policy : policies) { |
| 72 | if (l2Policy.tunnelId() == l2Tunnel.tunnelId()) { |
| 73 | policy = l2Policy; |
| 74 | break; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // return a copy |
| 79 | return new DefaultL2TunnelDescription(l2Tunnel, policy); |
| 80 | }) |
| 81 | .collect(Collectors.toList()); |
| 82 | |
| 83 | ObjectNode result = new ObjectMapper().createObjectNode(); |
| 84 | result.set("pseudowires", new PseudowireCodec().encode(pseudowires, this)); |
| 85 | |
| 86 | return ok(result.toString()).build(); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Create a new pseudowire. |
| 91 | * |
| 92 | * @param input JSON stream for pseudowire to create |
| 93 | * @return Response with appropriate status |
| 94 | * @throws IOException Throws IO exception. |
| 95 | * @onos.rsModel PseudowireCreate |
| 96 | */ |
| 97 | @POST |
| 98 | @Consumes(MediaType.APPLICATION_JSON) |
| 99 | public Response createPseudowire(InputStream input) throws IOException { |
| 100 | |
| 101 | ObjectMapper mapper = new ObjectMapper(); |
| 102 | ObjectNode pseudowireJson = (ObjectNode) mapper.readTree(input); |
| 103 | SegmentRoutingService srService = get(SegmentRoutingService.class); |
| 104 | |
| 105 | DefaultL2TunnelDescription pseudowire = PSEUDOWIRE_CODEC.decode(pseudowireJson, this); |
| 106 | if (pseudowire == null) { |
| 107 | return Response.serverError().status(Response.Status.BAD_REQUEST).build(); |
| 108 | } |
| 109 | |
| 110 | log.info("Creating pseudowire {} from rest api!", pseudowire.l2Tunnel().tunnelId()); |
| 111 | |
| 112 | L2TunnelHandler.Result res = srService.addPseudowire(pseudowire); |
| 113 | switch (res) { |
| 114 | case ADDITION_ERROR: |
| 115 | log.error("Pseudowire {} could not be added, error in configuration," + |
| 116 | " please check logs for more details!", |
| 117 | pseudowire.l2Tunnel().tunnelId()); |
| 118 | return Response.serverError().status(Response.Status.INTERNAL_SERVER_ERROR).build(); |
| 119 | |
| 120 | case SUCCESS: |
| 121 | log.info("Pseudowire {} succesfully deployed!", pseudowire.l2Tunnel().tunnelId()); |
| 122 | return Response.ok().build(); |
| 123 | default: |
| 124 | return Response.ok().build(); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Delete a pseudowire. |
| 130 | * |
| 131 | * @param input JSON stream for pseudowire to delete |
| 132 | * @return Response with appropriate status |
| 133 | * @throws IOException Throws IO exception. |
| 134 | * @onos.rsModel PseudowireDelete |
| 135 | */ |
| 136 | @DELETE |
| 137 | @Consumes(MediaType.APPLICATION_JSON) |
| 138 | public Response removePseudowire(InputStream input) throws IOException { |
| 139 | |
| 140 | ObjectMapper mapper = new ObjectMapper(); |
| 141 | ObjectNode pseudowireJson = (ObjectNode) mapper.readTree(input); |
| 142 | SegmentRoutingService srService = get(SegmentRoutingService.class); |
| 143 | |
| 144 | Integer pseudowireId = PSEUDOWIRE_CODEC.decodeId(pseudowireJson); |
| 145 | if (pseudowireId == null) { |
| 146 | return Response.serverError().status(Response.Status.BAD_REQUEST).build(); |
| 147 | } |
| 148 | |
| 149 | log.info("Deleting pseudowire {} from rest api!", pseudowireId); |
| 150 | |
| 151 | L2TunnelHandler.Result res = srService.removePseudowire(pseudowireId); |
| 152 | switch (res) { |
| 153 | case REMOVAL_ERROR: |
| 154 | log.error("Pseudowire {} could not be removed, error in configuration," + |
| 155 | " please check logs for more details!", |
| 156 | pseudowireId); |
| 157 | |
| 158 | return Response.noContent().build(); |
| 159 | case SUCCESS: |
| 160 | log.info("Pseudowire {} was removed succesfully!", pseudowireId); |
| 161 | return Response.noContent().build(); |
| 162 | default: |
| 163 | return Response.noContent().build(); |
| 164 | } |
| 165 | } |
| 166 | } |