Commit e7939cd6 authored by wutu's avatar wutu

更新OvsService接口

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