blob: 72f2d3d1fd0526f84638ffac6b94367854fb42fd [file] [log] [blame]
Pingping Linffa27d32015-04-30 14:41:03 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Pingping Linffa27d32015-04-30 14:41:03 -07003 *
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.virtualbng;
17
18import static org.slf4j.LoggerFactory.getLogger;
19
Pingping Lin339cdfb2015-06-04 10:02:47 -070020import com.fasterxml.jackson.databind.ObjectMapper;
21import com.fasterxml.jackson.databind.node.ObjectNode;
22
23import java.util.Map;
24
Pingping Lind2afaf22015-06-02 10:46:29 -070025import javax.ws.rs.DELETE;
Pingping Lin339cdfb2015-06-04 10:02:47 -070026import javax.ws.rs.GET;
Pingping Linffa27d32015-04-30 14:41:03 -070027import javax.ws.rs.POST;
28import javax.ws.rs.Path;
29import javax.ws.rs.PathParam;
Pingping Lin339cdfb2015-06-04 10:02:47 -070030import javax.ws.rs.Produces;
31import javax.ws.rs.core.MediaType;
32import javax.ws.rs.core.Response;
Pingping Linffa27d32015-04-30 14:41:03 -070033
34import org.onlab.packet.IpAddress;
Pingping Lindead2052015-06-08 16:07:23 -070035import org.onlab.packet.MacAddress;
Pingping Lin339cdfb2015-06-04 10:02:47 -070036import org.onosproject.rest.AbstractWebResource;
Pingping Linffa27d32015-04-30 14:41:03 -070037import org.slf4j.Logger;
38
39/**
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070040 * REST services to interact with the virtual BNG.
Pingping Linffa27d32015-04-30 14:41:03 -070041 */
42@Path("privateip")
Pingping Lin339cdfb2015-06-04 10:02:47 -070043public class VbngResource extends AbstractWebResource {
Pingping Linffa27d32015-04-30 14:41:03 -070044
45 private final Logger log = getLogger(getClass());
46
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070047 /**
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 Linffa27d32015-04-30 14:41:03 -070055 @POST
Pingping Lindead2052015-06-08 16:07:23 -070056 @Path("{privateip}/{mac}/{hostname}")
Pingping Lind2afaf22015-06-02 10:46:29 -070057 public String privateIpAddNotification(@PathParam("privateip")
Pingping Lindead2052015-06-08 16:07:23 -070058 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 Linffa27d32015-04-30 14:41:03 -070067 return "0";
68 }
Pingping Lindead2052015-06-08 16:07:23 -070069
Pingping Linffa27d32015-04-30 14:41:03 -070070 IpAddress privateIpAddress = IpAddress.valueOf(privateIp);
Pingping Lindead2052015-06-08 16:07:23 -070071 MacAddress hostMacAddress = MacAddress.valueOf(mac);
Pingping Linffa27d32015-04-30 14:41:03 -070072
73 VbngService vbngService = get(VbngService.class);
74
75 IpAddress publicIpAddress = null;
Pingping Lin99d0b202015-05-05 14:08:48 -070076 // Create a virtual BNG
Pingping Lindead2052015-06-08 16:07:23 -070077 publicIpAddress = vbngService.createVbng(privateIpAddress,
78 hostMacAddress,
79 hostName);
Pingping Linffa27d32015-04-30 14:41:03 -070080
81 if (publicIpAddress != null) {
82 return publicIpAddress.toString();
83 } else {
84 return "0";
85 }
86 }
Pingping Lind2afaf22015-06-02 10:46:29 -070087
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070088 /**
89 * Delete a virtual BNG connection.
90 *
91 * @param privateIp IP Address for the BNG private network
Jian Lic2a542b2016-05-10 11:48:19 -070092 * @return 200 OK
Ray Milkeyf2ab6f32015-08-20 14:25:51 -070093 */
Pingping Lind2afaf22015-06-02 10:46:29 -070094 @DELETE
95 @Path("{privateip}")
Jian Lic2a542b2016-05-10 11:48:19 -070096 public Response privateIpDeleteNotification(@PathParam("privateip")
Pingping Lind2afaf22015-06-02 10:46:29 -070097 String privateIp) {
Jian Lic2a542b2016-05-10 11:48:19 -070098 String result;
Pingping Lind2afaf22015-06-02 10:46:29 -070099 if (privateIp == null) {
100 log.info("Private IP address to delete is null");
Jian Lic2a542b2016-05-10 11:48:19 -0700101 result = "0";
Pingping Lind2afaf22015-06-02 10:46:29 -0700102 }
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 Lic2a542b2016-05-10 11:48:19 -0700113 result = assignedPublicIpAddress.toString();
Pingping Lind2afaf22015-06-02 10:46:29 -0700114 } else {
Jian Lic2a542b2016-05-10 11:48:19 -0700115 result = "0";
Pingping Lind2afaf22015-06-02 10:46:29 -0700116 }
Jian Lic2a542b2016-05-10 11:48:19 -0700117 return Response.ok().entity(result).build();
Pingping Lind2afaf22015-06-02 10:46:29 -0700118 }
Pingping Lin339cdfb2015-06-04 10:02:47 -0700119
Ray Milkeyf2ab6f32015-08-20 14:25:51 -0700120 /**
121 * Query virtual BNG map.
122 *
123 * @return IP Address map
124 */
Pingping Lin339cdfb2015-06-04 10:02:47 -0700125 @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 Milkeyf2ab6f32015-08-20 14:25:51 -0700143}