Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 8e57fd5 | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 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.cordvtn.rest; |
| 17 | |
Hyunsun Moon | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 20 | import com.google.common.collect.Maps; |
Hyunsun Moon | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 21 | import org.onlab.packet.IpAddress; |
| 22 | import org.onlab.packet.MacAddress; |
Hyunsun Moon | f948020 | 2016-04-14 16:13:42 -0700 | [diff] [blame] | 23 | import org.onosproject.cordvtn.api.CordVtnService; |
Hyunsun Moon | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 24 | import org.onosproject.net.HostId; |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 25 | import org.onosproject.rest.AbstractWebResource; |
| 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.POST; |
| 32 | import javax.ws.rs.PUT; |
| 33 | import javax.ws.rs.Path; |
| 34 | import javax.ws.rs.PathParam; |
| 35 | import javax.ws.rs.Produces; |
| 36 | import javax.ws.rs.core.MediaType; |
| 37 | import javax.ws.rs.core.Response; |
| 38 | import java.io.InputStream; |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 39 | import java.util.Map; |
Hyunsun Moon | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 40 | |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * Dummy Neutron ML2 mechanism driver. |
Hyunsun Moon | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 44 | * It just returns OK for ports resource requests except for the port update. |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 45 | */ |
| 46 | @Path("ports") |
| 47 | public class NeutronMl2PortsWebResource extends AbstractWebResource { |
| 48 | protected final Logger log = LoggerFactory.getLogger(getClass()); |
| 49 | private static final String PORTS_MESSAGE = "Received ports %s"; |
| 50 | |
Hyunsun Moon | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 51 | private static final String PORT = "port"; |
| 52 | private static final String DEVICE_ID = "device_id"; |
| 53 | private static final String NAME = "name"; |
| 54 | private static final String MAC_ADDRESS = "mac_address"; |
| 55 | private static final String ADDRESS_PAIRS = "allowed_address_pairs"; |
| 56 | private static final String IP_ADDERSS = "ip_address"; |
| 57 | private static final String STAG_PREFIX = "stag-"; |
| 58 | private static final int STAG_BEGIN_INDEX = 5; |
| 59 | |
| 60 | private final CordVtnService service = get(CordVtnService.class); |
| 61 | |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 62 | @POST |
| 63 | @Consumes(MediaType.APPLICATION_JSON) |
| 64 | @Produces(MediaType.APPLICATION_JSON) |
| 65 | public Response createPorts(InputStream input) { |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 66 | log.trace(String.format(PORTS_MESSAGE, "create")); |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 67 | return Response.status(Response.Status.OK).build(); |
| 68 | } |
| 69 | |
| 70 | @PUT |
| 71 | @Path("{id}") |
| 72 | @Consumes(MediaType.APPLICATION_JSON) |
| 73 | @Produces(MediaType.APPLICATION_JSON) |
| 74 | public Response updatePorts(@PathParam("id") String id, InputStream input) { |
| 75 | log.debug(String.format(PORTS_MESSAGE, "update")); |
Hyunsun Moon | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 76 | |
| 77 | try { |
| 78 | ObjectMapper mapper = new ObjectMapper(); |
| 79 | JsonNode jsonNode = mapper.readTree(input).get(PORT); |
| 80 | log.trace("{}", jsonNode.toString()); |
| 81 | |
| 82 | String deviceId = jsonNode.path(DEVICE_ID).asText(); |
| 83 | String name = jsonNode.path(NAME).asText(); |
| 84 | if (deviceId.isEmpty() || name.isEmpty() || !name.startsWith(STAG_PREFIX)) { |
| 85 | // ignore all updates other than allowed address pairs |
| 86 | return Response.status(Response.Status.OK).build(); |
| 87 | } |
| 88 | |
| 89 | // this is allowed address pairs updates |
| 90 | MacAddress mac = MacAddress.valueOf(jsonNode.path(MAC_ADDRESS).asText()); |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 91 | Map<IpAddress, MacAddress> vSgs = Maps.newHashMap(); |
Hyunsun Moon | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 92 | jsonNode.path(ADDRESS_PAIRS).forEach(addrPair -> { |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 93 | IpAddress pairIp = IpAddress.valueOf(addrPair.path(IP_ADDERSS).asText()); |
| 94 | MacAddress pairMac = MacAddress.valueOf(addrPair.path(MAC_ADDRESS).asText()); |
| 95 | vSgs.put(pairIp, pairMac); |
Hyunsun Moon | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 96 | }); |
| 97 | |
| 98 | service.updateVirtualSubscriberGateways( |
| 99 | HostId.hostId(mac), |
| 100 | name.substring(STAG_BEGIN_INDEX), |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 101 | vSgs); |
Hyunsun Moon | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 102 | } catch (Exception e) { |
| 103 | return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); |
| 104 | } |
| 105 | |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 106 | return Response.status(Response.Status.OK).build(); |
| 107 | } |
| 108 | |
| 109 | @Path("{id}") |
| 110 | @DELETE |
Hyunsun Moon | 27fc3f7 | 2016-04-28 17:33:41 -0500 | [diff] [blame^] | 111 | @Consumes(MediaType.APPLICATION_JSON) |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 112 | @Produces(MediaType.APPLICATION_JSON) |
| 113 | public Response deletePorts(@PathParam("id") String id) { |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 114 | log.trace(String.format(PORTS_MESSAGE, "delete")); |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 115 | return Response.status(Response.Status.OK).build(); |
| 116 | } |
| 117 | } |