Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [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.virtualbng; |
| 17 | |
| 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
Pingping Lin | 339cdfb | 2015-06-04 10:02:47 -0700 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 22 | |
| 23 | import java.util.Map; |
| 24 | |
Pingping Lin | d2afaf2 | 2015-06-02 10:46:29 -0700 | [diff] [blame] | 25 | import javax.ws.rs.DELETE; |
Pingping Lin | 339cdfb | 2015-06-04 10:02:47 -0700 | [diff] [blame] | 26 | import javax.ws.rs.GET; |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 27 | import javax.ws.rs.POST; |
| 28 | import javax.ws.rs.Path; |
| 29 | import javax.ws.rs.PathParam; |
Pingping Lin | 339cdfb | 2015-06-04 10:02:47 -0700 | [diff] [blame] | 30 | import javax.ws.rs.Produces; |
| 31 | import javax.ws.rs.core.MediaType; |
| 32 | import javax.ws.rs.core.Response; |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 33 | |
| 34 | import org.onlab.packet.IpAddress; |
Pingping Lin | dead205 | 2015-06-08 16:07:23 -0700 | [diff] [blame] | 35 | import org.onlab.packet.MacAddress; |
Pingping Lin | 339cdfb | 2015-06-04 10:02:47 -0700 | [diff] [blame] | 36 | import org.onosproject.rest.AbstractWebResource; |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 37 | import org.slf4j.Logger; |
| 38 | |
| 39 | /** |
Ray Milkey | f2ab6f3 | 2015-08-20 14:25:51 -0700 | [diff] [blame] | 40 | * REST services to interact with the virtual BNG. |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 41 | */ |
| 42 | @Path("privateip") |
Pingping Lin | 339cdfb | 2015-06-04 10:02:47 -0700 | [diff] [blame] | 43 | public class VbngResource extends AbstractWebResource { |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 44 | |
| 45 | private final Logger log = getLogger(getClass()); |
| 46 | |
Ray Milkey | f2ab6f3 | 2015-08-20 14:25:51 -0700 | [diff] [blame] | 47 | /** |
| 48 | * Create a new virtual BNG connection. |
| 49 | * |
| 50 | * @param privateIp IP Address for the BNG private network |
| 51 | * @param mac MAC address for the host |
| 52 | * @param hostName name of the host |
| 53 | * @return public IP address for the new connection |
| 54 | */ |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 55 | @POST |
Pingping Lin | dead205 | 2015-06-08 16:07:23 -0700 | [diff] [blame] | 56 | @Path("{privateip}/{mac}/{hostname}") |
Pingping Lin | d2afaf2 | 2015-06-02 10:46:29 -0700 | [diff] [blame] | 57 | public String privateIpAddNotification(@PathParam("privateip") |
Pingping Lin | dead205 | 2015-06-08 16:07:23 -0700 | [diff] [blame] | 58 | String privateIp, @PathParam("mac") String mac, |
| 59 | @PathParam("hostname") String hostName) { |
| 60 | |
| 61 | log.info("Received creating vBNG request, " |
| 62 | + "privateIp= {}, mac={}, hostName= {}", |
| 63 | privateIp, mac, hostName); |
| 64 | |
| 65 | if (privateIp == null || mac == null || hostName == null) { |
| 66 | log.info("Parameters can not be null"); |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 67 | return "0"; |
| 68 | } |
Pingping Lin | dead205 | 2015-06-08 16:07:23 -0700 | [diff] [blame] | 69 | |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 70 | IpAddress privateIpAddress = IpAddress.valueOf(privateIp); |
Pingping Lin | dead205 | 2015-06-08 16:07:23 -0700 | [diff] [blame] | 71 | MacAddress hostMacAddress = MacAddress.valueOf(mac); |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 72 | |
| 73 | VbngService vbngService = get(VbngService.class); |
| 74 | |
| 75 | IpAddress publicIpAddress = null; |
Pingping Lin | 99d0b20 | 2015-05-05 14:08:48 -0700 | [diff] [blame] | 76 | // Create a virtual BNG |
Pingping Lin | dead205 | 2015-06-08 16:07:23 -0700 | [diff] [blame] | 77 | publicIpAddress = vbngService.createVbng(privateIpAddress, |
| 78 | hostMacAddress, |
| 79 | hostName); |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 80 | |
| 81 | if (publicIpAddress != null) { |
| 82 | return publicIpAddress.toString(); |
| 83 | } else { |
| 84 | return "0"; |
| 85 | } |
| 86 | } |
Pingping Lin | d2afaf2 | 2015-06-02 10:46:29 -0700 | [diff] [blame] | 87 | |
Ray Milkey | f2ab6f3 | 2015-08-20 14:25:51 -0700 | [diff] [blame] | 88 | /** |
| 89 | * Delete a virtual BNG connection. |
| 90 | * |
| 91 | * @param privateIp IP Address for the BNG private network |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 92 | * @return 200 OK |
Ray Milkey | f2ab6f3 | 2015-08-20 14:25:51 -0700 | [diff] [blame] | 93 | */ |
Pingping Lin | d2afaf2 | 2015-06-02 10:46:29 -0700 | [diff] [blame] | 94 | @DELETE |
| 95 | @Path("{privateip}") |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 96 | public Response privateIpDeleteNotification(@PathParam("privateip") |
Pingping Lin | d2afaf2 | 2015-06-02 10:46:29 -0700 | [diff] [blame] | 97 | String privateIp) { |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 98 | String result; |
Pingping Lin | d2afaf2 | 2015-06-02 10:46:29 -0700 | [diff] [blame] | 99 | if (privateIp == null) { |
| 100 | log.info("Private IP address to delete is null"); |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 101 | result = "0"; |
Pingping Lin | d2afaf2 | 2015-06-02 10:46:29 -0700 | [diff] [blame] | 102 | } |
| 103 | log.info("Received a private IP address : {} to delete", privateIp); |
| 104 | IpAddress privateIpAddress = IpAddress.valueOf(privateIp); |
| 105 | |
| 106 | VbngService vbngService = get(VbngService.class); |
| 107 | |
| 108 | IpAddress assignedPublicIpAddress = null; |
| 109 | // Delete a virtual BNG |
| 110 | assignedPublicIpAddress = vbngService.deleteVbng(privateIpAddress); |
| 111 | |
| 112 | if (assignedPublicIpAddress != null) { |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 113 | result = assignedPublicIpAddress.toString(); |
Pingping Lin | d2afaf2 | 2015-06-02 10:46:29 -0700 | [diff] [blame] | 114 | } else { |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 115 | result = "0"; |
Pingping Lin | d2afaf2 | 2015-06-02 10:46:29 -0700 | [diff] [blame] | 116 | } |
Jian Li | c2a542b | 2016-05-10 11:48:19 -0700 | [diff] [blame] | 117 | return Response.ok().entity(result).build(); |
Pingping Lin | d2afaf2 | 2015-06-02 10:46:29 -0700 | [diff] [blame] | 118 | } |
Pingping Lin | 339cdfb | 2015-06-04 10:02:47 -0700 | [diff] [blame] | 119 | |
Ray Milkey | f2ab6f3 | 2015-08-20 14:25:51 -0700 | [diff] [blame] | 120 | /** |
| 121 | * Query virtual BNG map. |
| 122 | * |
| 123 | * @return IP Address map |
| 124 | */ |
Pingping Lin | 339cdfb | 2015-06-04 10:02:47 -0700 | [diff] [blame] | 125 | @GET |
| 126 | @Path("map") |
| 127 | @Produces(MediaType.APPLICATION_JSON) |
| 128 | public Response privateIpDeleteNotification() { |
| 129 | |
| 130 | log.info("Received vBNG IP address map request"); |
| 131 | |
| 132 | VbngConfigurationService vbngConfigurationService = |
| 133 | get(VbngConfigurationService.class); |
| 134 | |
| 135 | Map<IpAddress, IpAddress> map = |
| 136 | vbngConfigurationService.getIpAddressMappings(); |
| 137 | ObjectNode result = new ObjectMapper().createObjectNode(); |
| 138 | |
| 139 | result.set("map", new IpAddressMapEntryCodec().encode(map.entrySet(), this)); |
| 140 | |
| 141 | return ok(result.toString()).build(); |
| 142 | } |
Ray Milkey | f2ab6f3 | 2015-08-20 14:25:51 -0700 | [diff] [blame] | 143 | } |