Charles Chan | c7b3c45 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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.xconnect.api.XconnectDesc; |
| 22 | import org.onosproject.segmentrouting.xconnect.api.XconnectService; |
| 23 | import org.slf4j.Logger; |
| 24 | import org.slf4j.LoggerFactory; |
| 25 | |
| 26 | import javax.ws.rs.Consumes; |
| 27 | import javax.ws.rs.DELETE; |
| 28 | import javax.ws.rs.GET; |
| 29 | import javax.ws.rs.POST; |
| 30 | import javax.ws.rs.Path; |
| 31 | import javax.ws.rs.Produces; |
| 32 | import javax.ws.rs.core.MediaType; |
| 33 | import javax.ws.rs.core.Response; |
| 34 | import java.io.IOException; |
| 35 | import java.io.InputStream; |
| 36 | import java.util.Set; |
| 37 | |
| 38 | import static org.onlab.util.Tools.readTreeFromStream; |
| 39 | |
| 40 | /** |
| 41 | * Query, create and remove Xconnects. |
| 42 | */ |
| 43 | @Path("xconnect") |
| 44 | public class XconnectWebResource extends AbstractWebResource { |
| 45 | private static final String XCONNECTS = "xconnects"; |
| 46 | private static Logger log = LoggerFactory.getLogger(XconnectWebResource.class); |
| 47 | |
| 48 | /** |
| 49 | * Gets all Xconnects. |
| 50 | * |
| 51 | * @return an array of xconnects |
| 52 | */ |
| 53 | @GET |
| 54 | @Produces(MediaType.APPLICATION_JSON) |
| 55 | public Response getXconnects() { |
| 56 | XconnectService xconnectService = get(XconnectService.class); |
| 57 | Set<XconnectDesc> xconnects = xconnectService.getXconnects(); |
| 58 | |
| 59 | ObjectNode result = encodeArray(XconnectDesc.class, XCONNECTS, xconnects); |
| 60 | return ok(result).build(); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Create a new Xconnect. |
| 65 | * |
| 66 | * @param input JSON stream for xconnect to create |
| 67 | * @return 200 OK |
| 68 | * @throws IOException Throws IO exception |
| 69 | * @onos.rsModel XconnectCreate |
| 70 | */ |
| 71 | @POST |
| 72 | @Consumes(MediaType.APPLICATION_JSON) |
| 73 | public Response addOrUpdateXconnect(InputStream input) throws IOException { |
| 74 | ObjectMapper mapper = new ObjectMapper(); |
| 75 | ObjectNode json = readTreeFromStream(mapper, input); |
| 76 | XconnectDesc desc = codec(XconnectDesc.class).decode(json, this); |
| 77 | |
Charles Chan | 16631de | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 78 | if (desc.endpoints().size() != 2) { |
Charles Chan | c7b3c45 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 79 | throw new IllegalArgumentException("Ports should have only two items."); |
| 80 | } |
| 81 | |
| 82 | XconnectService xconnectService = get(XconnectService.class); |
Charles Chan | 16631de | 2019-01-02 13:46:16 -0800 | [diff] [blame] | 83 | xconnectService.addOrUpdateXconnect(desc.key().deviceId(), desc.key().vlanId(), desc.endpoints()); |
Charles Chan | c7b3c45 | 2018-06-19 20:31:57 -0700 | [diff] [blame] | 84 | |
| 85 | return Response.ok().build(); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | /** |
| 90 | * Delete an existing Xconnect. |
| 91 | * |
| 92 | * @param input JSON stream for xconnect to remove |
| 93 | * @return 204 NO CONTENT |
| 94 | * @throws IOException Throws IO exception |
| 95 | * @onos.rsModel XconnectDelete |
| 96 | */ |
| 97 | @DELETE |
| 98 | @Consumes(MediaType.APPLICATION_JSON) |
| 99 | public Response removeXconnect(InputStream input) throws IOException { |
| 100 | ObjectMapper mapper = new ObjectMapper(); |
| 101 | ObjectNode json = readTreeFromStream(mapper, input); |
| 102 | XconnectDesc desc = codec(XconnectDesc.class).decode(json, this); |
| 103 | |
| 104 | XconnectService xconnectService = get(XconnectService.class); |
| 105 | xconnectService.removeXonnect(desc.key().deviceId(), desc.key().vlanId()); |
| 106 | |
| 107 | return Response.noContent().build(); |
| 108 | } |
| 109 | } |