blob: 51c9cc92ad59799e7d48eedb012103a363c79c89 [file] [log] [blame]
Charles Chanc7b3c452018-06-19 20:31:57 -07001/*
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 */
16package org.onosproject.segmentrouting.web;
17
18import com.fasterxml.jackson.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.onosproject.rest.AbstractWebResource;
21import org.onosproject.segmentrouting.xconnect.api.XconnectDesc;
22import org.onosproject.segmentrouting.xconnect.api.XconnectService;
23import org.slf4j.Logger;
24import org.slf4j.LoggerFactory;
25
26import javax.ws.rs.Consumes;
27import javax.ws.rs.DELETE;
28import javax.ws.rs.GET;
29import javax.ws.rs.POST;
30import javax.ws.rs.Path;
31import javax.ws.rs.Produces;
32import javax.ws.rs.core.MediaType;
33import javax.ws.rs.core.Response;
34import java.io.IOException;
35import java.io.InputStream;
36import java.util.Set;
37
38import static org.onlab.util.Tools.readTreeFromStream;
39
40/**
41 * Query, create and remove Xconnects.
42 */
43@Path("xconnect")
44public 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 Chan16631de2019-01-02 13:46:16 -080078 if (desc.endpoints().size() != 2) {
Charles Chanc7b3c452018-06-19 20:31:57 -070079 throw new IllegalArgumentException("Ports should have only two items.");
80 }
81
82 XconnectService xconnectService = get(XconnectService.class);
Charles Chan16631de2019-01-02 13:46:16 -080083 xconnectService.addOrUpdateXconnect(desc.key().deviceId(), desc.key().vlanId(), desc.endpoints());
Charles Chanc7b3c452018-06-19 20:31:57 -070084
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}