package com.bocloud.ims.service.scheduler.scheduler;

import com.bocloud.cmp.driver.hmc.HmcPartitionDriver;
import com.bocloud.cmp.entity.enums.CloudProvider;
import com.bocloud.cmp.entity.enums.HmcPartitionType;
import com.bocloud.cmp.model.hmc.sdk.module.LogicalPartitionEntry;
import com.bocloud.cmp.model.hmc.sdk.module.VirtualIOServerEntry;
import com.bocloud.common.model.BocloudResult;
import com.bocloud.ims.common.enums.HmcStatus;
import com.bocloud.ims.entity.resource.CloudVendor;
import com.bocloud.ims.entity.resource.hmc.HmcPartition;
import com.bocloud.ims.repository.resource.CloudVendorRepository;
import com.bocloud.ims.repository.resource.hmc.HmcPartitionRepository;
import com.bocloud.service.core.CurrentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 定时更新HMC平台数据
 */
@Component
public class HMCScheduler {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private CurrentService service;
	@Autowired
	private CloudVendorRepository cloudVendorDao;
	@Autowired
	private HmcPartitionRepository hmcPartitionRepository;
	@Autowired
	private HmcPartitionDriver hmcPartitionDriver;

	@Scheduled(cron = "${hmc.partition.scheduler:0 0/10 * * * ?}")
	void updatePartition() {
		if (!service.isLeader()) {
			return;
		}
		List<CloudVendor> vendors = cloudVendorDao.listByType(CloudProvider.HMC.name());
		if (vendors.isEmpty()) {
			return;
		}
		logger.info("开始定时更新HMC的逻辑分区和VIOS...");
		for (CloudVendor vendor : vendors) {
			List<HmcPartition> partitions = hmcPartitionRepository.listByVendorId(vendor.getId(), null);

			// 更新VIOS和逻辑分区状态
			for (HmcPartition partition : partitions) {
				BocloudResult bocloudResult = hmcPartitionDriver.getPartitionAttribute(vendor.getUuid(),
						partition.getUuid(), HmcPartitionType.VIOS.getType().equals(partition.getPartitionType()));
				if (bocloudResult.isFailed()) {
					logger.error("获取HMC平台[" + vendor.getName() + "]的逻辑分区/VIOS[" + partition.getName() + "]数据失败："
							+ bocloudResult.getMessage());
					continue;
				}
				String status, ip, code, rmc;
				if (HmcPartitionType.VIOS.getType().equals(partition.getPartitionType())) {
					VirtualIOServerEntry viosEntry = (VirtualIOServerEntry) bocloudResult.getData();
					status = viosEntry.getContent().getVirtualIOServer().getPartitionState();
					ip = viosEntry.getContent().getVirtualIOServer().getResourceMonitoringIPAddress();
					code = viosEntry.getContent().getVirtualIOServer().getReferenceCode();
					rmc = viosEntry.getContent().getVirtualIOServer().getResourceMonitoringControlState();
				} else {
					LogicalPartitionEntry partitionEntry = (LogicalPartitionEntry) bocloudResult.getData();
					status = partitionEntry.getContent().getLogicalPartition().getPartitionState();
					ip = partitionEntry.getContent().getLogicalPartition().getResourceMonitoringIPAddress();
					code = partitionEntry.getContent().getLogicalPartition().getReferenceCode();
					rmc = partitionEntry.getContent().getLogicalPartition().getResourceMonitoringControlState();
				}
				if (HmcStatus.PARTITION.getByName(partition.getStatus()) != null) {
					logger.info("HMC平台[{}]-[{}]当前状态为：{}，本次定时任务暂不更新状态",
							vendor.getName(), partition.getName(), partition.getStatus());
				} else {
					partition.setStatus(status);
					logger.info("更新HMC平台[{}]-[{}]状态为：{}", vendor.getName(), partition.getName(), status);
				}
				if (HmcStatus.PARTITION.INSTALLING.getName().equals(partition.getStatus())) {
					logger.info("HMC平台[{}]-[{}]当前状态为：{}，本次定时任务暂不更新IP",
							vendor.getName(), partition.getName(), partition.getStatus());
				} else {
					partition.setIp(ip);
					logger.info("更新HMC平台[{}]-[{}]IP为：{}", vendor.getName(), partition.getName(), ip);

				}
				partition.setCode(code);
				logger.info("更新HMC平台[{}]-[{}]状态码为：{}", vendor.getName(), partition.getName(), code);
				partition.setRmcState(rmc);
				logger.info("更新HMC平台[{}]-[{}]RMC连接状态为：{}", vendor.getName(), partition.getName(), rmc);

				hmcPartitionRepository.update(partition);
			}
		}
		logger.info("定时更新HMC的逻辑分区和VIOS完成");
	}
}
