package com.bocloud.ims.booter.listener;

import java.nio.charset.Charset;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.bocloud.common.model.BocloudResult;
import com.bocloud.common.model.RequestContext;
import com.bocloud.common.utils.JSONTools;
import com.bocloud.ims.interfaces.resource.RosProcessService;

/**
 * 资源编排监听器
 *
 * @author weiwei
 */
@Component("rosListener")
public class RosListener {

	private static Logger logger = LoggerFactory.getLogger(RosListener.class);
	@Autowired
	private RosProcessService rosProcessService;

	@RabbitListener(queues = "bocloud.ros.queue", admin = "amqpAdmin", autoStartup = "true")
	public void process(Message message) {
		try {

			String content = new String(message.getBody(), Charset.forName("UTF-8"));
			processDebugLog("get bocloud message:{}", content);
			if (StringUtils.isBlank(content)) {
				// 消息没有内容，直接确认收到 淹没处理
				return;
			}
			JSONObject object = JSONTools.isJSONObj(content);
			if (null == object) {
				// 消息参数格式错误，直接确认收到淹没处理
				return;
			}
			logger.info("收到资源编排创建资源消息{}", content);
			RequestContext context = JSONObject.parseObject(object.getString("context"), RequestContext.class);
			String queue = object.getString("callback");
			JSONObject paramObj = object.getJSONObject("params");
			if (null == paramObj) {
				return;
			}
			paramObj.put("origin", object.getString("origin"));
			switch (object.getString("code")) {
			case "task.resource.cvm.vsp":
			case "task.resource.cvm.osp":
				rosProcessService.createServer(paramObj.toJSONString(), context, queue);
				break;
			case "task.resource.cbs.osp":
				rosProcessService.createVolume(paramObj.toJSONString(), context, queue);
				break;
			case "task.resource.cfw.osp":
				rosProcessService.createFirewall(paramObj.toJSONString(), context, queue);
				break;
			case "task.resource.fwp.osp":
				rosProcessService.createFirewallPolicy(paramObj.toJSONString(), context, queue);
				break;
			case "task.resource.fwr.osp":
				rosProcessService.createFirewallRule(paramObj.toJSONString(), context, queue);
				break;
			case "task.resource.clb.osp":
				rosProcessService.createLoadBalancer(paramObj.toJSONString(), context, queue);
				break;
			case "task.resource.cls.osp":
				rosProcessService.createListener(paramObj.toJSONString(), context, queue);
				break;
			case "task.resource.clm.osp":
				rosProcessService.createLbMember(paramObj.toJSONString(), context, queue);
				break;
			case "task.resource.vpc.osp":
				rosProcessService.createNetwork(paramObj.toJSONString(), context, queue);
				break;
			case "task.resource.crs.osp":
				rosProcessService.createRouter(paramObj.toJSONString(), context, queue);
				break;
			case "task.resource.seg.osp":
				rosProcessService.createSecGroup(paramObj.toJSONString(), context, queue);
				break;
			case "task.resource.sgr.osp":
				rosProcessService.createSecGroupRule(paramObj.toJSONString(), context, queue);
				break;
			case "task.resource.sbn.osp":
				rosProcessService.createSubnet(paramObj.toJSONString(), context, queue);
				break;
			case "task.resource.vpc.vip":
				rosProcessService.createVip(paramObj.toJSONString(), context, queue);
				break;
			default:
				new BocloudResult(false, "平台不支持该类资源");
				break;
			}

		} catch (Exception e) {
			logger.error("Get error message:{}", e.getMessage());
		}
	}

	private void processDebugLog(String format, Object... objects) {
		if (logger.isDebugEnabled()) {
			logger.debug(format, objects);
		}
	}

}
