//Editor-Info: -*- C++ -*-
//
//Subject: TOVE project / ILMI
//
//File: ilmiswitchgetcommand.cpp
//
//Version: $Revision: 1.8 $
//
//State: $State: Exp $
//
//Date: $Date: 1998/10/20 13:03:14 $
//
//Organisation:
//      Helsinki University of Technology
//      Laboratory of Telecommunications Software and Multimedia
//
//Author:
//      Timo Pärnänen
//
//Description:
//      See corresponding header file.
//
//Copyright:
//      Copyright 1999 Helsinki University of Technology
//      ALL RIGHTS RESERVED BETWEEN JANUARY 1996 AND JUNE 1999.
//
//Licence:
//
//
//History: 

#include "ilmiprotocol.h"
#include "ilmiswitchgetcommand.h"

#include "pf/tools.h"
#include "pf/error.h"

#include <OB/CORBA.h>
#include "mgmt/toveswitch.h"
#include "mgmt/mgmtcommand.h"

#include "pf/exception.h"

ilmiCommand *ilmiSwitchGetCommand :: createSwitchGetCommand(
    ilmiProtocol *protocol_)
{
    ilmiSwitchGetCommand *newCommand =
        new ilmiSwitchGetCommand(protocol_);
    return newCommand;
}

ilmiSwitchGetCommand :: ilmiSwitchGetCommand(ilmiProtocol *protocol_)
    : ilmiCommand(),
      _protocol(protocol_)
{
    return;
}

ilmiSwitchGetCommand :: ~ilmiSwitchGetCommand(void)
{
    _protocol = 0;
    return;
}

void ilmiSwitchGetCommand :: response(PDUInt &errorStatus_,
                                      AsnInt &errorIndex_,
                                      VarBindList &list_)
{
    if (_protocol != 0)
    {
        mgmtCommand command("return");
        if ((AsnIntType)errorStatus_ != 0)
        {
            command = mgmtCommand("error");
            string errorStatus = 
                pfTools::intToString((AsnIntType)errorStatus_);
            string errorIndex = 
                pfTools::intToString((AsnIntType)errorIndex_);
            command.addParameter("errorStatus", errorStatus);
            command.addParameter("errorIndex", errorIndex);
        }

        try
        {
            list_.SetCurrToFirst();            
            for(int i=0; i<list_.Count(); i++, list_.GoNext())
            {
                VarBind *var = list_.Curr();
                string name;
                string value;
                _protocol->varBindToString(var, name, value);
                command.addParameter(name, value);
            }
        }
        catch (mibException &mibexception)
        {
            // If local varBindToString is failed, switch
            // is informed using error command with genErr reason. 
            command = mgmtCommand("error");
            command.addParameter("errorStatus", "5"); // genErr
            command.addParameter("errorIndex", "0");
        }

        try
        {
            _protocol->sendExecute(command);
        }
        catch (toveSwitch_InvalidCommand &exception)
        {
            THROW_MESSAGE_TYPE_NON_EXISTENT_OR_NOT_IMPLEMENTED;
        }
        catch (CORBA_UserException &userException)
        {
            THROW_INVALID_INFORMATION_ELEMENT_CONTENTS; 
        }        
    }
    else
    {
        throw pfNullPointerException(PF_EX_INFO);
    }
    return;
}
