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

Commit

Permalink
Add local cache for DPM
Browse files Browse the repository at this point in the history
  • Loading branch information
chenpiaoping committed Nov 26, 2020
1 parent 2d1490d commit f45e561
Show file tree
Hide file tree
Showing 10 changed files with 276 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.futurewei.alcor.dataplane.cache;

import com.futurewei.alcor.web.entity.dataplane.v2.NetworkConfiguration;
import com.futurewei.alcor.web.entity.subnet.InternalSubnetPorts;

public interface LocalCache {
void addSubnetPorts(NetworkConfiguration networkConfig) throws Exception;
void updateSubnetPorts(NetworkConfiguration networkConfig) throws Exception;
void deleteSubnetPorts(NetworkConfiguration networkConfig);
InternalSubnetPorts getSubnetPorts(String subnetId) throws Exception;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
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.cache;

import com.futurewei.alcor.dataplane.exception.SubnetEntityNotFound;
import com.futurewei.alcor.web.entity.dataplane.InternalPortEntity;
import com.futurewei.alcor.web.entity.dataplane.InternalSubnetEntity;
import com.futurewei.alcor.web.entity.dataplane.v2.NetworkConfiguration;
import com.futurewei.alcor.web.entity.port.PortEntity;
import com.futurewei.alcor.web.entity.port.PortHostInfo;
import com.futurewei.alcor.web.entity.subnet.InternalSubnetPorts;
import com.futurewei.alcor.web.entity.subnet.SubnetEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class LocalCacheImpl implements LocalCache {
@Autowired
private SubnetPortsCache subnetPortsCache;

@Override
public void addSubnetPorts(NetworkConfiguration networkConfig) throws Exception {
List<InternalPortEntity> portEntities = networkConfig.getPortEntities();
if (portEntities == null) {
return;
}

Map<String, InternalSubnetPorts> subnetPortsMap = new HashMap<>();
for (InternalPortEntity portEntity: portEntities) {
List<PortEntity.FixedIp> fixedIps = portEntity.getFixedIps();
if (fixedIps == null) {
continue;
}

for (PortEntity.FixedIp fixedIp: fixedIps) {
String subnetId = fixedIp.getSubnetId();

PortHostInfo portHostInfo = new PortHostInfo();
portHostInfo.setPortMac(portEntity.getMacAddress());
portHostInfo.setPortIp(fixedIp.getIpAddress());
portHostInfo.setPortId(portEntity.getId());
portHostInfo.setHostIp(portEntity.getBindingHostIP());
portHostInfo.setHostId(portEntity.getBindingHostId());

InternalSubnetPorts subnetPorts = subnetPortsMap.get(subnetId);
if (subnetPorts == null) {
SubnetEntity subnetEntity = getSubnetEntity(networkConfig, fixedIp.getSubnetId());
subnetPorts = new InternalSubnetPorts();
subnetPorts.setSubnetId(subnetId);
subnetPorts.setGatewayPortMac(subnetEntity.getGatewayMacAddress());
subnetPorts.setGatewayPortIp(subnetEntity.getGatewayIp());

//FIXME: get the gateway port id
subnetPorts.setGatewayPortId(null);
subnetPorts.setPorts(new ArrayList<>());

subnetPortsMap.put(subnetId, subnetPorts);
}

subnetPorts.getPorts().add(portHostInfo);
}
}

for (Map.Entry<String, InternalSubnetPorts> entry: subnetPortsMap.entrySet()) {
subnetPortsCache.updateSubnetPorts(entry.getValue());
}
}

private SubnetEntity getSubnetEntity(NetworkConfiguration networkConfig, String subnetId) throws Exception {
List<InternalSubnetEntity> subnetEntities = networkConfig.getSubnets();
if (subnetEntities == null) {
throw new SubnetEntityNotFound();
}

for (SubnetEntity subnetEntity: subnetEntities) {
if (subnetId.equals(subnetEntity.getId())) {
return subnetEntity;
}
}

throw new SubnetEntityNotFound();
}

@Override
public void updateSubnetPorts(NetworkConfiguration networkConfig) throws Exception {

}

@Override
public void deleteSubnetPorts(NetworkConfiguration networkConfig) {

}

@Override
public InternalSubnetPorts getSubnetPorts(String subnetId) throws Exception {
return subnetPortsCache.getSubnetPorts(subnetId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
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.cache;

import com.futurewei.alcor.common.db.CacheException;
import com.futurewei.alcor.common.db.CacheFactory;
import com.futurewei.alcor.common.db.ICache;
import com.futurewei.alcor.common.stats.DurationStatistics;
import com.futurewei.alcor.web.entity.subnet.InternalSubnetPorts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Repository;

import java.util.Map;

@Repository
@ComponentScan(value="com.futurewei.alcor.common.db")
public class SubnetPortsCache {
private ICache<String, InternalSubnetPorts> subnetPortsCache;

@Autowired
public SubnetPortsCache(CacheFactory cacheFactory) {
subnetPortsCache = cacheFactory.getCache(InternalSubnetPorts.class);
}

@DurationStatistics
public InternalSubnetPorts getSubnetPorts(String subnetId) throws CacheException {
return subnetPortsCache.get(subnetId);
}

@DurationStatistics
public Map<String, InternalSubnetPorts> getAllSubnetPorts() throws CacheException {
return subnetPortsCache.getAll();
}

@DurationStatistics
public Map<String, InternalSubnetPorts> getAllSubnetPorts(Map<String, Object[]> queryParams) throws CacheException {
return subnetPortsCache.getAll(queryParams);
}

@DurationStatistics
public synchronized void addSubnetPorts(InternalSubnetPorts internalSubnetPorts) throws Exception {
subnetPortsCache.put(internalSubnetPorts.getSubnetId(), internalSubnetPorts);
}

@DurationStatistics
public void updateSubnetPorts(InternalSubnetPorts internalSubnetPorts) throws Exception {
subnetPortsCache.put(internalSubnetPorts.getSubnetId(), internalSubnetPorts);
}

@DurationStatistics
public void deleteSubnetPorts(String subnetId) throws Exception {
subnetPortsCache.remove(subnetId);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package com.futurewei.alcor.dataplane.controller;

import com.futurewei.alcor.common.stats.DurationStatistics;
import com.futurewei.alcor.dataplane.service.DataPlaneService;
import com.futurewei.alcor.dataplane.service.DpmService;
import com.futurewei.alcor.web.entity.dataplane.InternalDPMResultList;
import com.futurewei.alcor.web.entity.dataplane.v2.NetworkConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -28,30 +28,30 @@

@RestController
@ComponentScan(value = "com.futurewei.alcor.common.stats")
public class DataPlaneController {
public class DpmController {

@Autowired
private DataPlaneService dataPlaneService;
private DpmService dpmService;

@PostMapping({"/network-configuration", "v4/network-configuration"})
@ResponseStatus(HttpStatus.CREATED)
@DurationStatistics
public InternalDPMResultList createNetworkConfiguration(@RequestBody NetworkConfiguration networkConfiguration) throws Exception {
checkNetworkConfiguration(networkConfiguration);
return dataPlaneService.createNetworkConfiguration(networkConfiguration);
return dpmService.createNetworkConfiguration(networkConfiguration);
}

@PutMapping({"/network-configuration", "v4/network-configuration"})
@DurationStatistics
public InternalDPMResultList updateNetworkConfiguration(@RequestBody NetworkConfiguration networkConfiguration) throws Exception {
checkNetworkConfiguration(networkConfiguration);
return dataPlaneService.updateNetworkConfiguration(networkConfiguration);
return dpmService.updateNetworkConfiguration(networkConfiguration);
}

@DeleteMapping({"/network-configuration", "v4/network-configuration"})
@DurationStatistics
public InternalDPMResultList deleteNetworkConfiguration(@RequestBody NetworkConfiguration networkConfiguration) throws Exception {
checkNetworkConfiguration(networkConfiguration);
return dataPlaneService.deleteNetworkConfiguration(networkConfiguration);
return dpmService.deleteNetworkConfiguration(networkConfiguration);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import com.futurewei.alcor.web.entity.dataplane.InternalDPMResultList;
import com.futurewei.alcor.web.entity.dataplane.v2.NetworkConfiguration;

public interface DataPlaneService {
public interface DpmService {

/**
* process create network configuration message and send to ACA nodes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
*/
package com.futurewei.alcor.dataplane.service.mizar;

import com.futurewei.alcor.dataplane.service.DataPlaneService;
import com.futurewei.alcor.dataplane.service.DpmService;
import com.futurewei.alcor.web.entity.dataplane.InternalDPMResultList;
import com.futurewei.alcor.web.entity.dataplane.v2.NetworkConfiguration;

public class DataPlaneServiceImpl implements DataPlaneService {
public class DpmServiceImpl implements DpmService {

@Override
public InternalDPMResultList createNetworkConfiguration(NetworkConfiguration networkConfiguration) throws Exception {
Expand Down
Loading

0 comments on commit f45e561

Please sign in to comment.