Commit 83eeee8f authored by Elf's avatar Elf

增加获取卫星topo和卫星IPlist的接口

parent a5debe6d
package top.ninwoo.edgecenter.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
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 java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
@RestController
@RequestMapping("/dockerData")
public class DockerController {
@Autowired
private IpService ipService;
@Autowired
ClusterService clusterService;
@Autowired
private LogicTopoService logicTopoService;
//要将过去的删除掉
@GetMapping("/getIPlist")
public List<String> getIPlist() {
System.out.println("已返回IPlist");
return logicTopoService.getIPlist();
}
//需下发逻辑拓扑后才能获得,todo
@GetMapping("/getTopo")
public int[][] getTopo() {
System.out.println("已返回topo");
return logicTopoService.getTopo(11111l);
}
@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;
}
}
...@@ -152,6 +152,7 @@ public class IndexController { ...@@ -152,6 +152,7 @@ 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;
} }
......
...@@ -13,4 +13,11 @@ public interface LogicTopoService { ...@@ -13,4 +13,11 @@ public interface LogicTopoService {
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容器端
int[][] getTopo(long clusterId);
//返回IPlist到docker容器端
List<String> getIPlist();
} }
...@@ -11,6 +11,8 @@ import top.ninwoo.edgecenter.service.IpService; ...@@ -11,6 +11,8 @@ import top.ninwoo.edgecenter.service.IpService;
import top.ninwoo.edgecenter.service.LogicTopoService; import top.ninwoo.edgecenter.service.LogicTopoService;
import top.ninwoo.utils.service.IptablesService; import top.ninwoo.utils.service.IptablesService;
import java.util.ArrayList;
import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
...@@ -19,6 +21,8 @@ public class LogicTopoServiceImpl implements LogicTopoService { ...@@ -19,6 +21,8 @@ public class LogicTopoServiceImpl implements LogicTopoService {
private final static Logger LOG = LoggerFactory.getLogger(TopologyServiceImpl.class); private final static Logger LOG = LoggerFactory.getLogger(TopologyServiceImpl.class);
// 用于存储集群的拓扑 // 用于存储集群的拓扑
private ConcurrentHashMap<Long, NetworkTopology> clustersTopo = new ConcurrentHashMap<>(); private ConcurrentHashMap<Long, NetworkTopology> clustersTopo = new ConcurrentHashMap<>();
//根据appnames来存储IP
private List<String> IPlist = new ArrayList<String>();
@Value("${bs.sdn-controller.host}") @Value("${bs.sdn-controller.host}")
private String host; private String host;
...@@ -60,6 +64,8 @@ public class LogicTopoServiceImpl implements LogicTopoService { ...@@ -60,6 +64,8 @@ public class LogicTopoServiceImpl implements LogicTopoService {
} }
} }
} }
clustersTopo.put(clusterId, logictopo); clustersTopo.put(clusterId, logictopo);
} }
...@@ -182,4 +188,36 @@ public class LogicTopoServiceImpl implements LogicTopoService { ...@@ -182,4 +188,36 @@ public class LogicTopoServiceImpl implements LogicTopoService {
iptablesService.dropTraffic(appID1, appIP2, appIP1); iptablesService.dropTraffic(appID1, appIP2, appIP1);
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
public int[][] getTopo(long clusterId) {
return clustersTopo.get(clusterId).getTopology();
}
@Override
public List<String> getIPlist() {
return IPlist;
}
} }
...@@ -140,7 +140,7 @@ public class BisheTests { ...@@ -140,7 +140,7 @@ public class BisheTests {
DockerContainer container = new DockerContainer(); DockerContainer container = new DockerContainer();
container.setName("D1"); container.setName("D1");
container.setCommand("sh"); container.setCommand("sh");
container.setImage("joliu/networktest"); container.setImage("test2");
containerDescription.setDockerContainer(container); containerDescription.setDockerContainer(container);
List<ContainerDescription> cds = new ArrayList<>(); List<ContainerDescription> cds = new ArrayList<>();
...@@ -152,7 +152,7 @@ public class BisheTests { ...@@ -152,7 +152,7 @@ public class BisheTests {
DockerContainer disContainer = new DockerContainer(); DockerContainer disContainer = new DockerContainer();
disContainer.setName("D2"); disContainer.setName("D2");
disContainer.setCommand("sh"); disContainer.setCommand("sh");
disContainer.setImage("joliu/networktest"); disContainer.setImage("test2");
dis.setDockerContainer(disContainer); dis.setDockerContainer(disContainer);
cds.add(dis); cds.add(dis);
...@@ -162,15 +162,15 @@ public class BisheTests { ...@@ -162,15 +162,15 @@ public class BisheTests {
DockerContainer disContainer2 = new DockerContainer(); DockerContainer disContainer2 = new DockerContainer();
disContainer2.setName("D3"); disContainer2.setName("D3");
disContainer2.setCommand("sh"); disContainer2.setCommand("sh");
disContainer2.setImage("joliu/networktest"); disContainer2.setImage("test2");
dis2.setDockerContainer(disContainer2); dis2.setDockerContainer(disContainer2);
cds.add(dis2); cds.add(dis2);
String[] appnames = new String[]{"D1", "D2", "D3", "br:ovs"}; String[] appnames = new String[]{"D1", "D2", "D3", "br:ovs"};
int[][] topology = new int[][]{ int[][] topology = new int[][]{
{0, 0, 0 ,0}, {0, 0, 0 ,0},
{0, 0, 0 ,0}, {1, 0, 0 ,0},
{0, 0, 0 ,0}, {0, 1, 0 ,0},
{1, 1, 1 ,0}}; {1, 1, 1 ,0}};
clusterConfig.setDockers(cds); clusterConfig.setDockers(cds);
...@@ -188,6 +188,7 @@ public class BisheTests { ...@@ -188,6 +188,7 @@ public class BisheTests {
//下发逻辑拓扑 //下发逻辑拓扑
clusterService.sendLogicTopoToEdgeNode(clusterConfigs); clusterService.sendLogicTopoToEdgeNode(clusterConfigs);
/*
//改变逻辑拓扑 //改变逻辑拓扑
int[][] newtopology = new int[][]{ int[][] newtopology = new int[][]{
{0, 0, 0, 0 }, {0, 0, 0, 0 },
...@@ -199,6 +200,8 @@ public class BisheTests { ...@@ -199,6 +200,8 @@ public class BisheTests {
//下发修改后的逻辑拓扑 //下发修改后的逻辑拓扑
clusterService.adjustLogicTopoToEdgeNode(clusterConfigs); clusterService.adjustLogicTopoToEdgeNode(clusterConfigs);
*/
} }
// 测试初始化设置 // 测试初始化设置
...@@ -315,6 +318,154 @@ public class BisheTests { ...@@ -315,6 +318,154 @@ public class BisheTests {
//下发修改后逻辑拓扑 //下发修改后逻辑拓扑
clusterService.adjustLogicTopoToEdgeNode(clusterConfigs); clusterService.adjustLogicTopoToEdgeNode(clusterConfigs);
} }
} }
//测试历史功能,需要提前定义好输出文本地址
@Test
public void test4() throws IllegalAccessException, InterruptedException {
ArrayList<SeparatedClusterConfig> clusterConfigs = new ArrayList<>();
SeparatedClusterConfig separatedClusterConfig = new SeparatedClusterConfig();
List<ContainerDescription> cds = new ArrayList<>();
// TODO: 这个ID应该是从借口获取的
separatedClusterConfig.setEdgeNodeId("192.168.190.135:8081");
ClusterConfig clusterConfig = new ClusterConfig();
clusterConfig.setId(11111l);
clusterConfig.setOwner("joliu");
//从文本中获取卫星的数据
WeixingImpl wx = new WeixingImpl();
ToponetImpl tp = new ToponetImpl();
SimData sd = new SimData();
tp.delHistory();
File file = new File("C:\\WorkSpace\\test\\topusim_1.txt");
List<WeiXingData> wxData = wx.iniTopo(file);
int[][] toponet = tp.getTopology(wxData,sd);
//创建容器
for (int k = 0; k < wxData.size(); k++) {
ContainerDescription containerDescription = new ContainerDescription();
containerDescription.setMode("normal");
containerDescription.setReplicas(1);
DockerContainer container = new DockerContainer();
container.setName(wxData.get(k).getName());
container.setCommand("sh");
container.setImage("joliu/networktest");
containerDescription.setDockerContainer(container);
cds.add(containerDescription);
}
clusterConfig.setDockers(cds);
NetworkTopology topo = new NetworkTopology();
topo.setAppNames(tp.getAppNames(wxData));
// 这个参数好像没啥用
topo.setTopologyId(11);
topo.setTopology(toponet);
clusterConfig.setTopology(topo);
separatedClusterConfig.setClusterConfig(clusterConfig);
clusterConfigs.add(separatedClusterConfig);
clusterService.sendClusterConfigToEdgeNode(clusterConfigs);
//下发逻辑拓扑
clusterService.sendLogicTopoToEdgeNode(clusterConfigs);
for (int i = 1; ; i++) {
Thread.sleep(2000);
int time = i * sd.getJiange();
List<WeiXingData> changetp = wx.changeTopo(wxData,time);
topo.setTopology(tp.getTopology(changetp,sd));
tp.history(topo.getAppNames(),topo.getTopology());
//下发修改后逻辑拓扑
clusterService.adjustLogicTopoToEdgeNode(clusterConfigs);
}
}
//测试容器运行程序
@Test
public void test5() {
ArrayList<SeparatedClusterConfig> clusterConfigs = new ArrayList<>();
SeparatedClusterConfig separatedClusterConfig = new SeparatedClusterConfig();
//需修改为下发的IP地址
separatedClusterConfig.setEdgeNodeId("192.168.190.135:8081");
ClusterConfig clusterConfig = new ClusterConfig();
clusterConfig.setId(11111l);
clusterConfig.setOwner("joliu");
ContainerDescription containerDescription = new ContainerDescription();
containerDescription.setMode("normal");
containerDescription.setReplicas(1);
DockerContainer container = new DockerContainer();
container.setName("D1");
container.setCommand("-e PARAMS=\"--name=" +container.getName()+"\"");
container.setImage("joliu/networktest");
containerDescription.setDockerContainer(container);
List<ContainerDescription> cds = new ArrayList<>();
cds.add(containerDescription);
ContainerDescription dis = new ContainerDescription();
dis.setMode("normal");
dis.setReplicas(1);
DockerContainer disContainer = new DockerContainer();
disContainer.setName("D2");
disContainer.setCommand("-e PARAMS=\"--name=" +disContainer.getName()+"\"");
disContainer.setImage("joliu/networktest");
dis.setDockerContainer(disContainer);
cds.add(dis);
ContainerDescription dis2 = new ContainerDescription();
dis2.setMode("normal");
dis2.setReplicas(1);
DockerContainer disContainer2 = new DockerContainer();
disContainer2.setName("D3");
disContainer2.setCommand("-e PARAMS=\"--name=" +disContainer2.getName()+"\"");
disContainer2.setImage("joliu/networktest");
dis2.setDockerContainer(disContainer2);
cds.add(dis2);
String[] appnames = new String[]{"D1", "D2", "D3", "br:ovs"};
int[][] topology = new int[][]{
{0, 0, 0 ,0},
{1, 0, 0 ,0},
{0, 1, 0 ,0},
{1, 1, 1 ,0}};
clusterConfig.setDockers(cds);
NetworkTopology topo = new NetworkTopology();
topo.setAppNames(appnames);
// 这个参数好像没啥用
topo.setTopologyId(11);
topo.setTopology(topology);
clusterConfig.setTopology(topo);
separatedClusterConfig.setClusterConfig(clusterConfig);
clusterConfigs.add(separatedClusterConfig);
clusterService.sendClusterConfigToEdgeNode(clusterConfigs);
//下发逻辑拓扑
clusterService.sendLogicTopoToEdgeNode(clusterConfigs);
/*
//改变逻辑拓扑
int[][] newtopology = new int[][]{
{0, 0, 0, 0 },
{1, 0, 0, 0 },
{1, 1, 0, 0 },
{1, 1, 1, 0 }};
topo.setTopology(newtopology);
//下发修改后的逻辑拓扑
clusterService.adjustLogicTopoToEdgeNode(clusterConfigs);
*/
}
} }
...@@ -80,7 +80,7 @@ public class ToponetImpl implements Toponet { ...@@ -80,7 +80,7 @@ public class ToponetImpl implements Toponet {
bw.write(str + "开始的拓扑结构"+ "\n"); bw.write(str + "开始的拓扑结构"+ "\n");
//todo 需要设计排版 //todo 需要设计排版
int num = appNames.length/2; int num = appNames.length;
int[][] logic = new int[num][num]; int[][] logic = new int[num][num];
for(int i = 0; i<num;i++){ for(int i = 0; i<num;i++){
for(int m =0;m<num;m++){ for(int m =0;m<num;m++){
......
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