blob: cd8f5558ec89b827f8f46c406edf8cd39f0055f0 [file] [log] [blame]
Hyunsun Moon9cf43db2016-02-12 15:59:53 -08001/*
2 * Copyright 2016 Open Networking Laboratory
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.cordvtn.rest;
17
Hyunsun Moon6d247342016-02-12 12:48:47 -080018import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.google.common.collect.Sets;
21import org.onlab.packet.IpAddress;
22import org.onlab.packet.MacAddress;
23import org.onosproject.cordvtn.CordVtnService;
24import 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 Moon6d247342016-02-12 12:48:47 -080039import java.util.Set;
40
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) {
66 log.debug(String.format(PORTS_MESSAGE, "create"));
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 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());
91 Set<IpAddress> vSgIps = Sets.newHashSet();
92 jsonNode.path(ADDRESS_PAIRS).forEach(addrPair -> {
93 IpAddress ip = IpAddress.valueOf(addrPair.path(IP_ADDERSS).asText());
94 vSgIps.add(ip);
95 });
96
97 service.updateVirtualSubscriberGateways(
98 HostId.hostId(mac),
99 name.substring(STAG_BEGIN_INDEX),
100 vSgIps);
101 } catch (Exception e) {
102 return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
103 }
104
Hyunsun Moon9cf43db2016-02-12 15:59:53 -0800105 return Response.status(Response.Status.OK).build();
106 }
107
108 @Path("{id}")
109 @DELETE
110 @Produces(MediaType.APPLICATION_JSON)
111 public Response deletePorts(@PathParam("id") String id) {
112 log.debug(String.format(PORTS_MESSAGE, "delete"));
113 return Response.status(Response.Status.OK).build();
114 }
115}