Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | */ |
| 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 | 339cdfb | 2015-06-04 10:02:47 -0700 | [diff] [blame^] | 35 | import org.onosproject.rest.AbstractWebResource; |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 36 | import org.slf4j.Logger; |
| 37 | |
| 38 | /** |
| 39 | * This class provides REST services to virtual BNG. |
| 40 | */ |
| 41 | @Path("privateip") |
Pingping Lin | 339cdfb | 2015-06-04 10:02:47 -0700 | [diff] [blame^] | 42 | public class VbngResource extends AbstractWebResource { |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 43 | |
| 44 | private final Logger log = getLogger(getClass()); |
| 45 | |
| 46 | @POST |
| 47 | @Path("{privateip}") |
Pingping Lin | d2afaf2 | 2015-06-02 10:46:29 -0700 | [diff] [blame] | 48 | public String privateIpAddNotification(@PathParam("privateip") |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 49 | String privateIp) { |
| 50 | if (privateIp == null) { |
Pingping Lin | d2afaf2 | 2015-06-02 10:46:29 -0700 | [diff] [blame] | 51 | log.info("Private IP address to add is null"); |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 52 | return "0"; |
| 53 | } |
Pingping Lin | d2afaf2 | 2015-06-02 10:46:29 -0700 | [diff] [blame] | 54 | log.info("Received a private IP address : {} to add", privateIp); |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 55 | IpAddress privateIpAddress = IpAddress.valueOf(privateIp); |
| 56 | |
| 57 | VbngService vbngService = get(VbngService.class); |
| 58 | |
| 59 | IpAddress publicIpAddress = null; |
Pingping Lin | 99d0b20 | 2015-05-05 14:08:48 -0700 | [diff] [blame] | 60 | // Create a virtual BNG |
| 61 | publicIpAddress = vbngService.createVbng(privateIpAddress); |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 62 | |
| 63 | if (publicIpAddress != null) { |
| 64 | return publicIpAddress.toString(); |
| 65 | } else { |
| 66 | return "0"; |
| 67 | } |
| 68 | } |
Pingping Lin | d2afaf2 | 2015-06-02 10:46:29 -0700 | [diff] [blame] | 69 | |
| 70 | @DELETE |
| 71 | @Path("{privateip}") |
| 72 | public String privateIpDeleteNotification(@PathParam("privateip") |
| 73 | String privateIp) { |
| 74 | if (privateIp == null) { |
| 75 | log.info("Private IP address to delete is null"); |
| 76 | return "0"; |
| 77 | } |
| 78 | log.info("Received a private IP address : {} to delete", privateIp); |
| 79 | IpAddress privateIpAddress = IpAddress.valueOf(privateIp); |
| 80 | |
| 81 | VbngService vbngService = get(VbngService.class); |
| 82 | |
| 83 | IpAddress assignedPublicIpAddress = null; |
| 84 | // Delete a virtual BNG |
| 85 | assignedPublicIpAddress = vbngService.deleteVbng(privateIpAddress); |
| 86 | |
| 87 | if (assignedPublicIpAddress != null) { |
| 88 | return assignedPublicIpAddress.toString(); |
| 89 | } else { |
| 90 | return "0"; |
| 91 | } |
| 92 | } |
Pingping Lin | 339cdfb | 2015-06-04 10:02:47 -0700 | [diff] [blame^] | 93 | |
| 94 | @GET |
| 95 | @Path("map") |
| 96 | @Produces(MediaType.APPLICATION_JSON) |
| 97 | public Response privateIpDeleteNotification() { |
| 98 | |
| 99 | log.info("Received vBNG IP address map request"); |
| 100 | |
| 101 | VbngConfigurationService vbngConfigurationService = |
| 102 | get(VbngConfigurationService.class); |
| 103 | |
| 104 | Map<IpAddress, IpAddress> map = |
| 105 | vbngConfigurationService.getIpAddressMappings(); |
| 106 | ObjectNode result = new ObjectMapper().createObjectNode(); |
| 107 | |
| 108 | result.set("map", new IpAddressMapEntryCodec().encode(map.entrySet(), this)); |
| 109 | |
| 110 | return ok(result.toString()).build(); |
| 111 | } |
Pingping Lin | ffa27d3 | 2015-04-30 14:41:03 -0700 | [diff] [blame] | 112 | } |