Commit 23da4682 authored by Elf's avatar Elf

修改后的代码

parent 3100dd01
...@@ -152,7 +152,6 @@ public class IndexController { ...@@ -152,7 +152,6 @@ public class IndexController {
// 创建网络 // 创建网络
// 使用topo创建工具 // 使用topo创建工具
topologyService.createTopo(clusterConfig.getId(), clusterConfig.getTopology()); topologyService.createTopo(clusterConfig.getId(), clusterConfig.getTopology());
logicTopoService.saveIPlist(clusterConfig.getId(),clusterConfig.getTopology().getAppNames());
return resMap; return resMap;
} }
...@@ -317,16 +316,21 @@ public class IndexController { ...@@ -317,16 +316,21 @@ public class IndexController {
} }
//下发逻辑拓扑 //下发逻辑拓扑
@RequestMapping(value = "/sendLogicTopo", method = RequestMethod.POST) @RequestMapping(value = "/sendLogicTopo", method = RequestMethod.POST)
public void sendLogicTopo(@RequestBody ClusterConfig clusterConfig) { public String sendLogicTopo(@RequestBody ClusterConfig clusterConfig) {
// 创建网络 if(clusterConfig == null){
return "请传入正确的集群设置";
}
// 使用topo创建工具 // 使用topo创建工具
logicTopoService.creatLogicTopo(clusterConfig.getId(), clusterConfig.getTopology()); String res = logicTopoService.creatLogicTopo(clusterConfig.getId(), clusterConfig.getTopology());
//System.out.println("ok"); //System.out.println("ok");
//return "sucess"; return res;
} }
//修改逻辑拓扑 //修改逻辑拓扑
@RequestMapping(value = "/adjustLogicTopo", method = RequestMethod.POST) @RequestMapping(value = "/adjustLogicTopo", method = RequestMethod.POST)
public String adjustLogicTopo(@RequestBody ClusterConfig clusterConfig) { public String adjustLogicTopo(@RequestBody ClusterConfig clusterConfig) {
if(clusterConfig == null){
return "请传入正确的集群设置";
}
// 判断id // 判断id
if (clusterConfig.getId() == 0) { if (clusterConfig.getId() == 0) {
return "没有设置clusterid"; return "没有设置clusterid";
...@@ -335,4 +339,22 @@ public class IndexController { ...@@ -335,4 +339,22 @@ public class IndexController {
//System.out.println("ok?"); //System.out.println("ok?");
return res1; return res1;
} }
//
@GetMapping("/getIPbyName")
public String getIPbyname(@RequestParam(name = "appname")String appname,@RequestParam(name = "clusterId")long clusterId) {
Set<String> cid = clusterService.getContainerIdsByClusterId(clusterId, appname);
if (cid == null){
System.out.println("错误的clusterID");
return "获取失败";
}
String appID = ((String) cid.toArray()[0]);
if(appID ==null){
System.out.println("错误的appnmae");
return "获取失败";
}
String appIP = ipService.getContainerIp(appID).split("/")[0];
return appIP;
}
} }
\ No newline at end of file
...@@ -5,53 +5,28 @@ import org.springframework.web.bind.annotation.GetMapping; ...@@ -5,53 +5,28 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import top.ninwoo.edgecenter.service.ClusterService;
import top.ninwoo.edgecenter.service.IpService;
import top.ninwoo.edgecenter.service.LogicTopoService; import top.ninwoo.edgecenter.service.LogicTopoService;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
@RestController @RestController
@RequestMapping("/dockerData") @RequestMapping("/dockerData")
public class DockerController { public class TransferTopoController {
@Autowired
private IpService ipService;
@Autowired
ClusterService clusterService;
@Autowired @Autowired
private LogicTopoService logicTopoService; private LogicTopoService logicTopoService;
//要将过去的删除掉
@GetMapping("/getIPlist") @GetMapping("/getIPlist")
public List<String> getIPlist() { public List<String> getIPlist(@RequestParam(name = "clusterId")long clusterId) {
System.out.println("已返回IPlist"); //System.out.println("已返回IPlist");
return logicTopoService.getIPlist(); return logicTopoService.getIPlist(clusterId);
} }
//需下发逻辑拓扑后才能获得,todo //需下发逻辑拓扑后才能获得
@GetMapping("/getTopo") @GetMapping("/getTopo")
public int[][] getTopo() { public int[][] getTopo(@RequestParam(name = "clusterId")long clusterId) {
System.out.println("已返回topo"); //System.out.println("已返回topo");
return logicTopoService.getTopo(11111l); return logicTopoService.getTopo(clusterId);
} }
@GetMapping("/getIPbyName")
public String getIPbyname(@RequestParam(name = "appname")String appname) {
//System.out.println("接受到消息了");
//System.out.println(appname);
//这里可能以后要加个传入的集群ID
Set<String> cid1 = clusterService.getContainerIdsByClusterId(11111l, appname);
//System.out.println(cid1);
String appID1 = ((String) cid1.toArray()[0]);
//System.out.println(appID1);
String appIP1 = ipService.getContainerIp(appID1).split("/")[0];
System.out.println(appIP1);
return appIP1;
}
} }
package top.ninwoo.edgecenter.service; package top.ninwoo.edgecenter.service;
import top.ninwoo.common.entity.ClusterConfig;
import top.ninwoo.common.entity.NetworkTopology; import top.ninwoo.common.entity.NetworkTopology;
import java.util.List; import java.util.List;
...@@ -8,16 +7,14 @@ import java.util.List; ...@@ -8,16 +7,14 @@ import java.util.List;
//逻辑网络 //逻辑网络
public interface LogicTopoService { public interface LogicTopoService {
//通过逻辑拓扑创造逻辑网络 //通过逻辑拓扑创造逻辑网络
void creatLogicTopo(long clusterId, NetworkTopology logictopo); String creatLogicTopo(long clusterId, NetworkTopology logictopo);
//调整逻辑网络 //调整逻辑网络
String modifyLogicTopology(long clusterId, NetworkTopology logictopo); String modifyLogicTopology(long clusterId, NetworkTopology logictopo);
//得到逻辑拓扑 //得到逻辑拓扑
NetworkTopology getLogictopo(NetworkTopology oritopo); NetworkTopology getLogictopo(NetworkTopology oritopo);
//储存IPlist
void saveIPlist(long clusterId,String[] appnames);
//返回topo到docker容器端 //返回topo到docker容器端
int[][] getTopo(long clusterId); int[][] getTopo(long clusterId);
//返回IPlist到docker容器端 //返回IPlist到docker容器端
List<String> getIPlist(); List<String> getIPlist(long clusterId);
} }
...@@ -22,63 +22,60 @@ public class LogicTopoServiceImpl implements LogicTopoService { ...@@ -22,63 +22,60 @@ public class LogicTopoServiceImpl implements LogicTopoService {
// 用于存储集群的拓扑 // 用于存储集群的拓扑
private ConcurrentHashMap<Long, NetworkTopology> clustersTopo = new ConcurrentHashMap<>(); private ConcurrentHashMap<Long, NetworkTopology> clustersTopo = new ConcurrentHashMap<>();
//根据appnames来存储IP //根据appnames来存储IP
private List<String> IPlist = new ArrayList<String>(); private ConcurrentHashMap<Long, List<String>> clustersIPlist = new ConcurrentHashMap<>();
@Value("${bs.sdn-controller.host}")
private String host;
@Value("${bs.sdn-controller.port}")
private int port;
@Autowired @Autowired
ClusterService clusterService; private ClusterService clusterService;
@Autowired @Autowired
IpService ipService; private IpService ipService;
@Autowired @Autowired
IptablesService iptablesService; private IptablesService iptablesService;
@Override @Override
public void creatLogicTopo(long clusterId, NetworkTopology oldtopo) { public String creatLogicTopo(long clusterId, NetworkTopology oldtopo) {
List<String> IPlist = new ArrayList<String>();
NetworkTopology logictopo = this.getLogictopo(oldtopo); NetworkTopology logictopo = this.getLogictopo(oldtopo);
if (logictopo == null) { if (logictopo == null) {
LOG.warn("集群[" + clusterId + "]未设置网络"); LOG.warn("集群[" + clusterId + "]未设置网络");
return; return "集群创建失败";
} }
// 校验信息 // 校验信息
String[] cNames = logictopo.getAppNames(); String[] cNames = logictopo.getAppNames();
int[][] topo = logictopo.getTopology(); int[][] topo = logictopo.getTopology();
if (cNames == null || cNames.length <= 1) { if (cNames == null || cNames.length <= 1) {
return; return "集群创建失败";
} }
/* for(int k=0;k<cNames.length;k++){
// 这里声明:cNames中不包含ovs虚拟机,仅包含docker容器组成的逻辑拓扑 Set<String> cid = clusterService.getContainerIdsByClusterId(clusterId, cNames[k]);
for (int i = 1; i < cNames.length; i++) { if (cid == null){
for (int j = 0; j < i; j++) { System.out.println("错误的clusterID");
// 判断每一个节点的连接状态 return "获取失败";
if (topo[i][j] == 0) {
// 如果判断为0,添加IPtabale,使其断开
this.addDropIPtable(clusterId, cNames[i], cNames[j]);
}
} }
} String appID = ((String) cid.toArray()[0]);
if(appID ==null){
*/ System.out.println("错误的appnmae");
return "获取失败";
}
String appIP = ipService.getContainerIp(appID).split("/")[0];
IPlist.add(appIP);
}
clustersTopo.put(clusterId, logictopo); clustersTopo.put(clusterId, logictopo);
clustersIPlist.put(clusterId,IPlist);
return "success";
} }
@Override @Override
public String modifyLogicTopology(long clusterId, NetworkTopology oldtopo) { public String modifyLogicTopology(long clusterId, NetworkTopology oldtopo) {
//System.out.println("我运行了么?");
NetworkTopology logictopo = this.getLogictopo(oldtopo); NetworkTopology logictopo = this.getLogictopo(oldtopo);
// 先进行校验 // 先进行校验
if (logictopo == null) { if (logictopo == null) {
System.out.println("topoloy不能为空"); LOG.error("topoloy不能为空");
return "topoloy不能为空"; return "topoloy不能为空";
} }
...@@ -91,48 +88,26 @@ public class LogicTopoServiceImpl implements LogicTopoService { ...@@ -91,48 +88,26 @@ public class LogicTopoServiceImpl implements LogicTopoService {
NetworkTopology origTopo = clustersTopo.get(clusterId); NetworkTopology origTopo = clustersTopo.get(clusterId);
// 校验topoId是否一致 // 校验topoId是否一致
if (origTopo.getTopologyId() != logictopo.getTopologyId()) { if (origTopo.getTopologyId() != logictopo.getTopologyId()) {
System.out.println("不是相同的网络拓扑,无法更改"); LOG.error("不是相同的网络拓扑,无法更改");
return "不是相同的网络拓扑,无法更改"; return "不是相同的网络拓扑,无法更改";
} }
// 校验成员名称是否相同 // 校验成员名称是否相同
for (int i = 0; i < origTopo.getAppNames().length; i++) { for (int i = 0; i < origTopo.getAppNames().length; i++) {
if (!origTopo.getAppNames()[i].equals(logictopo.getAppNames()[i])) { if (!origTopo.getAppNames()[i].equals(logictopo.getAppNames()[i])) {
System.out.println("App名称必须一致"); LOG.error("App名称必须一致");
return "App名称必须一致"; return "App名称必须一致";
} }
} }
int[][] origTopoArr = origTopo.getTopology(); int[][] origTopoArr = origTopo.getTopology();
int[][] newTopoArr = logictopo.getTopology(); int[][] newTopoArr = logictopo.getTopology();
//System.out.println("康康我的新拓扑怎样呢");
//System.out.println(newTopoArr[2][1]);
// 校验topo是否一致 // 校验topo是否一致
if ((origTopoArr.length != newTopoArr.length) if ((origTopoArr.length != newTopoArr.length)
|| (origTopoArr[0].length != newTopoArr[0].length)) { || (origTopoArr[0].length != newTopoArr[0].length)) {
System.out.println("拓扑大小不一致"); LOG.error("拓扑大小不一致");
return "拓扑大小不一致"; return "拓扑大小不一致";
} }
/*
//System.out.println("终于要更新网络了啦");
// 更新网络拓扑
for (int i = 1; i < newTopoArr.length; i++) {
for (int j = 0; j < i; j++) {
//增加新的 dropIPtable
if (origTopoArr[i][j] == 1 && newTopoArr[i][j] == 0) {
// 创建新的连接
this.addDropIPtable(clusterId, origTopo.getAppNames()[i], origTopo.getAppNames()[j]);
//System.out.println("改了一次");
} else if (origTopoArr[i][j] == 0 && newTopoArr[i][j] == 1) {
// 删除旧的连接
this.addDelDropIPtable(clusterId, origTopo.getAppNames()[i], origTopo.getAppNames()[j]);
//System.out.println("gaile");
}
}
}
*/
clustersTopo.put(clusterId, logictopo); clustersTopo.put(clusterId, logictopo);
return "success"; return "success";
...@@ -195,35 +170,21 @@ public class LogicTopoServiceImpl implements LogicTopoService { ...@@ -195,35 +170,21 @@ public class LogicTopoServiceImpl implements LogicTopoService {
iptablesService.dropTraffic(appID2, appIP1, appIP2); iptablesService.dropTraffic(appID2, appIP1, appIP2);
} }
@Override
public void saveIPlist(long clusterId,String[] appnames){
//获取appnames
//todo 这里用dowhile似乎更快些
int flag = 0;
for (int i = 0; i < appnames.length - 1; i++) {
if (appnames[i].startsWith("br:")) {
break;
}
flag++;
}
for(int k=0;k<flag;k++){
Set<String> cid = clusterService.getContainerIdsByClusterId(clusterId, appnames[k]);
String appID = ((String) cid.toArray()[0]);
String appIP = ipService.getContainerIp(appID).split("/")[0];
IPlist.add(appIP);
}
System.out.println("已储存IPlist");
}
@Override @Override
public int[][] getTopo(long clusterId) { public int[][] getTopo(long clusterId) {
if(!clustersTopo.containsKey(clusterId)){
LOG.error("没有该集群的Topo");
return null;
}
return clustersTopo.get(clusterId).getTopology(); return clustersTopo.get(clusterId).getTopology();
} }
@Override @Override
public List<String> getIPlist() { public List<String> getIPlist(long clusterId) {
return IPlist; if(!clustersIPlist.containsKey(clusterId)){
LOG.error("没有该集群的IPlist");
return new ArrayList<String>();
}
return clustersIPlist.get(clusterId);
} }
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment