Skip to content

Instantly share code, notes, and snippets.

@slankdev
Last active November 7, 2017 09:34
Show Gist options
  • Save slankdev/07197edd45a50e5d4846525deb1a58f7 to your computer and use it in GitHub Desktop.
Save slankdev/07197edd45a50e5d4846525deb1a58f7 to your computer and use it in GitHub Desktop.
backup
int rest_api_thread(void* arg_)
{
ssn_nfvi* sys = reinterpret_cast<ssn_nfvi*>(arg_);
crow::SimpleApp app;
CROW_ROUTE(app,"/") ( [&sys]() {
crow::json::wvalue x;
x["vnfs"] = "vnfs informantions";
x["stats"] = "system infos";
return x;
});
CROW_ROUTE(app,"/vnfs") ( [&sys]() {
crow::json::wvalue x;
size_t n_vnf = sys->vnfs.size();
for (size_t i=0; i<n_vnf; i++) {
x[std::to_string(i)] = sys->vnfs[i].name;
}
return x;
});
CROW_ROUTE(app,"/vnfs/<int>") ( [&sys](int n) {
crow::json::wvalue x;
if (n >= sys->vnfs.size()) {
x["msg"] = "index is too large";
return x;
}
x[std::to_string(n)] = sys->vnfs[n].name;
return x;
});
CROW_ROUTE(app,"/print") ( [&sys]() {
crow::json::wvalue x;
x["a"] = slankdev::format("%d", sys->get_a());
x["b"] = slankdev::format("%d", sys->get_b());
x["c"] = slankdev::format("%d", sys->get_c());
return x;
});
CROW_ROUTE(app, "/a") .methods("POST"_method,"GET"_method)
([&sys](const crow::request& req) {
if (req.method == crow::HTTPMethod::POST) {
auto x = crow::json::load(req.body);
if (!x) {
printf("json error: \n%s\n\n", req.body.c_str());
crow::json::wvalue x;
x["type"] = "Error";
return x;
}
sys->set_a(x["a"].i());
crow::json::wvalue ret;
return ret;
} else if (req.method == crow::HTTPMethod::GET) {
crow::json::wvalue x;
x["a"] = slankdev::format("%d", sys->get_a());
return x;
} else {
throw slankdev::exception("Unknown HTTP Method");
}
});
CROW_ROUTE(app, "/b") .methods("POST"_method,"GET"_method)
([&sys](const crow::request& req) {
if (req.method == crow::HTTPMethod::POST) {
auto x = crow::json::load(req.body);
if (!x) {
printf("json error: \n%s\n\n", req.body.c_str());
crow::json::wvalue x;
x["type"] = "Error";
return x;
}
sys->set_b(x["b"].i());
crow::json::wvalue ret;
return ret;
} else if (req.method == crow::HTTPMethod::GET) {
crow::json::wvalue x;
x["b"] = slankdev::format("%d", sys->get_b());
return x;
} else {
throw slankdev::exception("Unknown HTTP Method");
}
});
CROW_ROUTE(app, "/c") .methods("POST"_method,"GET"_method)
([&sys](const crow::request& req) {
if (req.method == crow::HTTPMethod::POST) {
auto x = crow::json::load(req.body);
if (!x) {
printf("json error: \n%s\n\n", req.body.c_str());
crow::json::wvalue x;
x["type"] = "Error";
return x;
}
sys->set_c(x["c"].i());
crow::json::wvalue ret;
return ret;
} else if (req.method == crow::HTTPMethod::GET) {
crow::json::wvalue x;
x["c"] = slankdev::format("%d", sys->get_c());
return x;
} else {
throw slankdev::exception("Unknown HTTP Method");
}
});
app.loglevel(crow::LogLevel::Critical);
app.port(8888).run();
}
#!/usr/bin/env python3
#
# MIT License
# Copyright (c) 2017 Susanow
# Copyright (c) 2017 Hiroki SHIROKURA
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# Available Commands
# - [x] show
# - [x] show ports
# - [x] show vnfs
# - [x] vnf
# - [x] vnf <name> stat
# - [x] vnf <name> deploy <coremask>
# - [x] vnf <name> undeploy
# - [x] vnf <name> portattach <port-name> <id>
# - [x] port
# - [x] port <name> stat
import pycurl
import time
import sys
def usage():
print("Usage: ssnctl [OPTIONS]")
print("")
print(" OPTIONS")
print(" show")
print(" vnf")
print(" port")
def usage_show():
print("Usage: ssnctl show [OPTIONS]")
print("")
print(" OPTIONS")
print(" vnfs ")
print(" ports ")
def usage_vnf():
print("Usage: ssnctl vnf <vnfname> [OPTIONS]")
print("")
print(" OPTIONS")
print(" stat ")
print(" deploy <coremask>")
print(" undeploy ")
print(" portattach <port-name> <pid>")
def usage_port():
print("Usage: ssnctl port <portname> [OPTIONS]")
print("")
print(" OPTIONS")
print(" stat ")
def vnf_NAME_stat(vnfname):
print("vnf_NAME_stat")
print(" vnfname : {}".format(vnfname))
return
def vnf_NAME_deploy_COREMASK(vnfname, coremask):
print("vnf_NAME_deploy_COREMASK")
print(" vnfname : {}".format(vnfname))
print(" coremask: {}".format(coremask))
return
def vnf_NAME_undeploy(vnfname):
print("vnf_NAME_undeploy")
print(" vnfname : {}".format(vnfname))
return
def vnf_NAME_portattach_PORTNAME_ID(vnfname, portname, pid):
print("vnf_NAME_portattach_PORTNAME_ID")
print(" vnfname : {}".format(vnfname))
print(" portname: {}".format(portname))
print(" pid : {}".format(pid))
return
def port_NAME_stat(portname):
print("port_NAME_stat")
print(" portname: {}".format(portname))
return
def show_vnfs():
print("show vnfs")
def show_ports():
print("show ports")
def show(argc, argv):
if (argc < 2):
usage_show()
exit(-1)
if (argv[1] == "vnfs"): show_vnfs()
elif (argv[1] == "ports"): show_ports()
else:
print("show: unknown option")
usage_show()
exit(-1)
def vnf(argc, argv):
if (argc < 3):
usage_vnf()
exit(-1)
vnfname = argv[1]
option = argv[2]
if (option == "stat"):
vnf_NAME_stat(vnfname)
exit(1)
elif (option == "undeploy"):
vnf_NAME_undeploy(vnfname)
exit(1)
elif (option == "deploy"):
if (argc >= 4):
coremask = argv[3]
vnf_NAME_deploy_COREMASK(vnfname, coremask)
exit(1)
elif (option == "portattach"):
if (argc >= 5):
portname = argv[3]
pid = argv[4]
vnf_NAME_portattach_PORTNAME_ID(vnfname, portname, pid)
exit(1)
usage_vnf()
exit(-1)
def port(argc, argv):
if (argc < 3):
usage_port()
exit(-1)
portname = argv[1]
option = argv[2]
if (option == "stat"):
port_NAME_stat(portname)
exit(1)
usage_port()
exit(-1)
def main():
argc = len(sys.argv)
argv = sys.argv
if (argc < 2):
usage()
exit(-1)
option = argv[1]
if (option == "show"):
show(argc-1, argv[1:])
exit(1)
elif (option == "vnf"):
vnf(argc-1, argv[1:])
exit(1)
elif (option == "port"):
port(argc-1, argv[1:])
exit(1)
else:
print("ssnctl: option not found")
usage()
exit(-1)
def curl_google_loop():
target = 'http://google.com'
while True:
curl = pycurl.Curl()
curl.setopt(pycurl.URL, target)
curl.perform()
print('')
time.sleep(1)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment