blob: 1d29594120531297cceb4608b7394694518740ef [file] [log] [blame]
Hyunsun Moon9cf43db2016-02-12 15:59:53 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Hyunsun Moon9cf43db2016-02-12 15:59:53 -08003 *
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.cordvtn.rest;
17
Hyunsun Moon6d247342016-02-12 12:48:47 -080018import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
Hyunsun Moonae39ae82016-02-17 15:02:06 -080020import com.google.common.collect.Maps;
Hyunsun Moon6d247342016-02-12 12:48:47 -080021import org.onlab.packet.IpAddress;
22import org.onlab.packet.MacAddress;
Hyunsun Moon7f4ed9d2016-04-14 16:13:42 -070023import org.onosproject.cordvtn.api.CordVtnService;
Hyunsun Moon6d247342016-02-12 12:48:47 -080024import org.onosproject.net.HostId;
Hyunsun Moon9cf43db2016-02-12 15:59:53 -080025import org.onosproject.rest.AbstractWebResource;
26import org.slf4j.Logger;
27import org.slf4j.LoggerFactory;
28
29import javax.ws.rs.Consumes;
30import javax.ws.rs.DELETE;
31import javax.ws.rs.POST;
32import javax.ws.rs.PUT;
33import javax.ws.rs.Path;
34import javax.ws.rs.PathParam;
35import javax.ws.rs.Produces;
36import javax.ws.rs.core.MediaType;
37import javax.ws.rs.core.Response;
38import java.io.InputStream;
Hyunsun Moonae39ae82016-02-17 15:02:06 -080039import java.util.Map;
Hyunsun Moon6d247342016-02-12 12:48:47 -080040
Hyunsun Moon9cf43db2016-02-12 15:59:53 -080041
42/**
43 * Dummy Neutron ML2 mechanism driver.
Hyunsun Moon6d247342016-02-12 12:48:47 -080044 * It just returns OK for ports resource requests except for the port update.
Hyunsun Moon9cf43db2016-02-12 15:59:53 -080045 */
46@Path("ports")
47public class NeutronMl2PortsWebResource extends AbstractWebResource {
48 protected final Logger log = LoggerFactory.getLogger(getClass());
49 private static final String PORTS_MESSAGE = "Received ports %s";
50
Hyunsun Moon6d247342016-02-12 12:48:47 -080051 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 Moon9cf43db2016-02-12 15:59:53 -080062 @POST
63 @Consumes(MediaType.APPLICATION_JSON)
64 @Produces(MediaType.APPLICATION_JSON)
65 public Response createPorts(InputStream input) {
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -080066 log.trace(String.format(PORTS_MESSAGE, "create"));
Hyunsun Moon9cf43db2016-02-12 15:59:53 -080067 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 Moon6d247342016-02-12 12:48:47 -080076
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 Moonae39ae82016-02-17 15:02:06 -080091 Map<IpAddress, MacAddress> vSgs = Maps.newHashMap();
Hyunsun Moon6d247342016-02-12 12:48:47 -080092 jsonNode.path(ADDRESS_PAIRS).forEach(addrPair -> {
Hyunsun Moonae39ae82016-02-17 15:02:06 -080093 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 Moon6d247342016-02-12 12:48:47 -080096 });
97
98 service.updateVirtualSubscriberGateways(
99 HostId.hostId(mac),
100 name.substring(STAG_BEGIN_INDEX),
Hyunsun Moonae39ae82016-02-17 15:02:06 -0800101 vSgs);
Hyunsun Moon6d247342016-02-12 12:48:47 -0800102 } catch (Exception e) {
103 return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
104 }
105
Hyunsun Moon9cf43db2016-02-12 15:59:53 -0800106 return Response.status(Response.Status.OK).build();
107 }
108
109 @Path("{id}")
110 @DELETE
111 @Produces(MediaType.APPLICATION_JSON)
112 public Response deletePorts(@PathParam("id") String id) {
Hyunsun Moon32f3b8e2016-03-02 19:27:26 -0800113 log.trace(String.format(PORTS_MESSAGE, "delete"));
Hyunsun Moon9cf43db2016-02-12 15:59:53 -0800114 return Response.status(Response.Status.OK).build();
115 }
116}