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

实体对象: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", "administrator@vsphere.local", "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;

    }


}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值