Skip to content

Commit

Permalink
[Port Mgr] Support multiple ResourceOperation types (futurewei-cloud#603
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Liguang Xie authored Apr 29, 2021
1 parent a9e3d8b commit 5f71b54
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,13 @@ public static String[] getBeanNullPropertyNames(Object bean){
}
}).toArray(String[]::new);
}

/**
* Determine if a given string is empty or null
* @param an input string
* @return a boolean value, true if null or empty
*/
public static boolean isNullOrEmpty(String string) {
return string == null || string.trim().isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ free of charge, to any person obtaining a copy of this software and associated d
package com.futurewei.alcor.portmanager.processor;

import com.futurewei.alcor.common.enumClass.StatusEnum;
import com.futurewei.alcor.common.utils.CommonUtil;
import com.futurewei.alcor.common.utils.SpringContextUtil;
import com.futurewei.alcor.portmanager.exception.GetNodeInfoException;
import com.futurewei.alcor.portmanager.exception.NodeInfoNotFound;
Expand Down Expand Up @@ -229,6 +230,38 @@ private void setTheMissingFields(PortContext context, List<PortEntity> portEntit

}

private List<ResourceOperation> initializeResourceOperationTypes(PortContext context, List<PortEntity> portEntities) {
List<ResourceOperation> resourceOperationTypes = new ArrayList<>();
resourceOperationTypes.add(new ResourceOperation(Common.ResourceType.PORT, Common.OperationType.CREATE));
resourceOperationTypes.add(new ResourceOperation(Common.ResourceType.NEIGHBOR, Common.OperationType.CREATE));

// TODO: to enable full sg support, we will need to add full security group entities to context and check context here
for (PortEntity port : portEntities) {
if (!CommonUtil.isNullOrEmpty(context.getDefaultSgId()) && port.getPortSecurityEnabled()) {
resourceOperationTypes.add(new ResourceOperation(Common.ResourceType.SECURITYGROUP, Common.OperationType.CREATE));
break;
}
}

if (context.containRouters()) {
resourceOperationTypes.add(new ResourceOperation(Common.ResourceType.NEIGHBOR, Common.OperationType.CREATE));
}

return resourceOperationTypes;
}

private void markOperationTypes(NetworkConfiguration networkConfig, Common.OperationType operationType) {
if (networkConfig == null || networkConfig.getRsOpTypes() == null) {
return;
}

List<ResourceOperation> resourceOperationTypes = networkConfig.getRsOpTypes();
for (ResourceOperation type : resourceOperationTypes) {
type.setOpType(operationType);
}
}


private NetworkConfiguration buildNetworkConfig(PortContext context, List<PortEntity> portEntities) throws Exception {
/**
DataPlaneProcessor needs to wait for all previous Processor runs to
Expand All @@ -245,9 +278,11 @@ private NetworkConfiguration buildNetworkConfig(PortContext context, List<PortEn
}

setTheMissingFields(context, portEntities);
List<ResourceOperation> resourceOperationTypes = initializeResourceOperationTypes(context, portEntities);

NetworkConfiguration networkConfiguration = new NetworkConfiguration();
networkConfiguration.setRsType(Common.ResourceType.PORT);
networkConfiguration.setRsOpTypes(resourceOperationTypes);
networkConfiguration.setVpcs(networkConfig.getVpcEntities());
networkConfiguration.setSubnets(networkConfig.getSubnetEntities());
networkConfiguration.setSecurityGroups(networkConfig.getSecurityGroups());
Expand All @@ -267,8 +302,8 @@ private void createNetworkConfig(PortContext context, NetworkConfiguration netwo
networkConfig.setOpType(Common.OperationType.CREATE);
IRestRequest createNetworkConfigRequest =
new CreateNetworkConfigRequest(context, networkConfig);
context.getRequestManager().sendRequestAsync(createNetworkConfigRequest, request -> portService.updatePortStatus(request,networkConfig,null));
portService.updatePortStatus(createNetworkConfigRequest,networkConfig, StatusEnum.CREATED.getStatus());
context.getRequestManager().sendRequestAsync(createNetworkConfigRequest, request -> portService.updatePortStatus(request, networkConfig, null));
portService.updatePortStatus(createNetworkConfigRequest, networkConfig, StatusEnum.CREATED.getStatus());
}
}

Expand All @@ -278,14 +313,15 @@ private void updateNetworkConfig(PortContext context, NetworkConfiguration netwo
networkConfig.setOpType(Common.OperationType.UPDATE);
IRestRequest updateNetworkConfigRequest =
new UpdateNetworkConfigRequest(context, networkConfig);
context.getRequestManager().sendRequestAsync(updateNetworkConfigRequest, request -> portService.updatePortStatus(request,networkConfig,null));
portService.updatePortStatus(updateNetworkConfigRequest,networkConfig, StatusEnum.PENDING.getStatus());
context.getRequestManager().sendRequestAsync(updateNetworkConfigRequest, request -> portService.updatePortStatus(request, networkConfig, null));
portService.updatePortStatus(updateNetworkConfigRequest, networkConfig, StatusEnum.PENDING.getStatus());
}
}

private void deleteNetworkConfig(PortContext context, NetworkConfiguration networkConfig) {
if (networkConfig != null) {
networkConfig.setOpType(Common.OperationType.DELETE);
markOperationTypes(networkConfig, Common.OperationType.DELETE);
IRestRequest deleteNetworkConfigRequest =
new DeleteNetworkConfigRequest(context, networkConfig);
context.getRequestManager().sendRequestAsync(deleteNetworkConfigRequest, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,14 @@ public InternalRouterInfo getRouterByVpcOrSubnetId(String vpcOrSubnetId) {
return (this.routers != null) ? this.routers.get(vpcOrSubnetId) : null;
}

public boolean containRouters() {
if (this.routers == null || this.routers.isEmpty()) {
return false;
}

return true;
}

public void setRouters(Map<String, InternalRouterInfo> routers) {
this.routers = routers;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public class ResourceOperation {
public ResourceOperation() {
}

public ResourceOperation(ResourceType rsType, OperationType opType) {
this.rsType = rsType;
this.opType = opType;
}

public ResourceType getRsType() {
return this.rsType;
}
Expand All @@ -43,12 +48,7 @@ public OperationType getOpType() {
return this.opType;
}

public void setHostId(OperationType opType) {
this.opType = opType;
}

public ResourceOperation(ResourceType rsType, OperationType opType) {
this.rsType = rsType;
public void setOpType(OperationType opType) {
this.opType = opType;
}
}

0 comments on commit 5f71b54

Please sign in to comment.