summaryrefslogtreecommitdiffstats
path: root/examples/policies/policy_utils.py
blob: 0c1b5951f95b4deddf295765931b1431814b99d7 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
from jinja2 import Template
import json
import os
import requests
import sys

BASE_DIR = os.path.dirname(os.path.dirname(__file__))

HEADERS = {'Content-Type': 'application/json'}
AUTH = requests.auth.HTTPBasicAuth('healthcheck', 'zb!XztG34')


def get_tosca_policy(policy):
    pol = json.loads(policy)
    tosca_policy = {
            'tosca_definitions_version': 'tosca_simple_yaml_1_1_0',
            'topology_template': {
                    'policies': [pol]
                }
            }
    return json.dumps(tosca_policy)

def gen_policy(template_dir, gen_dir, filename, jinja_args):
    with open(os.path.join(template_dir, filename), 'r') as file:
        contents = file.read()
    tm = Template(contents)
    gen = tm.render(jinja_args)
    tosca_policy = get_tosca_policy(gen)
    with open(os.path.join(gen_dir, filename), 'w') as file:
        file.write(tosca_policy)

def create_and_push_policies(policy_dir):
    for filename in os.listdir(policy_dir):
        if filename.endswith('.json'):
            with open(os.path.join(policy_dir, filename), 'r') as file:
                data = json.loads(file.read())
                metadata = create_policy(data)
                if metadata:
                    push_policy(metadata)

def delete_policies(policy_dir):
    for filename in os.listdir(policy_dir):
        if filename.endswith('.json'):
            with open(os.path.join(policy_dir, filename), 'r') as file:
                data = json.loads(file.read())
                policy_id = list(data['topology_template']['policies'][0].keys())[0]
                undeploy_policy(policy_id)
                metadata = delete_policy(data)

def create_policy(data):
    policy = data['topology_template']['policies'][0]
    content = policy[list(policy.keys())[0]]
    policy_type = content['type']
    type_version = content['type_version']
    policy_url = "https://policy-api:6969"
    path = '/policy/api/v1/policytypes/{}/versions/{}/policies'.format(policy_type, type_version)
    url = policy_url + path
    try:
        response = requests.post(url, headers=HEADERS, auth=AUTH, data=json.dumps(data), verify=False)
    except Exception as e:
        print(str(e))
        return None
    if response.status_code == 200:
        print('Policy {} created'.format(content['metadata']['policy-id']))
        return content['metadata']
    else:
        print(response.content)
        return None

def push_policy(metadata):
    data = {'policies': [metadata]}
    policy_url = "https://policy-pap:6969"
    path = '/policy/pap/v1/pdps/policies'
    url = policy_url + path
    try:
        response = requests.post(url, headers=HEADERS, auth=AUTH, data=json.dumps(data), verify=False)
    except Exception as e:
        print(str(e))
        print("Cannot push policy {}".format(metadata['policy-id']))
    if response.status_code == 200:
        print("Policy {} pushed".format(metadata['policy-id']))
    else:
        print(response.content)

def undeploy_policy(policy_id):
    policy_url = "https://policy-pap:6969"
    path = '/policy/pap/v1/pdps/policies/{}'.format(policy_id)
    url = policy_url + path
    try:
        response = requests.delete(url, headers=HEADERS, auth=AUTH, verify=False)
    except Exception as e:
        print(str(e))
        print("Cannot undeploy policy {}".format(policy_id))
    if response.status_code == 200:
        print("Policy {} undeployed".format(policy_id))
    else:
        print(response.content)

def delete_policy(data):
    policy = data['topology_template']['policies'][0]
    content = policy[list(policy.keys())[0]]
    policy_type = content['type']
    type_version = content['type_version']
    policy_id = content['metadata']['policy-id']
    version = content['version']
    policy_url = "https://policy-api:6969"
    path = '/policy/api/v1/policytypes/{}/versions/{}/policies/{}/versions/{}'.format(policy_type, type_version, policy_id, version)
    url = policy_url + path
    try:
        response = requests.delete(url, headers=HEADERS, auth=AUTH, data=json.dumps(data), verify=False)
    except Exception as e:
        print(str(e))
        return None
    if response.status_code == 200:
        print('Policy {} deleted'.format(content['metadata']['policy-id']))
        return content['metadata']
    else:
        print(response.content)
        return None

def generate_nssi_policies(jinja_args):
    template_dir = BASE_DIR + 'nssi_policies'
    gen_dir = BASE_DIR + 'gen_nssi_policies'

    if not os.path.exists(gen_dir):
        os.mkdir(gen_dir)

    for filename in os.listdir(template_dir):
        if filename.endswith('.json'):
            gen_policy(template_dir, gen_dir, filename, jinja_args)

def generate_nsi_policies(jinja_args):
    template_dir = BASE_DIR + 'nsi_policies'
    gen_dir = BASE_DIR + 'gen_nsi_policies'

    if not os.path.exists(gen_dir):
        os.mkdir(gen_dir)

    for filename in os.listdir(template_dir):
        if filename.endswith('.json'):
            gen_policy(template_dir, gen_dir, filename, jinja_args)

def create_policy_types(policy_dir):
    for filename in os.listdir(policy_dir):
        if filename.endswith('.json'):
            with open(os.path.join(policy_dir, filename), 'r') as file:
                data = json.loads(file.read())
                create_policy_type(data)

def create_policy_type(data):
    policy_url = "https://policy-api:6969"
    path = '/policy/api/v1/policytypes'
    url = policy_url + path
    try:
        response = requests.post(url, headers=HEADERS, auth=AUTH, data=json.dumps(data), verify=False)
    except Exception as e:
        print(str(e))
        return None
    if response.status_code == 200:
        print('Policy type created')
    else:
        print(response.content)
        return None


action = sys.argv[1]

if action == "generate_nssi_policies":
    jinja_args = {
      'service_name': sys.argv[2],
      'goal': sys.argv[3],
      'attribute': sys.argv[4]
    }
    generate_nssi_policies(jinja_args)

elif action == "create_and_push_policies":
    policy_dir = sys.argv[2]
    create_and_push_policies(policy_dir)

elif action == "delete_policies":
    policy_dir = sys.argv[2]
    delete_policies(policy_dir)

elif action == "generate_nsi_policies":
    jinja_args = {
      'service_name': sys.argv[2]
    }
    generate_nsi_policies(jinja_args)

elif action == "create_policy_types":
    policy_dir = sys.argv[2]
    create_policy_types(policy_dir)