diff --git a/services/data_plane_manager/src/main/java/com/futurewei/alcor/dataplane/service/ovs/DhcpService.java b/services/data_plane_manager/src/main/java/com/futurewei/alcor/dataplane/service/ovs/DhcpService.java new file mode 100644 index 000000000..e98fd62fc --- /dev/null +++ b/services/data_plane_manager/src/main/java/com/futurewei/alcor/dataplane/service/ovs/DhcpService.java @@ -0,0 +1,55 @@ +/* +Copyright 2019 The Alcor Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ +package com.futurewei.alcor.dataplane.service.ovs; + +import com.futurewei.alcor.dataplane.entity.UnicastGoalState; +import com.futurewei.alcor.schema.DHCP; +import com.futurewei.alcor.schema.Port; +import com.futurewei.alcor.web.entity.dataplane.v2.NetworkConfiguration; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class DhcpService extends ResourceService { + public void buildDhcpStates(NetworkConfiguration networkConfig, UnicastGoalState unicastGoalState) throws Exception { + List portStates = unicastGoalState.getGoalStateBuilder().getPortStatesList(); + if (portStates == null || portStates.size() == 0) { + return; + } + + for (Port.PortState portState: portStates) { + String macAddress = portState.getConfiguration().getMacAddress(); + List fixedIps = portState.getConfiguration().getFixedIpsList(); + for (Port.PortConfiguration.FixedIp fixedIp: fixedIps) { + DHCP.DHCPConfiguration.Builder dhcpConfigBuilder = DHCP.DHCPConfiguration.newBuilder(); + dhcpConfigBuilder.setRevisionNumber(FORMAT_REVISION_NUMBER); + dhcpConfigBuilder.setMacAddress(macAddress); + dhcpConfigBuilder.setIpv4Address(fixedIp.getIpAddress()); + //TODO: support ipv6 + //dhcpConfigBuilder.setIpv6Address(); + //dhcpConfigBuilder.setPortHostName(); + //dhcpConfigBuilder.setExtraDhcpOptions(); + //dhcpConfigBuilder.setDnsEntryList(); + + DHCP.DHCPState.Builder dhcpStateBuilder = DHCP.DHCPState.newBuilder(); + dhcpStateBuilder.setOperationType(networkConfig.getOpType()); + dhcpStateBuilder.setConfiguration(dhcpConfigBuilder.build()); + unicastGoalState.getGoalStateBuilder().addDhcpStates(dhcpStateBuilder.build()); + } + } + } +} diff --git a/services/data_plane_manager/src/main/java/com/futurewei/alcor/dataplane/service/ovs/DpmServiceImpl.java b/services/data_plane_manager/src/main/java/com/futurewei/alcor/dataplane/service/ovs/DpmServiceImpl.java index ac855e1bd..abaf20946 100644 --- a/services/data_plane_manager/src/main/java/com/futurewei/alcor/dataplane/service/ovs/DpmServiceImpl.java +++ b/services/data_plane_manager/src/main/java/com/futurewei/alcor/dataplane/service/ovs/DpmServiceImpl.java @@ -16,57 +16,19 @@ package com.futurewei.alcor.dataplane.service.ovs; import com.futurewei.alcor.dataplane.config.Config; -import com.futurewei.alcor.common.enumClass.VpcRouteTarget; import com.futurewei.alcor.dataplane.client.DataPlaneClient; import com.futurewei.alcor.dataplane.entity.MulticastGoalState; import com.futurewei.alcor.dataplane.entity.UnicastGoalState; import com.futurewei.alcor.dataplane.exception.*; import com.futurewei.alcor.dataplane.cache.LocalCache; import com.futurewei.alcor.dataplane.service.DpmService; -import com.futurewei.alcor.schema.Common; -import com.futurewei.alcor.schema.Common.EtherType; -import com.futurewei.alcor.schema.Common.NetworkType; -import com.futurewei.alcor.schema.Common.OperationType; -import com.futurewei.alcor.schema.Common.Protocol; -import com.futurewei.alcor.schema.DHCP.DHCPConfiguration; -import com.futurewei.alcor.schema.DHCP.DHCPState; -import com.futurewei.alcor.schema.Goalstate; import com.futurewei.alcor.schema.Goalstateprovisioner.GoalStateOperationReply.GoalStateOperationStatus; -import com.futurewei.alcor.schema.Neighbor.NeighborConfiguration; -import com.futurewei.alcor.schema.Neighbor.NeighborState; -import com.futurewei.alcor.schema.Neighbor.NeighborType; -import com.futurewei.alcor.schema.Port.PortConfiguration; -import com.futurewei.alcor.schema.Port.PortConfiguration.AllowAddressPair; -import com.futurewei.alcor.schema.Port.PortConfiguration.FixedIp; -import com.futurewei.alcor.schema.Port.PortConfiguration.HostInfo; -import com.futurewei.alcor.schema.Port.PortConfiguration.SecurityGroupId; -import com.futurewei.alcor.schema.Port.PortState; -import com.futurewei.alcor.schema.Router; -import com.futurewei.alcor.schema.Router.RouterState; -import com.futurewei.alcor.schema.Router.DestinationType; -import com.futurewei.alcor.schema.Router.RouterConfiguration; -import com.futurewei.alcor.schema.Router.RouterConfiguration.SubnetRoutingTable; -import com.futurewei.alcor.schema.Router.RouterConfiguration.RoutingRule; -import com.futurewei.alcor.schema.Router.RouterConfiguration.RoutingRuleExtraInfo; -import com.futurewei.alcor.schema.SecurityGroup.SecurityGroupConfiguration; -import com.futurewei.alcor.schema.SecurityGroup.SecurityGroupConfiguration.Direction; -import com.futurewei.alcor.schema.SecurityGroup.SecurityGroupState; -import com.futurewei.alcor.schema.Subnet.SubnetConfiguration; -import com.futurewei.alcor.schema.Subnet.SubnetConfiguration.Gateway; -import com.futurewei.alcor.schema.Subnet.SubnetState; -import com.futurewei.alcor.schema.Vpc.VpcConfiguration; -import com.futurewei.alcor.schema.Vpc.VpcConfiguration.SubnetId; -import com.futurewei.alcor.schema.Vpc.VpcState; import com.futurewei.alcor.web.entity.dataplane.*; import com.futurewei.alcor.web.entity.dataplane.v2.NetworkConfiguration; import com.futurewei.alcor.web.entity.port.PortHostInfo; import com.futurewei.alcor.web.entity.route.InternalRouterInfo; -import com.futurewei.alcor.web.entity.route.InternalRoutingRule; import com.futurewei.alcor.web.entity.route.InternalSubnetRoutingTable; -import com.futurewei.alcor.web.entity.securitygroup.SecurityGroup; -import com.futurewei.alcor.web.entity.securitygroup.SecurityGroupRule; import com.futurewei.alcor.web.entity.subnet.InternalSubnetPorts; -import com.futurewei.alcor.web.entity.vpc.VpcEntity; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -77,424 +39,37 @@ @Service public class DpmServiceImpl implements DpmService { private int goalStateMessageVersion; - private static final int FORMAT_REVISION_NUMBER = 1; @Autowired private LocalCache localCache; - @Autowired - private DpmServiceImpl(Config globalConfig) { - this.goalStateMessageVersion = globalConfig.goalStateMessageVersion; - } - @Autowired private DataPlaneClient dataPlaneClient; - private VpcEntity getVpcEntity(NetworkConfiguration networkConfig, String vpcId) throws Exception { - VpcEntity result = null; - for (VpcEntity vpcEntity: networkConfig.getVpcs()) { - if (vpcEntity.getId().equals(vpcId)) { - result = vpcEntity; - } - } - - if (result == null) { - throw new VpcEntityNotFound(); - } - - return result; - } - - private void buildVpcStates(NetworkConfiguration networkConfig, UnicastGoalState unicastGoalState) throws Exception { - List portStates = unicastGoalState.getGoalStateBuilder().getPortStatesList(); - if (portStates == null || portStates.size() == 0) { - return; - } - - for (PortState portState: portStates) { - VpcEntity vpcEntity = getVpcEntity(networkConfig, portState.getConfiguration().getVpcId()); - VpcConfiguration.Builder vpcConfigBuilder = VpcConfiguration.newBuilder(); - vpcConfigBuilder.setRevisionNumber(FORMAT_REVISION_NUMBER); - vpcConfigBuilder.setId(vpcEntity.getId()); - vpcConfigBuilder.setProjectId(vpcEntity.getProjectId()); - - if (vpcEntity.getName() != null) { - vpcConfigBuilder.setName(vpcEntity.getName()); - } - - if (vpcEntity.getCidr() != null) { - vpcConfigBuilder.setCidr(vpcEntity.getCidr()); - } - - //vpcConfigBuilder.setTunnelId(); - - if (networkConfig.getSubnets() != null) { - networkConfig.getSubnets().stream() - .filter(s -> s.getVpcId().equals(vpcEntity.getId())) - .map(InternalSubnetEntity::getId) - .forEach(id -> { - SubnetId.Builder subnetIdBuilder = SubnetId.newBuilder(); - subnetIdBuilder.setId(id); - vpcConfigBuilder.addSubnetIds(subnetIdBuilder.build()); - }); - } - //set routes here - - VpcState.Builder vpcStateBuilder = VpcState.newBuilder(); - vpcStateBuilder.setOperationType(networkConfig.getOpType()); - vpcStateBuilder.setConfiguration(vpcConfigBuilder.build()); - - unicastGoalState.getGoalStateBuilder().addVpcStates(vpcStateBuilder.build()); - } - } - - private InternalSubnetEntity getInternalSubnetEntity(NetworkConfiguration networkConfig, String subnetId) throws Exception { - InternalSubnetEntity result = null; - for (InternalSubnetEntity internalSubnetEntity: networkConfig.getSubnets()) { - if (internalSubnetEntity.getId().equals(subnetId)) { - result = internalSubnetEntity; - } - } - - if (result == null) { - throw new SubnetEntityNotFound(); - } - - return result; - } - - private void buildSubnetStates(NetworkConfiguration networkConfig, UnicastGoalState unicastGoalState) throws Exception { - List portStates = unicastGoalState.getGoalStateBuilder().getPortStatesList(); - if (portStates == null || portStates.size() == 0) { - return; - } - - List subnetEntities = new ArrayList<>(); - for (PortState portState: portStates) { - for (FixedIp fixedIp: portState.getConfiguration().getFixedIpsList()) { - InternalSubnetEntity internalSubnetEntity = getInternalSubnetEntity( - networkConfig, fixedIp.getSubnetId()); - subnetEntities.add(internalSubnetEntity); - } - } - - for (InternalSubnetEntity subnetEntity: subnetEntities) { - SubnetConfiguration.Builder subnetConfigBuilder = SubnetConfiguration.newBuilder(); - subnetConfigBuilder.setRevisionNumber(FORMAT_REVISION_NUMBER); - subnetConfigBuilder.setId(subnetEntity.getId()); - subnetConfigBuilder.setNetworkType(NetworkType.VXLAN); - subnetConfigBuilder.setVpcId(subnetEntity.getVpcId()); - subnetConfigBuilder.setName(subnetEntity.getName()); - subnetConfigBuilder.setCidr(subnetEntity.getCidr()); - subnetConfigBuilder.setTunnelId(subnetEntity.getTunnelId()); - - Gateway.Builder gatewayBuilder = Gateway.newBuilder(); - gatewayBuilder.setIpAddress(subnetEntity.getGatewayIp()); - gatewayBuilder.setMacAddress(subnetEntity.getGatewayMacAddress()); - subnetConfigBuilder.setGateway(gatewayBuilder.build()); - - if (subnetEntity.getDhcpEnable() != null) { - subnetConfigBuilder.setDhcpEnable(subnetEntity.getDhcpEnable()); - } - - // TODO: need to set DNS based on latest contract - - if (subnetEntity.getAvailabilityZone() != null) { - subnetConfigBuilder.setAvailabilityZone(subnetEntity.getAvailabilityZone()); - } - - SubnetState.Builder subnetStateBuilder = SubnetState.newBuilder(); - subnetStateBuilder.setOperationType(networkConfig.getOpType()); - subnetStateBuilder.setConfiguration(subnetConfigBuilder.build()); - unicastGoalState.getGoalStateBuilder().addSubnetStates(subnetStateBuilder.build()); - } - } - - private void buildPortState(NetworkConfiguration networkConfig, List portEntities, - UnicastGoalState unicastGoalState) { - for (InternalPortEntity portEntity: portEntities) { - PortConfiguration.Builder portConfigBuilder = PortConfiguration.newBuilder(); - portConfigBuilder.setRevisionNumber(FORMAT_REVISION_NUMBER); - portConfigBuilder.setId(portEntity.getId()); - portConfigBuilder.setUpdateType(Common.UpdateType.FULL); - portConfigBuilder.setVpcId(portEntity.getVpcId()); - - if (portEntity.getName() != null) { - portConfigBuilder.setName(portEntity.getName()); - } - - portConfigBuilder.setMacAddress(portEntity.getMacAddress()); - portConfigBuilder.setAdminStateUp(portEntity.isAdminStateUp()); - - HostInfo.Builder hostInfoBuilder = HostInfo.newBuilder(); - hostInfoBuilder.setIpAddress(portEntity.getBindingHostIP()); - //TODO: Do we need mac address? - //hostInfoBuilder.setMacAddress() - portConfigBuilder.setHostInfo(hostInfoBuilder.build()); - if (portEntity.getFixedIps() != null) { - portEntity.getFixedIps().forEach(fixedIp -> { - FixedIp.Builder fixedIpBuilder = FixedIp.newBuilder(); - fixedIpBuilder.setSubnetId(fixedIp.getSubnetId()); - fixedIpBuilder.setIpAddress(fixedIp.getIpAddress()); - portConfigBuilder.addFixedIps(fixedIpBuilder.build()); - }); - } - - if (portEntity.getAllowedAddressPairs() != null) { - portEntity.getAllowedAddressPairs().forEach(pair -> { - AllowAddressPair.Builder allowAddressPairBuilder = AllowAddressPair.newBuilder(); - allowAddressPairBuilder.setIpAddress(pair.getIpAddress()); - allowAddressPairBuilder.setMacAddress(pair.getMacAddress()); - portConfigBuilder.addAllowAddressPairs(allowAddressPairBuilder.build()); - }); - } - - if (portEntity.getSecurityGroups() != null) { - portEntity.getSecurityGroups().forEach(securityGroupId-> { - SecurityGroupId.Builder securityGroupIdBuilder = SecurityGroupId.newBuilder(); - securityGroupIdBuilder.setId(securityGroupId); - portConfigBuilder.addSecurityGroupIds(securityGroupIdBuilder.build()); - }); - } - - //PortState - PortState.Builder portStateBuilder = PortState.newBuilder(); - portStateBuilder.setOperationType(networkConfig.getOpType()); - portStateBuilder.setConfiguration(portConfigBuilder.build()); - unicastGoalState.getGoalStateBuilder().addPortStates(portStateBuilder.build()); - } - } - - private NeighborState buildNeighborState(NeighborEntry neighborEntry, NeighborInfo neighborInfo, OperationType operationType) { - NeighborConfiguration.Builder neighborConfigBuilder = NeighborConfiguration.newBuilder(); - neighborConfigBuilder.setRevisionNumber(FORMAT_REVISION_NUMBER); - //neighborConfigBuilder.setId(); // TODO: We are going to need this per latest ACA change - neighborConfigBuilder.setVpcId(neighborInfo.getVpcId()); - //neighborConfigBuilder.setName(); - neighborConfigBuilder.setMacAddress(neighborInfo.getPortMac()); - neighborConfigBuilder.setHostIpAddress(neighborInfo.getHostIp()); - NeighborType neighborType = NeighborType.valueOf(neighborEntry.getNeighborType().getType()); - - //TODO:setNeighborHostDvrMac - //neighborConfigBuilder.setNeighborHostDvrMac(); - NeighborConfiguration.FixedIp.Builder fixedIpBuilder = NeighborConfiguration.FixedIp.newBuilder(); - fixedIpBuilder.setSubnetId(neighborInfo.getSubnetId()); - fixedIpBuilder.setIpAddress(neighborInfo.getPortIp()); - fixedIpBuilder.setNeighborType(neighborType); - neighborConfigBuilder.addFixedIps(fixedIpBuilder.build()); - //TODO:setAllowAddressPairs - //neighborConfigBuilder.setAllowAddressPairs(); - - NeighborState.Builder neighborStateBuilder = NeighborState.newBuilder(); - neighborStateBuilder.setOperationType(operationType); - neighborStateBuilder.setConfiguration(neighborConfigBuilder.build()); - - return neighborStateBuilder.build(); - } - - private void buildNeighborStates(NetworkConfiguration networkConfig, String hostIp, - UnicastGoalState unicastGoalState, - MulticastGoalState multicastGoalState) throws Exception { - Map neighborInfos = networkConfig.getNeighborInfos(); - if (neighborInfos == null || neighborInfos.size() == 0) { - return; - } - - Map> neighborTable = networkConfig.getNeighborTable(); - if (neighborTable == null || neighborTable.size() == 0) { - return; - } - - List portStates = unicastGoalState.getGoalStateBuilder().getPortStatesList(); - if (portStates == null || portStates.size() == 0) { - return; - } - - List multicastNeighborEntries = new ArrayList<>(); - for (PortState portState: portStates) { - List fixedIps = portState.getConfiguration().getFixedIpsList(); - if (fixedIps == null) { - throw new PortFixedIpNotFound(); - } - - for (FixedIp fixedIp: fixedIps) { - List neighborEntries = neighborTable.get(fixedIp.getIpAddress()); - if (neighborEntries == null) { - throw new NeighborInfoNotFound(); - } - - for (NeighborEntry neighborEntry: neighborEntries) { - NeighborInfo neighborInfo = neighborInfos.get(neighborEntry.getNeighborIp()); - if (neighborInfo == null) { - throw new NeighborInfoNotFound(); - } - - if (hostIp.equals(neighborInfo.getHostIp())) { - continue; - } - - unicastGoalState.getGoalStateBuilder().addNeighborStates(buildNeighborState( - neighborEntry, neighborInfo, networkConfig.getOpType())); - } - - multicastNeighborEntries.addAll(neighborEntries); - } - } - - Set neighborInfoSet = new HashSet<>(); - for (NeighborEntry neighborEntry: multicastNeighborEntries) { - String localIp = neighborEntry.getLocalIp(); - String neighborIp = neighborEntry.getNeighborIp(); - NeighborInfo neighborInfo1 = neighborInfos.get(localIp); - NeighborInfo neighborInfo2 = neighborInfos.get(neighborIp); - if (neighborInfo1 == null || neighborInfo2 == null) { - throw new NeighborInfoNotFound(); - } - - if (!multicastGoalState.getHostIps().contains(neighborInfo2.getHostIp())) { - multicastGoalState.getHostIps().add(neighborInfo2.getHostIp()); - } - - if (!neighborInfoSet.contains(neighborInfo1)) { - multicastGoalState.getGoalStateBuilder().addNeighborStates(buildNeighborState( - neighborEntry, neighborInfo1, networkConfig.getOpType())); - neighborInfoSet.add(neighborInfo1); - } - } - } - - private SecurityGroup getSecurityGroup(NetworkConfiguration networkConfig, String securityGroupId) throws Exception { - SecurityGroup result = null; - for (SecurityGroup securityGroup: networkConfig.getSecurityGroups()) { - if (securityGroup.getId().equals(securityGroupId)) { - result = securityGroup; - break; - } - } - - if (result == null) { - throw new SecurityGroupNotFound(); - } - - return result; - } - - - private void buildSecurityGroupStates(NetworkConfiguration networkConfig, UnicastGoalState unicastGoalState) throws Exception { - List portStates = unicastGoalState.getGoalStateBuilder().getPortStatesList(); - if (portStates == null || portStates.size() == 0) { - return; - } - - Set securityGroupIds = new HashSet<>(); - for (PortState portState: portStates) { - List securityGroupIdList= portState.getConfiguration().getSecurityGroupIdsList(); - if (securityGroupIdList != null && securityGroupIdList.size() >0) { - securityGroupIds.addAll(securityGroupIdList.stream() - .map(SecurityGroupId::getId) - .collect(Collectors.toList())); - } - } - - for (String securityGroupId: securityGroupIds) { - SecurityGroup securityGroup = getSecurityGroup(networkConfig, securityGroupId); - SecurityGroupConfiguration.Builder securityGroupConfigBuilder = SecurityGroupConfiguration.newBuilder(); - securityGroupConfigBuilder.setRevisionNumber(FORMAT_REVISION_NUMBER); - securityGroupConfigBuilder.setId(securityGroup.getId()); - //securityGroupConfigBuilder.setVpcId(); - securityGroupConfigBuilder.setName(securityGroup.getName()); - - if (securityGroup.getSecurityGroupRules() == null) { - throw new SecurityGroupRuleNotFound(); - } - - for (SecurityGroupRule securityGroupRule: securityGroup.getSecurityGroupRules()) { - SecurityGroupConfiguration.SecurityGroupRule.Builder securityGroupRuleBuilder = - SecurityGroupConfiguration.SecurityGroupRule.newBuilder(); - securityGroupRuleBuilder.setSecurityGroupId(securityGroup.getId()); - securityGroupRuleBuilder.setId(securityGroupRule.getId()); - securityGroupRuleBuilder.setDirection(Direction.valueOf(securityGroupRule.getDirection())); - securityGroupRuleBuilder.setEthertype(EtherType.valueOf(securityGroupRule.getEtherType())); - securityGroupRuleBuilder.setProtocol(Protocol.valueOf(securityGroupRule.getProtocol())); - securityGroupRuleBuilder.setPortRangeMin(securityGroupRule.getPortRangeMin()); - securityGroupRuleBuilder.setPortRangeMax(securityGroupRule.getPortRangeMax()); - securityGroupRuleBuilder.setRemoteIpPrefix(securityGroupRule.getRemoteIpPrefix()); - securityGroupRuleBuilder.setRemoteGroupId(securityGroupRule.getRemoteGroupId()); - securityGroupConfigBuilder.addSecurityGroupRules(securityGroupRuleBuilder.build()); - } - - SecurityGroupState.Builder securityGroupStateBuilder = SecurityGroupState.newBuilder(); - securityGroupStateBuilder.setOperationType(networkConfig.getOpType()); - securityGroupStateBuilder.setConfiguration(securityGroupConfigBuilder.build()); - unicastGoalState.getGoalStateBuilder().addSecurityGroupStates(securityGroupStateBuilder.build()); - } - } + @Autowired + private VpcService vpcService; - private void buildDhcpStates(NetworkConfiguration networkConfig, UnicastGoalState unicastGoalState) throws Exception { - List portStates = unicastGoalState.getGoalStateBuilder().getPortStatesList(); - if (portStates == null || portStates.size() == 0) { - return; - } + @Autowired + private SubnetService subnetService; - for (PortState portState: portStates) { - String macAddress = portState.getConfiguration().getMacAddress(); - List fixedIps = portState.getConfiguration().getFixedIpsList(); - for (FixedIp fixedIp: fixedIps) { - DHCPConfiguration.Builder dhcpConfigBuilder = DHCPConfiguration.newBuilder(); - dhcpConfigBuilder.setRevisionNumber(FORMAT_REVISION_NUMBER); - dhcpConfigBuilder.setMacAddress(macAddress); - dhcpConfigBuilder.setIpv4Address(fixedIp.getIpAddress()); - //TODO: support ipv6 - //dhcpConfigBuilder.setIpv6Address(); - //dhcpConfigBuilder.setPortHostName(); - //dhcpConfigBuilder.setExtraDhcpOptions(); - //dhcpConfigBuilder.setDnsEntryList(); - - DHCPState.Builder dhcpStateBuilder = DHCPState.newBuilder(); - dhcpStateBuilder.setOperationType(networkConfig.getOpType()); - dhcpStateBuilder.setConfiguration(dhcpConfigBuilder.build()); - unicastGoalState.getGoalStateBuilder().addDhcpStates(dhcpStateBuilder.build()); - } - } - } + @Autowired + private PortService portService; - private void buildRouterStates(NetworkConfiguration networkConfig, UnicastGoalState unicastGoalState) throws Exception { - List portStates = unicastGoalState.getGoalStateBuilder().getPortStatesList(); - if (portStates == null || portStates.size() == 0) { - return; - } + @Autowired + private NeighborService neighborService; - Set subnetIds = new HashSet<>(); - for (PortState portState: portStates) { - List fixedIps = portState.getConfiguration().getFixedIpsList(); - for (FixedIp fixedIp: fixedIps) { - InternalSubnetEntity internalSubnetEntity = - getInternalSubnetEntity(networkConfig, fixedIp.getSubnetId()); - subnetIds.add(internalSubnetEntity.getId()); - } - } + @Autowired + private SecurityGroupService securityGroupService; - List internalRouterInfos = networkConfig.getInternalRouterInfos(); - if (internalRouterInfos == null || internalRouterInfos.size() == 0) { - return; - } + @Autowired + private DhcpService dhcpService; - for (String subnetId: subnetIds) { - for (InternalRouterInfo routerInfo : internalRouterInfos) { - List subnetRoutingTables = - routerInfo.getRouterConfiguration().getSubnetRoutingTables(); - if (subnetRoutingTables == null) { - continue; - } + @Autowired + private RouterService routerService; - for (InternalSubnetRoutingTable subnetRoutingTable : subnetRoutingTables) { - if (subnetId.equals(subnetRoutingTable.getSubnetId())) { - addSubnetRoutingTable(routerInfo, subnetRoutingTable, unicastGoalState); - break; - } - } - } - } + @Autowired + private DpmServiceImpl(Config globalConfig) { + this.goalStateMessageVersion = globalConfig.goalStateMessageVersion; } private UnicastGoalState buildUnicastGoalState(NetworkConfiguration networkConfig, String hostIp, @@ -506,15 +81,15 @@ private UnicastGoalState buildUnicastGoalState(NetworkConfiguration networkConfi unicastGoalState.getGoalStateBuilder().setFormatVersion(this.goalStateMessageVersion); if (portEntities != null && portEntities.size() > 0) { - buildPortState(networkConfig, portEntities, unicastGoalState); + portService.buildPortState(networkConfig, portEntities, unicastGoalState); } - buildVpcStates(networkConfig, unicastGoalState); - buildSubnetStates(networkConfig, unicastGoalState); - buildNeighborStates(networkConfig, hostIp, unicastGoalState, multicastGoalState); - buildSecurityGroupStates(networkConfig, unicastGoalState); - buildDhcpStates(networkConfig, unicastGoalState); - buildRouterStates(networkConfig, unicastGoalState); + vpcService.buildVpcStates(networkConfig, unicastGoalState); + subnetService.buildSubnetStates(networkConfig, unicastGoalState); + neighborService.buildNeighborStates(networkConfig, hostIp, unicastGoalState, multicastGoalState); + securityGroupService.buildSecurityGroupStates(networkConfig, unicastGoalState); + dhcpService.buildDhcpStates(networkConfig, unicastGoalState); + routerService.buildRouterStates(networkConfig, unicastGoalState); unicastGoalState.setGoalState(unicastGoalState.getGoalStateBuilder().build()); unicastGoalState.setGoalStateBuilder(null); @@ -562,87 +137,6 @@ private List>> createSecurityGroupCon return null; } - private OperationType getOperationType(com.futurewei.alcor.common.enumClass.OperationType operationType) { - switch (operationType) { - case CREATE: - return OperationType.CREATE; - case UPDATE: - return OperationType.UPDATE; - case INFO: - return OperationType.INFO; - case DELETE: - return OperationType.DELETE; - default: - return OperationType.UNRECOGNIZED; - } - } - - private DestinationType getDestinationType(VpcRouteTarget vpcRouteTarget) { - switch (vpcRouteTarget) { - case LOCAL: - return DestinationType.INTERNET; - case INTERNET_GW: - return DestinationType.VPC_GW; - case NAT_GW: - default: - return DestinationType.UNRECOGNIZED; - } - } - - - private void addSubnetRoutingTable(InternalRouterInfo routerInfo, InternalSubnetRoutingTable subnetRoutingTable, UnicastGoalState unicastGoalState) { SubnetRoutingTable.Builder subnetRoutingTableBuilder = SubnetRoutingTable.newBuilder(); - String subnetId = subnetRoutingTable.getSubnetId(); - subnetRoutingTableBuilder.setSubnetId(subnetId); - List routingRules = subnetRoutingTable.getRoutingRules(); - if (routingRules == null || routingRules.size() == 0) { - return; - } - - for (InternalRoutingRule routingRule: routingRules) { - RoutingRule.Builder routingRuleBuilder = RoutingRule.newBuilder(); - routingRuleBuilder.setOperationType(getOperationType(routingRule.getOperationType())); - routingRuleBuilder.setId(routingRule.getId()); - routingRuleBuilder.setName(routingRule.getName()); - routingRuleBuilder.setDestination(routingRule.getDestination()); - routingRuleBuilder.setNextHopIp(routingRule.getNextHopIp()); - routingRuleBuilder.setPriority(Integer.parseInt(routingRule.getPriority())); - - if (routingRule.getRoutingRuleExtraInfo() != null) { - RoutingRuleExtraInfo.Builder extraInfoBuilder = RoutingRuleExtraInfo.newBuilder(); - extraInfoBuilder.setDestinationType(getDestinationType( - routingRule.getRoutingRuleExtraInfo().getDestinationType())); - extraInfoBuilder.setNextHopMac(routingRule.getRoutingRuleExtraInfo().getNextHopMac()); - routingRuleBuilder.setRoutingRuleExtraInfo(extraInfoBuilder.build()); - } - - subnetRoutingTableBuilder.addRoutingRules(routingRuleBuilder.build()); - } - - List subnetRoutingTablesList = new ArrayList<>(); - subnetRoutingTablesList.add(subnetRoutingTableBuilder.build()); - - Goalstate.GoalState.Builder goalStateBuilder = unicastGoalState.getGoalStateBuilder(); - List routerStatesBuilders = goalStateBuilder.getRouterStatesBuilderList(); - if (routerStatesBuilders != null && routerStatesBuilders.size() > 0) { - subnetRoutingTablesList.addAll(goalStateBuilder. - getRouterStatesBuilder(0). - getConfiguration(). - getSubnetRoutingTablesList()); - goalStateBuilder.removeRouterStates(0); - } - - RouterConfiguration.Builder routerConfigBuilder = RouterConfiguration.newBuilder(); - routerConfigBuilder.setRevisionNumber(FORMAT_REVISION_NUMBER); - - //TODO: where does the hostDvrMacAddress come from ? - routerConfigBuilder.setHostDvrMacAddress(routerInfo.getRouterConfiguration().getHostDvrMac()); - - routerConfigBuilder.addAllSubnetRoutingTables(subnetRoutingTablesList); - RouterState.Builder routerStateBuilder = RouterState.newBuilder(); - routerStateBuilder.setConfiguration(routerConfigBuilder.build()); - goalStateBuilder.addRouterStates(0,routerStateBuilder.build()); - } - private List>> createRouterConfiguration(NetworkConfiguration networkConfig) throws Exception { List internalRouterInfos = networkConfig.getInternalRouterInfos(); if (internalRouterInfos == null) { @@ -673,7 +167,7 @@ private List>> createRouterConfigurat unicastGoalStateMap.put(hostIp, unicastGoalState); } - addSubnetRoutingTable(routerInfo, subnetRoutingTable, unicastGoalState); + routerService.buildRouterState(routerInfo, subnetRoutingTable, unicastGoalState); } } } diff --git a/services/data_plane_manager/src/main/java/com/futurewei/alcor/dataplane/service/ovs/NeighborService.java b/services/data_plane_manager/src/main/java/com/futurewei/alcor/dataplane/service/ovs/NeighborService.java new file mode 100644 index 000000000..b621d3fff --- /dev/null +++ b/services/data_plane_manager/src/main/java/com/futurewei/alcor/dataplane/service/ovs/NeighborService.java @@ -0,0 +1,131 @@ +/* +Copyright 2019 The Alcor Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ +package com.futurewei.alcor.dataplane.service.ovs; + +import com.futurewei.alcor.dataplane.entity.MulticastGoalState; +import com.futurewei.alcor.dataplane.entity.UnicastGoalState; +import com.futurewei.alcor.dataplane.exception.NeighborInfoNotFound; +import com.futurewei.alcor.dataplane.exception.PortFixedIpNotFound; +import com.futurewei.alcor.schema.Common; +import com.futurewei.alcor.schema.Neighbor; +import com.futurewei.alcor.schema.Port; +import com.futurewei.alcor.web.entity.dataplane.NeighborEntry; +import com.futurewei.alcor.web.entity.dataplane.NeighborInfo; +import com.futurewei.alcor.web.entity.dataplane.v2.NetworkConfiguration; +import org.springframework.stereotype.Service; + +import java.util.*; + +@Service +public class NeighborService extends ResourceService { + public Neighbor.NeighborState buildNeighborState(NeighborEntry neighborEntry, NeighborInfo neighborInfo, Common.OperationType operationType) { + Neighbor.NeighborConfiguration.Builder neighborConfigBuilder = Neighbor.NeighborConfiguration.newBuilder(); + neighborConfigBuilder.setRevisionNumber(FORMAT_REVISION_NUMBER); + //neighborConfigBuilder.setId(); // TODO: We are going to need this per latest ACA change + neighborConfigBuilder.setVpcId(neighborInfo.getVpcId()); + //neighborConfigBuilder.setName(); + neighborConfigBuilder.setMacAddress(neighborInfo.getPortMac()); + neighborConfigBuilder.setHostIpAddress(neighborInfo.getHostIp()); + Neighbor.NeighborType neighborType = Neighbor.NeighborType.valueOf(neighborEntry.getNeighborType().getType()); + + //TODO:setNeighborHostDvrMac + //neighborConfigBuilder.setNeighborHostDvrMac(); + Neighbor.NeighborConfiguration.FixedIp.Builder fixedIpBuilder = Neighbor.NeighborConfiguration.FixedIp.newBuilder(); + fixedIpBuilder.setSubnetId(neighborInfo.getSubnetId()); + fixedIpBuilder.setIpAddress(neighborInfo.getPortIp()); + fixedIpBuilder.setNeighborType(neighborType); + neighborConfigBuilder.addFixedIps(fixedIpBuilder.build()); + //TODO:setAllowAddressPairs + //neighborConfigBuilder.setAllowAddressPairs(); + + Neighbor.NeighborState.Builder neighborStateBuilder = Neighbor.NeighborState.newBuilder(); + neighborStateBuilder.setOperationType(operationType); + neighborStateBuilder.setConfiguration(neighborConfigBuilder.build()); + + return neighborStateBuilder.build(); + } + + public void buildNeighborStates(NetworkConfiguration networkConfig, String hostIp, + UnicastGoalState unicastGoalState, + MulticastGoalState multicastGoalState) throws Exception { + Map neighborInfos = networkConfig.getNeighborInfos(); + if (neighborInfos == null || neighborInfos.size() == 0) { + return; + } + + Map> neighborTable = networkConfig.getNeighborTable(); + if (neighborTable == null || neighborTable.size() == 0) { + return; + } + + List portStates = unicastGoalState.getGoalStateBuilder().getPortStatesList(); + if (portStates == null || portStates.size() == 0) { + return; + } + + List multicastNeighborEntries = new ArrayList<>(); + for (Port.PortState portState: portStates) { + List fixedIps = portState.getConfiguration().getFixedIpsList(); + if (fixedIps == null) { + throw new PortFixedIpNotFound(); + } + + for (Port.PortConfiguration.FixedIp fixedIp: fixedIps) { + List neighborEntries = neighborTable.get(fixedIp.getIpAddress()); + if (neighborEntries == null) { + throw new NeighborInfoNotFound(); + } + + for (NeighborEntry neighborEntry: neighborEntries) { + NeighborInfo neighborInfo = neighborInfos.get(neighborEntry.getNeighborIp()); + if (neighborInfo == null) { + throw new NeighborInfoNotFound(); + } + + if (hostIp.equals(neighborInfo.getHostIp())) { + continue; + } + + unicastGoalState.getGoalStateBuilder().addNeighborStates(buildNeighborState( + neighborEntry, neighborInfo, networkConfig.getOpType())); + } + + multicastNeighborEntries.addAll(neighborEntries); + } + } + + Set neighborInfoSet = new HashSet<>(); + for (NeighborEntry neighborEntry: multicastNeighborEntries) { + String localIp = neighborEntry.getLocalIp(); + String neighborIp = neighborEntry.getNeighborIp(); + NeighborInfo neighborInfo1 = neighborInfos.get(localIp); + NeighborInfo neighborInfo2 = neighborInfos.get(neighborIp); + if (neighborInfo1 == null || neighborInfo2 == null) { + throw new NeighborInfoNotFound(); + } + + if (!multicastGoalState.getHostIps().contains(neighborInfo2.getHostIp())) { + multicastGoalState.getHostIps().add(neighborInfo2.getHostIp()); + } + + if (!neighborInfoSet.contains(neighborInfo1)) { + multicastGoalState.getGoalStateBuilder().addNeighborStates(buildNeighborState( + neighborEntry, neighborInfo1, networkConfig.getOpType())); + neighborInfoSet.add(neighborInfo1); + } + } + } +} diff --git a/services/data_plane_manager/src/main/java/com/futurewei/alcor/dataplane/service/ovs/PortService.java b/services/data_plane_manager/src/main/java/com/futurewei/alcor/dataplane/service/ovs/PortService.java new file mode 100644 index 000000000..bedd7f83c --- /dev/null +++ b/services/data_plane_manager/src/main/java/com/futurewei/alcor/dataplane/service/ovs/PortService.java @@ -0,0 +1,83 @@ +/* +Copyright 2019 The Alcor Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ +package com.futurewei.alcor.dataplane.service.ovs; + +import com.futurewei.alcor.dataplane.entity.UnicastGoalState; +import com.futurewei.alcor.schema.Common; +import com.futurewei.alcor.schema.Port; +import com.futurewei.alcor.web.entity.dataplane.InternalPortEntity; +import com.futurewei.alcor.web.entity.dataplane.v2.NetworkConfiguration; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class PortService extends ResourceService { + public void buildPortState(NetworkConfiguration networkConfig, List portEntities, + UnicastGoalState unicastGoalState) { + for (InternalPortEntity portEntity: portEntities) { + Port.PortConfiguration.Builder portConfigBuilder = Port.PortConfiguration.newBuilder(); + portConfigBuilder.setRevisionNumber(FORMAT_REVISION_NUMBER); + portConfigBuilder.setId(portEntity.getId()); + portConfigBuilder.setUpdateType(Common.UpdateType.FULL); + portConfigBuilder.setVpcId(portEntity.getVpcId()); + + if (portEntity.getName() != null) { + portConfigBuilder.setName(portEntity.getName()); + } + + portConfigBuilder.setMacAddress(portEntity.getMacAddress()); + portConfigBuilder.setAdminStateUp(portEntity.isAdminStateUp()); + + Port.PortConfiguration.HostInfo.Builder hostInfoBuilder = Port.PortConfiguration.HostInfo.newBuilder(); + hostInfoBuilder.setIpAddress(portEntity.getBindingHostIP()); + //TODO: Do we need mac address? + //hostInfoBuilder.setMacAddress() + portConfigBuilder.setHostInfo(hostInfoBuilder.build()); + if (portEntity.getFixedIps() != null) { + portEntity.getFixedIps().forEach(fixedIp -> { + Port.PortConfiguration.FixedIp.Builder fixedIpBuilder = Port.PortConfiguration.FixedIp.newBuilder(); + fixedIpBuilder.setSubnetId(fixedIp.getSubnetId()); + fixedIpBuilder.setIpAddress(fixedIp.getIpAddress()); + portConfigBuilder.addFixedIps(fixedIpBuilder.build()); + }); + } + + if (portEntity.getAllowedAddressPairs() != null) { + portEntity.getAllowedAddressPairs().forEach(pair -> { + Port.PortConfiguration.AllowAddressPair.Builder allowAddressPairBuilder = Port.PortConfiguration.AllowAddressPair.newBuilder(); + allowAddressPairBuilder.setIpAddress(pair.getIpAddress()); + allowAddressPairBuilder.setMacAddress(pair.getMacAddress()); + portConfigBuilder.addAllowAddressPairs(allowAddressPairBuilder.build()); + }); + } + + if (portEntity.getSecurityGroups() != null) { + portEntity.getSecurityGroups().forEach(securityGroupId-> { + Port.PortConfiguration.SecurityGroupId.Builder securityGroupIdBuilder = Port.PortConfiguration.SecurityGroupId.newBuilder(); + securityGroupIdBuilder.setId(securityGroupId); + portConfigBuilder.addSecurityGroupIds(securityGroupIdBuilder.build()); + }); + } + + //PortState + Port.PortState.Builder portStateBuilder = Port.PortState.newBuilder(); + portStateBuilder.setOperationType(networkConfig.getOpType()); + portStateBuilder.setConfiguration(portConfigBuilder.build()); + unicastGoalState.getGoalStateBuilder().addPortStates(portStateBuilder.build()); + } + } +} diff --git a/services/data_plane_manager/src/main/java/com/futurewei/alcor/dataplane/service/ovs/ResourceService.java b/services/data_plane_manager/src/main/java/com/futurewei/alcor/dataplane/service/ovs/ResourceService.java new file mode 100644 index 000000000..6cee2e732 --- /dev/null +++ b/services/data_plane_manager/src/main/java/com/futurewei/alcor/dataplane/service/ovs/ResourceService.java @@ -0,0 +1,37 @@ +/* +Copyright 2019 The Alcor Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ +package com.futurewei.alcor.dataplane.service.ovs; + +import com.futurewei.alcor.schema.Common.OperationType; + +public class ResourceService { + protected static final int FORMAT_REVISION_NUMBER = 1; + + protected OperationType getOperationType(com.futurewei.alcor.common.enumClass.OperationType operationType) { + switch (operationType) { + case CREATE: + return OperationType.CREATE; + case UPDATE: + return OperationType.UPDATE; + case INFO: + return OperationType.INFO; + case DELETE: + return OperationType.DELETE; + default: + return OperationType.UNRECOGNIZED; + } + } +} diff --git a/services/data_plane_manager/src/main/java/com/futurewei/alcor/dataplane/service/ovs/RouterService.java b/services/data_plane_manager/src/main/java/com/futurewei/alcor/dataplane/service/ovs/RouterService.java new file mode 100644 index 000000000..3d7eed702 --- /dev/null +++ b/services/data_plane_manager/src/main/java/com/futurewei/alcor/dataplane/service/ovs/RouterService.java @@ -0,0 +1,144 @@ +/* +Copyright 2019 The Alcor Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ +package com.futurewei.alcor.dataplane.service.ovs; + +import com.futurewei.alcor.common.enumClass.VpcRouteTarget; +import com.futurewei.alcor.dataplane.entity.UnicastGoalState; +import com.futurewei.alcor.schema.Goalstate; +import com.futurewei.alcor.schema.Port; +import com.futurewei.alcor.schema.Router; +import com.futurewei.alcor.web.entity.dataplane.InternalSubnetEntity; +import com.futurewei.alcor.web.entity.dataplane.v2.NetworkConfiguration; +import com.futurewei.alcor.web.entity.route.InternalRouterInfo; +import com.futurewei.alcor.web.entity.route.InternalRoutingRule; +import com.futurewei.alcor.web.entity.route.InternalSubnetRoutingTable; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +@Service +public class RouterService extends ResourceService { + @Autowired + private SubnetService subnetService; + + private Router.DestinationType getDestinationType(VpcRouteTarget vpcRouteTarget) { + switch (vpcRouteTarget) { + case LOCAL: + return Router.DestinationType.INTERNET; + case INTERNET_GW: + return Router.DestinationType.VPC_GW; + case NAT_GW: + default: + return Router.DestinationType.UNRECOGNIZED; + } + } + + public void buildRouterState(InternalRouterInfo routerInfo, InternalSubnetRoutingTable subnetRoutingTable, UnicastGoalState unicastGoalState) { Router.RouterConfiguration.SubnetRoutingTable.Builder subnetRoutingTableBuilder = Router.RouterConfiguration.SubnetRoutingTable.newBuilder(); + String subnetId = subnetRoutingTable.getSubnetId(); + subnetRoutingTableBuilder.setSubnetId(subnetId); + List routingRules = subnetRoutingTable.getRoutingRules(); + if (routingRules == null || routingRules.size() == 0) { + return; + } + + for (InternalRoutingRule routingRule: routingRules) { + Router.RouterConfiguration.RoutingRule.Builder routingRuleBuilder = Router.RouterConfiguration.RoutingRule.newBuilder(); + routingRuleBuilder.setOperationType(getOperationType(routingRule.getOperationType())); + routingRuleBuilder.setId(routingRule.getId()); + routingRuleBuilder.setName(routingRule.getName()); + routingRuleBuilder.setDestination(routingRule.getDestination()); + routingRuleBuilder.setNextHopIp(routingRule.getNextHopIp()); + routingRuleBuilder.setPriority(Integer.parseInt(routingRule.getPriority())); + + if (routingRule.getRoutingRuleExtraInfo() != null) { + Router.RouterConfiguration.RoutingRuleExtraInfo.Builder extraInfoBuilder = Router.RouterConfiguration.RoutingRuleExtraInfo.newBuilder(); + extraInfoBuilder.setDestinationType(getDestinationType( + routingRule.getRoutingRuleExtraInfo().getDestinationType())); + extraInfoBuilder.setNextHopMac(routingRule.getRoutingRuleExtraInfo().getNextHopMac()); + routingRuleBuilder.setRoutingRuleExtraInfo(extraInfoBuilder.build()); + } + + subnetRoutingTableBuilder.addRoutingRules(routingRuleBuilder.build()); + } + + List subnetRoutingTablesList = new ArrayList<>(); + subnetRoutingTablesList.add(subnetRoutingTableBuilder.build()); + + Goalstate.GoalState.Builder goalStateBuilder = unicastGoalState.getGoalStateBuilder(); + List routerStatesBuilders = goalStateBuilder.getRouterStatesBuilderList(); + if (routerStatesBuilders != null && routerStatesBuilders.size() > 0) { + subnetRoutingTablesList.addAll(goalStateBuilder. + getRouterStatesBuilder(0). + getConfiguration(). + getSubnetRoutingTablesList()); + goalStateBuilder.removeRouterStates(0); + } + + Router.RouterConfiguration.Builder routerConfigBuilder = Router.RouterConfiguration.newBuilder(); + routerConfigBuilder.setRevisionNumber(FORMAT_REVISION_NUMBER); + + //TODO: where does the hostDvrMacAddress come from ? + routerConfigBuilder.setHostDvrMacAddress(routerInfo.getRouterConfiguration().getHostDvrMac()); + + routerConfigBuilder.addAllSubnetRoutingTables(subnetRoutingTablesList); + Router.RouterState.Builder routerStateBuilder = Router.RouterState.newBuilder(); + routerStateBuilder.setConfiguration(routerConfigBuilder.build()); + goalStateBuilder.addRouterStates(routerStateBuilder.build()); + } + + public void buildRouterStates(NetworkConfiguration networkConfig, UnicastGoalState unicastGoalState) throws Exception { + List portStates = unicastGoalState.getGoalStateBuilder().getPortStatesList(); + if (portStates == null || portStates.size() == 0) { + return; + } + + Set subnetIds = new HashSet<>(); + for (Port.PortState portState: portStates) { + List fixedIps = portState.getConfiguration().getFixedIpsList(); + for (Port.PortConfiguration.FixedIp fixedIp: fixedIps) { + InternalSubnetEntity internalSubnetEntity = + subnetService.getInternalSubnetEntity(networkConfig, fixedIp.getSubnetId()); + subnetIds.add(internalSubnetEntity.getId()); + } + } + + List internalRouterInfos = networkConfig.getInternalRouterInfos(); + if (internalRouterInfos == null || internalRouterInfos.size() == 0) { + return; + } + + for (String subnetId: subnetIds) { + for (InternalRouterInfo routerInfo : internalRouterInfos) { + List subnetRoutingTables = + routerInfo.getRouterConfiguration().getSubnetRoutingTables(); + if (subnetRoutingTables == null) { + continue; + } + + for (InternalSubnetRoutingTable subnetRoutingTable : subnetRoutingTables) { + if (subnetId.equals(subnetRoutingTable.getSubnetId())) { + buildRouterState(routerInfo, subnetRoutingTable, unicastGoalState); + break; + } + } + } + } + } +} diff --git a/services/data_plane_manager/src/main/java/com/futurewei/alcor/dataplane/service/ovs/SecurityGroupService.java b/services/data_plane_manager/src/main/java/com/futurewei/alcor/dataplane/service/ovs/SecurityGroupService.java new file mode 100644 index 000000000..e483dc943 --- /dev/null +++ b/services/data_plane_manager/src/main/java/com/futurewei/alcor/dataplane/service/ovs/SecurityGroupService.java @@ -0,0 +1,101 @@ +/* +Copyright 2019 The Alcor Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ +package com.futurewei.alcor.dataplane.service.ovs; + +import com.futurewei.alcor.dataplane.entity.UnicastGoalState; +import com.futurewei.alcor.dataplane.exception.SecurityGroupNotFound; +import com.futurewei.alcor.dataplane.exception.SecurityGroupRuleNotFound; +import com.futurewei.alcor.schema.Common; +import com.futurewei.alcor.schema.Port; +import com.futurewei.alcor.web.entity.dataplane.v2.NetworkConfiguration; +import com.futurewei.alcor.web.entity.securitygroup.SecurityGroup; +import com.futurewei.alcor.web.entity.securitygroup.SecurityGroupRule; +import org.springframework.stereotype.Service; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +@Service +public class SecurityGroupService extends ResourceService { + private SecurityGroup getSecurityGroup(NetworkConfiguration networkConfig, String securityGroupId) throws Exception { + SecurityGroup result = null; + for (SecurityGroup securityGroup: networkConfig.getSecurityGroups()) { + if (securityGroup.getId().equals(securityGroupId)) { + result = securityGroup; + break; + } + } + + if (result == null) { + throw new SecurityGroupNotFound(); + } + + return result; + } + + + public void buildSecurityGroupStates(NetworkConfiguration networkConfig, UnicastGoalState unicastGoalState) throws Exception { + List portStates = unicastGoalState.getGoalStateBuilder().getPortStatesList(); + if (portStates == null || portStates.size() == 0) { + return; + } + + Set securityGroupIds = new HashSet<>(); + for (Port.PortState portState: portStates) { + List securityGroupIdList= portState.getConfiguration().getSecurityGroupIdsList(); + if (securityGroupIdList != null && securityGroupIdList.size() >0) { + securityGroupIds.addAll(securityGroupIdList.stream() + .map(Port.PortConfiguration.SecurityGroupId::getId) + .collect(Collectors.toList())); + } + } + + for (String securityGroupId: securityGroupIds) { + SecurityGroup securityGroup = getSecurityGroup(networkConfig, securityGroupId); + com.futurewei.alcor.schema.SecurityGroup.SecurityGroupConfiguration.Builder securityGroupConfigBuilder = com.futurewei.alcor.schema.SecurityGroup.SecurityGroupConfiguration.newBuilder(); + securityGroupConfigBuilder.setRevisionNumber(FORMAT_REVISION_NUMBER); + securityGroupConfigBuilder.setId(securityGroup.getId()); + //securityGroupConfigBuilder.setVpcId(); + securityGroupConfigBuilder.setName(securityGroup.getName()); + + if (securityGroup.getSecurityGroupRules() == null) { + throw new SecurityGroupRuleNotFound(); + } + + for (SecurityGroupRule securityGroupRule: securityGroup.getSecurityGroupRules()) { + com.futurewei.alcor.schema.SecurityGroup.SecurityGroupConfiguration.SecurityGroupRule.Builder securityGroupRuleBuilder = + com.futurewei.alcor.schema.SecurityGroup.SecurityGroupConfiguration.SecurityGroupRule.newBuilder(); + securityGroupRuleBuilder.setSecurityGroupId(securityGroup.getId()); + securityGroupRuleBuilder.setId(securityGroupRule.getId()); + securityGroupRuleBuilder.setDirection(com.futurewei.alcor.schema.SecurityGroup.SecurityGroupConfiguration.Direction.valueOf(securityGroupRule.getDirection())); + securityGroupRuleBuilder.setEthertype(Common.EtherType.valueOf(securityGroupRule.getEtherType())); + securityGroupRuleBuilder.setProtocol(Common.Protocol.valueOf(securityGroupRule.getProtocol())); + securityGroupRuleBuilder.setPortRangeMin(securityGroupRule.getPortRangeMin()); + securityGroupRuleBuilder.setPortRangeMax(securityGroupRule.getPortRangeMax()); + securityGroupRuleBuilder.setRemoteIpPrefix(securityGroupRule.getRemoteIpPrefix()); + securityGroupRuleBuilder.setRemoteGroupId(securityGroupRule.getRemoteGroupId()); + securityGroupConfigBuilder.addSecurityGroupRules(securityGroupRuleBuilder.build()); + } + + com.futurewei.alcor.schema.SecurityGroup.SecurityGroupState.Builder securityGroupStateBuilder = com.futurewei.alcor.schema.SecurityGroup.SecurityGroupState.newBuilder(); + securityGroupStateBuilder.setOperationType(networkConfig.getOpType()); + securityGroupStateBuilder.setConfiguration(securityGroupConfigBuilder.build()); + unicastGoalState.getGoalStateBuilder().addSecurityGroupStates(securityGroupStateBuilder.build()); + } + } +} diff --git a/services/data_plane_manager/src/main/java/com/futurewei/alcor/dataplane/service/ovs/SubnetService.java b/services/data_plane_manager/src/main/java/com/futurewei/alcor/dataplane/service/ovs/SubnetService.java new file mode 100644 index 000000000..bcef84857 --- /dev/null +++ b/services/data_plane_manager/src/main/java/com/futurewei/alcor/dataplane/service/ovs/SubnetService.java @@ -0,0 +1,93 @@ +/* +Copyright 2019 The Alcor Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ +package com.futurewei.alcor.dataplane.service.ovs; + +import com.futurewei.alcor.dataplane.entity.UnicastGoalState; +import com.futurewei.alcor.dataplane.exception.SubnetEntityNotFound; +import com.futurewei.alcor.schema.Common; +import com.futurewei.alcor.schema.Port; +import com.futurewei.alcor.schema.Subnet; +import com.futurewei.alcor.web.entity.dataplane.InternalSubnetEntity; +import com.futurewei.alcor.web.entity.dataplane.v2.NetworkConfiguration; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; + +@Service +public class SubnetService extends ResourceService { + public InternalSubnetEntity getInternalSubnetEntity(NetworkConfiguration networkConfig, String subnetId) throws Exception { + InternalSubnetEntity result = null; + for (InternalSubnetEntity internalSubnetEntity: networkConfig.getSubnets()) { + if (internalSubnetEntity.getId().equals(subnetId)) { + result = internalSubnetEntity; + } + } + + if (result == null) { + throw new SubnetEntityNotFound(); + } + + return result; + } + + public void buildSubnetStates(NetworkConfiguration networkConfig, UnicastGoalState unicastGoalState) throws Exception { + List portStates = unicastGoalState.getGoalStateBuilder().getPortStatesList(); + if (portStates == null || portStates.size() == 0) { + return; + } + + List subnetEntities = new ArrayList<>(); + for (Port.PortState portState: portStates) { + for (Port.PortConfiguration.FixedIp fixedIp: portState.getConfiguration().getFixedIpsList()) { + InternalSubnetEntity internalSubnetEntity = getInternalSubnetEntity( + networkConfig, fixedIp.getSubnetId()); + subnetEntities.add(internalSubnetEntity); + } + } + + for (InternalSubnetEntity subnetEntity: subnetEntities) { + Subnet.SubnetConfiguration.Builder subnetConfigBuilder = Subnet.SubnetConfiguration.newBuilder(); + subnetConfigBuilder.setRevisionNumber(FORMAT_REVISION_NUMBER); + subnetConfigBuilder.setId(subnetEntity.getId()); + subnetConfigBuilder.setNetworkType(Common.NetworkType.VXLAN); + subnetConfigBuilder.setVpcId(subnetEntity.getVpcId()); + subnetConfigBuilder.setName(subnetEntity.getName()); + subnetConfigBuilder.setCidr(subnetEntity.getCidr()); + subnetConfigBuilder.setTunnelId(subnetEntity.getTunnelId()); + + Subnet.SubnetConfiguration.Gateway.Builder gatewayBuilder = Subnet.SubnetConfiguration.Gateway.newBuilder(); + gatewayBuilder.setIpAddress(subnetEntity.getGatewayIp()); + gatewayBuilder.setMacAddress(subnetEntity.getGatewayMacAddress()); + subnetConfigBuilder.setGateway(gatewayBuilder.build()); + + if (subnetEntity.getDhcpEnable() != null) { + subnetConfigBuilder.setDhcpEnable(subnetEntity.getDhcpEnable()); + } + + // TODO: need to set DNS based on latest contract + + if (subnetEntity.getAvailabilityZone() != null) { + subnetConfigBuilder.setAvailabilityZone(subnetEntity.getAvailabilityZone()); + } + + Subnet.SubnetState.Builder subnetStateBuilder = Subnet.SubnetState.newBuilder(); + subnetStateBuilder.setOperationType(networkConfig.getOpType()); + subnetStateBuilder.setConfiguration(subnetConfigBuilder.build()); + unicastGoalState.getGoalStateBuilder().addSubnetStates(subnetStateBuilder.build()); + } + } +} diff --git a/services/data_plane_manager/src/main/java/com/futurewei/alcor/dataplane/service/ovs/VpcService.java b/services/data_plane_manager/src/main/java/com/futurewei/alcor/dataplane/service/ovs/VpcService.java new file mode 100644 index 000000000..ee2c09fb8 --- /dev/null +++ b/services/data_plane_manager/src/main/java/com/futurewei/alcor/dataplane/service/ovs/VpcService.java @@ -0,0 +1,88 @@ +/* +Copyright 2019 The Alcor Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ +package com.futurewei.alcor.dataplane.service.ovs; + +import com.futurewei.alcor.dataplane.entity.UnicastGoalState; +import com.futurewei.alcor.dataplane.exception.VpcEntityNotFound; +import com.futurewei.alcor.schema.Port; +import com.futurewei.alcor.schema.Vpc; +import com.futurewei.alcor.web.entity.dataplane.InternalSubnetEntity; +import com.futurewei.alcor.web.entity.dataplane.v2.NetworkConfiguration; +import com.futurewei.alcor.web.entity.vpc.VpcEntity; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class VpcService extends ResourceService { + private VpcEntity getVpcEntity(NetworkConfiguration networkConfig, String vpcId) throws Exception { + VpcEntity result = null; + for (VpcEntity vpcEntity: networkConfig.getVpcs()) { + if (vpcEntity.getId().equals(vpcId)) { + result = vpcEntity; + } + } + + if (result == null) { + throw new VpcEntityNotFound(); + } + + return result; + } + + public void buildVpcStates(NetworkConfiguration networkConfig, UnicastGoalState unicastGoalState) throws Exception { + List portStates = unicastGoalState.getGoalStateBuilder().getPortStatesList(); + if (portStates == null || portStates.size() == 0) { + return; + } + + for (Port.PortState portState: portStates) { + VpcEntity vpcEntity = getVpcEntity(networkConfig, portState.getConfiguration().getVpcId()); + Vpc.VpcConfiguration.Builder vpcConfigBuilder = Vpc.VpcConfiguration.newBuilder(); + vpcConfigBuilder.setRevisionNumber(FORMAT_REVISION_NUMBER); + vpcConfigBuilder.setId(vpcEntity.getId()); + vpcConfigBuilder.setProjectId(vpcEntity.getProjectId()); + + if (vpcEntity.getName() != null) { + vpcConfigBuilder.setName(vpcEntity.getName()); + } + + if (vpcEntity.getCidr() != null) { + vpcConfigBuilder.setCidr(vpcEntity.getCidr()); + } + + //vpcConfigBuilder.setTunnelId(); + + if (networkConfig.getSubnets() != null) { + networkConfig.getSubnets().stream() + .filter(s -> s.getVpcId().equals(vpcEntity.getId())) + .map(InternalSubnetEntity::getId) + .forEach(id -> { + Vpc.VpcConfiguration.SubnetId.Builder subnetIdBuilder = Vpc.VpcConfiguration.SubnetId.newBuilder(); + subnetIdBuilder.setId(id); + vpcConfigBuilder.addSubnetIds(subnetIdBuilder.build()); + }); + } + //set routes here + + Vpc.VpcState.Builder vpcStateBuilder = Vpc.VpcState.newBuilder(); + vpcStateBuilder.setOperationType(networkConfig.getOpType()); + vpcStateBuilder.setConfiguration(vpcConfigBuilder.build()); + + unicastGoalState.getGoalStateBuilder().addVpcStates(vpcStateBuilder.build()); + } + } +}