#! /usr/bin/python

import sys
import logging
import requests
import json
from vcpecommon import *


class SdcUtils:
    def __init__(self, vcpecommon):
        """
        :param vcpecommon:
        """
        self.logger = logging.getLogger(__name__)
        self.logger.setLevel(logging.DEBUG)
        self.vcpecommon = vcpecommon

    def download_vcpe_service_template(self):
        """
        :return: 
        """

        url = self.vcpecommon.sdc_service_list_url
        self.logger.info(url)
        r = requests.get(url, headers=self.vcpecommon.sdc_be_request_headers, auth=self.vcpecommon.sdc_be_request_userpass)
        self.logger.debug(r)
        data = r.json()

        self.logger.debug('---------------------------------------------------------------')
        self.logger.debug('------- Creation request submitted to SDC, got response --------')
        self.logger.debug('response code = %s' % r.status_code )
        self.logger.debug(json.dumps(data, indent=4, sort_keys=True))
        self.logger.debug('---------------------------------------------------------------')

        for service in data:
            if service['name'].startswith('demoVCPE') or service['name'].startswith('vCPEResCust'):
                self.logger.debug('service name = %s, url = %s' % (service['name'], service['toscaModelURL']))
                self.download_file(self.vcpecommon.sdc_be_url_prefix + service['toscaModelURL'])

    def get_filename_from_cd(self, cd):
        """
        cd: in the format of Content-Disposition: attachment; filename="service-Demovcpeinfra-csar.csar"
        """
        fname = re.findall('filename="(.+)"', cd)
        self.logger.debug('fname = %s' % fname[0])
        return fname[0]

    def download_file(self, url):
        self.logger.info(url)
        response = requests.get(url, headers=self.vcpecommon.sdc_be_request_headers, auth=self.vcpecommon.sdc_be_request_userpass)
        filename = 'csar/' + self.get_filename_from_cd(response.headers.get('Content-Disposition'))
        open(filename, 'wb').write(response.content)

    def create_allotted_resource_subcategory(self, newSubcategory):
        """
        :param newSubcategory: a new subcategory under Allotted Resource, like BRG
        :return:
        """
        url = self.vcpecommon.sdc_get_category_list_url
        self.logger.info(url)
        resp = requests.get(url, headers=self.vcpecommon.sdc_fe_request_headers, auth=self.vcpecommon.sdc_fe_request_userpass)
        data = resp.json()

        self.logger.debug('---------------------------------------------------------------')
        self.logger.debug('------- Creation request submitted to SDC, got response --------')
        self.logger.debug('response code = %s' % resp.status_code )
        self.logger.debug(json.dumps(data, indent=4, sort_keys=True))
        self.logger.debug('---------------------------------------------------------------')

        if data['resourceCategories']:
            for category in data['resourceCategories']:
                if category['name'] == 'Allotted Resource':
                    for subcategory in category['subcategories']:
                        if subcategory['name'] == newSubcategory:
                            self.logger.debug('Subcategory %s already exists' % newSubcategory)
                            return
                    self.logger.debug('Creating a new subcategory %s' % newSubcategory)
                    url = self.vcpecommon.sdc_create_allotted_resource_subcategory_url
                    self.logger.info(url)
                    details = {"name" : newSubcategory}
                    resp = requests.post(url, headers=self.vcpecommon.sdc_fe_request_headers, auth=self.vcpecommon.sdc_fe_request_userpass, json=details)
                    self.logger.debug('---------------------------------------------------------------')
                    self.logger.debug('------- Creation subcategory request submitted to SDC, got response --------')
                    self.logger.debug('response code = %s' % resp.status_code )
                    self.logger.debug('---------------------------------------------------------------')



