summaryrefslogtreecommitdiffstats
path: root/test/vcpe/sdcutils.py
blob: 60050a4b508ac5556985e3604c82345f4b6e1dd1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#! /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')) and service['distributionStatus'] == 'DISTRIBUTED':
                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('---------------------------------------------------------------')