package com.bocloud.ims.booter.controller.resource.hmc;

import com.bocloud.common.annotations.Log;
import com.bocloud.common.enums.BoCloudService;
import com.bocloud.common.model.BocloudResult;
import com.bocloud.common.model.RequestContext;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.ResultTools;
import com.bocloud.ims.entity.resource.hmc.HmcPhysicalDisk;
import com.bocloud.ims.entity.resource.hmc.HmcPhysicalFibreChannel;
import com.bocloud.ims.entity.resource.hmc.HmcVirtualDisk;
import com.bocloud.ims.entity.resource.hmc.HmcVirtualFibreChannelMapping;
import com.bocloud.ims.entity.resource.hmc.params.HmcStorageUnassign;
import com.bocloud.ims.interfaces.resource.hmc.HmcVolumeGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * hmc storage controller
 *
 * @author lkx007
 */
@RestController
@RequestMapping("/v1/hmc/vios")
@Api(value = "HMC虚拟存储器", tags = "HMC虚拟存储器")
public class HmcStorageController {
    @Autowired
    private HmcVolumeGroupService hmcVolumeGroupService;

    /**
     * 列表物理卷信息
     *
     * @return 列表结果
     */
    @Log(module = BoCloudService.IMS, target = "HMC虚拟存储器", operation = "查询物理磁盘")
    @RequestMapping(value = "/{id}/pds", method = {RequestMethod.GET})
    @ApiOperation(tags = {"CMC", "CSC"}, value = "查询物理磁盘", httpMethod = "GET")
    public BocloudResult<List<HmcPhysicalDisk>> pdList(
            @RequestParam(value = "serverId") Long serverId,
            @RequestParam(value = "vendorId") Long vendorId,
            @RequestParam(value = "viosId") Long viosId) {
        return hmcVolumeGroupService.listPd(serverId, vendorId, viosId);
    }

    /**
     * 列表VIOS的物理光纤通道
     *
     * @return 列表结果
     */
    @Log(module = BoCloudService.IMS, target = "HMC虚拟存储器", operation = "查询物理光纤卡")
    @RequestMapping(value = "/{id}/fcs", method = {RequestMethod.GET})
    @ApiOperation(tags = {"CMC", "CSC"}, value = "查询物理光纤卡", httpMethod = "GET")
    public BocloudResult<List<HmcPhysicalFibreChannel>> fcList(@PathVariable Long id) {
        return hmcVolumeGroupService.listFc(id);
    }

    /**
     * 列表VIOS的虚拟光纤通道映射
     *
     * @return 列表结果
     */
    @Log(module = BoCloudService.IMS, target = "HMC虚拟存储器", operation = "查询虚拟光纤适配器")
    @RequestMapping(value = "/{viosId}/fcs/adapters", method = {RequestMethod.GET})
    @ApiOperation(tags = {"CMC", "CSC"}, value = "查询虚拟光纤适配器", httpMethod = "GET")
    public BocloudResult<List<HmcVirtualFibreChannelMapping>> fcMapping(@RequestParam(value = "partitionId") Long partitionId,
                                                                        @RequestParam(value = "uniqueDeviceId") Long uniqueDeviceId) {
        return hmcVolumeGroupService.listFcMapping(partitionId, uniqueDeviceId);
    }

    /**
     * 列表虚拟磁盘信息
     *
     * @return 列表结果
     */
    @Log(module = BoCloudService.IMS, target = "HMC虚拟存储器", operation = "查询虚拟磁盘")
    @RequestMapping(value = "/{id}/vds", method = {RequestMethod.GET})
    @ApiOperation(tags = {"CMC", "CSC"}, value = "查询虚拟磁盘", httpMethod = "GET")
    public BocloudResult<List<HmcVirtualDisk>> vdList(@RequestParam(value = "serverId") Long serverId,
                                                      @RequestParam(value = "vendorId") Long vendorId,
                                                      @RequestParam(value = "lparId") Long lparId) {
        return hmcVolumeGroupService.listVd(serverId, vendorId, lparId);
    }

    /**
     * 分配虚拟磁盘
     *
     * @param params
     * @param context
     * @return 列表结果
     */
    @Log(module = BoCloudService.IMS, target = "HMC虚拟存储器", operation = "取消分配虚拟磁盘")
    @RequestMapping(value = "/{id}/vds", method = {RequestMethod.POST})
    @ApiOperation(tags = {"CMC", "CSC"}, value = "取消分配虚拟磁盘", httpMethod = "POST")
    public BocloudResult unassign(@RequestBody HmcStorageUnassign params,
                                  @Value(Common.REQ_CONTEXT) RequestContext context) {
        Long volumeId = params.getVolumeId();
        Long diskId = params.getDiskId();
        if (volumeId != null) {
            return hmcVolumeGroupService.unAssignPd(params, context);
        }
        if (diskId != null) {
            return hmcVolumeGroupService.unAssignVd(params, context);
        }
        return ResultTools.formatErrResult();
    }
}
