Commit fdfd2146 authored by wutu's avatar wutu

第一次提交,解决了utils和spring boot之间的版本冲突

parents
/bishe-edge-center/target/
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>bishe</artifactId>
<groupId>top.ninwoo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>bishe-edge-center</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.1.2.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>top.ninwoo</groupId>
<artifactId>bishe-utils</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package top.ninwoo.edgecenter;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@SpringBootApplication
@MapperScan("top.ninwoo.edgecenter.dao")
public class EdgeCenterApp {
public static void main(String[] args) {
SpringApplication.run(EdgeCenterApp.class, args);
}
}
\ No newline at end of file
package top.ninwoo.edgecenter.balancer;
import java.util.List;
public interface Balancer {
public List<String> balance(int number);
public List<String> balance(int number, int[] weights);
}
package top.ninwoo.edgecenter.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("top.ninwoo.utils")
public class LinuxUtilsConfig {
}
package top.ninwoo.edgecenter.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.ninwoo.utils.service.DockerService;
import java.util.List;
@RestController
public class IndexController {
@Autowired
DockerService dockerService;
@RequestMapping("/index")
public List<String> index() {
dockerService.updateContainers();
List<String> dockerIds = dockerService.getDockerIds();
return dockerIds;
}
}
package top.ninwoo.edgecenter.dao;
import org.apache.ibatis.annotations.Mapper;
import top.ninwoo.edgecenter.entity.User;
@Mapper
public interface UserMapper {
User getUserById(long id);
}
package top.ninwoo.edgecenter.entity;
public class User {
private long userId;
private String password;
public long getUserId() {
return userId;
}
public void setUserId(long userId) {
this.userId = userId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
spring:
application:
name: bishe-edge-center
datasource:
url: jdbc:mysql://127.0.0.1:3306/bishe
username: debian-sys-maint
password: rJu7Hmq3eIozdGwd
mybatis:
configuration:
map-underscore-to-camel-case: true
mapper-locations: classpath:mapping/*.xml
debug: true
server:
port: 8081
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="top.ninwoo.edgecenter.dao.UserMapper">
<select id="getUserById" resultType="top.ninwoo.edgecenter.entity.User">
select * from user where user_id = #{userId}
</select>
</mapper>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>bishe</artifactId>
<groupId>top.ninwoo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>bishe-utils</artifactId>
<properties>
<jersey.version>2.27</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>com.spotify</groupId>
<artifactId>docker-client</artifactId>
<version>8.16.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package top.ninwoo.utils.config;
import com.spotify.docker.client.DefaultDockerClient;
import com.spotify.docker.client.DockerClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import java.net.URI;
@Configuration
@ComponentScan("top.ninwoo.utils")
public class DockerConfig {
@Bean(destroyMethod = "close")
public DockerClient dockerClient() {
return DefaultDockerClient.builder()
.uri(URI.create("http://127.0.0.1:2375"))
.build();
}
}
package top.ninwoo.utils.entity;
public class BridgePort {
private String portId;
private String interfaceId;
private String error;
public BridgePort() {
this.error = "";
}
public BridgePort(String portId, String interfaceId, String error) {
this.portId = portId;
this.interfaceId = interfaceId;
this.error = error;
}
public static class Builder {
private String portId;
private String interfaceId;
private String error;
public Builder() {
this.error = "";
}
public String portId() {
return this.portId;
}
public BridgePort.Builder portId(String portId) {
this.portId = portId;
return this;
}
public String interfaceId() {
return this.interfaceId;
}
public BridgePort.Builder interfaceId(String interfaceId) {
this.interfaceId = interfaceId;
return this;
}
public String error() {
return this.error;
}
public BridgePort.Builder error(String error) {
this.error = error;
return this;
}
public BridgePort build() {
return new BridgePort(this);
}
}
public BridgePort(BridgePort.Builder builder) {
if(builder.portId == null || builder.portId().equals("")) {
throw new RuntimeException("portId cannot be null or empty");
}
this.portId = builder.portId;
this.error = builder.error;
this.interfaceId = builder.interfaceId;
}
public static BridgePort.Builder builder() {
return new BridgePort.Builder();
}
public String getPortId() {
return portId;
}
public void setPortId(String portId) {
this.portId = portId;
}
public String getInterfaceId() {
return interfaceId;
}
public void setInterfaceId(String interfaceId) {
this.interfaceId = interfaceId;
}
public String getError() {
return error;
}
public void setError(String error) {
this.error = error;
}
}
package top.ninwoo.utils.entity;
public class DockerContainer {
private String id;
private String image;
private String command;
private long created;
private String status;
private String ports;
// 这里只使用Container.names的第一个元素作为名称
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getCommand() {
return command;
}
public void setCommand(String command) {
this.command = command;
}
public long getCreated() {
return created;
}
public void setCreated(long created) {
this.created = created;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getPorts() {
return ports;
}
public void setPorts(String ports) {
this.ports = ports;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package top.ninwoo.utils.entity;
import java.util.List;
import java.util.Objects;
/**
* @author joliu
* @date 2019-9-13
*/
public class Ovs {
private String id;
private List<OvsBridge> bridges;
private String ovsVersion;
public String getOvsVersion() {
return ovsVersion;
}
public void setOvsVersion(String ovsVersion) {
this.ovsVersion = ovsVersion;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public List<OvsBridge> getBridges() {
return bridges;
}
public void setBridges(List<OvsBridge> bridges) {
this.bridges = bridges;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Ovs)) {
return false;
}
Ovs ovs = (Ovs) o;
return id.equals(ovs.id) &&
Objects.equals(bridges, ovs.bridges) &&
ovsVersion.equals(ovs.ovsVersion);
}
@Override
public int hashCode() {
return Objects.hash(id, bridges, ovsVersion);
}
}
package top.ninwoo.utils.entity;
import java.util.List;
public class OvsBridge {
private String bridgeId;
private List<BridgePort> ports;
public String getBridgeId() {
return bridgeId;
}
public void setBridgeId(String bridgeId) {
this.bridgeId = bridgeId;
}
public List<BridgePort> getPorts() {
return ports;
}
public void setPorts(List<BridgePort> ports) {
this.ports = ports;
}
}
package top.ninwoo.utils.service;
import top.ninwoo.utils.entity.DockerContainer;
import java.util.List;
public interface DockerService {
List<String> getDockerIds();
List<String> getDockerIds(boolean isAll);
void updateContainers();
void updateContainers(boolean flag);
DockerContainer runDocker(DockerContainer container);
DockerContainer getDockerById(String id);
boolean deleteDockerById(String id);
}
package top.ninwoo.utils.service;
public interface LinuxCtlService {
String runCmd(String cmd);
}
package top.ninwoo.utils.service;
import top.ninwoo.utils.entity.Ovs;
public interface OVSService {
boolean isInstall();
Ovs showDetails();
Ovs parseOvsString(String ovsString);
String getValueInString(String input);
void addBridge(String name);
void setBridgeProtocol(String bridgeName, String protocol);
void setController(String bridgeName, String host, int port);
void delBridge(String name);
}
package top.ninwoo.utils.service;
/**
* @author joliu
* @date 2019-10-16
*/
public interface OvsDockerService {
String addPort(String bridgeName, String devName, String containerId, String ip);
}
\ No newline at end of file
package top.ninwoo.utils.service.impl;
import com.spotify.docker.client.DockerClient;
import com.spotify.docker.client.exceptions.DockerException;
import com.spotify.docker.client.messages.Container;
import com.spotify.docker.client.messages.ContainerInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.ninwoo.utils.entity.DockerContainer;
import top.ninwoo.utils.service.DockerService;
import top.ninwoo.utils.service.LinuxCtlService;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
@Service
public class DockerServiceImpl implements DockerService {
@Autowired
private LinuxCtlService linuxService;
@Autowired
private DockerClient dockerClient;
// 这里考虑要不要区分正在运行中的容器
private ConcurrentHashMap<String, DockerContainer> containersMap = new ConcurrentHashMap<>();
@Override
public List<String> getDockerIds() {
return getDockerIds(false);
}
/**
* 获取容器Id
* @param isAll 是否获取全部的容器id,包含未启动的容器
* @return
*/
@Override
public List<String> getDockerIds(boolean isAll) {
return new ArrayList<String>(containersMap.keySet());
}
/**
* 将DockerClient获取的到结果封装成为我们自定义的数据结构
* @param containers
* @return
*/
public List<DockerContainer> convertDockerResult(List<Container> containers) {
if(containers == null) {
throw new RuntimeException("容器列表不能为null");
}
List<DockerContainer> dockerContainers = new ArrayList<DockerContainer>(containers.size());
containers.forEach(c -> {
DockerContainer container = new DockerContainer();
container.setId(c.id());
container.setCommand(c.command());
container.setCreated(c.created());
container.setPorts(c.portsAsString());
container.setImage(c.image());
container.setStatus(c.status());
container.setName(c.names().get(0));
dockerContainers.add(container);
});
return dockerContainers;
}
@Override
public void updateContainers() {
updateContainers(true);
}
/**
* 更新容器列表
* @param isfull true:全量更新
*/
@Override
public void updateContainers(boolean isfull) {
if(isfull) {
// 清空
containersMap.clear();
List<Container> containers =
null;
try {
containers = dockerClient.listContainers(DockerClient.ListContainersParam.withStatusRunning());
} catch (DockerException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
List<DockerContainer> dockerContainers = convertDockerResult(containers);
dockerContainers.forEach(dockerContainer -> containersMap.put(dockerContainer.getId(), dockerContainer));
} else {
// TODO: 增量备份
}
}
/**
* 使用系统命令 docker run -itd 在后台启动一个容器,
* 使用这种方式的一个重要愿意,是因为我没搞明白docker java启动容器的api到底如何使用
* @param container 输入一个容器的描述信息
* @return 返回容器启动之后真实的描述信息
*/
@Override
public DockerContainer runDocker(DockerContainer container) {
// TODO: 这里启动Docker容器,需要再研究port如何起作用
String cmd = "docker run -itd --name " + container.getName() + " " + container.getImage() + " " + container.getCommand();
String result = linuxService.runCmd(cmd);
if(result.contains("Error")) {
throw new RuntimeException("Run Docker failed!");
}
// TODO:需要从Docker中查询处完整的信息
container.setId(result);
return container;
}
@Override
public DockerContainer getDockerById(String id) {
ContainerInfo containerInfo = null;
try {
containerInfo = dockerClient.inspectContainer(id);
} catch (DockerException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return exchangeFromDockerInfo(containerInfo);
}
public DockerContainer exchangeFromDockerInfo(ContainerInfo containerInfo) {
if(containerInfo == null) {
throw new RuntimeException("ContainerInfo cannot be null");
}
DockerContainer dockerContainer = new DockerContainer();
dockerContainer.setImage(containerInfo.image());
dockerContainer.setName(containerInfo.name());
dockerContainer.setId(containerInfo.id());
dockerContainer.setStatus(containerInfo.state().status());
dockerContainer.setCreated(containerInfo.created().getTime());
return dockerContainer;
}
@Override
public boolean deleteDockerById(String id) {
try {
dockerClient.stopContainer(id, 0);
dockerClient.removeContainer(id);
return true;
} catch (DockerException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return false;
}
}
package top.ninwoo.utils.service.impl;
import org.springframework.stereotype.Service;
import top.ninwoo.utils.service.LinuxCtlService;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
@Service
public class LinuxCtlServiceImpl implements LinuxCtlService {
/**
* 这里存在一个限制,不能执行复合的命令
* @param cmd
* @return
*/
@Override
// TODO: 这个函数中的异常处理是一个问题
public String runCmd(String cmd) {
String result = "";
try {
Process exec = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", cmd});
exec.waitFor();
InputStream in = exec.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String msg;
while((msg = br.readLine())!= null && msg.length() > 0) {
result += msg + "\n";
}
if("".equals(result)) {
// 构建Error
InputStream errorStream = exec.getErrorStream();
BufferedReader ebr = new BufferedReader(new InputStreamReader(errorStream));
String eMsg;
while((eMsg = ebr.readLine())!= null && eMsg.length() > 0) {
result += eMsg + "\n";
}
if(result.equals("")) {
return result;
}
result = "Error: " + result;
}
if(result.length() > 0) {
result = result.substring(0, result.length()-1);
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return result;
}
}
package top.ninwoo.utils.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.ninwoo.utils.entity.BridgePort;
import top.ninwoo.utils.entity.Ovs;
import top.ninwoo.utils.entity.OvsBridge;
import top.ninwoo.utils.service.LinuxCtlService;
import top.ninwoo.utils.service.OVSService;
import java.util.ArrayList;
import java.util.List;
@Service
public class OVSServiceImpl implements OVSService {
@Autowired
private LinuxCtlService linuxCtlService;
@Override
public boolean isInstall() {
// 可能会遇到权限问题
String s = linuxCtlService.runCmd("ovs-vsctl --version");
if(s.contains("Error")) {
return false;
} else {
return true;
}
}
@Override
public Ovs showDetails() {
String res = linuxCtlService.runCmd("echo 'Vudo3423' | sudo -S ovs-vsctl show");
Ovs ovs = parseOvsString(res);
return ovs;
}
/**
* 将返回的字符串转换为Ovs数据结构
* @param ovsString
* @return
*/
@Override
public Ovs parseOvsString(String ovsString) {
if(ovsString == null || "".equals(ovsString)) {
throw new RuntimeException("ovs String cannot be null or empty!");
}
String[] results = ovsString.split("\n");
if(results.length < 2) {
throw new RuntimeException("Illegal ovs Result!");
}
Ovs ovs = new Ovs();
String ovsId = results[0].substring(0,results[0].length()-1);
ovs.setId(ovsId);
String ovsVersion = getValueInString(results[results.length - 1]);
ovs.setOvsVersion(ovsVersion);
int i = 0;
OvsBridge bridge = null;
BridgePort port = null;
List<BridgePort> ports = new ArrayList<>();
List<OvsBridge> bridges = new ArrayList<>();
while(i < results.length) {
// 创建Bridge
if(results[i].contains("Bridge")) {
port = null;
ports.clear();
if(bridge != null) {
// 放入到数组中
bridges.add(bridge);
}
String bridgeId = getValueInString(results[i]);
bridge = new OvsBridge();
bridge.setBridgeId(bridgeId);
}
else if(results[i].contains("Port")) {
if(port != null) {
ports.add(port);
}
String portId = getValueInString(results[i]);
port = new BridgePort();
port.setPortId(portId.replace("\"", ""));
}
else if(results[i].contains("Interface")) {
if(port == null) {
throw new RuntimeException("Cannot find Port");
}
String interfaceId = getValueInString(results[i]);
port.setInterfaceId(interfaceId);
}
else if(results[i].contains("Error")) {
if(port == null) {
throw new RuntimeException("Cannot find Port");
}
String error = getValueInString(results[i]);
port.setError(error);
} else if(results[i].contains("ovs_version")) {
if(port != null) {
ports.add(port);
bridge.setPorts(ports);
bridges.add(bridge);
}
ovs.setBridges(bridges);
}
i++;
}
return ovs;
}
@Override
public String getValueInString(String input) {
String value = input.trim().split(" ")[1];
value = value.replaceAll("\"", "");
return value;
}
@Override
public void addBridge(String name) {
String cmd = "echo 'Vudo3423' | sudo -S ovs-vsctl add-br " + name;
String res = linuxCtlService.runCmd(cmd);
System.out.println(res);
if(res.contains("Error")) {
throw new RuntimeException("linux bridge has existed!");
}
}
@Override
public void setBridgeProtocol(String bridgeName, String protocol) {
String cmd = "echo 'Vudo3423' | sudo -S ovs-vsctl set bridge " + bridgeName + " protocols=" + protocol;
String res = linuxCtlService.runCmd(cmd);
if(res.contains("Error")) {
throw new RuntimeException(res);
}
}
@Override
public void setController(String bridgeName, String host, int port) {
String cmd = "echo 'Vudo3423' | sudo -S ovs-vsctl set-controller " + bridgeName + " tcp:" + host + ":" + port;
String res = linuxCtlService.runCmd(cmd);
if(res.contains("Error")) {
throw new RuntimeException(res);
}
}
@Override
public void delBridge(String name) {
String cmd = "echo 'Vudo3423' | sudo -S ovs-vsctl del-br " + name;
String res = linuxCtlService.runCmd(cmd);
if(res.contains("Error")) {
throw new RuntimeException("bridge not found!");
}
}
}
package top.ninwoo.utils.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.ninwoo.utils.service.LinuxCtlService;
import top.ninwoo.utils.service.OvsDockerService;
@Service
public class OvsDockerServiceImpl implements OvsDockerService {
@Autowired
LinuxCtlService linuxCtlService;
@Override
public String addPort(String bridgeName, String devName,String containerId, String ip) {
String cmd = "ovs-docker add-port " + bridgeName + " " + devName + " " + containerId + " --ipaddress=" + ip;
String res = linuxCtlService.runCmd(cmd);
if(res.contains("Error")) {
throw new RuntimeException(res);
}
return res;
}
}
package top.ninwoo.utils;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import top.ninwoo.utils.config.DockerConfig;
import top.ninwoo.utils.entity.DockerContainer;
import top.ninwoo.utils.service.DockerService;
import top.ninwoo.utils.service.LinuxCtlService;
import java.util.ArrayList;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = DockerConfig.class)
public class DockerServiceTests {
@Autowired
private LinuxCtlService linuxCtlService;
@Autowired
private DockerService dockerService;
@Test
public void testRunLinuxCmd() {
String input = "testRunLinuxCmd";
String cmd = "echo " + input;
String result = linuxCtlService.runCmd(cmd);
Assert.assertEquals(input, result);
cmd = "cat 1xxaaf";
result = linuxCtlService.runCmd(cmd);
Assert.assertEquals("Error: cat: 1xxaaf: 没有那个文件或目录", result);
}
@Test
public void testSudo() {
String result = linuxCtlService.runCmd("echo Vudo3423 | sudo -S ovs-vsctl show");
System.out.println(result);
}
@Test
public void testUpdateDockerService() {
dockerService.updateContainers();
List<String> dockerIds = dockerService.getDockerIds();
dockerIds.forEach(d-> System.out.println(d));
}
@Test
public void testRunDockerService() {
DockerContainer container = new DockerContainer();
List<String> names = new ArrayList<String>();
container.setName("Router4");
container.setImage("joliu/networktest");
container.setCommand("sh");
try {
DockerContainer dockerContainer = dockerService.runDocker(container);
Assert.assertTrue(dockerService.deleteDockerById(dockerContainer.getId()));
} catch (RuntimeException ex) {
Assert.assertEquals("Run Docker failed!", ex.getMessage());
}
}
}
package top.ninwoo.utils;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import top.ninwoo.utils.config.DockerConfig;
import top.ninwoo.utils.entity.BridgePort;
import top.ninwoo.utils.entity.Ovs;
import top.ninwoo.utils.entity.OvsBridge;
import top.ninwoo.utils.service.OVSService;
import java.util.ArrayList;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = DockerConfig.class)
public class OVSServiceTests {
@Autowired
private OVSService ovsService;
@Test
public void testBasicFunc() {
Assert.assertTrue(ovsService.isInstall());
ovsService.showDetails();
}
/**
* 这个测试没跑过
*/
@Test
public void testOvsParser() {
String input = "72238dea-74f4-44c7-bba3-03cb71847197\n" +
" Bridge vnbr\n" +
" Port \"9e66d7fc5c254_l\"\n" +
" Interface \"9e66d7fc5c254_l\"\n" +
" Port \"8935ff9c8d914_l\"\n" +
" Interface \"8935ff9c8d914_l\"\n" +
" Port vnbr\n" +
" Interface vnbr\n" +
" type: internal\n" +
" Port \"abb221b99b8a4_l\"\n" +
" Interface \"abb221b99b8a4_l\"\n" +
" ovs_version: \"2.9.2\"";
Ovs ovs = new Ovs();
ovs.setId("72238dea-74f4-44c7-bba3-03cb71847197");
List<OvsBridge> ovsBridges = new ArrayList<>();
OvsBridge ovsBridge = new OvsBridge();
ovsBridge.setBridgeId("vnbr");
BridgePort port1 = BridgePort.builder().portId("9e66d7fc5c254_l").interfaceId("9e66d7fc5c254_l").build();
BridgePort port2 = BridgePort.builder().portId("8935ff9c8d914_l").interfaceId("8935ff9c8d914_l").build();
BridgePort port3 = BridgePort.builder().portId("vnbr").interfaceId("vnbr").build();
BridgePort port4 = BridgePort.builder().portId("abb221b99b8a4_l").interfaceId("abb221b99b8a4_l").build();
List<BridgePort> bridgePorts = new ArrayList<>();
bridgePorts.add(port1);
bridgePorts.add(port2);
bridgePorts.add(port3);
bridgePorts.add(port4);
ovsBridge.setPorts(bridgePorts);
ovsBridges.add(ovsBridge);
ovs.setBridges(ovsBridges);
ovs.setOvsVersion("2.9.2");
Ovs ovsResult = ovsService.parseOvsString(input);
//Assert.assertEquals(ovs, ovsResult);
System.out.println(ovsResult.equals(ovs));
}
@Test
public void testString() {
String s = " Port \"9e66d7fc5c254_l\"\n";
String valueInString = ovsService.getValueInString(s);
System.out.println(valueInString);
}
@Test
public void controlOvs() {
Ovs ovs = ovsService.showDetails();
System.out.println(ovs);
String bridgeName = "vnbr5";
ovsService.addBridge(bridgeName);
ovsService.setBridgeProtocol(bridgeName, "OpenFlow13");
ovsService.setController(bridgeName, "127.0.0.1", 6653);
ovsService.delBridge(bridgeName);
}
}
package top.ninwoo.utils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import top.ninwoo.utils.config.DockerConfig;
public class SpringMain {
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(DockerConfig.class);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>top.ninwoo</groupId>
<artifactId>bishe</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>bishe-edge-center</module>
<module>bishe-utils</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<target>8</target>
<source>8</source>
</configuration>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
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