Commit e7939cd6 authored by wutu's avatar wutu

更新OvsService接口

parent 200e5658
......@@ -92,4 +92,14 @@ public class BridgePort {
public void setError(String error) {
this.error = error;
}
@Override
public boolean equals(Object obj) {
if(obj instanceof BridgePort) {
return this.error.equals(((BridgePort) obj).getError())
&& this.interfaceId.equals(((BridgePort) obj).getInterfaceId())
&& this.portId.equals(((BridgePort) obj).getPortId());
}
return false;
}
}
......@@ -21,4 +21,22 @@ public class OvsBridge {
public void setPorts(List<BridgePort> ports) {
this.ports = ports;
}
@Override
public boolean equals(Object obj) {
if(obj instanceof OvsBridge) {
OvsBridge bridge = (OvsBridge) obj;
if(bridge.bridgeId.equals(this.bridgeId)) {
if(this.ports.size() == bridge.ports.size()) {
for (int i = 0; i < this.ports.size(); i++) {
if(!this.ports.get(i).equals(bridge.ports.get(i))) {
return false;
}
}
}
return true;
}
}
return false;
}
}
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.impl;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
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.OVSService;
import top.ninwoo.utils.util.LinuxCtlUtils;
import top.ninwoo.utils.util.OvsUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
/**
* @description 拟采用带状态的实现方案,采用单独线程进行异步更新,无论外部请求多少
* 始终限制在一个用户去查询
* @author joliu
*/
@Service
public class OVSServiceImpl implements OVSService {
public class OVSServiceImpl implements OVSService, InitializingBean {
@Autowired
private LinuxCtlUtils linuxCtlUtils;
private OvsUtils ovsUtils;
@Override
public boolean isInstall() {
// 可能会遇到权限问题
String s = linuxCtlUtils.runCmd("ovs-vsctl --version");
if(s.contains("Error")) {
return false;
} else {
return true;
}
}
// TODO: 感觉应该把这个OVS类拆分开
private ConcurrentHashMap<String, OvsBridge> ovsBridges = new ConcurrentHashMap<>();
// 是否安装了ovs
private boolean isInstall = false;
// ovs版本号
private String ovsVersion;
private String ovsId;
@Override
public Ovs showDetails() {
String res = linuxCtlUtils.runCmd("echo 'Vudo3423' | sudo -S ovs-vsctl show");
public void afterPropertiesSet() throws Exception {
isInstall = ovsUtils.isInstall();
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!");
if(!isInstall) {
throw new RuntimeException("Ovs is not install properly!");
}
String[] results = ovsString.split("\n");
if(results.length < 2) {
throw new RuntimeException("Illegal ovs Result!");
// 更新ovsBridge
Ovs ovs = ovsUtils.showDetails();
for (OvsBridge bridge : ovs.getBridges()) {
ovsBridges.put(bridge.getBridgeId(), bridge);
}
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;
ovsVersion = ovs.getOvsVersion();
ovsId = ovs.getId();
}
@Override
public String getValueInString(String input) {
String value = input.trim().split(" ")[1];
value = value.replaceAll("\"", "");
return value;
}
// TODO:定时更新ovs-vsctl
@Scheduled(fixedRate = 500)
public void updateOvsStatus() {
Ovs ovs = ovsUtils.showDetails();
// 第一次检查,判断Bridge列表是否减少
@Override
public void addBridge(String name) {
String cmd = "echo 'Vudo3423' | sudo -S ovs-vsctl add-br " + name;
String res = linuxCtlUtils.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 = linuxCtlUtils.runCmd(cmd);
if(res.contains("Error")) {
throw new RuntimeException(res);
}
}
// 第二次检查,判断是否有新的bridge添加
@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 = linuxCtlUtils.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 = linuxCtlUtils.runCmd(cmd);
if(res.contains("Error")) {
throw new RuntimeException("bridge not found!");
}
}
}
......@@ -34,4 +34,6 @@ public interface DockerUtils {
DockerContainer getDockerById(String id);
boolean deleteDockerById(String id);
String execInDocker(String containerId, String... args);
}
......@@ -20,6 +20,10 @@ public interface OvsUtils {
*/
Ovs showDetails();
Ovs parseOvsString(String ovsString);
String getValueInString(String input);
/**
* ovs-vsctl add-br
* @param name
......
package top.ninwoo.utils.util.impl;
import com.spotify.docker.client.DockerClient;
import com.spotify.docker.client.LogStream;
import com.spotify.docker.client.exceptions.ContainerNotFoundException;
import com.spotify.docker.client.exceptions.DockerException;
import com.spotify.docker.client.messages.Container;
import com.spotify.docker.client.messages.ContainerInfo;
import com.spotify.docker.client.messages.ExecState;
import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;
import org.springframework.beans.factory.annotation.Autowired;
import top.ninwoo.utils.entity.DockerContainer;
import top.ninwoo.utils.util.DockerUtils;
import top.ninwoo.utils.util.LinuxCtlUtils;
import top.ninwoo.utils.util.Utils;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
......@@ -143,4 +148,21 @@ public class DockerUtilsImpl implements DockerUtils {
}
return false;
}
@Override
public String execInDocker(String containerId, String... args) {
String result = "";
try {
String id = dockerClient.execCreate(containerId, args, DockerClient.ExecCreateParam.attachStdout(),
DockerClient.ExecCreateParam.attachStderr()).id();
try(LogStream logStream = dockerClient.execStart(id)) {
result = logStream.readFully();
}
} catch (DockerException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return result;
}
}
......@@ -45,6 +45,7 @@ public class OvsUtilsImpl implements OvsUtils {
* @param ovsString
* @return
*/
@Override
public Ovs parseOvsString(String ovsString) {
if(ovsString == null || "".equals(ovsString)) {
throw new RuntimeException("ovs String cannot be null or empty!");
......@@ -112,6 +113,7 @@ public class OvsUtilsImpl implements OvsUtils {
return ovs;
}
@Override
public String getValueInString(String input) {
String value = input.trim().split(" ")[1];
value = value.replaceAll("\"", "");
......
......@@ -26,4 +26,12 @@ public class DockerUtilsTest {
DockerContainer dockerById = dockerUtils.getDockerById("123123");
Assert.isNull(dockerById);
}
@Test
public void testExecInDocker() {
final String s =
dockerUtils.execInDocker("Router_4", new String[]{"ifconfig"});
System.out.println(s);
}
}
package top.ninwoo.utils;
/**
* @Author joliu
* @Description
* @Date Create in 下午10:50 2019/10/27
*/
public class EntityTests {
}
......@@ -11,6 +11,7 @@ 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 top.ninwoo.utils.util.OvsUtils;
import java.util.ArrayList;
......@@ -20,7 +21,7 @@ import java.util.List;
@ContextConfiguration(classes = DockerConfig.class)
public class OVSServiceTests {
@Autowired
private OVSService ovsService;
private OvsUtils ovsService;
@Test
public void testBasicFunc() {
......@@ -68,7 +69,8 @@ public class OVSServiceTests {
Ovs ovsResult = ovsService.parseOvsString(input);
//Assert.assertEquals(ovs, ovsResult);
System.out.println(ovsResult.equals(ovs));
Assert.assertTrue(ovs.getBridges().get(0).equals(ovsResult.getBridges().get(0)));
//System.out.println(ovsResult.equals(ovs));
}
@Test
......
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