Vijava 学习笔记之数据中心下(集群和非集群的资源池和ESXI)树形结构展示解析

不点 阅读:203 2021-03-31 22:28:01 评论:0

实体对象:DtreeObj.java

package com.vmware.pojo; 
 
/** 
 * Created by vixuan-008 on 2015/6/27. 
 */ 
public class DtreeObj { 
    private int id;//编号 
    private String name;//name 
    private int pid;//父类id 
    private int type;//设备类型 
 
    public int getId() { 
        return id; 
    } 
 
    public void setId(int id) { 
        this.id = id; 
    } 
 
    public int getPid() { 
        return pid; 
    } 
 
    public void setPid(int pid) { 
        this.pid = pid; 
    } 
 
    public String getName() { 
        return name; 
    } 
 
    public void setName(String name) { 
        this.name = name; 
    } 
 
    public int getType() { 
        return type; 
    } 
 
    public void setType(int type) { 
        this.type = type; 
    } 
} 

逻辑方法:ClusetDemo.java

package com.vmware.tree; 
 
 
import com.vmware.pojo.DtreeObj; 
import com.vmware.util.Session; 
import com.vmware.vim25.mo.*; 
 
import java.util.ArrayList; 
import java.util.List; 
 
/** 
 * Created by vixuan-008 on 2015/6/27. 
 */ 
public class ClusetDemo { 
 
    public static void main(String[] args) throws Exception{ 
        List<DtreeObj> list=new ArrayList<DtreeObj>(); 
 
 
 
        ServiceInstance serviceInstance = null; 
        //ServiceInstance----服务实例 
        //serviceInstance = Session.getInstance("192.168.0.22", "[email protected]", "Vixuan12#"); 
        serviceInstance = Session.getInstance("172.16.1.20","root","vmware"); 
 
        list=getTree(serviceInstance); 
 
 
 
 
        if(list.size()>0){ 
            for(int i=0;i<list.size();i++){ 
                DtreeObj obj=list.get(i); 
                System.out.println("id is:" + obj.getId() + ";name is:" + obj.getName() + ";pid is:" + obj.getPid() + ";type is:" + obj.getType()); 
                System.out.println("-----------------------------------------------"); 
            } 
        } 
 
 
    } 
 
    public static List<String> getdifference(List<String> a,List<String> b){ 
        List<String> content=new ArrayList<String>(); 
        if(a !=null &&  b!=null){ 
            if(a.size()>0 && b.size()>0){ 
                for(int i=0;i<a.size();i++){ 
                    boolean target=false;//默认在b集合中不存在 
                    String diffA=a.get(i); 
                    //判断是否字符串在指定的集合当中 
                    for(int j=0;j<b.size();j++){ 
                        String diffB=b.get(j); 
                        if(diffA.equalsIgnoreCase(diffB)){ 
                            target=true; 
                        } 
                    } 
                    //返回相关数据集合 
                    if(!target){ 
                        content.add(diffA); 
                    } 
 
 
                } 
            } 
        } 
 
        return content; 
    } 
 
