-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanagement_agent_api.py
More file actions
165 lines (138 loc) · 5.19 KB
/
management_agent_api.py
File metadata and controls
165 lines (138 loc) · 5.19 KB
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
#!/usr/bin/env python
import os
import json
from flask import jsonify
from flask import request
from eve import Eve
from eve.auth import BasicAuth, requires_auth
from subprocess import check_output, call
import time
from util import *
#======================================================
# API end-points
#======================================================
app = Eve()
# -------- Basics (Begin) ----------
@app.route('/api/vnf-exp-status', methods=['GET'])
def ems_status():
return "Running"
# -------- Basics (End) ----------
# -------- Lifecycle (Begin) ----------
@app.route('/api/install', methods=['POST'])
def install_function():
status = os.system(generate_cmd(cmd_path,"install.sh"))
return str(verify_status(status))
@app.route('/api/start', methods=['POST'])
def start_function():
status = os.system(generate_cmd(cmd_path,"start.sh"))
if verify_status(status):
is_running = open('status','w')
is_running.write("1")
is_running.close()
return str(verify_status(status))
@app.route('/api/stop', methods=['POST'])
def stop_function():
status = os.system(generate_cmd(cmd_path,"stop.sh"))
if verify_status(status):
is_running = open('status','w')
is_running.write("0")
is_running.close()
return str(verify_status(status))
@app.route('/api/status', methods=['GET'])
def get_running():
vnfd = read_vnfd()
script_name = "get_function_status.sh "
for op in vnfd['vnfd']['lifecycle']:
if op['operation'] == 'status':
script_name = op['file']+' '
function_name = vnfd['vnfd']['app']
status = run_cmd("sh "+VINES_PATH+script_name+function_name)
return str(status) # Running or Stopped
@app.route('/api/push-vnfp', methods=['POST'])
def write_file():
repo = VINES_PATH+'vnfp.zip'
with open(repo, 'wb') as f:
f.write(request.data)
status = os.system('unzip %s -d %svnfp' % (repo,VINES_PATH))
return str(verify_status(status))
@app.route('/api/metrics', methods=['GET'])
def get_metrics():
memory = get_memory_usage()
cpu = get_cpu_usage()
rx = get_bandwidth_usage('rx')
tx = get_bandwidth_usage('tx')
return jsonify(
{
"time_ms": 0,
"list": [
{
"percent_usage": cpu,
"type": "cpu"
},
{
"percent_usage": 0,
"type": "disk"
},
{
"percent_usage": memory,
"type": "memory"
},
{
"percent_usage": tx,
"type": "net_tx"
},
{
"percent_usage": rx,
"type": "net_rx"
}
]
})
# -------- Lifecycle (End) ----------
# -------- Service Function Chaining (Begin) ----------
@app.route('/api/set-sfc-forwarding', methods=['POST'])
def setsfcforwarding():
# parse data
data = json.loads(request.data)
last_vnf = data['last_vnf']
next_vnf = data['next_vnf']
classifier = data['classifier']
# Enabling IP forward
enable_ip_forward_cmd = "sudo bash -c 'echo 1 > /proc/sys/net/ipv4/ip_forward'"
response = run_shell_cmd(enable_ip_forward_cmd)
if response["status"] == "ERROR":
return build_response("error","Could not enable ip_forward")
for rule in classifier:
protocol = rule['protocol']
port = rule['port']
# Building commands lines
#enable_forward_cmd = "sudo iptables -A FORWARD -d %s -j ACCEPT" % (last_vnf)
enable_forward_cmd = "sudo iptables -A FORWARD -j ACCEPT"
config_forward_cmd = "sudo iptables -t nat -A PREROUTING -p %s --dport %s -j DNAT --to %s" % (protocol, port, next_vnf)
# Adding FORWARD rule
response = run_shell_cmd(enable_forward_cmd)
if response["status"] == "ERROR":
return build_response("error","Could not enable add forward rule")
# Adding PREROUTING rule
response = run_shell_cmd(config_forward_cmd)
if response["status"] == "ERROR":
return build_response("error","Could not add PREROUTING rule")
return build_response("success","VNF classifiers have been configured")
@app.route('/api/delete-sfc-forwarding', methods=['POST'])
def deletesfcforwarding():
# build command lines
clean_iptables_cmd = "sudo iptables -F"
clean_nat_iptables_cmd = "sudo iptables -t nat -F"
disable_ip_forward_cmd = "sudo bash -c 'echo 0 > /proc/sys/net/ipv4/ip_forward'"
response = run_shell_cmd(disable_ip_forward_cmd)
if response["status"] == "ERROR":
return build_response("error","Could not disable ip_forward")
response = run_shell_cmd(clean_iptables_cmd)
if response["status"] == "ERROR":
return build_response("error","Could not clean iptables")
response = run_shell_cmd(clean_nat_iptables_cmd)
if response["status"] == "ERROR":
return build_response("error","Could not clean iptables (NAT table)")
return build_response("success","SFC forwarding deleted")
# -------- Service Function Chaining (End) ----------
if __name__=='__main__':
app.run(host='0.0.0.0', port=8000)