Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
O
openXG-WIC-Cnf
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
alex037yang
openXG-WIC-Cnf
Commits
e7939cd6
Commit
e7939cd6
authored
Oct 27, 2019
by
wutu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
更新OvsService接口
parent
200e5658
Changes
11
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
113 additions
and
138 deletions
+113
-138
bishe-utils/src/main/java/top/ninwoo/utils/entity/BridgePort.java
...ils/src/main/java/top/ninwoo/utils/entity/BridgePort.java
+10
-0
bishe-utils/src/main/java/top/ninwoo/utils/entity/OvsBridge.java
...tils/src/main/java/top/ninwoo/utils/entity/OvsBridge.java
+18
-0
bishe-utils/src/main/java/top/ninwoo/utils/service/OVSService.java
...ls/src/main/java/top/ninwoo/utils/service/OVSService.java
+0
-10
bishe-utils/src/main/java/top/ninwoo/utils/service/impl/OVSServiceImpl.java
...in/java/top/ninwoo/utils/service/impl/OVSServiceImpl.java
+34
-126
bishe-utils/src/main/java/top/ninwoo/utils/util/DockerUtils.java
...tils/src/main/java/top/ninwoo/utils/util/DockerUtils.java
+2
-0
bishe-utils/src/main/java/top/ninwoo/utils/util/OvsUtils.java
...e-utils/src/main/java/top/ninwoo/utils/util/OvsUtils.java
+4
-0
bishe-utils/src/main/java/top/ninwoo/utils/util/impl/DockerUtilsImpl.java
...main/java/top/ninwoo/utils/util/impl/DockerUtilsImpl.java
+22
-0
bishe-utils/src/main/java/top/ninwoo/utils/util/impl/OvsUtilsImpl.java
...rc/main/java/top/ninwoo/utils/util/impl/OvsUtilsImpl.java
+2
-0
bishe-utils/src/test/java/top/ninwoo/utils/DockerUtilsTest.java
...utils/src/test/java/top/ninwoo/utils/DockerUtilsTest.java
+8
-0
bishe-utils/src/test/java/top/ninwoo/utils/EntityTests.java
bishe-utils/src/test/java/top/ninwoo/utils/EntityTests.java
+9
-0
bishe-utils/src/test/java/top/ninwoo/utils/OVSServiceTests.java
...utils/src/test/java/top/ninwoo/utils/OVSServiceTests.java
+4
-2
No files found.
bishe-utils/src/main/java/top/ninwoo/utils/entity/BridgePort.java
View file @
e7939cd6
...
@@ -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
;
}
}
}
bishe-utils/src/main/java/top/ninwoo/utils/entity/OvsBridge.java
View file @
e7939cd6
...
@@ -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
;
}
}
}
bishe-utils/src/main/java/top/ninwoo/utils/service/OVSService.java
View file @
e7939cd6
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
);
}
}
bishe-utils/src/main/java/top/ninwoo/utils/service/impl/OVSServiceImpl.java
View file @
e7939cd6
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
linuxCtl
Utils
;
private
OvsUtils
ovs
Utils
;
@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
);
if
(!
isInstall
)
{
return
ovs
;
throw
new
RuntimeException
(
"Ovs is not install properly!"
);
}
/**
* 将返回的字符串转换为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"
);
// 更新ovsBridge
if
(
results
.
length
<
2
)
{
Ovs
ovs
=
ovsUtils
.
showDetails
();
throw
new
RuntimeException
(
"Illegal ovs Result!"
);
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
;
ovsVersion
=
ovs
.
getOvsVersion
();
OvsBridge
bridge
=
null
;
ovsId
=
ovs
.
getId
();
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
// TODO:定时更新ovs-vsctl
public
String
getValueInString
(
String
input
)
{
@Scheduled
(
fixedRate
=
500
)
String
value
=
input
.
trim
().
split
(
" "
)[
1
];
public
void
updateOvsStatus
()
{
value
=
value
.
replaceAll
(
"\""
,
""
);
Ovs
ovs
=
ovsUtils
.
showDetails
();
return
value
;
// 第一次检查,判断Bridge列表是否减少
}
@Override
// 第二次检查,判断是否有新的bridge添加
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
);
}
}
@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!"
);
}
}
}
}
}
bishe-utils/src/main/java/top/ninwoo/utils/util/DockerUtils.java
View file @
e7939cd6
...
@@ -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
);
}
}
bishe-utils/src/main/java/top/ninwoo/utils/util/OvsUtils.java
View file @
e7939cd6
...
@@ -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
...
...
bishe-utils/src/main/java/top/ninwoo/utils/util/impl/DockerUtilsImpl.java
View file @
e7939cd6
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
;
}
}
}
bishe-utils/src/main/java/top/ninwoo/utils/util/impl/OvsUtilsImpl.java
View file @
e7939cd6
...
@@ -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
(
"\""
,
""
);
...
...
bishe-utils/src/test/java/top/ninwoo/utils/DockerUtilsTest.java
View file @
e7939cd6
...
@@ -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
);
}
}
}
bishe-utils/src/test/java/top/ninwoo/utils/EntityTests.java
0 → 100644
View file @
e7939cd6
package
top.ninwoo.utils
;
/**
* @Author joliu
* @Description
* @Date Create in 下午10:50 2019/10/27
*/
public
class
EntityTests
{
}
bishe-utils/src/test/java/top/ninwoo/utils/OVSServiceTests.java
View file @
e7939cd6
...
@@ -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
O
VSService
ovsService
;
private
O
vsUtils
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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment