Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.rest; |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 20 | import org.onosproject.net.device.DeviceProviderRegistry; |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 21 | import org.onosproject.net.device.DeviceService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 22 | import org.onosproject.net.host.HostProviderRegistry; |
| 23 | import org.onosproject.net.link.LinkProviderRegistry; |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 24 | import org.onlab.rest.BaseResource; |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 25 | import org.slf4j.Logger; |
| 26 | import org.slf4j.LoggerFactory; |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 27 | |
| 28 | import javax.ws.rs.Consumes; |
| 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 | |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 37 | import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR; |
| 38 | |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 39 | /** |
| 40 | * Resource that acts as an ancillary provider for uploading pre-configured |
| 41 | * devices, ports and links. |
| 42 | */ |
| 43 | @Path("config") |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 44 | public class ConfigWebResource extends BaseResource { |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 45 | |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 46 | private static Logger log = LoggerFactory.getLogger(ConfigWebResource.class); |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 47 | |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 48 | @POST |
| 49 | @Path("topology") |
| 50 | @Consumes(MediaType.APPLICATION_JSON) |
| 51 | @Produces(MediaType.APPLICATION_JSON) |
| 52 | public Response topology(InputStream input) throws IOException { |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 53 | try { |
| 54 | ObjectMapper mapper = new ObjectMapper(); |
| 55 | JsonNode cfg = mapper.readTree(input); |
Thomas Vachuska | c40d463 | 2015-04-09 16:55:03 -0700 | [diff] [blame] | 56 | new ConfigProvider(cfg, get(DeviceService.class), |
| 57 | get(DeviceProviderRegistry.class), |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 58 | get(LinkProviderRegistry.class), |
| 59 | get(HostProviderRegistry.class)).parse(); |
Thomas Vachuska | 3f06e08 | 2014-11-21 11:58:23 -0800 | [diff] [blame] | 60 | return Response.ok().build(); |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 61 | } catch (Exception e) { |
| 62 | log.error("Unable to parse topology configuration", e); |
| 63 | return Response.status(INTERNAL_SERVER_ERROR).entity(e.toString()).build(); |
| 64 | } |
Thomas Vachuska | 9252bc3 | 2014-10-23 02:33:25 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | } |