//Editor-Info: -*- C++ -*-
//
//Subject: TOVE project / sw
//
//File: swconfigimpl.cpp
//
//Version: $Revision: 1.10 $
//
//State: $State: Exp $
//
//Date: $Date: 1999/03/02 14:47:21 $
//
//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 "mgmt/mgmtcommand.h"
#include "pf/debug.h"

#include "swswitch.h"
#include "swport.h"
#include "swconfigimpl.h"

swConfigImpl :: swConfigImpl(void)
    : mgmtDelegationBase()
{
    return;
}

swConfigImpl :: ~swConfigImpl(void)
{
    // ++TODO++
    // unRegisterFromNameService();
    return;
}

void swConfigImpl :: init(const string &pointCode_)
{
    createOB();
    try
    {
        // Construct name for naming component
        string name = pointCode_;
        name += "/config";
        
        registerToNameService(name);
    }
    catch (...)
    {
        debugUser("registerToNameService failed");
        exit(1);
    }
    return;
}
        
string swConfigImpl :: receiveGet(const string &name_)
{
    debugString("receiveGet", name_);
    string result = "result";
    return result;
}

void swConfigImpl :: receiveSet(const string &name_, const string &value_)
{

    debugString(name_, value_);

    if (name_.compare("prefix") == 0)
    {
        swSwitch::instance()->setNetPrefix(value_);
    }
    else
    {
        debugString("Invalid name",name_);
        // ++TODO++ else an exception
    }
    return;
}

void swConfigImpl :: receiveExecute(mgmtCommand &command_)
{
    command_.setCurrentTheFirst();
    string name = command_.getCommandName();

    //++TODO++
    // ERROR HANDLING !!!
    // not found, already found, not enough parameter etc..
    
    if (name.compare("port") == 0)
    {
        string portNumberStr;
        string portType;
        command_.getNextParameter(portNumberStr);
        command_.getNextParameter(portType);

        int portNumber = atoi(portNumberStr.c_str());
        try
        {
            swSwitch::instance()->addPort(portNumber, portType);
        }
        catch (pfException &exception)
        {
            debugUser("Port Failed !!");
            debugPfUlong("Port: ", portNumber);
            debugString("type: ", portType);
            throw toveSwitch_InvalidValue(portNumberStr.c_str());
            // ++TODO++ or invalid type
        }
    }
    else if (name.compare("addLink") == 0)
    {
        debugUser("addLink");
        string portStr;
        string type;
        string linkStr;
        command_.getNextParameter(portStr);
        command_.getNextParameter(type);
        command_.getNextParameter(linkStr);
        pfUlong portNumber = atoi(portStr.c_str());
        pfUlong linkNumber = atoi(linkStr.c_str());

        try
        {
            swPort *port = swSwitch::instance()->findPort(portNumber);

            if (type.compare("UNI") == 0)
            {
                port->addUNIlink(linkNumber);
            }
            else if (type.compare("NNI") == 0)
            {
                string DPCstr;
                command_.getNextParameter(DPCstr);
                pfUlong destinationPointCode = atoi(DPCstr.c_str());
                port->addNNIlink(linkNumber, destinationPointCode);
            }
            else
            {
                debugString("Invalid type", type);
                // ++TODO++ throw an exception
            }
        }
        catch (pfException &exception)
        {
	    debugUser("--- throw exception to GUI ---");
            throw toveSwitch_InvalidValue(linkStr.c_str());
        }
    }
//    else if (name.compare("setParameter") == 0)
//    {        
//    }
//    else if (name.compare("setPrototypeParameter") == 0)
//    {
//    }
    else
    {
        debugString("Invalid command", name);
        //++TODO++ throw an exception
    }
    return;
}

