blob: 41bc5ccd0dc7774b124c93a1685efccb2ac548a4 [file] [log] [blame]
Pingping Linffa27d32015-04-30 14:41:03 -07001/*
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 */
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 Lin339cdfb2015-06-04 10:02:47 -070035import org.onosproject.rest.AbstractWebResource;
Pingping Linffa27d32015-04-30 14:41:03 -070036import org.slf4j.Logger;
37
38/**
39 * This class provides REST services to virtual BNG.
40 */
41@Path("privateip")
Pingping Lin339cdfb2015-06-04 10:02:47 -070042public class VbngResource extends AbstractWebResource {
Pingping Linffa27d32015-04-30 14:41:03 -070043
44 private final Logger log = getLogger(getClass());
45
46 @POST
47 @Path("{privateip}")
Pingping Lind2afaf22015-06-02 10:46:29 -070048 public String privateIpAddNotification(@PathParam("privateip")
Pingping Linffa27d32015-04-30 14:41:03 -070049 String privateIp) {
50 if (privateIp == null) {
Pingping Lind2afaf22015-06-02 10:46:29 -070051 log.info("Private IP address to add is null");
Pingping Linffa27d32015-04-30 14:41:03 -070052 return "0";
53 }
Pingping Lind2afaf22015-06-02 10:46:29 -070054 log.info("Received a private IP address : {} to add", privateIp);
Pingping Linffa27d32015-04-30 14:41:03 -070055 IpAddress privateIpAddress = IpAddress.valueOf(privateIp);
56
57 VbngService vbngService = get(VbngService.class);
58
59 IpAddress publicIpAddress = null;
Pingping Lin99d0b202015-05-05 14:08:48 -070060 // Create a virtual BNG
61 publicIpAddress = vbngService.createVbng(privateIpAddress);
Pingping Linffa27d32015-04-30 14:41:03 -070062
63 if (publicIpAddress != null) {
64 return publicIpAddress.toString();
65 } else {
66 return "0";
67 }
68 }
Pingping Lind2afaf22015-06-02 10:46:29 -070069
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 Lin339cdfb2015-06-04 10:02:47 -070093
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 Linffa27d32015-04-30 14:41:03 -0700112}