# -*- coding: utf-8 -*-
#
# This file is part of Karesansui.
#
# Copyright (C) 2009 HDE, Inc.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#

import web

import karesansui
from karesansui.lib.rest import Rest, auth
from karesansui.lib.virt.virt import KaresansuiVirtException, \
     KaresansuiVirtConnection
from karesansui.lib.utils import json_dumps, is_param
from karesansui.lib.checker import \
    Checker, CHECK_EMPTY, CHECK_VALID

from pysilhouette.command import dict2command
from karesansui.db.access._2pysilhouette import jg_save, jg_delete
from karesansui.db.model._2pysilhouette import Job, JobGroup

NETWORK_ACTIVE = 1
NETWORK_INACTIVE = 0

def validates_nw_status(obj):
    checker = Checker()
    check = True

    _ = obj._
    checker.errors = []

    if is_param(obj.input, 'status'):
        check = checker.check_status(
                _('Status'),
                obj.input.status,
                CHECK_EMPTY | CHECK_VALID,
                [NETWORK_ACTIVE, NETWORK_INACTIVE]
            ) and check
    else:
        check = False
        checker.add_error(_('"%s" is required.') % _('Status'))

    obj.view.alert = checker.errors

    return check

class HostBy1NetworkBy1Status(Rest):
    @auth
    def _GET(self, *param, **params):
        """<comment-ja>
        ネットワークがアクティブかどうかのステータス
         - inactive = 0
         - active = 1
        </comment-ja>
        <comment-en>
        TODO: English Comment
        </comment-en>
        """
        host_id = self.chk_hostby1(param)
        if host_id is None: web.notfound()

        # TODO valid
        network_name =param[1]
        if not network_name:
            return web.badrequest()

        kvc = KaresansuiVirtConnection()
        try:
            kvn = kvc.search_kvn_networks(network_name)[0]
            status = NETWORK_INACTIVE
            if kvn.is_active():
                status = NETWORK_ACTIVE

            if self.__template__["media"] == 'json':
                self.view.status = json_dumps(status)
            else:
                self.view.status = status

            self.__template__.dir = 'hostby1networkby1'
        finally:
            kvc.close()

        return True

    @auth
    def _PUT(self, *param, **params):
        host_id = self.chk_hostby1(param)
        if host_id is None: web.notfound()

        # TODO valid
        network_name =param[1]
        if not network_name:
            return web.notfound()

        if validates_nw_status(self):
            return web.badrequest(self.view.alert)

        status = int(self.input.status)

        kvc = KaresansuiVirtConnection()
        try:
            kvn = kvc.search_kvn_networks(network_name)[0]
            if status == NETWORK_INACTIVE:
                # Stop network
                network_start_stop_job(self, network_name, 'stop')
                return web.seeother("/host/%s/network/%s.%s" % (host_id, network_name, self.__template__['media']))
            elif status == NETWORK_ACTIVE:
                # Start network
                network_start_stop_job(self, network_name, 'start')
                return web.seeother("/host/%s/network/%s.%s" % (host_id, network_name, self.__template__['media']))
            else:
                return web.badrequest()
        finally:
            kvc.close()

def network_start_stop_job(obj, network_name, action):
    """
    Register start/stop network job into pysilhouette
    @param obj: Rest object
    @param network_name: Name of network to start or stop
    @type network_name: string
    @param action: 'start' or 'stop'
    @type action: string
    """
    if not network_name:
        raise KaresansuiException

    if (karesansui.sheconf.has_key('env.uniqkey') is False) \
           or (karesansui.sheconf['env.uniqkey'].strip('') == ''):
        raise KaresansuiException

    if not (action == 'start' or action == 'stop'):
        raise KaresansuiException

    _cmd = None
    _jobgroup = None
    if action == 'start':
        _cmd = dict2command(
            "%s/%s" % (karesansui.config['application.bin.dir'], 'restart_network.py'),
            dict(name=network_name, force=None))
        _jobgroup = JobGroup('(Re)start network', karesansui.sheconf['env.uniqkey'])
        _jobgroup.jobs.append(Job('(Re)start network', 0, _cmd))
    else:
        _cmd = dict2command(
            "%s/%s" % (karesansui.config['application.bin.dir'], 'stop_network.py'),
            dict(name=network_name))
        _jobgroup = JobGroup('Stop network', karesansui.sheconf['env.uniqkey'])
        _jobgroup.jobs.append(Job('Stop network', 0, _cmd))
    
    # JobGroup INSERT
    try:
        jg_save(obj.pysilhouette.orm, _jobgroup)
        obj.pysilhouette.orm.commit()
    except:
        obj.logger.error('Failed to register the JobGroup. - jobgroup name=%s' \
                          % _jobgroup.name)
        raise KaresansuiException

urls = (
    '/host/(\d+)/network/([^/]+)/status?(\.part|\.json)?$', HostBy1NetworkBy1Status,
    )
