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 | */ |
alshabib | b4d3171 | 2016-06-01 18:51:03 -0700 | [diff] [blame] | 16 | package org.opencord.cordvtn.rest; |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 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 | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 21 | import org.onlab.osgi.DefaultServiceDirectory; |
Hyunsun Moon | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 22 | import org.onlab.packet.IpAddress; |
| 23 | import org.onlab.packet.MacAddress; |
alshabib | b4d3171 | 2016-06-01 18:51:03 -0700 | [diff] [blame] | 24 | import org.opencord.cordvtn.impl.service.VsgInstanceHandler; |
Hyunsun Moon | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 25 | import org.onosproject.net.HostId; |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 26 | import org.onosproject.rest.AbstractWebResource; |
| 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
| 30 | import javax.ws.rs.Consumes; |
| 31 | import javax.ws.rs.DELETE; |
| 32 | import javax.ws.rs.POST; |
| 33 | import javax.ws.rs.PUT; |
| 34 | import javax.ws.rs.Path; |
| 35 | import javax.ws.rs.PathParam; |
| 36 | import javax.ws.rs.Produces; |
| 37 | import javax.ws.rs.core.MediaType; |
| 38 | import javax.ws.rs.core.Response; |
| 39 | import java.io.InputStream; |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 40 | import java.util.Map; |
Hyunsun Moon | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 41 | |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Dummy Neutron ML2 mechanism driver. |
Hyunsun Moon | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 45 | * 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] | 46 | */ |
| 47 | @Path("ports") |
| 48 | public class NeutronMl2PortsWebResource extends AbstractWebResource { |
| 49 | protected final Logger log = LoggerFactory.getLogger(getClass()); |
| 50 | private static final String PORTS_MESSAGE = "Received ports %s"; |
| 51 | |
Hyunsun Moon | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 52 | private static final String PORT = "port"; |
| 53 | private static final String DEVICE_ID = "device_id"; |
| 54 | private static final String NAME = "name"; |
| 55 | private static final String MAC_ADDRESS = "mac_address"; |
| 56 | private static final String ADDRESS_PAIRS = "allowed_address_pairs"; |
| 57 | private static final String IP_ADDERSS = "ip_address"; |
| 58 | private static final String STAG_PREFIX = "stag-"; |
| 59 | private static final int STAG_BEGIN_INDEX = 5; |
| 60 | |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 61 | private final VsgInstanceHandler service = DefaultServiceDirectory.getService(VsgInstanceHandler.class); |
Hyunsun Moon | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 62 | |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 63 | @POST |
| 64 | @Consumes(MediaType.APPLICATION_JSON) |
| 65 | @Produces(MediaType.APPLICATION_JSON) |
| 66 | public Response createPorts(InputStream input) { |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 67 | log.trace(String.format(PORTS_MESSAGE, "create")); |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 68 | return Response.status(Response.Status.OK).build(); |
| 69 | } |
| 70 | |
| 71 | @PUT |
| 72 | @Path("{id}") |
| 73 | @Consumes(MediaType.APPLICATION_JSON) |
| 74 | @Produces(MediaType.APPLICATION_JSON) |
| 75 | public Response updatePorts(@PathParam("id") String id, InputStream input) { |
| 76 | log.debug(String.format(PORTS_MESSAGE, "update")); |
Hyunsun Moon | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 77 | |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 78 | // TODO get vSG updates from XOS to CORD VTN service directly |
Hyunsun Moon | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 79 | try { |
| 80 | ObjectMapper mapper = new ObjectMapper(); |
| 81 | JsonNode jsonNode = mapper.readTree(input).get(PORT); |
| 82 | log.trace("{}", jsonNode.toString()); |
| 83 | |
| 84 | String deviceId = jsonNode.path(DEVICE_ID).asText(); |
| 85 | String name = jsonNode.path(NAME).asText(); |
| 86 | if (deviceId.isEmpty() || name.isEmpty() || !name.startsWith(STAG_PREFIX)) { |
| 87 | // ignore all updates other than allowed address pairs |
| 88 | return Response.status(Response.Status.OK).build(); |
| 89 | } |
| 90 | |
| 91 | // this is allowed address pairs updates |
| 92 | MacAddress mac = MacAddress.valueOf(jsonNode.path(MAC_ADDRESS).asText()); |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 93 | Map<IpAddress, MacAddress> vsgInstances = Maps.newHashMap(); |
Hyunsun Moon | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 94 | jsonNode.path(ADDRESS_PAIRS).forEach(addrPair -> { |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 95 | IpAddress pairIp = IpAddress.valueOf(addrPair.path(IP_ADDERSS).asText()); |
| 96 | MacAddress pairMac = MacAddress.valueOf(addrPair.path(MAC_ADDRESS).asText()); |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 97 | vsgInstances.put(pairIp, pairMac); |
Hyunsun Moon | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 98 | }); |
| 99 | |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 100 | service.updateVsgInstances(HostId.hostId(mac), |
| 101 | name.substring(STAG_BEGIN_INDEX), |
| 102 | vsgInstances); |
Hyunsun Moon | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 103 | } catch (Exception e) { |
| 104 | return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); |
| 105 | } |
| 106 | |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 107 | return Response.status(Response.Status.OK).build(); |
| 108 | } |
| 109 | |
| 110 | @Path("{id}") |
| 111 | @DELETE |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 112 | public Response deletePorts(@PathParam("id") String id) { |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 113 | log.trace(String.format(PORTS_MESSAGE, "delete")); |
Jian Li | 1c64601 | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 114 | return Response.noContent().build(); |
Hyunsun Moon | b6febbe | 2016-02-12 15:59:53 -0800 | [diff] [blame] | 115 | } |
| 116 | } |