Kyuhwi Choi
Committed by Gerrit Code Review

[ONOS-3952] Implement FloatingIP Handler for OpenstackRoutingService

 - Implements floatingIp REST interfaces & event handler
 - Implements rulePopulate method for floatingIp handler
 - Fixes minor logics
 - Changes app structure
 - exports configuration
 - Implements case issue.
   when openstack deletes vm w/o deassociating floatingIp,
   openstack doesn`t send floatingIp deassociation event.

Change-Id: If4d8ac3fecfed1957d84139f94ae31f593a9097b
......@@ -80,5 +80,11 @@ public interface OpenstackRoutingService {
*/
void removeRouterInterface(OpenstackRouterInterface openstackRouterInterface);
/**
* Checks floatingIp deassociation when corresponding deleted vm.
*
* @param portId Deleted vm
* @param portInfo
*/
void checkDisassociatedFloatingIp(String portId, OpenstackPortInfo portInfo);
}
......
......@@ -15,20 +15,34 @@
*/
package org.onosproject.openstacknetworking.routing;
import org.onosproject.event.AbstractEvent;
import org.onosproject.openstackinterface.OpenstackFloatingIP;
import org.onosproject.openstacknetworking.OpenstackPortInfo;
/**
* Handle FloatingIP Event for Managing Flow Rules In Openstack Nodes.
*/
public class OpenstackFloatingIPHandler implements Runnable {
volatile AbstractEvent event;
OpenstackFloatingIPHandler(AbstractEvent event) {
this.event = event;
private final OpenstackFloatingIP floatingIP;
private final OpenstackRoutingRulePopulator rulePopulator;
private boolean associate;
private final OpenstackPortInfo portInfo;
OpenstackFloatingIPHandler(OpenstackRoutingRulePopulator rulePopulator,
OpenstackFloatingIP openstackFloatingIP, boolean associate, OpenstackPortInfo portInfo) {
this.floatingIP = openstackFloatingIP;
this.rulePopulator = rulePopulator;
this.associate = associate;
this.portInfo = portInfo;
}
@Override
public void run() {
if (associate) {
rulePopulator.populateFloatingIpRules(floatingIP);
} else {
rulePopulator.removeFloatingIpRules(floatingIP, portInfo);
}
}
}
......
......@@ -56,19 +56,18 @@ public class OpenstackPnatHandler implements Runnable {
private final int portNum;
private final OpenstackPort openstackPort;
private final Port port;
private OpenstackRoutingConfig config;
private static final String DEVICE_OWNER_ROUTER_INTERFACE = "network:router_interface";
// TODO: This will be replaced to get the information from openstacknetworkingservice.
private static final MacAddress GATEWAYMAC = MacAddress.valueOf("1f:1f:1f:1f:1f:1f");
private static final MacAddress EXTERNAL_INTERFACE_MAC = MacAddress.valueOf("00:00:00:00:00:11");
OpenstackPnatHandler(OpenstackRoutingRulePopulator rulePopulator, PacketContext context,
int portNum, OpenstackPort openstackPort, Port port) {
int portNum, OpenstackPort openstackPort, Port port, OpenstackRoutingConfig config) {
this.rulePopulator = checkNotNull(rulePopulator);
this.context = checkNotNull(context);
this.portNum = checkNotNull(portNum);
this.openstackPort = checkNotNull(openstackPort);
this.port = checkNotNull(port);
this.config = checkNotNull(config);
}
@Override
......@@ -85,7 +84,8 @@ public class OpenstackPnatHandler implements Runnable {
OpenstackRouter router = getOpenstackRouter(openstackPort);
rulePopulator.populatePnatFlowRules(inboundPacket, openstackPort, portNum,
getExternalIp(router), getExternalInterfaceMacAddress(), getExternalRouterMacAddress());
getExternalIp(router), MacAddress.valueOf(config.gatewayExternalInterfaceMac()),
MacAddress.valueOf(config.physicalRouterMac()));
packetOut((Ethernet) ethernet.clone(), inboundPacket.receivedFrom().deviceId(), portNum, router);
}
......@@ -144,8 +144,9 @@ public class OpenstackPnatHandler implements Runnable {
iPacket.setSourceAddress(getExternalIp(router).toString());
iPacket.resetChecksum();
iPacket.setParent(ethernet);
ethernet.setSourceMACAddress(getExternalInterfaceMacAddress())
.setDestinationMACAddress(getExternalRouterMacAddress());
ethernet.setPayload(iPacket);
ethernet.setSourceMACAddress(config.gatewayExternalInterfaceMac())
.setDestinationMACAddress(config.physicalRouterMac());
ethernet.resetChecksum();
treatment.setOutput(port.number());
......@@ -153,11 +154,4 @@ public class OpenstackPnatHandler implements Runnable {
packetService.emit(new DefaultOutboundPacket(deviceId, treatment.build(),
ByteBuffer.wrap(ethernet.serialize())));
}
private MacAddress getExternalInterfaceMacAddress() {
return EXTERNAL_INTERFACE_MAC;
}
private MacAddress getExternalRouterMacAddress() {
return GATEWAYMAC;
}
}
\ No newline at end of file
......
......@@ -19,6 +19,8 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.onosproject.openstackinterface.OpenstackPort;
import org.onosproject.openstackinterface.web.OpenstackPortCodec;
import org.onosproject.openstacknetworking.OpenstackPortInfo;
import org.onosproject.openstacknetworking.OpenstackRoutingService;
import org.onosproject.openstacknetworking.OpenstackSwitchingService;
import org.onosproject.rest.AbstractWebResource;
import org.slf4j.Logger;
......@@ -42,7 +44,7 @@ import java.io.InputStream;
public class OpenstackPortWebResource extends AbstractWebResource {
private final Logger log = LoggerFactory.getLogger(getClass());
private static final String PORTNAME_PREFIX_VM = "tap";
private static final OpenstackPortCodec PORT_CODEC = new OpenstackPortCodec();
@POST
......@@ -75,7 +77,14 @@ public class OpenstackPortWebResource extends AbstractWebResource {
public Response deletePorts(@PathParam("portUUID") String id) {
OpenstackSwitchingService switchingService =
getService(OpenstackSwitchingService.class);
OpenstackPortInfo portInfo = switchingService.openstackPortInfo()
.get(PORTNAME_PREFIX_VM.concat(id.substring(0, 11)));
OpenstackRoutingService routingService =
getService(OpenstackRoutingService.class);
routingService.checkDisassociatedFloatingIp(id, portInfo);
switchingService.removePort(id);
return Response.status(Response.Status.OK).build();
}
......