Pingping Lin
Committed by Gerrit Code Review

add a delete vBNG feature

Change-Id: I21ce31d645be31320d06c36795e6b044f11924ab
......@@ -182,6 +182,27 @@ public class VbngConfigurationManager implements VbngConfigurationService {
return ipAddressMap.containsValue(ipAddress);
}
@Override
public IpAddress recycleAssignedPublicIpAddress(IpAddress
privateIpAddress) {
IpAddress publicIpAddress = ipAddressMap.remove(privateIpAddress);
if (publicIpAddress == null) {
return null;
}
Iterator<Entry<IpPrefix, Boolean>> prefixes =
localPublicIpPrefixes.entrySet().iterator();
while (prefixes.hasNext()) {
Entry<IpPrefix, Boolean> prefixEntry = prefixes.next();
if (prefixEntry.getKey().contains(publicIpAddress)
&& !prefixEntry.getValue()) {
updateIpPrefixStatus(prefixEntry.getKey(), true);
}
}
return publicIpAddress;
}
/**
* Generates a new IP address base on a given IP address plus a number to
* increase.
......
......@@ -61,4 +61,14 @@ public interface VbngConfigurationService {
* @return the assigned public IP address if it exists, otherwise null
*/
IpAddress getAssignedPublicIpAddress(IpAddress privateIpAddress);
/**
* Recycles the public IP address assigned for a private IP address, and
* at the same time deletes the mapping entry from this private IP address
* to the public IP address.
*
* @param privateIpAddress a private IP address
* @return the assigned public IP address if it exists, otherwise null
*/
IpAddress recycleAssignedPublicIpAddress(IpAddress privateIpAddress);
}
......
......@@ -91,7 +91,6 @@ public class VbngManager implements VbngService {
private HostListener hostListener;
private IpAddress nextHopIpAddress;
@Activate
public void activate() {
appId = coreService.registerApplication(APP_NAME);
......@@ -133,6 +132,47 @@ public class VbngManager implements VbngService {
return publicIpAddress;
}
@Override
public IpAddress deleteVbng(IpAddress privateIpAddress) {
// Recycle the public IP address assigned to this private IP address.
// Recycling will also delete the mapping entry from the private IP
// address to public IP address.
IpAddress assignedPublicIpAddress = vbngConfigurationService
.recycleAssignedPublicIpAddress(privateIpAddress);
if (assignedPublicIpAddress == null) {
return null;
}
// Remove the private IP address from privateIpAddressSet
privateIpAddressSet.remove(privateIpAddress);
// Remove intents
removeForwardingPaths(privateIpAddress);
return assignedPublicIpAddress;
}
/**
* Removes the forwarding paths in both two directions between host
* configured with private IP and next hop.
*
* @param privateIp the private IP address of a local host
*/
private void removeForwardingPaths(IpAddress privateIp) {
PointToPointIntent toNextHopIntent =
p2pIntentsFromHost.remove(privateIp);
if (toNextHopIntent != null) {
intentService.withdraw(toNextHopIntent);
//intentService.purge(toNextHopIntent);
}
PointToPointIntent toLocalHostIntent =
p2pIntentsToHost.remove(privateIp);
if (toLocalHostIntent != null) {
intentService.withdraw(toLocalHostIntent);
//intentService.purge(toLocalHostIntent);
}
}
/**
* Sets up forwarding paths in both two directions between host configured
* with private IP and next hop.
......@@ -207,7 +247,7 @@ public class VbngManager implements VbngService {
localHost.mac(),
localHostConnectPoint,
nextHopConnectPoint);
p2pIntentsToHost.put(nextHopIpAddress, toLocalHostIntent);
p2pIntentsToHost.put(privateIp, toLocalHostIntent);
intentService.submit(toLocalHostIntent);
}
......
......@@ -17,6 +17,7 @@ package org.onosproject.virtualbng;
import static org.slf4j.LoggerFactory.getLogger;
import javax.ws.rs.DELETE;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
......@@ -35,13 +36,13 @@ public class VbngResource extends BaseResource {
@POST
@Path("{privateip}")
public String privateIpNotification(@PathParam("privateip")
public String privateIpAddNotification(@PathParam("privateip")
String privateIp) {
if (privateIp == null) {
log.info("Private IP address is null");
log.info("Private IP address to add is null");
return "0";
}
log.info("Received a private IP address : {}", privateIp);
log.info("Received a private IP address : {} to add", privateIp);
IpAddress privateIpAddress = IpAddress.valueOf(privateIp);
VbngService vbngService = get(VbngService.class);
......@@ -56,4 +57,28 @@ public class VbngResource extends BaseResource {
return "0";
}
}
@DELETE
@Path("{privateip}")
public String privateIpDeleteNotification(@PathParam("privateip")
String privateIp) {
if (privateIp == null) {
log.info("Private IP address to delete is null");
return "0";
}
log.info("Received a private IP address : {} to delete", privateIp);
IpAddress privateIpAddress = IpAddress.valueOf(privateIp);
VbngService vbngService = get(VbngService.class);
IpAddress assignedPublicIpAddress = null;
// Delete a virtual BNG
assignedPublicIpAddress = vbngService.deleteVbng(privateIpAddress);
if (assignedPublicIpAddress != null) {
return assignedPublicIpAddress.toString();
} else {
return "0";
}
}
}
\ No newline at end of file
......
......@@ -36,4 +36,13 @@ public interface VbngService {
*/
IpAddress createVbng(IpAddress privateIpAddress);
/**
* Deletes a virtual BNG.
*
* @param privateIpAddress a private IP address
* @return the public address assigned for this private IP address if the
* virtual BGN exists and is deleted, otherwise return null if
* there is no vBNG for this private IP address
*/
IpAddress deleteVbng(IpAddress privateIpAddress);
}
......