tacker.py 4.13 KB
Newer Older
Burak Kelgin's avatar
Burak Kelgin committed
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import json
import requests
import argparse
import sys
from .communication import myparser
from polls.models import vnf, servers

VNF_ID_DEFAULT='6be04d23-6329-4c7b-a276-9a0a184bf22d'
VNFD_ID_DEFAULT='e2975fc4-2414-4b49-960b-5cdb43b61f35'
VIM_ID_DEFAULT='f565dbdc-74aa-4ea9-8c65-511b4fcd1afb'
TRAFFIC_TYPE_DEFAULT='icmp'
VNF_UPDATE_BODY_DEFAULT = {"vnf": {"attributes": {"config":"vdus:\n  VDU1:\n    config:\n      firewall: |\n       package firewall\n       config defaults\n            option src net_mgmt\n            option proto icmp\n            option target REJECT\n"}}}


def auth():
    headers = {'Content-Type': 'application/json', }
    #data = '{"auth": {"tenantName": "", "passwordCredentials":{"username": "tacker", "password": "tacker"}}}'
    data = '{ "auth": { "identity": { "methods": ["password"],"password": {"user": {"domain": {"name": "'"default"'"},"name": "'"tacker"'", "password": "'"tacker"'"} } }, "scope": { "project": { "domain": { "name": "'"default"'" }, "name":  "'"services"'" } } }}'
    resp = requests.post('http://192.168.171.3:5000/v3/auth/tokens?nocatalog', headers=headers, data=data)
    # test3 = requests.get('http://192.168.171.3/horizon/project/network_topology/', headers=headers, data=data )
    # ('http://192.168.171.3/horizon/project/network_topology/', headers=headers, data=data)
    # convert response to dict
    body = resp.json()
    headers = resp.headers
    # convert dict to str
    #test2 = json.dumps(test2)
    # convert str to json
    #test2 = json.loads(test2)
    # access auth token id
    #tokenid = test2['access']['token']['id']  # token id is inside here :)
    print(headers)
    headers2 = {'X-Auth-Token': headers['X-Subject-Token'] }
    return headers2

def create_vnf(vim_id, vnfd_id, vnf_name, vnf_description):
    header = auth()
    print(header)
    anotherUrl = 'http://192.168.171.3:9890/v1.0/vnfs'
    data={"vnf": {"attributes": {}, "vim_id": vim_id, "description": vnf_description, "vnfd_id": vnfd_id, "name": vnf_name}}
    nextreq = requests.post(anotherUrl, headers=header, data=json.dumps(data))
    print(nextreq.content)
    return nextreq

def get_vnfd(vnfd_id):
    header = auth()
    print(header)
    anotherUrl = 'http://192.168.171.3:9890/v1.0/vnfds/' + vnfd_id
    nextreq = requests.get(anotherUrl, headers=header)
    print(nextreq.content)
    return nextreq

def get_vnf_list():
    header=auth()
    print(header)
    anotherUrl='http://192.168.171.3:9890/v1.0/vnfs'
    nextreq= requests.get(anotherUrl, headers=header)
    print( nextreq.content)
    parsed = myparser(nextreq)
    print(parsed)
    vnf.objects.all().delete()
    for parsed in parsed["vnfs"]:
        id=parsed["id"]

        print(servers.objects.all())
        for s in servers.objects.all():
            print(id[-12:])
            print(s.server_name)
            if id[-12:] in s.server_name:
                a = vnf(vnf_name=parsed["name"], vnf_id=parsed["id"], vnf_status=parsed["status"], vnf_server_id=s.server_id)
                print(s.server_id)
                b=servers.objects.get(id=s.id)
                b.server_type='VNF'
                b.save()
                print(s.id)
                break
            else:
                a = vnf(vnf_name=parsed["name"], vnf_id=parsed["id"], vnf_status=parsed["status"])
        a.save()

    return parsed

def get_vnfd_list():
    header = auth()
    print(header)
    anotherUrl = 'http://192.168.171.3:9890/v1.0/vnfds'
    nextreq = requests.get(anotherUrl, headers=header)
    #res=nextreq.content
    #print(nextreq.content)
    #print(type(nextreq.content))
    #parsed= res.decode()  #convert to string
    #print(type(parsed))
    #decoded = json.loads(parsed)
    #print(json.dumps(decoded, sort_keys=True, indent=4))
    parsed=myparser(nextreq)
    print("JSON parsing example: ", parsed['vnfds'][1]['description'])
    return nextreq

def update_vnf(vnf_id, update_body):
    header = auth()
    print(header)
    anotherUrl = 'http://192.168.171.3:9890/v1.0/vnfs/' + vnf_id
    data = update_body
    nextreq= requests.put(anotherUrl, headers=header, data=json.dumps(data))
    print(nextreq.content)
    return nextreq

if __name__ == '__main__':
    get_vnf_list()