blob: 560d01db97b2981397fa133f98abf2f0182b1836 [file] [log] [blame]
Hyunsun Moonb6febbe2016-02-12 15:59:53 -08001/*
Brian O'Connor8e57fd52016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Hyunsun Moonb6febbe2016-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 */
alshabibb4d31712016-06-01 18:51:03 -070016package org.opencord.cordvtn.rest;
Hyunsun Moonb6febbe2016-02-12 15:59:53 -080017
Hyunsun Moon01556a52016-02-12 12:48:47 -080018import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
Hyunsun Moonb5f92e52016-02-17 15:02:06 -080020import com.google.common.collect.Maps;
Hyunsun Moone7e4bb32016-05-16 04:32:45 -070021import org.onlab.osgi.DefaultServiceDirectory;
Hyunsun Moon01556a52016-02-12 12:48:47 -080022import org.onlab.packet.IpAddress;
23import org.onlab.packet.MacAddress;
Hyunsun Moon5401aaa2016-06-12 17:40:34 -070024import org.opencord.cordvtn.impl.handler.VsgInstanceHandler;
Hyunsun Moon01556a52016-02-12 12:48:47 -080025import org.onosproject.net.HostId;
Hyunsun Moonb6febbe2016-02-12 15:59:53 -080026import org.onosproject.rest.AbstractWebResource;
27import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
30import javax.ws.rs.Consumes;
31import javax.ws.rs.DELETE;
32import javax.ws.rs.POST;
33import javax.ws.rs.PUT;
34import javax.ws.rs.Path;
35import javax.ws.rs.PathParam;
36import javax.ws.rs.Produces;
37import javax.ws.rs.core.MediaType;
38import javax.ws.rs.core.Response;
39import java.io.InputStream;
Hyunsun Moonb5f92e52016-02-17 15:02:06 -080040import java.util.Map;
Hyunsun Moon01556a52016-02-12 12:48:47 -080041
Hyunsun Moonb6febbe2016-02-12 15:59:53 -080042
43/**
44 * Dummy Neutron ML2 mechanism driver.
Hyunsun Moon01556a52016-02-12 12:48:47 -080045 * It just returns OK for ports resource requests except for the port update.
Hyunsun Moonb6febbe2016-02-12 15:59:53 -080046 */
47@Path("ports")
48public class NeutronMl2PortsWebResource extends AbstractWebResource {
49 protected final Logger log = LoggerFactory.getLogger(getClass());
50 private static final String PORTS_MESSAGE = "Received ports %s";
51
Hyunsun Moon01556a52016-02-12 12:48:47 -080052 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 Moone7e4bb32016-05-16 04:32:45 -070061 private final VsgInstanceHandler service = DefaultServiceDirectory.getService(VsgInstanceHandler.class);
Hyunsun Moon01556a52016-02-12 12:48:47 -080062
Hyunsun Moonb6febbe2016-02-12 15:59:53 -080063 @POST
64 @Consumes(MediaType.APPLICATION_JSON)
65 @Produces(MediaType.APPLICATION_JSON)
66 public Response createPorts(InputStream input) {
Hyunsun Moond05b32e2016-03-02 19:27:26 -080067 log.trace(String.format(PORTS_MESSAGE, "create"));
Hyunsun Moonb6febbe2016-02-12 15:59:53 -080068 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 Moon01556a52016-02-12 12:48:47 -080077
Hyunsun Moone7e4bb32016-05-16 04:32:45 -070078 // TODO get vSG updates from XOS to CORD VTN service directly
Hyunsun Moon01556a52016-02-12 12:48:47 -080079 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 Moone7e4bb32016-05-16 04:32:45 -070093 Map<IpAddress, MacAddress> vsgInstances = Maps.newHashMap();
Hyunsun Moon01556a52016-02-12 12:48:47 -080094 jsonNode.path(ADDRESS_PAIRS).forEach(addrPair -> {
Hyunsun Moonb5f92e52016-02-17 15:02:06 -080095 IpAddress pairIp = IpAddress.valueOf(addrPair.path(IP_ADDERSS).asText());
96 MacAddress pairMac = MacAddress.valueOf(addrPair.path(MAC_ADDRESS).asText());
Hyunsun Moone7e4bb32016-05-16 04:32:45 -070097 vsgInstances.put(pairIp, pairMac);
Hyunsun Moon01556a52016-02-12 12:48:47 -080098 });
99
Hyunsun Moone7e4bb32016-05-16 04:32:45 -0700100 service.updateVsgInstances(HostId.hostId(mac),
101 name.substring(STAG_BEGIN_INDEX),
102 vsgInstances);
Hyunsun Moon01556a52016-02-12 12:48:47 -0800103 } catch (Exception e) {
104 return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
105 }
106
Hyunsun Moonb6febbe2016-02-12 15:59:53 -0800107 return Response.status(Response.Status.OK).build();
108 }
109
110 @Path("{id}")
111 @DELETE
Hyunsun Moonb6febbe2016-02-12 15:59:53 -0800112 public Response deletePorts(@PathParam("id") String id) {
Hyunsun Moond05b32e2016-03-02 19:27:26 -0800113 log.trace(String.format(PORTS_MESSAGE, "delete"));
Jian Li1c646012016-05-10 11:48:19 -0700114 return Response.noContent().build();
Hyunsun Moonb6febbe2016-02-12 15:59:53 -0800115 }
116}