    public static List<DtreeObj> getTree( ServiceInstance serviceInstance) throws Exception{ 
        List<DtreeObj> list=new ArrayList<DtreeObj>(); 
 
        //数据中心关联HostSystem 
        List<String> allhost=new ArrayList<String>(); 
        //已经存在HostSystem 
        List<String> hostList=new ArrayList<String>(); 
        //非集群HostSystem 
        List<String> noClusterHostList=null; 
 
 
        //rootFolder-------根文件夹 
        Folder rootFolder = serviceInstance.getRootFolder(); 
        System.out.println("datacenter is:"+rootFolder.getName()); 
 
        int counterInt=1; 
 
        int rootId=0; 
        DtreeObj root=new DtreeObj(); 
        root.setId(rootId); 
        root.setName(rootFolder.getName()); 
        root.setPid(-1); 
        root.setType(4); 
 
        list.add(root); 
 
        //inventoryNavigator----文件夹目录 
        InventoryNavigator inventoryNavigator =new InventoryNavigator(rootFolder); 
        //hostEntities--------查询实体对象(esxi) 
        ManagedEntity[] hostEntities=inventoryNavigator.searchManagedEntities("HostSystem"); 
        if(hostEntities!=null && hostEntities.length>0){ 
            for(int i=0;i<hostEntities.length;i++){ 
                HostSystem hostSystem=(HostSystem)hostEntities[i]; 
                allhost.add(hostSystem.getName()); 
            } 
        } 
 
 
        //managedEntities------查询实体对象 
        ManagedEntity[] managedEntities=inventoryNavigator.searchManagedEntities("ClusterComputeResource"); 
        if(managedEntities!=null && managedEntities.length>0){ 
            for(int i=0;i<managedEntities.length;i++){ 
                ClusterComputeResource cluster = (ClusterComputeResource)managedEntities[i]; 
                counterInt=counterInt+i;//统计数增加 
                int clusterId=counterInt;//集群Id 
 
                DtreeObj clusterObj=new DtreeObj(); 
                clusterObj.setId(clusterId); 
                clusterObj.setName(cluster.getName()); 
                clusterObj.setPid(rootId); 
                clusterObj.setType(1); 
 
                list.add(clusterObj); 
 
                //集群关联服务器 
                HostSystem[] hostSystems=cluster.getHosts(); 
                if(hostSystems!=null && hostSystems.length>0){ 
                    for(int j=0;j<hostSystems.length;j++){ 
                        HostSystem system=hostSystems[j]; 
                        int a=j+1; 
                        counterInt=counterInt+(a);//统计数增加 
                        int hostId=counterInt;//服务器Id 
 
                        DtreeObj hostObj=new DtreeObj(); 
                        hostObj.setId(hostId); 
                        hostObj.setName(system.getName()); 
                        hostObj.setPid(clusterId); 
                        hostObj.setType(3); 
 
                        hostList.add(system.getName()); 
                        list.add(hostObj); 
                    } 
 
                } 
                //集群关联资源池 
                ResourcePool resourcePool=cluster.getResourcePool(); 
                if(resourcePool!=null){ 
                    ResourcePool[] resourcePools=resourcePool.getResourcePools(); 
                    if(resourcePools!=null && resourcePools.length>0){ 
                        for(int k=0;k<resourcePools.length;k++){ 
                            ResourcePool pool=resourcePools[k]; 
                            int b=k+1; 
                            counterInt=counterInt+(b);//统计数增加 
                            int poolId=counterInt;//资源池Id 
 
                            DtreeObj poolObj=new DtreeObj(); 
                            poolObj.setId(poolId); 
                            poolObj.setName(pool.getName()); 
                            poolObj.setPid(clusterId); 
                            poolObj.setType(2); 
 
                            //  poolList.add(pool.getName()); 
                            list.add(poolObj); 
                        } 
 
                    } 
                } 
 
 
 
 
            } 
 
        } 
 
        //处理非集群HostSystem 
        noClusterHostList=getdifference(allhost,hostList); 
        if(noClusterHostList!=null && noClusterHostList.size()>0){ 
            for(int i=0;i<noClusterHostList.size();i++){ 
                String content=noClusterHostList.get(i); 
                int b=i+1; 
                counterInt=counterInt+(b); 
                int nohostId=counterInt; 
 
                DtreeObj nohostObj=new DtreeObj(); 
                nohostObj.setId(nohostId); 
                nohostObj.setName(content); 
                nohostObj.setPid(rootId); 
                nohostObj.setType(3); 
 
                list.add(nohostObj); 
 
            } 
        } 
 
 
 
 
 
        return list; 
 
    } 
 
 
} 




声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
排行榜
KIKK导航

KIKK导航

关注我们