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

Commit

Permalink
Split DpmService into multiple files according to resource type
Browse files Browse the repository at this point in the history
  • Loading branch information
chenpiaoping committed Nov 26, 2020
1 parent f45e561 commit 3b20ef1
Show file tree
Hide file tree
Showing 9 changed files with 757 additions and 531 deletions.
Original file line number Diff line number Diff line change
@@ -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<Port.PortState> portStates = unicastGoalState.getGoalStateBuilder().getPortStatesList();
if (portStates == null || portStates.size() == 0) {
return;
}

for (Port.PortState portState: portStates) {
String macAddress = portState.getConfiguration().getMacAddress();
List<Port.PortConfiguration.FixedIp> 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());
}
}
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -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<String, NeighborInfo> neighborInfos = networkConfig.getNeighborInfos();
if (neighborInfos == null || neighborInfos.size() == 0) {
return;
}

Map<String, List<NeighborEntry>> neighborTable = networkConfig.getNeighborTable();
if (neighborTable == null || neighborTable.size() == 0) {
return;
}

List<Port.PortState> portStates = unicastGoalState.getGoalStateBuilder().getPortStatesList();
if (portStates == null || portStates.size() == 0) {
return;
}

List<NeighborEntry> multicastNeighborEntries = new ArrayList<>();
for (Port.PortState portState: portStates) {
List<Port.PortConfiguration.FixedIp> fixedIps = portState.getConfiguration().getFixedIpsList();
if (fixedIps == null) {
throw new PortFixedIpNotFound();
}

for (Port.PortConfiguration.FixedIp fixedIp: fixedIps) {
List<NeighborEntry> 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<NeighborInfo> 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);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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<InternalPortEntity> 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());
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Loading

0 comments on commit 3b20ef1

Please sign in to comment.