package com.huawei.esdk.fusionmanager.demo.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.google.gson.Gson;
import com.huawei.esdk.fusionmanager.demo.factory.ServiceManageFactory;
import com.huawei.esdk.fusionmanager.demo.utils.ParametersUtils;
import com.huawei.esdk.fusionmanager.local.model.net.QueryVPCReqEx;
import com.huawei.esdk.fusionmanager.local.model.net.QueryVPCResp;
import com.huawei.esdk.fusionmanager.local.model.user.AuthenticateReq;
import com.huawei.esdk.fusionmanager.local.model.user.AuthenticateResp;
import com.huawei.esdk.fusionmanager.local.model.user.LoginUserStatus;

/**
 * “查询VPC”请求处理类
 * @author dWX213051
 * @see
 * @since eSDK Cloud V100R003C30
 */
public class QueryVPCServlet extends HttpServlet
{
    /**
     * 序列化版本标识
     */
    private static final long serialVersionUID = 6749720431926648350L;

    /**
     * log日志对象
     */
    private static final Logger LOGGER = Logger
            .getLogger(QueryVPCServlet.class);

    /**
     * gson，用于转换String和json之间的转换
     */
    private Gson gson = new Gson();

    @Override
    protected void doPost(
        HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
    {
        doGet(request, response);
    }

    @Override
    protected void doGet(
        HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
    {
        //设置request的编码
        request.setCharacterEncoding("UTF-8");
        
        // 获取需要调用的方法名
        String methodName = request.getParameter("method");
        String resp = "";

        if ("queryVPC".equals(methodName))
        {
            // 查询VPC
            resp = queryVPC(request);
        }

        //设置response的编码
        response.setContentType("application/json;charset=UTF-8");
        // 输出流
        PrintWriter pw = response.getWriter();
        pw.print(resp);
        pw.close();
    }

    /**
     * 查询VPC信息
     * 
     * @param request HttpServletRequest对象
     * @return json格式的字符串
     * @see
     * @since eSDK Cloud V100R003C30
     */
    public String queryVPC(HttpServletRequest request)
    {
    	// 定义返回结果
        String response = null;

        AuthenticateReq req = new AuthenticateReq();
        req.setUserName(ParametersUtils.userName);
        req.setPassword(ParametersUtils.password);
        
        // 鉴权
        AuthenticateResp authntiateResp = ServiceManageFactory.getUserService().authenticate(req);
        
        if (!LoginUserStatus.SUCCESS.equals(authntiateResp.getLoginUserStatus()))
        {
        	LOGGER.error("Failed to Login FM System!");
        	return gson.toJson(authntiateResp);
        }
        
        LOGGER.info("Login Success!");

        LOGGER.info("Begin to query VPC information.");
        
        String vdcID = request.getParameter("tenantID");
        String cloudInfraID = request.getParameter("cloudInfraID");
        String name = request.getParameter("name");
        String limit = request.getParameter("limit");
        String start = request.getParameter("start");
        
        ServiceManageFactory.getUserVdcRelationResource().modify(authntiateResp.getUserId(), vdcID);
        
        QueryVPCReqEx reqEx = new QueryVPCReqEx();
        reqEx.setVdcId(vdcID);
        reqEx.setCloudInfraId(cloudInfraID);
        reqEx.setName(name);
        reqEx.setLimit(Integer.valueOf(limit));
        reqEx.setStart(Integer.valueOf(start));
        
        QueryVPCResp resp = ServiceManageFactory.getVPCResource().query(reqEx);
        
        HttpSession session = request.getSession();
        session.setAttribute("VPC_RESP", resp);
        
        // 根据接口返回数据生成JSON字符串，以便于页面展示
        response = gson.toJson(resp);
        
        LOGGER.info("Finish to query VPC, response is : " + response);

        return response;
    }
}
