/***********************************************************
 Copyright (C) 2017 VeriSign, Inc.

 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.

 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

 http://www.verisign.com/nds/naming/namestore/techdocs.html
 ***********************************************************/
package com.verisign.epp.serverstub;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.verisign.epp.codec.change.type.EPPChangeAction;
import com.verisign.epp.codec.changeext.EPPChangeExt;
import com.verisign.epp.codec.gen.EPPResponse;
import com.verisign.epp.codec.gen.EPPResult;
import com.verisign.epp.codec.gen.EPPTransId;
import com.verisign.epp.codec.host.EPPHostUpdateCmd;
import com.verisign.epp.framework.EPPEvent;
import com.verisign.epp.framework.EPPEventResponse;
import com.verisign.epp.rootzone.util.RootZoneManager;


/**
 * The {@code RZHostHandler} class is a concrete implementation of the
 * {@code com.verisign.epp.framework.EPPHostHandler} class for the Root Zone. It
 * defines the Server's response to all received EPP Host Commands.
 * 
 * @see com.verisign.epp.framework.EPPEvent
 * @see com.verisign.epp.framework.EPPEventResponse
 */
public class RZHostHandler extends HostHandler {
	/** Constant server transaction id */
	private static final String svrTransId = "54322-SVR";

	/** Logger category */
	    private static Logger cat = LoggerFactory.getLogger(RZHostHandler.class);
	      


	/**
	 * Invoked when a Domain command is received in the RZMS. A Change Action is
	 * constructed from the incoming message and passed to the server to be
	 * associated with the specified Change Request.
	 * 
	 * @param aEvent
	 *           The {@code EPPEvent} that is being handled
	 * @param aData
	 *           Any data that a Server needs to send to this
	 *           {@code EPPHostdHandler}
	 * @return EPPEventResponse The response that should be sent back to the
	 *         client.
	 */
	protected EPPEventResponse doHostUpdate(EPPEvent aEvent, Object aData) {
		EPPHostUpdateCmd cmd = (EPPHostUpdateCmd) aEvent.getMessage();

		cat.debug("doHostUpdate: command = [" + cmd + "]");

		String requestId = null;

		EPPTransId transId = new EPPTransId(cmd.getTransId(), svrTransId);

		if (cmd.getTransId() == null) {
			cat.error("doHostUpdate: Client transaction identifier is missing");

			EPPResult theResult = new EPPResult(EPPResult.PARAM_VALUE_POLICY_ERROR,
			      "Client transaction identifier is required on update");
			EPPResponse theResponse = new EPPResponse(transId, theResult);

			return new EPPEventResponse(theResponse);
		}

		if (cmd.hasExtension(EPPChangeExt.class)) {
			EPPChangeExt ext = (EPPChangeExt) cmd.getExtension(EPPChangeExt.class);
			requestId = ext.getRequestId();
		}
		else {
			cat.error("doHostUpdate: EPPChangeExt missing");

			EPPResult theResult = new EPPResult(EPPResult.PARAM_VALUE_POLICY_ERROR,
			      "Change Extension is required on update");
			EPPResponse theResponse = new EPPResponse(transId, theResult);

			return new EPPEventResponse(theResponse);
		}

		EPPChangeAction action = new EPPChangeAction(requestId, cmd.getTransId(), svrTransId);
		action.setDefinition(cmd.toString());

		/**
		 * LOGIC: Simulate adding Change Action
		 */
		RootZoneManager.addChangeAction(requestId, action);

		// Respond
		EPPResponse rsp = new EPPResponse(transId);
		rsp.setResult(EPPResult.SUCCESS);
		return new EPPEventResponse(rsp);
	}


}
