package com.bocloud.ims.service.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.entity.enums.CloudProvider;
import com.bocloud.cmp.entity.enums.ModuleCode;
import com.bocloud.cmp.model.PortFixedIpModel;
import com.bocloud.common.enums.BoCloudService;
import com.bocloud.common.model.SimpleBean;
import com.bocloud.common.utils.IDFactory;
import com.bocloud.common.utils.ListTool;
import com.bocloud.ims.entity.common.Project;
import com.bocloud.ims.entity.resource.*;
import com.bocloud.ims.service.model.cmdb.CmdbInfo;
import com.bocloud.ims.service.model.cmdb.CmdbModel;
import com.bocloud.ims.service.model.cmdb.CmdbProperty;
import com.bocloud.ims.service.model.cmdb.CmdbRelation;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class CmdbInfoConvertor {

	public static CmdbModel getModel(BoCloudService service, List<CmdbInfo> infos) {
		CmdbModel cmdbModel = new CmdbModel();
		cmdbModel.setUuid(IDFactory.instance().uuid());
		cmdbModel.setServiceName(service.name());
		cmdbModel.setData(infos);
		return cmdbModel;
	}

	public static CmdbModel getCmdbModel(String service, List<CmdbInfo> infos) {
		CmdbModel cmdbModel = new CmdbModel();
		cmdbModel.setUuid(IDFactory.instance().uuid());
		cmdbModel.setServiceName(service);
		cmdbModel.setData(infos);
		return cmdbModel;
	}

	public static CmdbInfo getInfo(Router router, String operation, List<CmdbRelation> relations) {
		List<CmdbProperty> properties = null;
		if (StringUtils.isEmpty(operation)) {
			properties = new ArrayList<>();
			properties.add(getProperty("bname", router.getGuid(), null));
			properties.add(getProperty("name", router.getName(), null));
			properties.add(getProperty("status", router.getStatus(), null));
			properties.add(getProperty("adminState", router.getAdminStateup().toString(), null));
		}
		return getInfo(router.getGuid(), ModuleCode.ROUTER.name(), operation, properties, relations);
	}

	public static CmdbInfo getInfo(Router router, String operation, List<CmdbRelation> relations,
			CloudProvider provider) {
		List<CmdbProperty> properties = null;
		if (StringUtils.isEmpty(operation)) {
			properties = new ArrayList<>();
			properties.add(getProperty("bname", router.getGuid(), null));
			properties.add(getProperty("name", router.getName(), null));
			properties.add(getProperty("status", router.getStatus(), null));
			properties.add(getProperty("adminState", router.getAdminStateup().toString(), null));
		}
		switch (provider) {
		case OPENSTACK:
			return getInfo(router.getGuid(), ModuleCode.OS_CLOUD_ROUTING.name(), operation, properties, relations);
		default:
			return getInfo(router.getGuid(), ModuleCode.ROUTER.name(), operation, properties, relations);
		}
	}

	public static CmdbInfo getInfo(Switcher switcher, String operation, List<CmdbRelation> relations) {
		List<CmdbProperty> properties = null;
		if (StringUtils.isEmpty(operation)) {
			properties = new ArrayList<>();
			properties.add(getProperty("bname", switcher.getGuid(), null));
			properties.add(getProperty("name", switcher.getName(), null));
			properties.add(getProperty("status", switcher.getStatus(), null));
			properties.add(getProperty("type", switcher.getType(), null));
		}
		return getInfo(switcher.getGuid(), ModuleCode.SWITCH.name(), operation, properties, relations);
	}

	public static CmdbInfo getInfo(Switcher switcher, String operation, List<CmdbRelation> relations,
			CloudProvider provider) {
		List<CmdbProperty> properties = null;
		if (StringUtils.isEmpty(operation)) {
			properties = new ArrayList<>();
			properties.add(getProperty("bname", switcher.getGuid(), null));
			properties.add(getProperty("name", switcher.getName(), null));
			properties.add(getProperty("status", switcher.getStatus(), null));
			properties.add(getProperty("type", switcher.getType(), null));
		}
		switch (provider) {
		case VMWARE:
			return getInfo(switcher.getGuid(), ModuleCode.VC_SWITCH.name(), operation, properties, relations);
		default:
			return getInfo(switcher.getGuid(), ModuleCode.SWITCH.name(), operation, properties, relations);
		}
	}

	public static CmdbInfo getInfo(PortGroup group, String operation, List<CmdbRelation> relations) {
		List<CmdbProperty> properties = null;
		if (StringUtils.isEmpty(operation)) {
			properties = new ArrayList<>();
			properties.add(getProperty("bname", group.getGuid(), null));
			properties.add(getProperty("name", group.getName(), null));
			properties.add(getProperty("type", group.getType(), null));
		}
		return getInfo(group.getGuid(), ModuleCode.PORT_GROUP.name(), operation, properties, relations);
	}

	public static CmdbInfo getInfo(PortGroup group, String operation, List<CmdbRelation> relations,
			CloudProvider provider) {
		List<CmdbProperty> properties = null;
		if (StringUtils.isEmpty(operation)) {
			properties = new ArrayList<>();
			properties.add(getProperty("bname", group.getGuid(), null));
			properties.add(getProperty("name", group.getName(), null));
			properties.add(getProperty("type", group.getType(), null));
		}
		switch (provider) {
		case VMWARE:
			return getInfo(group.getGuid(), ModuleCode.VC_PORT_GROUP.name(), operation, properties, relations);
		default:
			return getInfo(group.getGuid(), ModuleCode.PORT_GROUP.name(), operation, properties, relations);
		}
	}

	public static CmdbInfo getInfo(Subnet subnet, String operation, List<CmdbRelation> relations) {
		List<CmdbProperty> properties = null;
		if (StringUtils.isEmpty(operation)) {
			properties = new ArrayList<>();
			properties.add(getProperty("bname", subnet.getGuid(), null));
			properties.add(getProperty("name", subnet.getName(), null));
			properties.add(getProperty("ippools", subnet.getIpPools(), null));
			properties.add(getProperty("cidr", subnet.getCidr(), null));
			properties.add(getProperty("dhcpEnabled", subnet.getDhcpEnabled() == null ? "1" : "0", null));
			properties.add(getProperty("gateway", subnet.getGateway(), null));
			if (subnet.getDnsNames().length() <= 2) {
				properties.add(getProperty("dns", "", null));
			} else {
				properties.add(getProperty("dns", subnet.getDnsNames(), null));
			}
			properties.add(getProperty("hostRouters", subnet.getHostRoutes(), null));
		}
		return getInfo(subnet.getGuid(), ModuleCode.SUBNET.name(), operation, properties, relations);
	}

	public static CmdbInfo getInfo(Subnet subnet, String operation, List<CmdbRelation> relations,
			CloudProvider provider) {
		List<CmdbProperty> properties = null;
		if (StringUtils.isEmpty(operation)) {
			properties = new ArrayList<>();
			properties.add(getProperty("bname", subnet.getGuid(), null));
			properties.add(getProperty("name", subnet.getName(), null));
			properties.add(getProperty("ippools", subnet.getIpPools(), null));
			properties.add(getProperty("cidr", subnet.getCidr(), null));
			properties.add(getProperty("dhcpEnabled", subnet.getDhcpEnabled() == null ? "1" : "0", null));
			properties.add(getProperty("gateway", subnet.getGateway(), null));
			if (subnet.getDnsNames().length() <= 2) {
				properties.add(getProperty("dns", "", null));
			} else {
				properties.add(getProperty("dns", subnet.getDnsNames(), null));
			}
			properties.add(getProperty("hostRouters", subnet.getHostRoutes(), null));
		}
		switch (provider) {
		case OPENSTACK:
			return getInfo(subnet.getGuid(), ModuleCode.OS_SUBNET.name(), operation, properties, relations);
		default:
			return getInfo(subnet.getGuid(), ModuleCode.SUBNET.name(), operation, properties, relations);
		}
	}

	public static CmdbInfo getInfo(Port port, List<PortFixedIpModel> fixedIps, String operation,
			List<CmdbRelation> relations) {
		List<CmdbProperty> properties = null;
		if (StringUtils.isEmpty(operation)) {
			properties = new ArrayList<>();
			properties.add(getProperty("bname", port.getGuid(), null));
			properties.add(getProperty("name", port.getName(), null));
			properties.add(getProperty("mac", port.getMacAddress(), null));
			List<List<SimpleBean>> table = new ArrayList<>();
			if (!ListTool.isEmpty(fixedIps)) {
				List<SimpleBean> beans = new ArrayList<>();
				for (PortFixedIpModel fixedIp : fixedIps) {
					SimpleBean ipBean = new SimpleBean();
					ipBean.setName("IP");
					ipBean.setValue(fixedIp.getIpAddress());
					beans.add(ipBean);
				}
				table.add(beans);
			}
			properties.add(getProperty("addresses", null, table));
		}
		return getInfo(port.getGuid(), ModuleCode.PORT.name(), operation, properties, relations);
	}

	public static CmdbInfo getInfo(Port port, List<PortFixedIpModel> fixedIps, String operation,
			List<CmdbRelation> relations, CloudProvider provider) {
		List<CmdbProperty> properties = null;
		if (StringUtils.isEmpty(operation)) {
			properties = new ArrayList<>();
			properties.add(getProperty("bname", port.getGuid(), null));
			properties.add(getProperty("name", port.getName(), null));
			properties.add(getProperty("mac", port.getMacAddress(), null));
			List<List<SimpleBean>> table = new ArrayList<>();
			if (!ListTool.isEmpty(fixedIps)) {
				List<SimpleBean> beans = new ArrayList<>();
				for (PortFixedIpModel fixedIp : fixedIps) {
					SimpleBean ipBean = new SimpleBean();
					ipBean.setName("IP");
					ipBean.setValue(fixedIp.getIpAddress());
					beans.add(ipBean);
				}
				table.add(beans);
			}
			properties.add(getProperty("addresses", null, table));
		}
		switch (provider) {
		case OPENSTACK:
			return getInfo(port.getGuid(), ModuleCode.OS_PORT.name(), operation, properties, relations);
		default:
			return getInfo(port.getGuid(), ModuleCode.PORT.name(), operation, properties, relations);
		}
	}

	/**
	 * 获取虚拟机信息
	 *
	 * @param server
	 * @param operation
	 * @param relations
	 * @return
	 */
	public static CmdbInfo getInfo(CloudServer server, String operation, List<CmdbRelation> relations) {
		List<CmdbProperty> properties = null;
		if (StringUtils.isEmpty(operation)) {
			properties = new ArrayList<>();
			properties.add(getProperty("bname", server.getGuid(), null));
			properties.add(getProperty("name", server.getName(), null));
			properties.add(getProperty("status", server.getStatus(), null));
			properties.add(getProperty("cpu", String.valueOf(server.getCpu()), null));
			properties.add(getProperty("memory", String.valueOf(server.getMemory()), null));
			properties.add(getProperty("disk", String.valueOf(server.getDisk()), null));
			if (!StringUtils.isEmpty(server.getImageName())) {
				properties.add(getProperty("imageName", server.getImageName(), null));
			}
			String privateIps = server.getPrivateIps();
			if (!StringUtils.isEmpty(privateIps)) {
				if (CloudProvider.TENCENT.name().equals(server.getVendorType())
						|| CloudProvider.ALIYUN.name().equals(server.getVendorType())
						|| CloudProvider.AZURE.name().equals(server.getVendorType())
						|| CloudProvider.AWS.name().equals(server.getVendorType())
						|| CloudProvider.TCE.name().equals(server.getVendorType())
						|| CloudProvider.TIANYI.name().equals(server.getVendorType())) {
					List<JSONObject> list = new ArrayList<JSONObject>();
					List<String> ips = JSONObject.parseArray(privateIps, String.class);
					JSONObject obj = null;
					for (String ip : ips) {
						obj = new JSONObject();
						obj.put("address", ip);
						list.add(obj);
					}
					privateIps = JSON.toJSONString(list);
				}
				List<List<SimpleBean>> table = new ArrayList<>();
				List<JSONObject> ips = JSONObject.parseArray(privateIps, JSONObject.class);
				for (JSONObject object : ips) {
					if (CloudProvider.OPENSTACK.name().equals(server.getVendorType())) {
						List<JSONObject> addresses = JSONObject.parseArray(object.getString("addresses"),
								JSONObject.class);
						for (JSONObject address : addresses) {
							List<SimpleBean> beans = new ArrayList<>();
							SimpleBean ipBean = new SimpleBean();
							ipBean.setName("IP");
							ipBean.setValue(address.getString("address"));
							beans.add(ipBean);
							SimpleBean netBean = new SimpleBean();
							netBean.setName("所属网络");
							netBean.setValue(object.getString("networkName"));
							beans.add(netBean);
							table.add(beans);
						}
					} else if (CloudProvider.VMWARE.name().equals(server.getVendorType())) {
						List<SimpleBean> beans = new ArrayList<>();
						SimpleBean ipBean = new SimpleBean();
						ipBean.setName("IP");
						ipBean.setValue(object.getString("address"));
						beans.add(ipBean);
						SimpleBean netBean = new SimpleBean();
						netBean.setName("所属网络");
						netBean.setValue(object.getString("networkName"));
						beans.add(netBean);
						table.add(beans);
					} else if (CloudProvider.H3C.name().equals(server.getVendorType())) {
						List<SimpleBean> beans = new ArrayList<>();
						SimpleBean ipBean = new SimpleBean();
						ipBean.setName("IP");
						ipBean.setValue(object.getString("address"));
						beans.add(ipBean);
						SimpleBean netBean = new SimpleBean();
						netBean.setName("所属网络");
						netBean.setValue(object.getString("networkName"));
						beans.add(netBean);
						table.add(beans);
					} else if (CloudProvider.TENCENT.name().equals(server.getVendorType())) {
						List<SimpleBean> beans = new ArrayList<>();
						SimpleBean ipBean = new SimpleBean();
						ipBean.setName("IP");
						ipBean.setValue(object.getString("address"));
						beans.add(ipBean);
						SimpleBean netBean = new SimpleBean();
						netBean.setName("所属网络");
						netBean.setValue(object.getString("networkName"));
						beans.add(netBean);
						table.add(beans);
					} else if (CloudProvider.AZURE.name().equals(server.getVendorType())) {
						List<SimpleBean> beans = new ArrayList<>();
						SimpleBean ipBean = new SimpleBean();
						ipBean.setName("IP");
						ipBean.setValue(object.getString("address"));
						beans.add(ipBean);
						SimpleBean netBean = new SimpleBean();
						netBean.setName("所属网络");
						netBean.setValue(object.getString("networkName"));
						beans.add(netBean);
						table.add(beans);
					} else if (CloudProvider.AWS.name().equals(server.getVendorType())) {
						List<SimpleBean> beans = new ArrayList<>();
						SimpleBean ipBean = new SimpleBean();
						ipBean.setName("IP");
						ipBean.setValue(object.getString("address"));
						beans.add(ipBean);
						SimpleBean netBean = new SimpleBean();
						netBean.setName("所属网络");
						netBean.setValue(object.getString("networkName"));
						beans.add(netBean);
						table.add(beans);
					} else if (CloudProvider.HUAWEI.name().equals(server.getVendorType())) {
						List<SimpleBean> beans = new ArrayList<>();
						SimpleBean ipBean = new SimpleBean();
						ipBean.setName("IP");
						ipBean.setValue(object.getString("address"));
						beans.add(ipBean);
						SimpleBean netBean = new SimpleBean();
						netBean.setName("所属网络");
						netBean.setValue(object.getString("networkId"));
						beans.add(netBean);
						table.add(beans);
					} else if (CloudProvider.TCE.name().equals(server.getVendorType())) {
						List<SimpleBean> beans = new ArrayList<>();
						SimpleBean ipBean = new SimpleBean();
						ipBean.setName("IP");
						ipBean.setValue(object.getString("address"));
						beans.add(ipBean);
						SimpleBean netBean = new SimpleBean();
						netBean.setName("所属网络");
						netBean.setValue(object.getString("networkName"));
						beans.add(netBean);
						table.add(beans);
					}
				}
				properties.add(getProperty("address", null, table));
			}
			properties.add(getProperty("osCategory", server.getOsCategory(), null));
			properties.add(getProperty("osName", server.getOsName(), null));
		}
		return getInfo(server.getGuid(), ModuleCode.VM.name(), operation, properties, relations);
	}

	/**
	 * 推送消息到itms
	 *
	 * @param server
	 * @param operation
	 * @param relations
	 * @return
	 */
	public static CmdbInfo getItsmInfo(CloudServer server, String operation, List<CmdbRelation> relations) {
		List<CmdbProperty> properties = null;
		if (StringUtils.isEmpty(operation) || "create".equalsIgnoreCase(operation)) {
			properties = new ArrayList<>();
			properties.add(getProperty("bname", server.getGuid(), null));
			properties.add(getProperty("name", server.getName(), null));
			properties.add(getProperty("status", server.getStatus(), null));
			properties.add(getProperty("cpu", String.valueOf(server.getCpu()), null));
			properties.add(getProperty("memory", String.valueOf(server.getMemory()), null));
			properties.add(getProperty("disk", String.valueOf(server.getDisk()), null));
			if (!StringUtils.isEmpty(server.getImageName())) {
				properties.add(getProperty("imageName", server.getImageName(), null));
			}
			String privateIps = server.getPrivateIps();
			if (!StringUtils.isEmpty(privateIps)) {
				if (CloudProvider.TIANYI.name().equals(server.getVendorType())) {
					List<JSONObject> list = new ArrayList<JSONObject>();
					List<String> ips = JSONObject.parseArray(privateIps, String.class);
					JSONObject obj = null;
					for (String ip : ips) {
						obj = new JSONObject();
						obj.put("address", ip);
						list.add(obj);
					}
					privateIps = JSON.toJSONString(list);
				}
				List<List<SimpleBean>> table = new ArrayList<>();
				List<JSONObject> ips = JSONObject.parseArray(privateIps, JSONObject.class);
				for (JSONObject object : ips) {
					if (CloudProvider.VMWARE.name().equals(server.getVendorType())) {
						List<SimpleBean> beans = new ArrayList<>();
						SimpleBean ipBean = new SimpleBean();
						ipBean.setName("IP");
						ipBean.setValue(object.getString("address"));
						beans.add(ipBean);
						table.add(beans);
					} else if (CloudProvider.H3C.name().equals(server.getVendorType())) {
						List<SimpleBean> beans = new ArrayList<>();
						SimpleBean ipBean = new SimpleBean();
						ipBean.setName("IP");
						ipBean.setValue(object.getString("address"));
						beans.add(ipBean);
						SimpleBean netBean = new SimpleBean();
						netBean.setName("所属网络");
						netBean.setValue(object.getString("networkName"));
						beans.add(netBean);
						table.add(beans);
					} else if (CloudProvider.TIANYI.name().equals(server.getVendorType())) {
						List<SimpleBean> beans = new ArrayList<>();
						SimpleBean ipBean = new SimpleBean();
						ipBean.setName("IP");
						ipBean.setValue(object.getString("address"));
						beans.add(ipBean);
						table.add(beans);
					} else if (CloudProvider.HUAWEI.name().equals(server.getVendorType())) {
						List<SimpleBean> beans = new ArrayList<>();
						SimpleBean ipBean = new SimpleBean();
						ipBean.setName("IP");
						ipBean.setValue(object.getString("address"));
						beans.add(ipBean);
						SimpleBean netBean = new SimpleBean();
						netBean.setName("所属网络");
						netBean.setValue(object.getString("networkId"));
						beans.add(netBean);
						table.add(beans);
					}
				}
				properties.add(getItsmProperty("address", table));
			}
		}
		return getInfo(server.getGuid(), ModuleCode.VM.name(), operation, properties, relations);
	}

	public static CmdbInfo getInfo(CloudServer server, String operation, List<CmdbRelation> relations,
			CloudProvider provider) {
		List<CmdbProperty> properties = null;
		if (StringUtils.isEmpty(operation)) {
			properties = new ArrayList<>();
			properties.add(getProperty("bname", server.getGuid(), null));
			properties.add(getProperty("name", server.getName(), null));
			properties.add(getProperty("status", server.getStatus(), null));
			properties.add(getProperty("cpu", String.valueOf(server.getCpu()), null));
			properties.add(getProperty("memory", String.valueOf(server.getMemory()), null));
			properties.add(getProperty("disk", String.valueOf(server.getDisk()), null));
			if (!StringUtils.isEmpty(server.getImageName())) {
				properties.add(getProperty("imageName", server.getImageName(), null));
			}
			String privateIps = server.getPrivateIps();
			if (!StringUtils.isEmpty(privateIps)) {
				if (CloudProvider.TENCENT.name().equals(server.getVendorType())
						|| CloudProvider.ALIYUN.name().equals(server.getVendorType())
						|| CloudProvider.AZURE.name().equals(server.getVendorType())
						|| CloudProvider.AWS.name().equals(server.getVendorType())
						|| CloudProvider.TCE.name().equals(server.getVendorType())) {
					List<JSONObject> list = new ArrayList<JSONObject>();
					List<String> ips = JSONObject.parseArray(privateIps, String.class);
					JSONObject obj = null;
					for (String ip : ips) {
						obj = new JSONObject();
						obj.put("address", ip);
						list.add(obj);
					}
					privateIps = JSON.toJSONString(list);
				}
				List<List<SimpleBean>> table = new ArrayList<>();
				List<JSONObject> ips = JSONObject.parseArray(privateIps, JSONObject.class);
				for (JSONObject object : ips) {
					if (CloudProvider.OPENSTACK.name().equals(server.getVendorType())) {
						List<JSONObject> addresses = JSONObject.parseArray(object.getString("addresses"),
								JSONObject.class);
						for (JSONObject address : addresses) {
							List<SimpleBean> beans = new ArrayList<>();
							SimpleBean ipBean = new SimpleBean();
							ipBean.setName("IP");
							ipBean.setValue(address.getString("address"));
							beans.add(ipBean);
							SimpleBean netBean = new SimpleBean();
							netBean.setName("所属网络");
							netBean.setValue(object.getString("networkName"));
							beans.add(netBean);
							table.add(beans);
						}
					} else if (CloudProvider.VMWARE.name().equals(server.getVendorType())) {
						List<SimpleBean> beans = new ArrayList<>();
						SimpleBean ipBean = new SimpleBean();
						ipBean.setName("IP");
						ipBean.setValue(object.getString("address"));
						beans.add(ipBean);
						SimpleBean netBean = new SimpleBean();
						netBean.setName("所属网络");
						netBean.setValue(object.getString("networkName"));
						beans.add(netBean);
						table.add(beans);
					} else if (CloudProvider.H3C.name().equals(server.getVendorType())) {
						List<SimpleBean> beans = new ArrayList<>();
						SimpleBean ipBean = new SimpleBean();
						ipBean.setName("IP");
						ipBean.setValue(object.getString("address"));
						beans.add(ipBean);
						SimpleBean netBean = new SimpleBean();
						netBean.setName("所属网络");
						netBean.setValue(object.getString("networkName"));
						beans.add(netBean);
						table.add(beans);
					} else if (CloudProvider.TENCENT.name().equals(server.getVendorType())) {
						List<SimpleBean> beans = new ArrayList<>();
						SimpleBean ipBean = new SimpleBean();
						ipBean.setName("IP");
						ipBean.setValue(object.getString("address"));
						beans.add(ipBean);
						SimpleBean netBean = new SimpleBean();
						netBean.setName("所属网络");
						netBean.setValue(object.getString("networkName"));
						beans.add(netBean);
						table.add(beans);
					} else if (CloudProvider.AZURE.name().equals(server.getVendorType())) {
						List<SimpleBean> beans = new ArrayList<>();
						SimpleBean ipBean = new SimpleBean();
						ipBean.setName("IP");
						ipBean.setValue(object.getString("address"));
						beans.add(ipBean);
						SimpleBean netBean = new SimpleBean();
						netBean.setName("所属网络");
						netBean.setValue(object.getString("networkName"));
						beans.add(netBean);
						table.add(beans);
					} else if (CloudProvider.AWS.name().equals(server.getVendorType())) {
						List<SimpleBean> beans = new ArrayList<>();
						SimpleBean ipBean = new SimpleBean();
						ipBean.setName("IP");
						ipBean.setValue(object.getString("address"));
						beans.add(ipBean);
						SimpleBean netBean = new SimpleBean();
						netBean.setName("所属网络");
						netBean.setValue(object.getString("networkName"));
						beans.add(netBean);
						table.add(beans);
					} else if (CloudProvider.HUAWEI.name().equals(server.getVendorType())) {
						List<SimpleBean> beans = new ArrayList<>();
						SimpleBean ipBean = new SimpleBean();
						ipBean.setName("IP");
						ipBean.setValue(object.getString("address"));
						beans.add(ipBean);
						SimpleBean netBean = new SimpleBean();
						netBean.setName("所属网络");
						netBean.setValue(object.getString("networkId"));
						beans.add(netBean);
						table.add(beans);
					} else if (CloudProvider.TCE.name().equals(server.getVendorType())) {
						List<SimpleBean> beans = new ArrayList<>();
						SimpleBean ipBean = new SimpleBean();
						ipBean.setName("IP");
						ipBean.setValue(object.getString("address"));
						beans.add(ipBean);
						SimpleBean netBean = new SimpleBean();
						netBean.setName("所属网络");
						netBean.setValue(object.getString("networkName"));
						beans.add(netBean);
						table.add(beans);
					}
				}
				properties.add(getProperty("address", null, table));
			}
			properties.add(getProperty("osCategory", server.getOsCategory(), null));
			properties.add(getProperty("osName", server.getOsName(), null));
		}
		switch (provider) {
		case OPENSTACK:
			return getInfo(server.getGuid(), ModuleCode.OS_VM.name(), operation, properties, relations);
		case VMWARE:
			return getInfo(server.getGuid(), ModuleCode.VC_VM.name(), operation, properties, relations);
		default:
			return getInfo(server.getGuid(), ModuleCode.VM.name(), operation, properties, relations);
		}
	}

	/**
	 * 获取物理主机信息
	 *
	 * @param server
	 * @param config
	 * @param operation
	 * @param relations
	 * @return
	 */
	public static CmdbInfo getInfo(Server server, ServerConfig config, String operation, List<CmdbRelation> relations) {
		List<CmdbProperty> properties = null;
		if (StringUtils.isEmpty(operation)) {
			properties = new ArrayList<>();
			properties.add(getProperty("bname", server.getGuid(), null));
			properties.add(getProperty("name", server.getName(), null));
			properties.add(getProperty("status", server.getStatus(), null));
			properties.add(getProperty("manageIp", server.getManageIp(), null));
			properties.add(getProperty("powerState", server.getPowerState(), null));
			properties.add(getProperty("cpuAmount", String.valueOf(config.getCpuAmount()), null));
			properties.add(getProperty("memoryCapacity", String.valueOf(config.getMemoryCapacity()), null));
			properties.add(getProperty("diskCapacity", String.valueOf(config.getDiskCapacity()), null));
			properties.add(getProperty("cpuCore", String.valueOf(config.getCpuCore()), null));
			properties.add(getProperty("vendor", config.getVendor(), null));
		}
		return getInfo(server.getGuid(), ModuleCode.PM.name(), operation, properties, relations);
	}

	public static CmdbInfo getInfo(Server server, ServerConfig config, String operation, List<CmdbRelation> relations,
			CloudProvider provider) {
		List<CmdbProperty> properties = null;
		if (StringUtils.isEmpty(operation)) {
			properties = new ArrayList<>();
			properties.add(getProperty("bname", server.getGuid(), null));
			properties.add(getProperty("name", server.getName(), null));
			properties.add(getProperty("status", server.getStatus(), null));
			properties.add(getProperty("manageIp", server.getManageIp(), null));
			properties.add(getProperty("powerState", server.getPowerState(), null));
			properties.add(getProperty("cpuAmount", String.valueOf(config.getCpuAmount()), null));
			properties.add(getProperty("memoryCapacity", String.valueOf(config.getMemoryCapacity()), null));
			properties.add(getProperty("diskCapacity", String.valueOf(config.getDiskCapacity()), null));
			properties.add(getProperty("cpuCore", String.valueOf(config.getCpuCore()), null));
			properties.add(getProperty("vendor", config.getVendor(), null));
		}
		switch (provider) {
		case OPENSTACK:
			return getInfo(server.getGuid(), ModuleCode.OS_PM.name(), operation, properties, relations);
		case VMWARE:
			return getInfo(server.getGuid(), ModuleCode.VC_PM.name(), operation, properties, relations);
		default:
			return getInfo(server.getGuid(), ModuleCode.PM.name(), operation, properties, relations);
		}
	}

	public static CmdbInfo getItsmInfo(Server server, ServerConfig config, String operation,
			List<CmdbRelation> relations, CloudProvider provider) {
		List<CmdbProperty> properties = null;
		if (StringUtils.isEmpty(operation) || "create".equalsIgnoreCase(operation)) {
			properties = new ArrayList<>();
			properties.add(getProperty("bname", server.getGuid(), null));
			properties.add(getProperty("name", server.getName(), null));
			properties.add(getProperty("status", server.getStatus(), null));
			properties.add(getProperty("manageIp", server.getManageIp(), null));
			properties.add(getProperty("powerState", server.getPowerState(), null));
			properties.add(getProperty("cpuAmount", String.valueOf(config.getCpuAmount()), null));
			properties.add(getProperty("memoryCapacity", String.valueOf(config.getMemoryCapacity()), null));
			properties.add(getProperty("diskCapacity", String.valueOf(config.getDiskCapacity()), null));
			properties.add(getProperty("cpuCore", String.valueOf(config.getCpuCore()), null));
		}
		switch (provider) {
		case OPENSTACK:
			return getInfo(server.getGuid(), ModuleCode.OS_PM.name(), operation, properties, relations);
		case VMWARE:
			return getInfo(server.getGuid(), ModuleCode.VC_PM.name(), operation, properties, relations);
		default:
			return getInfo(server.getGuid(), ModuleCode.PM.name(), operation, properties, relations);
		}
	}

	/**
	 * 获取网络cmdb信息
	 *
	 * @param network
	 * @param operation
	 * @return
	 */
	public static CmdbInfo getInfo(Network network, String operation, List<CmdbRelation> relations) {
		List<CmdbProperty> properties = null;
		if (StringUtils.isEmpty(operation)) {
			properties = new ArrayList<>();
			properties.add(getProperty("bname", network.getGuid(), null));
			properties.add(getProperty("name", network.getName(), null));
			properties.add(getProperty("type", network.getType(), null));
			properties.add(getProperty("status", network.getStatus(), null));
		}
		return getInfo(network.getGuid(), ModuleCode.NETWORK.name(), operation, properties, relations);
	}

	public static CmdbInfo getInfo(Network network, String operation, List<CmdbRelation> relations,
			CloudProvider provider) {
		List<CmdbProperty> properties = null;
		if (StringUtils.isEmpty(operation)) {
			properties = new ArrayList<>();
			properties.add(getProperty("bname", network.getGuid(), null));
			properties.add(getProperty("name", network.getName(), null));
			properties.add(getProperty("type", network.getType(), null));
			properties.add(getProperty("status", network.getStatus(), null));
		}
		switch (provider) {
		case OPENSTACK:
			return getInfo(network.getGuid(), ModuleCode.OS_VLAN.name(), operation, properties, relations);
		case VMWARE:
			return getInfo(network.getGuid(), ModuleCode.VC_NETWORK.name(), operation, properties, relations);
		default:
			return getInfo(network.getGuid(), ModuleCode.NETWORK.name(), operation, properties, relations);
		}
	}

	/**
	 * 获取数据存储cmdb信息
	 *
	 * @param store
	 * @param operation
	 * @return
	 */
	public static CmdbInfo getInfo(DataStore store, String operation, List<CmdbRelation> relations) {
		List<CmdbProperty> properties = null;
		if (StringUtils.isEmpty(operation)) {
			properties = new ArrayList<>();
			properties.add(getProperty("bname", store.getGuid(), null));
			properties.add(getProperty("name", store.getName(), null));
			properties.add(getProperty("type", store.getType(), null));
			properties.add(getProperty("status", store.getStatus(), null));
			properties.add(getProperty("path", store.getPath(), null));
			properties.add(getProperty("capacity", String.valueOf(store.getCapacity()), null));
		}
		return getInfo(store.getGuid(), ModuleCode.DATASTORE.name(), operation, properties, relations);
	}

	public static CmdbInfo getInfo(DataStore store, String operation, List<CmdbRelation> relations,
			CloudProvider provider) {
		List<CmdbProperty> properties = null;
		if (StringUtils.isEmpty(operation)) {
			properties = new ArrayList<>();
			properties.add(getProperty("bname", store.getGuid(), null));
			properties.add(getProperty("name", store.getName(), null));
			properties.add(getProperty("type", store.getType(), null));
			properties.add(getProperty("status", store.getStatus(), null));
			properties.add(getProperty("path", store.getPath(), null));
			properties.add(getProperty("capacity", String.valueOf(store.getCapacity()), null));
		}
		switch (provider) {
		case VMWARE:
			return getInfo(store.getGuid(), ModuleCode.VC_DS.name(), operation, properties, relations);
		default:
			return getInfo(store.getGuid(), ModuleCode.DATASTORE.name(), operation, properties, relations);
		}
	}

	/**
	 * 获取集群cmdb信息
	 *
	 * @param cluster
	 * @param operation
	 * @return
	 */
	public static CmdbInfo getInfo(Cluster cluster, String operation, List<CmdbRelation> relations) {
		List<CmdbProperty> properties = null;
		if (StringUtils.isEmpty(operation)) {
			properties = new ArrayList<>();
			properties.add(getProperty("bname", cluster.getGuid(), null));
			properties.add(getProperty("name", cluster.getName(), null));
			properties.add(getProperty("cpuTotal", String.valueOf(cluster.getCpuTotal()), null));
			properties.add(getProperty("diskTotal", String.valueOf(cluster.getDiskTotal()), null));
			properties.add(getProperty("memTotal", String.valueOf(cluster.getMemTotal()), null));
		}
		return getInfo(cluster.getGuid(), ModuleCode.CLUSTER.name(), operation, properties, relations);
	}

	public static CmdbInfo getInfo(Cluster cluster, String operation, List<CmdbRelation> relations,
			CloudProvider provider) {
		List<CmdbProperty> properties = null;
		if (StringUtils.isEmpty(operation)) {
			properties = new ArrayList<>();
			properties.add(getProperty("bname", cluster.getGuid(), null));
			properties.add(getProperty("name", cluster.getName(), null));
			properties.add(getProperty("cpuTotal", String.valueOf(cluster.getCpuTotal()), null));
			properties.add(getProperty("diskTotal", String.valueOf(cluster.getDiskTotal()), null));
			properties.add(getProperty("memTotal", String.valueOf(cluster.getMemTotal()), null));
		}
		switch (provider) {
		case OPENSTACK:
			return getInfo(cluster.getGuid(), ModuleCode.OS_HOST_GROUP.name(), operation, properties, relations);
		case VMWARE:
			return getInfo(cluster.getGuid(), ModuleCode.VC_CLUSTER.name(), operation, properties, relations);
		default:
			return getInfo(cluster.getGuid(), ModuleCode.CLUSTER.name(), operation, properties, relations);
		}
	}

	public static CmdbInfo getItmsInfo(Cluster cluster, String operation, List<CmdbRelation> relations,
			CloudProvider provider) {
		List<CmdbProperty> properties = null;
		if (StringUtils.isEmpty(operation) || "create".equalsIgnoreCase(operation)) {
			properties = new ArrayList<>();
			properties.add(getProperty("bname", cluster.getGuid(), null));
			properties.add(getProperty("name", cluster.getName(), null));
			properties.add(getProperty("cpuTotal", String.valueOf(cluster.getCpuTotal()), null));
			properties.add(getProperty("diskTotal", String.valueOf(cluster.getDiskTotal()), null));
			properties.add(getProperty("memTotal", String.valueOf(cluster.getMemTotal()), null));
		}
		switch (provider) {
		case OPENSTACK:
			return getInfo(cluster.getGuid(), ModuleCode.OS_HOST_GROUP.name(), operation, properties, relations);
		case VMWARE:
			return getInfo(cluster.getGuid(), ModuleCode.VC_CLUSTER.name(), operation, properties, relations);
		default:
			return getInfo(cluster.getGuid(), ModuleCode.CLUSTER.name(), operation, properties, relations);
		}
	}

	/**
	 * 获取虚拟数据中心cmdb信息
	 *
	 * @param vdc
	 * @param operation
	 * @return
	 */
	public static CmdbInfo getInfo(Vdc vdc, String operation, List<CmdbRelation> relations) {
		List<CmdbProperty> properties = null;
		if (StringUtils.isEmpty(operation)) {
			properties = new ArrayList<>();
			properties.add(getProperty("bname", vdc.getGuid(), null));
			properties.add(getProperty("name", vdc.getName(), null));
			properties.add(getProperty("cpuTotal", String.valueOf(vdc.getCpuTotal()), null));
			properties.add(getProperty("diskTotal", String.valueOf(vdc.getDiskTotal()), null));
			properties.add(getProperty("memTotal", String.valueOf(vdc.getMemTotal()), null));
		}
		return getInfo(vdc.getGuid(), ModuleCode.VDC.name(), operation, properties, relations);
	}

	public static CmdbInfo getInfo(Vdc vdc, String operation, List<CmdbRelation> relations, CloudProvider provider) {
		List<CmdbProperty> properties = null;
		if (StringUtils.isEmpty(operation)) {
			properties = new ArrayList<>();
			properties.add(getProperty("bname", vdc.getGuid(), null));
			properties.add(getProperty("name", vdc.getName(), null));
			properties.add(getProperty("cpuTotal", String.valueOf(vdc.getCpuTotal()), null));
			properties.add(getProperty("diskTotal", String.valueOf(vdc.getDiskTotal()), null));
			properties.add(getProperty("memTotal", String.valueOf(vdc.getMemTotal()), null));
		}
		switch (provider) {
		case VMWARE:
			return getInfo(vdc.getGuid(), ModuleCode.VC_DC.name(), operation, properties, relations);
		default:
			return getInfo(vdc.getGuid(), ModuleCode.VDC.name(), operation, properties, relations);
		}
	}

	/**
	 * 获取云平台cmdb信息
	 *
	 * @param vendor
	 * @param operation
	 * @return
	 */
	public static CmdbInfo getInfo(CloudVendor vendor, String operation, List<CmdbRelation> relations) {
		List<CmdbProperty> properties = null;
		if (StringUtils.isEmpty(operation)) {
			properties = new ArrayList<>();
			properties.add(getProperty("bname", vendor.getGuid(), null));
			properties.add(getProperty("name", vendor.getName(), null));
			properties.add(getProperty("type", vendor.getType(), null));
			properties.add(getProperty("status", vendor.getStatus(), null));
			JSONObject authentication = JSONObject.parseObject(vendor.getAuthentication());
			properties.add(getProperty("address", authentication.getString("address"), null));
			properties.add(getProperty("env", vendor.getEnvName(), null));
			properties.add(getProperty("version", vendor.getVersion(), null));
			properties.add(getProperty("remark", vendor.getRemark(), null));
			properties.add(getProperty("region", vendor.getRegionsName(), null));
		}
		return getInfo(vendor.getGuid(), ModuleCode.VENDOR.name(), operation, properties, relations);
	}

	/**
	 * 项目CMDB数据模型
	 *
	 * @param vendor
	 * @param operation
	 * @return
	 */
	public static CmdbInfo getItmsInfo(Project project, String operation, List<CmdbRelation> relations) {
		List<CmdbProperty> properties = null;
		if (StringUtils.isEmpty(operation) || "create".equalsIgnoreCase(operation)) {
			properties = new ArrayList<>();
			properties.add(getProperty("bname", project.getGuid(), null));
			properties.add(getProperty("name", project.getName(), null));
		}
		return getInfo(project.getGuid(), ModuleCode.PROJECT.name(), operation, properties, relations);
	}

	/**
	 * 获取云平台cmdb信息
	 *
	 * @param vendor
	 * @param operation
	 * @return
	 */
	public static CmdbInfo getItsmInfo(CloudVendor vendor, String operation, List<CmdbRelation> relations) {
		List<CmdbProperty> properties = null;
		if (StringUtils.isEmpty(operation) || "create".equalsIgnoreCase(operation)
				|| "update".equalsIgnoreCase(operation)) {
			properties = new ArrayList<>();
			properties.add(getProperty("bname", vendor.getGuid(), null));
			properties.add(getProperty("name", vendor.getName(), null));
			properties.add(getProperty("type", vendor.getType(), null));
			properties.add(getProperty("status", vendor.getStatus(), null));
			JSONObject authentication = JSONObject.parseObject(vendor.getAuthentication());
			properties.add(getProperty("address", authentication.getString("address"), null));
			properties.add(getProperty("env", vendor.getEnvName(), null));
			properties.add(getProperty("version", vendor.getVersion(), null));
			properties.add(getProperty("remark", vendor.getRemark(), null));
			properties.add(getProperty("region", vendor.getRegionsName(), null));
		}
		String vendorCode = ModuleCode.VENDOR.name();
		if (vendor.getIsPublic()) {
			vendorCode += "_PUBLIC";
		} else {
			vendorCode += "_PRIVATE";
		}
		return getInfo(vendor.getGuid(), vendorCode, operation, properties, relations);
	}

	public static CmdbInfo getInfo(CloudVendor vendor, String operation, List<CmdbRelation> relations,
			CloudProvider provider) {
		List<CmdbProperty> properties = null;
		if (StringUtils.isEmpty(operation)) {
			properties = new ArrayList<>();
			properties.add(getProperty("bname", vendor.getGuid(), null));
			properties.add(getProperty("name", vendor.getName(), null));
			properties.add(getProperty("type", vendor.getType(), null));
			properties.add(getProperty("status", vendor.getStatus(), null));
			JSONObject authentication = JSONObject.parseObject(vendor.getAuthentication());
			properties.add(getProperty("address", authentication.getString("address"), null));
			properties.add(getProperty("env", vendor.getEnvName(), null));
			properties.add(getProperty("version", vendor.getVersion(), null));
			properties.add(getProperty("remark", vendor.getRemark(), null));
			properties.add(getProperty("region", vendor.getRegionsName(), null));
		}
		switch (provider) {
		case VMWARE:
			return getInfo(vendor.getGuid(), ModuleCode.VC_VENDOR.name(), operation, properties, relations);
		case OPENSTACK:
			return getInfo(vendor.getGuid(), ModuleCode.OS_VENDOR.name(), operation, properties, relations);
		default:
			return getInfo(vendor.getGuid(), ModuleCode.VENDOR.name(), operation, properties, relations);
		}

	}

	private static CmdbInfo getInfo(String guid, String code, String operation, List<CmdbProperty> properties,
			List<CmdbRelation> relations) {
		return new CmdbInfo(guid, code, operation, properties, relations);
	}

	public static CmdbProperty getProperty(String code, String value, List<List<SimpleBean>> table) {
		CmdbProperty property = new CmdbProperty();
		property.setPropertyCode(code);
		if (!StringUtils.isEmpty(value)) {
			property.setPropertyValue(value);
		}
		if (!ListTool.isEmpty(table)) {
			property.setTable(table);
		}
		return property;
	}

	public static CmdbProperty getItsmProperty(String code, List<List<SimpleBean>> table) {
		CmdbProperty property = new CmdbProperty();
		property.setPropertyCode(code);
		if (!ListTool.isEmpty(table)) {
			property.setPropertyValue(table);
		}
		return property;
	}

	public static CmdbInfo getInfo(Region region, String operation, List<CmdbRelation> relations) {
		List<CmdbProperty> properties = null;
		if (StringUtils.isEmpty(operation)) {
			properties = new ArrayList<>();
			properties.add(getProperty("bname", region.getGuid(), null));
			properties.add(getProperty("name", region.getName(), null));
			properties.add(getProperty("code", region.getCode(), null));
		}
		return getInfo(region.getGuid(), ModuleCode.REGION.name(), operation, properties, relations);
	}

}
