Skip to content
This repository has been archived by the owner on Mar 31, 2023. It is now read-only.

Commit

Permalink
[Hotfix] Short-term Solution to GW Port Issues (#504)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-zhonghao authored Dec 4, 2020
1 parent 053018d commit 977ac09
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ private void allocateRandomIpCallback(IRestRequest request) throws Exception {
subnetIds.add(fixedIp.getSubnetId());
}

//getSubnetAndRoute(context, new ArrayList<>(subnetIds));
getSubnetAndRoute(context, new ArrayList<>(subnetIds));
}

private void allocateFixedIpsProcess(PortContext context, List<PortEntity> portEntities, CallbackFunction fetchSubnetCallback) {
Expand All @@ -297,9 +297,7 @@ private void allocateFixedIpsProcess(PortContext context, List<PortEntity> portE

for (PortEntity portEntity: portEntities){
List<PortEntity.FixedIp> fixedIps = portEntity.getFixedIps();
if (portEntity.isGatewayPort()) {
continue;
}

if (fixedIps != null) {
for (PortEntity.FixedIp fixedIp: fixedIps) {
if (fixedIp.getSubnetId() != null) {
Expand Down Expand Up @@ -330,13 +328,13 @@ private void allocateFixedIpsProcess(PortContext context, List<PortEntity> portE
context.setHasSubnetFixedIps(hasSubnetFixedIps);
context.setUnassignedIpPorts(noAssignedIpPorts);

if (subnetIds.size() > 0) {
//Get subnet
getSubnetEntities(context, new ArrayList<>(subnetIds), true, this::fetchSubnetForAddCallBack);

//Get subnet route
getSubnetRoutes(context, new ArrayList<>(subnetIds));
}
//Get subnet
getSubnetEntities(context, new ArrayList<>(subnetIds), true, this::fetchSubnetForAddCallBack);

//Get subnet route
getSubnetRoutes(context, new ArrayList<>(subnetIds));


//Allocate fixed ip addresses
allocateIpAddresses(context, fixedIpAddresses, this::allocateFixedIpCallback);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ public RouteTableWebJson updateVpcRouteTable(@PathVariable String projectid, @Pa
InternalRouterInfo internalRouterInfo = this.neutronRouterService.constructInternalRouterInfo(internalSubnetRoutingTableList);

// send InternalRouterInfo contract to DPM
this.routerToDPMService.sendInternalRouterInfoToDPM(internalRouterInfo);
// this.routerToDPMService.sendInternalRouterInfoToDPM(internalRouterInfo);

} catch (ParameterNullOrEmptyException e) {
throw e;
Expand Down Expand Up @@ -451,7 +451,7 @@ public RouteTableWebJson updateSubnetRouteTable(@PathVariable String projectid,
List<HostRoute> hostRouteToSubnet = updateRoutingRuleResponse.getHostRouteToSubnet();

// send InternalRouterInfo contract to DPM
this.routerToDPMService.sendInternalRouterInfoToDPM(internalRouterInfo);
// this.routerToDPMService.sendInternalRouterInfoToDPM(internalRouterInfo);

// update routes in subnet manager
if (hostRouteToSubnet == null) {
Expand Down Expand Up @@ -491,8 +491,6 @@ public ResponseId deleteSubnetRouteTable(@PathVariable String projectid, @PathVa
RestPreconditionsUtil.verifyParameterNotNullorEmpty(projectid);
RestPreconditionsUtil.verifyResourceFound(projectid);

routerId = this.routerService.deleteSubnetRouteTable(projectid, subnetid);

// sub-level routing rule update
NewRoutesWebRequest newRoutes = new NewRoutesWebRequest();
List<NewRoutesRequest> routes = new ArrayList<>();
Expand All @@ -505,14 +503,16 @@ public ResponseId deleteSubnetRouteTable(@PathVariable String projectid, @PathVa
List<HostRoute> hostRouteToSubnet = updateRoutingRuleResponse.getHostRouteToSubnet();

// send InternalRouterInfo contract to DPM
this.routerToDPMService.sendInternalRouterInfoToDPM(internalRouterInfo);
// this.routerToDPMService.sendInternalRouterInfoToDPM(internalRouterInfo);

// update routes in subnet manager
if (hostRouteToSubnet == null) {
throw new HostRoutesToSubnetIsNull();
}
this.vpcRouterToSubnetService.updateRoutingRuleInSubnetManager(projectid, subnetid, hostRouteToSubnet);

routerId = this.routerService.deleteSubnetRouteTable(projectid, subnetid);

} catch (ParameterNullOrEmptyException | HostRoutesToSubnetIsNull e) {
throw e;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -479,13 +479,11 @@ public UpdateRoutingRuleResponse updateRoutingRule (String owner, NewRoutesWebRe
}

// find routeTable
Map<String, String[]> requestParams = new HashMap<>();
String[] value = new String[1];
Map<String, Object[]> queryParams = new HashMap<>();
Object[] value = new Object[1];
value[0] = owner;
requestParams.put("owner", value);
queryParams.put("owner", value);

Map<String, Object[]> queryParams =
ControllerUtil.transformUrlPathParams(requestParams, RouteTable.class);
Map<String, RouteTable> routeTableMap = this.routeTableDatabaseService.getAllRouteTables(queryParams);
List<RouteTable> routeTables = new ArrayList<>(routeTableMap.values());
if (routeTables == null || routeTables.size() == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public SubnetWebJson getSubnet(String projectid, String subnetId) {

@Override
public SubnetsWebJson getSubnetsByPortId(String projectid, String portId) {
String subnetManagerServiceUrl = subnetUrl + "/project/" + projectid + "/subnets?port_id=" + portId;
String subnetManagerServiceUrl = subnetUrl + "/project/" + projectid + "/subnets?gatewayPortId=" + portId;
SubnetsWebJson response = restTemplate.getForObject(subnetManagerServiceUrl, SubnetsWebJson.class);
return response;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public static boolean checkSubnetRouteTableWebJsonResourceIsValid(RouteTableWebJ

// routeTableType
String routeTableTypeStr = resource.getRoutetable().getRouteTableType();
if (routeTableTypeStr != null && !routeTableTypeStr.equals("public_subnet") && !routeTableTypeStr.equals("private_subnet")) {
if (routeTableTypeStr != null && !routeTableTypeStr.equals("public_subnet") && !routeTableTypeStr.equals("private_subnet") && !routeTableTypeStr.equals("neutron_subnet")) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,12 +296,15 @@ public SubnetWebJson createSubnetState(@PathVariable String projectId, @RequestB
// if (macState != null) {
// inSubnetEntity.setGatewayMacAddress(macState.getMacAddress());
// }
this.subnetDatabaseService.addSubnet(inSubnetEntity);

if (gatewayIpIsInAllocatedRange) {
PortEntity portEntity = this.subnetService.constructPortEntity(portId, vpcId, subnetId, ipResponse.getIp(), ConstantsConfig.DeviceOwner);
GatewayPortDetail gatewayPortDetail = this.subnetToPortManagerService.createGatewayPort(projectId, portEntity);

inSubnetEntity.setGatewayIp(ipResponse.getIp());
inSubnetEntity.setGatewayPortDetail(gatewayPortDetail);
inSubnetEntity.setGatewayPortId(gatewayPortDetail.getGatewayPortId());
} else {
String gatewayIP = SubnetManagementUtil.setGatewayIpValue(gatewayIp, cidr);
if (gatewayIp != null) {
Expand All @@ -310,8 +313,10 @@ public SubnetWebJson createSubnetState(@PathVariable String projectId, @RequestB

inSubnetEntity.setGatewayIp(gatewayIP);
inSubnetEntity.setGatewayPortDetail(gatewayPortDetail);
inSubnetEntity.setGatewayPortId(gatewayPortDetail.getGatewayPortId());
}
}

if (ipResponse != null && ipResponse.getIpVersion() == 4) {
inSubnetEntity.setIpV4RangeId(ipResponse.getRangeId());
}else if (ipResponse != null && ipResponse.getIpVersion() == 6) {
Expand Down Expand Up @@ -441,6 +446,7 @@ public SubnetWebJson updateSubnetState(@PathVariable String projectId, @PathVari
GatewayPortDetail gatewayPortDetail = this.subnetToPortManagerService.createGatewayPort(projectId, portEntity);
subnetEntity.setGatewayPortDetail(gatewayPortDetail);
subnetEntity.setGatewayIp(newGatewayIp);
subnetEntity.setGatewayPortId(gatewayPortDetail.getGatewayPortId()); // -tem

// delete port with old gateway port IP & port Id
this.subnetToPortManagerService.deleteGatewayPort(projectId,oldPortId);
Expand All @@ -457,13 +463,12 @@ public SubnetWebJson updateSubnetState(@PathVariable String projectId, @PathVari
subnetEntity.setRevisionNumber(revisionNumber + 1);
}

// update subnet routing rule in route manager
this.subnetService.updateSubnetRoutingRuleInRM(projectId, subnetId, subnetEntity);

this.subnetDatabaseService.addSubnet(subnetEntity);
subnetEntity = this.subnetDatabaseService.getBySubnetId(subnetId);

// update subnet routing rule in route manager
this.subnetService.updateSubnetRoutingRuleInRM(projectId, subnetId, subnetEntity);

} catch (ParameterNullOrEmptyException e) {
logger.error(e.getMessage());
throw new Exception(e);
Expand Down Expand Up @@ -516,19 +521,19 @@ public ResponseId deleteSubnetState(@PathVariable String projectId, @PathVariabl
throw new SubnetBindRoutes();
}

this.subnetDatabaseService.deleteSubnet(subnetId);

// delete subnet id in vpc
this.subnetService.deleteSubnetIdInVpc(subnetId, projectId, subnetEntity.getVpcId());

// delete subnet routing rule in route manager
this.subnetService.deleteSubnetRoutingRuleInRM(projectId, subnetId);

// delete gateway port in port manager
GatewayPortDetail gatewayPortDetail = subnetEntity.getGatewayPortDetail();
if (gatewayPortDetail != null) {
this.subnetToPortManagerService.deleteGatewayPort(projectId, gatewayPortDetail.getGatewayPortId());
}
// TODO: delete gateway port in port manager. Temporary solution, need PM fix issue
// GatewayPortDetail gatewayPortDetail = subnetEntity.getGatewayPortDetail();
// if (gatewayPortDetail != null) {
// this.subnetToPortManagerService.deleteGatewayPort(projectId, gatewayPortDetail.getGatewayPortId());
// }

this.subnetDatabaseService.deleteSubnet(subnetId);

} catch (ParameterNullOrEmptyException | HavePortInSubnet | SubnetBindRoutes e) {
logger.error(e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import com.futurewei.alcor.common.exception.FallbackException;
import com.futurewei.alcor.common.exception.ResourceNotFoundException;
import com.futurewei.alcor.subnet.config.ConstantsConfig;
import com.futurewei.alcor.subnet.config.UnitTestConfig;
import com.futurewei.alcor.subnet.service.SubnetDatabaseService;
import com.futurewei.alcor.subnet.service.SubnetService;
Expand Down Expand Up @@ -116,6 +117,8 @@ public void createSubnetState_create_pass () throws Exception {
.thenReturn(new String[2]);
Mockito.when(subnetToPortManagerService.createGatewayPort(anyString(), any(PortEntity.class)))
.thenReturn(new GatewayPortDetail(UnitTestConfig.macAddress, UnitTestConfig.gatewayPortId));
Mockito.when(subnetService.constructPortEntity(anyString(), anyString(), anyString(), anyString(), anyString()))
.thenReturn(new PortEntity());

this.mockMvc.perform(post(createUri).contentType(MediaType.APPLICATION_JSON)
.content(UnitTestConfig.resource))
Expand Down Expand Up @@ -268,6 +271,8 @@ public void createSubnetState_canNotFindIP_notPass () throws Exception {
.thenThrow(new FallbackException("fallback request"));
Mockito.when(subnetToPortManagerService.createGatewayPort(anyString(), any(PortEntity.class)))
.thenReturn(new GatewayPortDetail(UnitTestConfig.macAddress, UnitTestConfig.gatewayPortId));
Mockito.when(subnetService.constructPortEntity(anyString(), anyString(), anyString(), anyString(), anyString()))
.thenReturn(new PortEntity());
try {
this.mockMvc.perform(post(createUri).contentType(MediaType.APPLICATION_JSON)
.content(UnitTestConfig.resource))
Expand Down Expand Up @@ -311,6 +316,8 @@ public void createSubnetState_invalidCidr_notPass () throws Exception {
.thenReturn(new String[2]);
Mockito.when(subnetToPortManagerService.createGatewayPort(anyString(), any(PortEntity.class)))
.thenReturn(new GatewayPortDetail(UnitTestConfig.macAddress, UnitTestConfig.gatewayPortId));
Mockito.when(subnetService.constructPortEntity(anyString(), anyString(), anyString(), anyString(), anyString()))
.thenReturn(new PortEntity());
try {
this.mockMvc.perform(post(createUri).contentType(MediaType.APPLICATION_JSON)
.content(UnitTestConfig.invalidCidrResource))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,6 @@ public class PortEntity extends CustomerResource {
@JsonProperty("mac_learning_enabled")
private boolean macLearningEnabled;

public boolean isGatewayPort () {
return deviceOwner != null;
}

public PortEntity() {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public class SubnetEntity extends CustomerResource {
@JsonProperty("gateway_ip")
private String gatewayIp = "";

// subnet_gateway_port_id
@JsonProperty("gatewayPortId")
private String gatewayPortId;

@JsonProperty("gateway_port_detail")
private GatewayPortDetail gatewayPortDetail;

Expand Down

0 comments on commit 977ac09

Please sign in to comment.