Vijava 学习笔记之NetWork(基础配置信息)
虾米哥
阅读:640
2021-03-31 22:36:57
评论:0
Vijava 代码:
实体类:
package com.vmware.pojo;
import java.util.List;
/**
* Created by vixuan-008 on 2015/4/24.
*/
public class NetworkInfo {
private List<VirtualNic> vnics;//虚拟网卡
private List<VirtualSwitch> vss;//虚拟交换机
public List<VirtualNic> getVnics() {
return vnics;
}
public void setVnics(List<VirtualNic> vnics) {
this.vnics = vnics;
}
public List<VirtualSwitch> getVss() {
return vss;
}
public void setVss(List<VirtualSwitch> vss) {
this.vss = vss;
}
}
package com.vmware.pojo;
/**
* Created by vixuan-008 on 2015/4/24.
*/
public class VirtualNic {
private String name;
private String portgroup;
private String mac;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPortgroup() {
return portgroup;
}
public void setPortgroup(String portgroup) {
this.portgroup = portgroup;
}
public String getMac() {
return mac;
}
public void setMac(String mac) {
this.mac = mac;
}
}
package com.vmware.pojo;
/**
* Created by vixuan-008 on 2015/4/24.
*/
public class VirtualSwitch {
private String name;
private String mac;
private String portgroup;
public String getMac() {
return mac;
}
public void setMac(String mac) {
this.mac = mac;
}
public String getPortgroup() {
return portgroup;
}
public void setPortgroup(String portgroup) {
this.portgroup = portgroup;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void addPhysicalMAC(String key){
if(this.mac==null){
mac=key;
}else{
mac=mac+";"+key;
}
setMac(mac);
}
public void addPortgroup(String key){
if(this.portgroup==null){
portgroup=key;
}else{
portgroup=portgroup+";"+key;
}
setPortgroup(portgroup);
}
}
逻辑代码:
package com.vmware.HostSystemOption;
import com.vmware.pojo.NetworkInfo;
import com.vmware.pojo.VirtualNic;
import com.vmware.pojo.VirtualSwitch;
import com.vmware.vim25.*;
import com.vmware.vim25.mo.HostSystem;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by vixuan-008 on 2015/4/24.
*/
public class HostSystemNetWork {
public NetworkInfo getNetworkInfo(HostSystem esxserver) throws Exception {
NetworkInfo ninfo = new NetworkInfo();
HostNetworkInfo nwi = esxserver.getConfig().getNetwork();
HostPortGroup[] portgroups = nwi.getPortgroup();
Map<String, String> pgMap = new HashMap<String, String>();
for (HostPortGroup portgroup : portgroups) {
pgMap.put(portgroup.getKey(), portgroup.getSpec().getName());
}
PhysicalNic[] pnics = nwi.getPnic();
Map<String, String> pnicMap = new HashMap<String, String>();
for (PhysicalNic pnic : pnics) {
pnicMap.put(pnic.getKey(), pnic.getMac());
}
List<VirtualSwitch> vss = new ArrayList<VirtualSwitch>();
HostVirtualSwitch[] vswtichs = nwi.getVswitch();
for (HostVirtualSwitch vswitch : vswtichs) {
VirtualSwitch vs = new VirtualSwitch();
vs.setName(vswitch.getName());
String[] macKeys = vswitch.getPnic();
if (macKeys != null) {
for (String key : macKeys) {
vs.addPhysicalMAC(pnicMap.get(key));
}
}
String[] pgs = vswitch.getPortgroup();
if (pgs != null) {
for (String pg : pgs) {
vs.addPortgroup(pgMap.get(pg));
}
}
// TODO
vss.add(vs);
}
ninfo.setVss(vss);
List<VirtualNic> vnics = new ArrayList<VirtualNic>();
HostVirtualNic[] virtualnics = nwi.getVnic();
for (HostVirtualNic virtualnic : virtualnics) {
VirtualNic vnic = new VirtualNic();
vnic.setName(virtualnic.getDevice());
vnic.setPortgroup(virtualnic.getPortgroup());
vnic.setMac(virtualnic.getSpec().getMac());
vnics.add(vnic);
}
ninfo.setVnics(vnics);
return ninfo;
}
}
测试代码:
package com.vmware.main;
import com.vmware.HostSystemOption.HostSystemNetWork;
import com.vmware.pojo.NetworkInfo;
import com.vmware.pojo.VirtualNic;
import com.vmware.pojo.VirtualSwitch;
import com.vmware.util.Session;
import com.vmware.vim25.mo.*;
import java.util.List;
/**
* Created by vixuan-008 on 2015/4/28.
*/
public class HostSystemNetworkTest {
public static void main(String[] args){
try{
String vmname="172.16.1.22";
ServiceInstance serviceInstance= Session.getInstance("172.16.1.20", "root", "vmware");
Folder rootFolder = serviceInstance.getRootFolder();
HostSystem hostSystem = (HostSystem) new InventoryNavigator(
rootFolder).searchManagedEntity("HostSystem", vmname);
if(hostSystem==null)
{
System.out.println("No VM " + vmname + " found");
serviceInstance.getServerConnection().logout();
return;
}
HostSystemNetWork hostSystemNetWork=new HostSystemNetWork();
NetworkInfo networkInfo=hostSystemNetWork.getNetworkInfo(hostSystem);
if(networkInfo !=null){
//虚拟网卡
List<VirtualNic> vnics=networkInfo.getVnics();
if(vnics.size()>0){
for(VirtualNic virtualNic:vnics){
System.out.println("--------start-----------");
System.out.println("name:"+virtualNic.getName());
System.out.println("mac:"+virtualNic.getMac());
System.out.println("protgroup:"+virtualNic.getPortgroup());
System.out.println("--------end-----------");
}
}
//虚拟交换机
List<VirtualSwitch> vss=networkInfo.getVss();
if(vss.size()>0){
for(VirtualSwitch virtualSwitch:vss){
System.out.println("--------start1-----------");
System.out.println("name:"+virtualSwitch.getName());
System.out.println("mac:"+virtualSwitch.getMac());
System.out.println("protgroup:"+virtualSwitch.getPortgroup());
System.out.println("--------end1-----------");
}
}
}
}catch (Exception e){
e.printStackTrace();
}
}
}
相关展示:
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。