-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslice.cc
141 lines (123 loc) · 4.2 KB
/
slice.cc
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
#include "nexran.h"
namespace nexran {
std::map<std::string,JsonTypeMap> Slice::propertyTypes = {
{ "name",JsonTypeMap::STRING },
{ "allocation_policy",JsonTypeMap::OBJECT },
};
std::map<std::string,std::list<std::string>> Slice::propertyEnums = {};
std::map<HttpMethod,std::list<std::string>> Slice::required = {
{ HttpMethod::POST,{ "name" } }
};
std::map<HttpMethod,std::list<std::string>> Slice::optional = {};
std::map<HttpMethod,std::list<std::string>> Slice::disallowed = {
{ HttpMethod::PUT,{ "name" } }
};
void Slice::serialize(rapidjson::Writer<rapidjson::StringBuffer>& writer)
{
writer.StartObject();
writer.String("name");
writer.String(name.c_str());
if (allocation_policy) {
writer.String("allocation_policy");
allocation_policy->serialize(writer);
}
writer.String("ues");
writer.StartArray();
for (auto it = ues.begin(); it != ues.end(); ++it) {
writer.String(it->first.c_str());
}
writer.EndArray();
writer.EndObject();
};
Slice *Slice::create(rapidjson::Document& d,AppError **ae)
{
if (!d.IsObject()) {
if (ae) {
if (!*ae)
*ae = new AppError(400);
(*ae)->add(std::string("request is not an object"));
}
return NULL;
}
const rapidjson::Value& obj = d.GetObject();
if (!Slice::validate_json(HttpMethod::POST,obj,ae))
return NULL;
AllocationPolicy *allocation_policy = NULL;
if (obj.HasMember("allocation_policy")) {
if (!obj["allocation_policy"].IsObject()
|| !obj["allocation_policy"].HasMember("type")
|| !obj["allocation_policy"]["type"].IsString()
|| strcmp(obj["allocation_policy"]["type"].GetString(),
"proportional") != 0
|| !obj["allocation_policy"].HasMember("share")
|| !obj["allocation_policy"]["share"].IsInt()
|| obj["allocation_policy"]["share"].GetInt() < 0
|| obj["allocation_policy"]["share"].GetInt() > 1024
|| (obj["allocation_policy"].HasMember("auto_equalize")
&& !obj["allocation_policy"]["auto_equalize"].IsBool())) {
if (ae) {
if (!*ae)
*ae = new AppError(400);
(*ae)->add(std::string("malformed allocation_policy property"));
}
return NULL;
}
bool auto_equalize = false;
if (obj["allocation_policy"].HasMember("auto_equalize"))
auto_equalize = obj["allocation_policy"]["auto_equalize"].GetBool();
bool throttle = false;
int throttle_threshold = -1;
int throttle_period = 1800;
int throttle_share = 0;
int throttle_target = 0;
if (obj["allocation_policy"].HasMember("throttle"))
throttle = obj["allocation_policy"]["throttle"].GetBool();
if (obj["allocation_policy"].HasMember("throttle_threshold"))
throttle_threshold = obj["allocation_policy"]["throttle_threshold"].GetInt();
if (obj["allocation_policy"].HasMember("throttle_period"))
throttle_period = obj["allocation_policy"]["throttle_period"].GetInt();
if (obj["allocation_policy"].HasMember("throttle_share"))
throttle_share = obj["allocation_policy"]["throttle_share"].GetInt();
if (obj["allocation_policy"].HasMember("throttle_target"))
throttle_target = obj["allocation_policy"]["throttle_target"].GetInt();
if (!throttle_share && !throttle_target)
throttle_share = 128;
allocation_policy = new ProportionalAllocationPolicy(
obj["allocation_policy"]["share"].GetInt(),auto_equalize,
throttle,throttle_threshold,throttle_period,throttle_share,
throttle_target);
}
if (allocation_policy)
return new Slice(std::string(obj["name"].GetString()),
allocation_policy);
else
return new Slice(std::string(obj["name"].GetString()));
}
bool Slice::update(rapidjson::Document& d,AppError **ae)
{
if (!d.IsObject()) {
if (ae) {
if (!*ae)
*ae = new AppError(400);
(*ae)->add(std::string("request is not an object"));
}
return NULL;
}
const rapidjson::Value& obj = d.GetObject();
if (!Slice::validate_json(HttpMethod::PUT,obj,ae))
return false;
if (obj.HasMember("allocation_policy")) {
if (!obj["allocation_policy"].IsObject()) {
if (ae) {
if (!*ae)
*ae = new AppError(400);
(*ae)->add(std::string("malformed allocation_policy property"));
}
return NULL;
}
if (!allocation_policy->update(obj["allocation_policy"],ae))
return false;
}
return true;
}
}